SORT 12
QUESTION DESCRIPTION
Given an array of integers, sort the array according to frequency of elements.
For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.
If frequencies of two elements are same, print them in increasing order.
Input:
The first line of input contains an integer T denoting the number of test cases.
The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print each sorted array in a seperate line.
For each array its numbers should be seperated by space.
Constraints:
1 ≤ T ≤ 70
30 ≤ N ≤ 130
1 ≤ A [ i ] ≤ 60
Given an array of integers, sort the array according to frequency of elements.
For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.
If frequencies of two elements are same, print them in increasing order.
Input:
The first line of input contains an integer T denoting the number of test cases.
The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print each sorted array in a seperate line.
For each array its numbers should be seperated by space.
Constraints:
1 ≤ T ≤ 70
30 ≤ N ≤ 130
1 ≤ A [ i ] ≤ 60
TEST CASE 2
INPUT
INPUT
2
6
1 2 3 2 1 5
8
2 22 3 6 55 22 1 3
OUTPUT1 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
Post a Comment