Thursday, 6 April 2017

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:-



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

No comments:
Write comments

Recommended Posts × +