-
Notifications
You must be signed in to change notification settings - Fork 5
/
SortBySetBitCount.java
48 lines (47 loc) · 1.34 KB
/
SortBySetBitCount.java
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
/*https://practice.geeksforgeeks.org/problems/sort-by-set-bit-count1153/1#*/
class Compute
{
static int countBits(int n)
{
int count = 0;
while (n > 0)
{
count += n&1;
n >>= 1;
}
return count;
}
static void sortBySetBitCount(Integer arr[], int n)
{
/*Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer arg0, Integer arg1)
{
// TODO Auto-generated method stub
int c1 = Integer.bitCount(arg0);
int c2 = Integer.bitCount(arg1);
if (c1 < c2)
return 1;
else if (c1 > c2)
return -1;
else return 0;
}
});*/
int i;
int[][] combined = new int[n][2];
for (i = 0; i < n; ++i)
{
combined[i][0] = arr[i];
combined[i][1] = countBits(arr[i]);
}
//for (i = 0; i < n; ++i) System.out.println(combined[i][0]+" "+combined[i][1]);
Arrays.sort(combined,new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b)
{
return b[1]-a[1];
}
});
for (i = 0; i < n; ++i) arr[i] = combined[i][0];
}
}