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.
Move from brute-force thinking to an efficient approach using array strategy.
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums.
Example 1:
Input: nums = [4,14,2] Output: 6 Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
Example 2:
Input: nums = [4,14,4] Output: 4
Constraints:
1 <= nums.length <= 1040 <= nums[i] <= 109Problem summary: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Bit Manipulation
[4,14,2]
[4,14,4]
hamming-distance)sum-of-digit-differences-of-all-pairs)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #477: Total Hamming Distance
class Solution {
public int totalHammingDistance(int[] nums) {
int ans = 0, n = nums.length;
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += (x >> i & 1);
}
int b = n - a;
ans += a * b;
}
return ans;
}
}
// Accepted solution for LeetCode #477: Total Hamming Distance
func totalHammingDistance(nums []int) (ans int) {
for i := 0; i < 32; i++ {
a := 0
for _, x := range nums {
a += x >> i & 1
}
b := len(nums) - a
ans += a * b
}
return
}
# Accepted solution for LeetCode #477: Total Hamming Distance
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
ans, n = 0, len(nums)
for i in range(32):
a = sum(x >> i & 1 for x in nums)
b = n - a
ans += a * b
return ans
// Accepted solution for LeetCode #477: Total Hamming Distance
impl Solution {
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for i in 0..32 {
let mut a = 0;
for &x in nums.iter() {
a += (x >> i) & 1;
}
let b = (nums.len() as i32) - a;
ans += a * b;
}
ans
}
}
// Accepted solution for LeetCode #477: Total Hamming Distance
function totalHammingDistance(nums: number[]): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const a = nums.filter(x => (x >> i) & 1).length;
const b = nums.length - a;
ans += a * b;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.