SORT 12


TEST CASE 1 

INPUT

1
5
5 4 5 4 6 
OUTPUT
4 4 5 5 6

TEST CASE 2 

INPUT

2
6
1 2 3 2 1 5
8
2 22 3 6 55 22 1 3 
OUTPUT
1 1 2 2 3 5 
3 3 22 22 1 2 6 

#include<iostream>
#include<algorithm>
using namespace std;
class mymap {
  public:
  int f,s;
  mymap(int f1,int s1)
  {
    f=f1;
      s=s1;
  }
  void print(){
    for (int i=0;i<s;i++)
cout<<f<<' ';
  }
  void printnospace(){
    for(int i=0;i<s;i++) cout<<f;
  }
};
bool cmp(mymap m1,mymap m2)
{
  if(m1.s==m2.s) return m1.f<m2.f;
  return m1.s > m2.s;
}
int main(){
  int t,n;
  cin>>t;
  while(t--){
    cin>>n;
    int a[n],count;
    vector<mymap> v;
    for(int i=0;i<n;i++)
      cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<n;i+=count){
      count =1;
      int j=i;
      while(a[j]==a[j+1]) {
        count++;
        j++;
      }
      v.push_back(mymap(a[i],count));
    }
    sort(v.begin(),v.end(),cmp);
    for(int i=0;i<v.size()-1;i++) v[i].print();
    t ? v[v.size()-1].print(): v[v.size()-1].printnospace();
    cout<<"\n";
  }
  return 0;
}

Comments

Popular posts from this blog

AR1

SER12

AR15