Description
Given an integer array arr, sort the integers by the number of 1s in their binary representation in ascending order. For same number of 1s, sort by integer value.
Examples
Input:
arr = [0,1,2,3,4,5,6,7,8]Output:
[0,1,2,4,8,3,5,6,7]Explanation:
Sort by bit count.
Input:
arr = [10,11,12,13,14,15]Output:
[10,12,11,13,14,15]Explanation:
Binary representations: 10(1010)=2 bits, 11(1011)=3 bits, 12(1100)=2 bits, 13(1101)=3 bits, 14(1110)=3 bits, 15(1111)=4 bits. Sorted by bit count: 2-bit numbers [10,12] ordered by value, then 3-bit numbers [11,13,14] ordered by value, then 4-bit number [15].
Input:
arr = [9,3,5]Output:
[3,5,9]Explanation:
Binary representations: 9(1001)=2 bits, 3(11)=2 bits, 5(101)=2 bits. All numbers have the same bit count (2), so they are sorted by their values in ascending order: 3, 5, 9.
Constraints
- •
1 ≤ arr.length ≤ 500