Cardinality Sorting
The binary cardinality of a number is the total number of 1's it contains in its binary representation. For example, the decimal integer 20 corresponds to the binary number 10100₂. There are 2 1's in the binary representation so its binary cardinality is 2.
Given an array of decimal integers, sort it ascending first by binary cardinality, then by decimal value. Return the resulting array.
Complete the function cardinalitySort in the editor.
cardinalitySort has the following parameter(s):
int nums[n]: an array of decimal integers
Returns
int[n]: the integer array nums sorted first by ascending binary cardinality, then by decimal value
1Example 1
2Example 2
3Example 3
4Example 4
Constraints
Limits and guarantees your solution can rely on.
1 ≤ n ≤ 10⁵1 ≤ nums[i] ≤ 10⁶