# Rank the array according to rightmost set bit and least set bits

Given an array **arr[]** of **N** integers, the task is to replace each element of Array with their rank according to Rightmost-Set Bit (RSB) in descending manner, If the RSB is the same for two numbers then choose the one which has the least number of set bits if the set bits of two numbers are same then choose the number who comes first in the array.

**Examples:**

Input:arr[] = {4, 5, 6, 7, 8}Output:2 4 3 5 1Explanation:Then rank of elements is given by sorted descending of RSB.

Rank(8) = 1 as Rsb of 8(1000) is 8 and setbit count is 1.

Rank(4) = 2 as RSB of 4(0100) is 4 and setbit count is 1.

Rank(6) = 3 as RSB of 6(0110) is 2 and setbit count is 2.

Rank(5) = 4 as RSB of 5(0101) is 1 and setbit count is 2.

Rank(7) = 5 as Rsb of 7(0111) is 1 and setbit count is 3.

So, the answer will be { 2, 4, 3, 5, 1 }.

Input:arr[] = {5, 10, 15, 32}Output:3 2 4 1

**Naive Approach:** The naive approach is to find the rank of each element by comparing the RSB of that element with other elements and incrementing the rank by 1 whenever a greater value of RSB is encountered.

**Time Complexity:**** **O(N*N).**Auxiliary Space:**** **O(N).

**Efficient Approach:** To optimize the above naive approach find ranks of elements and then assign the rank to the elements using a **comparator**. Using a comparator, the elements can be sorted based on data members. For instance, here the elements will be sorted based on the RSB and number of set bits.

Below is the implementation of the above approach.

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;` ` ` `// Class for pair` `class` `Pair {` ` ` `int` `index;` ` ` `int` `rsb;` ` ` `int` `setbit;` ` ` ` ` `// Constructor` ` ` `Pair(` `int` `index, ` `int` `rsb, ` `int` `setbit)` ` ` `{` ` ` `this` `.index = index;` ` ` `this` `.rsb = rsb;` ` ` `this` `.setbit = setbit;` ` ` `}` `}` ` ` `// Comparator for sorting based on RSB` `class` `pair_sort ` `implements` `Comparator<Pair> {` ` ` `// Used for sorting in descending order` ` ` `// of rightmost set bit` ` ` `public` `int` `compare(Pair a, Pair b)` ` ` `{` ` ` `if` `(a.rsb > b.rsb)` ` ` `return` `-` `1` `;` ` ` `else` `if` `(a.rsb < b.rsb)` ` ` `return` `1` `;` ` ` `else` `if` `(a.setbit < b.setbit)` ` ` `return` `-` `1` `;` ` ` `else` `if` `(b.setbit < a.setbit)` ` ` `return` `1` `;` ` ` `else` `if` `(a.index < b.index)` ` ` `return` `-` `1` `;` ` ` `else` ` ` `return` `1` `;` ` ` `}` `}` ` ` `// Class to implement the solution logic` `class` `GFG {` ` ` ` ` `// Function to rearrange the elements` ` ` `// according to Rightmpost set bits` ` ` `void` `rearrange(` `int` `ar[], ` `int` `n)` ` ` `{` ` ` `// Creating priority queue from` ` ` `// sorting according to` ` ` `// rightmost set bit.` ` ` `PriorityQueue<Pair> pq` ` ` `= ` `new` `PriorityQueue<Pair>(` `new` `pair_sort());` ` ` ` ` `// For creating object of each element` ` ` `// so that it can be sorted` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) {` ` ` ` ` `// To calculate the rightmost` ` ` `// set bit in O(1)` ` ` `int` `k = (ar[i] & -ar[i]);` ` ` ` ` `// Creating a pair object` ` ` `// with rsb and index` ` ` `int` `setbit` ` ` `= Integer.bitCount(ar[i]);` ` ` `Pair p = ` `new` `Pair(i, k, setbit);` ` ` ` ` `// Inserting the element` ` ` `// in priorityqueue` ` ` `pq.add(p);` ` ` `}` ` ` ` ` `int` `rank = ` `1` `;` ` ` `// Popping the element of queue` ` ` `// to get respective rank.` ` ` `while` `(!pq.isEmpty()) {` ` ` `Pair p = pq.poll();` ` ` ` ` `ar[p.index] = rank++;` ` ` `}` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `throws` `java.lang.Exception` ` ` `{` ` ` `int` `arr[] = { ` `4` `, ` `5` `, ` `6` `, ` `7` `, ` `8` `};` ` ` ` ` `// Creating an object of class` ` ` `GFG ob = ` `new` `GFG();` ` ` ` ` `// To store the length of array` ` ` `int` `N = arr.length;` ` ` ` ` `// To call the rearrange function` ` ` `ob.rearrange(arr, N);` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `System.out.print(arr[i] + ` `" "` `);` ` ` `}` `}` |

**Output**

2 4 3 5 1

* Time Complexity:* O(N*log(N))

*O(N).*

**Auxiliary Space**: