forked from akkupy/codeDump
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d29f7a1
commit 5e310c0
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
void sortZeroesAndOnes(vector<int> &v) | ||
{ | ||
int left_ptr = 0; | ||
int right_ptr = v.size()-1; | ||
|
||
while(left_ptr<right_ptr) | ||
{ | ||
if(v[left_ptr]==1 && v[right_ptr]==0){ | ||
v[left_ptr++]=0; | ||
v[right_ptr--]=1; | ||
} | ||
|
||
if(v[left_ptr]==0){ | ||
left_ptr++; | ||
} | ||
|
||
if(v[right_ptr]==1){ | ||
right_ptr--; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
|
||
vector<int> v; | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
int ele; | ||
cin>>ele; | ||
v.push_back(ele); | ||
} | ||
|
||
sortZeroesAndOnes(v); | ||
|
||
for(int i=0; i<n; i++) | ||
{ | ||
cout<<v[i]<<" "; | ||
}cout<<endl; | ||
return 0; | ||
} |