Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[4,5,4]
[3,6,5,8]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3769: Sort Integers by Binary Reflection
class Solution {
public int[] sortByReflection(int[] nums) {
int n = nums.length;
Integer[] a = new Integer[n];
Arrays.setAll(a, i -> nums[i]);
Arrays.sort(a, (u, v) -> {
int fu = f(u);
int fv = f(v);
if (fu != fv) {
return Integer.compare(fu, fv);
}
return Integer.compare(u, v);
});
for (int i = 0; i < n; i++) nums[i] = a[i];
return nums;
}
private int f(int x) {
int y = 0;
while (x != 0) {
y = (y << 1) | (x & 1);
x >>= 1;
}
return y;
}
}
// Accepted solution for LeetCode #3769: Sort Integers by Binary Reflection
func sortByReflection(nums []int) []int {
f := func(x int) int {
y := 0
for x != 0 {
y = (y << 1) | (x & 1)
x >>= 1
}
return y
}
sort.Slice(nums, func(i, j int) bool {
fi := f(nums[i])
fj := f(nums[j])
if fi != fj {
return fi < fj
}
return nums[i] < nums[j]
})
return nums
}
# Accepted solution for LeetCode #3769: Sort Integers by Binary Reflection
class Solution:
def sortByReflection(self, nums: List[int]) -> List[int]:
def f(x: int) -> int:
y = 0
while x:
y = y << 1 | (x & 1)
x >>= 1
return y
nums.sort(key=lambda x: (f(x), x))
return nums
// Accepted solution for LeetCode #3769: Sort Integers by Binary Reflection
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3769: Sort Integers by Binary Reflection
// class Solution {
// public int[] sortByReflection(int[] nums) {
// int n = nums.length;
// Integer[] a = new Integer[n];
// Arrays.setAll(a, i -> nums[i]);
//
// Arrays.sort(a, (u, v) -> {
// int fu = f(u);
// int fv = f(v);
// if (fu != fv) {
// return Integer.compare(fu, fv);
// }
// return Integer.compare(u, v);
// });
//
// for (int i = 0; i < n; i++) nums[i] = a[i];
// return nums;
// }
//
// private int f(int x) {
// int y = 0;
// while (x != 0) {
// y = (y << 1) | (x & 1);
// x >>= 1;
// }
// return y;
// }
// }
// Accepted solution for LeetCode #3769: Sort Integers by Binary Reflection
function sortByReflection(nums: number[]): number[] {
const f = (x: number): number => {
let y = 0;
for (; x; x >>= 1) {
y = (y << 1) | (x & 1);
}
return y;
};
nums.sort((a, b) => {
const fa = f(a);
const fb = f(b);
if (fa !== fb) {
return fa - fb;
}
return a - b;
});
return nums;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.