TechoSagar: Segregate 0s and 1s

Search

Google Alert - jobs

Segregate 0s and 1s

You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. 

Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input C[i].

Output:

Print 0s on left side and 1s on right side.

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 500
0 ≤ C[i] ≤ 1

Example:
Input:
2
5
0 0 1 1 0
4
1 1 1 1
Output:
0 0 0 1 1
1 1 1 1
Your Code 
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[500],i;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int b[500],c[500];
int j=0,k=0;
for(i=0; i<n; i++)
{
if(a[i]==0)
{
b[k]=a[i];
k++;
}
else
{
c[j]=a[i];
j++;
}
}
for(i=0; i<k; i++)
{
cout<<b[i]<<" ";
}
for(i=0; i<j; i++)
{
cout<<c[i]<<" ";
}
cout<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook