Skip to content

Instantly share code, notes, and snippets.

@mk6619
Created December 4, 2019 12:28
Show Gist options
  • Select an option

  • Save mk6619/27d764e2e5fb25824fb9f46b70009fc1 to your computer and use it in GitHub Desktop.

Select an option

Save mk6619/27d764e2e5fb25824fb9f46b70009fc1 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
/**
We could have done this problem by coounting the occurence of elements
*/
int main() {
int testCases;
cin>>testCases;
for(int i = 0 ; i < testCases; i++) {
int n;
cin>>n;
int arr[n];
for(int j =0 ; j < n ; j++) {
cin>>arr[j];
}
int pointer = 0, temp;
for(int j =0 ; j < n ; j++) {
if(arr[j]==0) {
temp = arr[j];
arr[j]=arr[pointer];
arr[pointer]= temp;
pointer++;
}
}
for(int j =0 ; j < n ; j++) {
if(arr[j]==1) {
temp = arr[j];
arr[j]=arr[pointer];
arr[pointer]= temp;
pointer++;
}
}
for(int j =0 ; j < n ; j++) {
cout<<arr[j]<<" ";
}
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment