You are given an integer array nums.
The binary reflection of a positive integer is defined as the number obtained by reversing the order of its binary digits (ignoring any leading zeros) and interpreting the resulting binary number as a decimal.
Sort the array in ascending order based on the binary reflection of each element. If two different numbers have the same binary reflection, the smaller original number should appear first.
Return the resulting sorted array.
Example 1:
Input: nums = [4,5,4]
Output: [4,4,5]
Explanation:
Binary reflections are:
100 -> (reversed) 001 -> 1101 -> (reversed) 101 -> 5100 -> (reversed) 001 -> 1[4, 4, 5].Example 2:
Input: nums = [3,6,5,8]
Output: [8,3,6,5]
Explanation:
Binary reflections are:
11 -> (reversed) 11 -> 3110 -> (reversed) 011 -> 3101 -> (reversed) 101 -> 51000 -> (reversed) 0001 -> 1[8, 3, 6, 5].
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 109Solutions for this problem are being prepared.
Try solving it yourselfPractice Sort Integers by Binary Reflection with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor