Company: Microsoft(Intern)
Difficulty: medium
1. Question 1 The binary cardinality of a number is the count of 1's in its binary representation. For example, the decimal number 10 corresponds to the binary number 1010, which has a binary cardinality of 2 because it contains two 1's. Given an array of decimal integers, sort it by: Increasing binary cardinality (primary criterion) Increasing decimal value (secondary criterion, when cardinalities are equal) Return the sorted array. Example n = 4 nums = [1, 2, 3, 4] 1 10 → 1 2 , so 1's binary cardinality is 1. 2 10 → 10 2 , so 2's binary cardinality is 1. 3 10 → 11 2 , so 3's binary cardinality is 2. 4 10 → 100 2 , so 4's binary cardinality is 1. The sorted elements with binary cardinality of 1 are [1, 2, 4]. The array to return is [1, 2, 4, 3]. Function Description Complete the function cardinalitySort in the editor below . cardinalitySort has the following parameter(s): int nums[n] : an array of integers Returns int[n] : the sorted array Constraints 1 ≤ n ≤