ECFBP203 - Editorial

PROBLEM LINK:

Practice
Contest

Author: Sandeep Singh
Tester: Arkapravo Ghosh
Editorialist: Sandeep Singh

DIFFICULTY:

SIMPLE

PROBLEM:

Give nan array of numbers, remove all 0 s without disturbing the order of other non zero elements

EXPLANATION:

This is a simple implementation based problem which can be solved in many ways.
One way is to traverse the array from left to right and push all zon-zero elements in a vector at the same time keeping count of the number of 0s encountered in cnt.
After this, push cnt number of 0s at the end of the vector.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
using namespace std;
void solve(){
	int N,i,j,cnt=0;
	cin>>N;
	int arr[N];
	vector<int>ans;
 
	for(i=0;i<N;i++){
		cin>>arr[i];
		if(arr[i])
			ans.push_back(arr[i]);
		else
			cnt++;
	}
	for(i=0;i<cnt;i++)
		ans.push_back(0);
	
	for(int x:ans)
		cout<<x<<" ";
	cout<<endl; 
 
}
int main() {
 
	int T ;
	cin>>T;
	while(T--)
		solve();
	return 0;
}
 
Tester's Solution
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define ppb pop_back
#define pii pair<ll, ll>
#define vi vector<ll>
#define vull vector<ull>
#define vpii vector<pii>
#define mt make_tuple
#define ff first
#define ss second
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define revall(x) x.rbegin(), x.rend()
#define rep(i, j, k) for(ll i = j; i < (k); ++i)
#define repr(i, j, k) for(ll i = k-1; i >= (j); --i)
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define T int tt; cin>>tt; while(tt--)
 
const ll MOD = (ll)(1e9+7);
const int inf = (int)INFINITY;
const ll INF = (ll)INFINITY;
const int MAX = (int)(1e6+5);
 
vi primes;
ll nI[MAX],fI[MAX],fact[MAX];
 
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
ll gcd(ll a,ll b){while(b){ll t=a;a=b;b=t%b;}return a;}     
ll lcm(ll a,ll b){return (max(a,b)/gcd(a,b))*min(a,b);}
ll power(ll a,ll b){ll r=1;while(b){if(b&1)r*=a;a=a*a;b>>=1;}return r;}
bool isPrime(ll n){if(n<=1)return 0;if(n<=3)return 1;if(n%2==0||n%3==0)return 0;for(ll i=5;i*i<=n;i+=6)if(n%i==0||n%(i+2)==0)return 0;return 1;}
void find_primes(ll n=100000000){ll limit=floor(sqrt(n))+1;vi test;test.pb(2),primes.pb(2);for(ll i=3;i<limit;i+=2)if(isPrime(i))test.pb(i),primes.pb(i);ll lo=limit,hi=2*limit;bool p[limit];while(lo<n){if(hi>n)hi=n;memset(p,true,sizeof(p));for(int i=0;i<test.size();++i){ll mn=(lo/test[i])*test[i];if(mn<lo)mn+=test[i];for(ll j=mn;j<hi;j+=test[i])p[j-lo]=0;}rep(i,0,limit)if(p[i] && i+lo<hi)primes.pb(i+lo);lo+=limit,hi+=limit;}}
ll modmult(ll a,ll b){ll r=0;a%=MOD;while(b){if(b&1)r=(r+a)%MOD;a=(a<<1)%MOD;b>>=1;}return r;}
ll modexpo(ll a,ll b){ll r=1;a%=MOD;while(b){if(b&1)r=(r*a)%MOD;a=(a*a)%MOD;b>>=1;}return r;}
ll nCr(ll n,ll r){ll res=1;if(r>n>>1)r=n-r;rep(i,0,r){res=(res*(n-i))%MOD;res=(res*modexpo(i+1,MOD-2))%MOD;}return res;}
void binomial_pre(){nI[0]=nI[1]=fI[0]=fI[1]=fact[0]=1;rep(i,2,MAX)nI[i]=nI[MOD%i]*(MOD-MOD/i)%MOD;rep(i,2,MAX)fI[i]=(nI[i]*fI[i-1])%MOD;rep(i,1,MAX)fact[i]=(fact[i-1]*i)%MOD;}
ll binomial(ll n,ll r){if(n<r)return 0;return ((fact[n]*fI[r])%MOD*fI[n-r])%MOD;}
 
int main() {
    fastio;
    //find_primes();
    //binomial_pre();
    #define JUDGE
    #ifndef JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        freopen("error.txt", "w", stderr);
    #endif
    T {
        int n;
        cin >> n;
        int a[n], z = 0;
        for(int i = 0; i < n; ++i) {
            cin >> a[i];
            if(!a[i])
                ++z;
            else
                cout << a[i] << ' ';
        }
        for(int i = 0; i < z; ++i)
            cout << 0 << ' ';
        cout << '\n';
    }
    return 0;
}