Company: Microsoft(Intern)
Difficulty: medium
1. Question 1 Define the binary cardinality of a number as 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, arrange it so that: Entries with a smaller binary cardinality come first (the main sort key) When two entries tie on binary cardinality, the smaller decimal value comes first (the tiebreaker) Return the array sorted this way. 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