-
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.
Create Sort an array of 0s, 1s and 2s.cpp
- Loading branch information
1 parent
ac99de0
commit db96211
Showing
1 changed file
with
36 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,36 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int main(){ | ||
int n; | ||
cin>>n; | ||
int a[n]; | ||
for(int i=0;i<n;i++) { | ||
cin>>a[i]; | ||
} | ||
int c0=0,c1=0,c2=0; | ||
|
||
for(int i=0;i<n;i++) { | ||
if(a[i]==0) | ||
c0++; | ||
|
||
else if(a[i]==1) | ||
c1++; | ||
|
||
else c2++; | ||
} | ||
for(int i=0;i<c0;i++) | ||
a[i]=0; | ||
|
||
for(int i=c0;i<c0+c1;i++) | ||
a[i]=1; | ||
|
||
while(c2--) { | ||
a[i++]=2; | ||
} | ||
for(int i=0;i<n;i++) | ||
cout<<a[i]<<" "; | ||
|
||
return 0; | ||
} | ||
//https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1 // |