-
Notifications
You must be signed in to change notification settings - Fork 30
/
Sort by Set Bit Count.cpp
77 lines (60 loc) · 1.71 KB
/
Sort by Set Bit Count.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Solution by Rahul Surana
***********************************************************
Given an array of integers, sort the array (in descending order) according to count of
set bits in binary representation of array elements.
Note: For integers having same number of set bits in their binary representation,
sort according to their position in the original array i.e., a stable sort.
***********************************************************
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int df(int arr){
int ans = 0;
for(int j = 31; j>= 0; j--){
if(arr & 1<<j) ans++;
}
return ans;
}
void sortBySetBitCount(int arr[], int n)
{
map<int, vector<int>, greater<int>> m;
vector<int> b(n,0);
for(int i = 0 ; i < n; i++){
int x = df(arr[i]);
m[x].push_back(arr[i]);
}
int j = 0;
for(auto i = m.begin(); i != m.end(); i++){
for(auto z: i->second){
arr[j++] = z;
}
}
// sort(arr,arr+n, [&](int &a, int &c){ bool f = b[a]>b[c]; swap(b[a],b[c]); return f; } );
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
Solution ob;
ob.sortBySetBitCount(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
return 0;
}
// } Driver Code Ends