Sort Array by Set Bit Count

Given an array of positive integers, your task is to sort the array in decreasing order based on the count of set bits in the binary representation of each integer.

If two numbers have the same number of set bits, maintain their original order.

Input:

T // number of test cases
N // size of the array
arr[] // array elements

Output:

Sorted array according to set bit count
Example:
Input:
1
3
2 4 3
Output:
3 2 4

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 104
  • 1 ≤ arr[i] ≤ 109
Note:

You do not need to print anything, it has already been handled. Just modify the array in-place within the given function.

Be the first one to answer
Add answer anonymously...
Adobe Product Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter