Sort Integers by The Number of 1 Bits Problem
Sort Integers by The Number of 1 Bits Problem — ExecCode Easy DSA Practice
Solve the Sort Integers by The Number of 1 Bits problem on ExecCode. Free online easy DSA practice in Arrays - Logic Building. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Description You are given an integer array arr. Your task is to compute the required answer and return the array after sorting it. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.
Examples
Input {"arr": [3, 7, 8, 9]}; Output [8, 3, 9, 7]. Input {"arr": [1, 3, 5, 7, 9]}; Output [1, 3, 5, 9, 7]. Input {"arr": [1, 0, 2, 3, 4]}; Output [0, 1, 2, 4, 3]
Constraints
1 <= arr.length <= 500 0 <= arr[i] <= 10^4
Practice Sort Integers by The Number of 1 Bits free on ExecCode. Browse DSA problems, topic map, and placement guides.