# Geeksforgeeks Solution For " Sort an array of 0s, 1s and 2s "

GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers

Problem :- Sort an array of 0s, 1s and 2s

Submit Your Solution :- Click Here

Solution :-

#include <bits/stdc++.h>
using namespace std;

int main()
{
int T;
cin>>T;
while(T--)
{
int N,i,count1,count2,count3;
cin>>N;
int ar[N];
count1=count2=count3=0;
for(i=0;i<N;i++)
{
cin>>ar[i];
}
for(i=0;i<N;i++)
{
if(ar[i]==0)
count1++;
else if(ar[i]==1)
count2++;
else if(ar[i]==2)
count3++;
}
i=0;
while(count1)
{
ar[i]=0;
cout<<ar[i]<<" ";
i++;
count1--;
}
while(count2)
{
ar[i]=1;
cout<<ar[i]<<" ";
i++;
count2--;
}

while(count3)
{
ar[i]=2;
cout<<ar[i]<<" ";
i++;
count3--;
}
cout<<"\n";
}
return 0;
}

Output:-

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me