Given an array arr[] of positive integers, the task is to sort the array in decreasing order of count of set bits in binary representations of array elements.
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. For example, if input array is {3, 5}, then output array should also be {3, 5}. Note that both 3 and 5 have same number set bits.
Examples:
Input: arr[] = {5, 2, 3, 9, 4, 6, 7, 15, 32}
Output: 15 7 5 3 9 6 2 4 32
The integers in their binary representation are:
15 – 1111
7 – 0111
5 – 0101
3 – 0011
9 – 1001
6 – 0110
2 – 0010
4 – 0100
32 – 10000
Hence, the non-increasing sorted order is:
{15, 7, 5, 3, 9, 6, 2, 4, 32}
Input: arr[] = {1, 2, 3, 4, 5, 6};
Output: 3 5 6 1 2 4
Approach: We have already discussed the method of sorting based on set bit count in the previous section with various methods. This post contains implementation using maps.
As we know that a map/multimap stores data in sorted manner. So if we store (32 – countsetbits(arr[i])) for an arr[i] in map, then the output will come out in decreasing order of set bit count which is the desired output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sortArr( int arr[], int n)
{
multimap< int , int > map;
for ( int i = 0; i < n; i++) {
int count = 0;
int k = arr[i];
while (k) {
k = k & k - 1;
count++;
}
map.insert(make_pair(32 - count, arr[i]));
}
for ( auto it = map.begin(); it != map.end(); it++) {
cout << (*it).second << " " ;
}
}
int main()
{
int arr[] = { 5, 2, 3, 9, 4, 6, 7, 15, 32 };
int n = sizeof (arr) / sizeof (arr[0]);
sortArr(arr, n);
return 0;
}
|
Java
import java.util.*;
class Compare implements Comparator< int []> {
@Override public int compare( int [] a, int [] b)
{
return a[ 0 ] - b[ 0 ];
}
}
class GFG {
static void sortArr( int [] arr, int n)
{
int [][] map = new int [n][ 2 ];
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
int k = arr[i];
while (k > 0 ) {
k = k & k - 1 ;
count++;
}
map[i][ 0 ] = 32 - count;
map[i][ 1 ] = arr[i];
}
Arrays.sort(map, new Compare());
for ( int i = 0 ; i < map.length; i++) {
System.out.print(map[i][ 1 ] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 5 , 2 , 3 , 9 , 4 , 6 , 7 , 15 , 32 };
int n = arr.length;
sortArr(arr, n);
}
}
|
Python3
def sortArr(arr, n):
mp = []
for i in range ( n):
count = 0
k = arr[i]
while (k):
k = k & k - 1
count + = 1
mp.append(( 32 - count, arr[i]))
mp.sort(key = lambda x: x[ 0 ])
for it in mp:
print (it[ 1 ], end = " " )
if __name__ = = "__main__" :
arr = [ 5 , 2 , 3 , 9 , 4 , 6 , 7 , 15 , 32 ]
n = len (arr)
sortArr(arr, n)
|
C#
using System;
using System.Collections.Generic;
class CompareArr : Comparer< int []> {
public override int Compare( int [] a, int [] b)
{
return a[0] - b[0];
}
}
class GFG
{
static void sortArr( int [] arr, int n)
{
int [][] map = new int [n][];
for ( int i = 0; i < n; i++) {
map[i] = new int [2];
int count = 0;
int k = arr[i];
while (k > 0) {
k = k & k - 1;
count++;
}
map[i][0] = 32 - count;
map[i][1] = arr[i];
}
Array.Sort(map, new CompareArr());
for ( int i = 0; i < map.Length; i++) {
Console.Write(map[i][1] + " " );
}
}
public static void Main( string [] args)
{
int [] arr = { 5, 2, 3, 9, 4, 6, 7, 15, 32 };
int n = arr.Length;
sortArr(arr, n);
}
}
|
Javascript
<script>
function sortArr(arr,n)
{
let map=[];
for (let i = 0; i < n; i++) {
let count = 0;
let k = arr[i];
while (k) {
k = k & k - 1;
count++;
}
map.push([32 - count, arr[i]]);
}
map.sort( function (a,b){ return a[0]-b[0];});
for (let i=0;i<map.length;i++) {
document.write(map[i][1]+ " " );
}
}
let arr=[5, 2, 3, 9, 4, 6, 7, 15, 32 ];
let n=arr.length;
sortArr(arr, n);
</script>
|
Output: 15 7 5 3 9 6 2 4 32
Time Complexity: O(n * log n)
Auxiliary Space: O(n)