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] <= 109Problem Overview: You are given a list of integers and need to sort them based on their binary reflection. For each number, convert it to binary, reverse the bit sequence, interpret the reversed bits as a new integer, and sort the array according to this reflected value.
Approach 1: Brute Force Reflection During Comparison (O(n log n * b) time, O(1) extra space)
The most direct solution uses a custom comparator during sorting. Every time two numbers are compared, convert each integer to its binary form, reverse the bit string, convert it back to a decimal value, and compare those reflected values. The sorting algorithm repeatedly performs these conversions, which adds overhead proportional to the number of bits b. This approach works but is inefficient because the same reflection may be recomputed many times during the sort.
Approach 2: Custom Sorting with Precomputed Reflection Keys (O(n log n) time, O(n) space)
A more efficient method computes the binary reflection for each integer once and uses it as a sorting key. Iterate through the array, convert each number to binary, reverse the bits, and store the resulting integer. Then apply a standard sort where the key is this precomputed reflection value. Most languages support this pattern using a key function or custom comparator. Because each reflection is calculated once, the overall complexity becomes O(n log n) for sorting plus O(n * b) preprocessing.
The core operation is the bit reflection step. You can perform it by converting the number to a binary string and reversing it, or by repeatedly extracting the least significant bit and building the reversed value using bit operations. The bitwise method avoids string allocations and can be slightly faster, though both approaches are acceptable for an easy-level problem.
This problem mainly tests your understanding of array manipulation and custom comparators in sorting. The key insight is that the original value does not determine order directly; the order is based on a transformed representation of each element.
Recommended for interviews: Use the precomputed reflection key approach with custom sorting. Start by explaining the brute force comparator to demonstrate the core idea, then optimize by computing reflections once and sorting with a key function. Interviewers expect you to recognize unnecessary repeated work inside comparators and move that logic outside the sort.
We define a function f(x) to calculate the binary reflection value of integer x. Specifically, we continuously extract the lowest bit of x and add it to the end of the result y until x becomes 0.
Then, we sort the array nums with the sorting key being the tuple (f(x), x) of each element's binary reflection value and original value. This ensures that when two elements have the same binary reflection value, the smaller original value will be placed first.
Finally, we return the sorted array.
The time complexity is O(n times log n), and the space complexity is O(log n). Where n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Reflection in Comparator | O(n log n * b) | O(1) | Simple implementation when input size is small and recomputation cost is negligible |
| Custom Sort with Precomputed Reflection Keys | O(n log n) | O(n) | Preferred solution for interviews and large arrays; avoids repeated reflection computation |
| Bitwise Reflection Key + Sort | O(n log n) | O(n) | When optimizing constant factors by avoiding string conversions during reflection |
Leetcode | 3769 Sort Integers by Binary Reflection | Java | Sorting | Weekly Contest - 479 • Komal Vhanmane • 287 views views
Watch 5 more video solutions →Practice Sort Integers by Binary Reflection with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor