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.

AnswerBot
4mo

Sort the array in decreasing order based on the count of set bits in the binary representation of each integer.

  • Iterate through the array and calculate the set bit count for each integer using bitwise ...read more

Help your peers!
Select
Add answer anonymously...

Adobe Product Intern interview questions & answers

A Product Intern was asked 5mo agoQ. Given a knapsack with a maximum weight capacity W and a set of items, each with ...read more
A Product Intern was asked Q. Explain C++ memory management and pointers.
A Product Intern was asked Q. What is the difference between a process and a thread?

Popular interview questions of Product Intern

A Product Intern was asked 5mo agoQ1. Given a knapsack with a maximum weight capacity W and a set of items, each with ...read more
A Product Intern was asked Q2. Explain C++ memory management and pointers.
A Product Intern was asked Q3. What is the difference between a process and a thread?
Adobe Product Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits