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] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the total Hamming distance using brute force, we're essentially comparing every number in the set with every other number. We carefully count the differences between their binary representations and sum these counts to find the total distance.
Here's how the algorithm would work step-by-step:
def total_hamming_distance_brute_force(numbers):
total_distance = 0
list_length = len(numbers)
for i in range(list_length):
for j in range(i + 1, list_length):
# Iterate through the remaining numbers
first_number = numbers[i]
second_number = numbers[j]
xor_result = first_number ^ second_number
hamming_distance = 0
# Count the set bits in XOR result to get the hamming distance
while xor_result:
hamming_distance += xor_result & 1
xor_result >>= 1
total_distance += hamming_distance
return total_distanceThe key to efficiently calculating the total Hamming distance lies in focusing on individual bit positions across all numbers. Instead of comparing every number with every other number directly, we analyze each bit position separately to count the number of pairs with differing bits. This drastically reduces the computational effort.
Here's how the algorithm would work step-by-step:
def total_hamming_distance(numbers):
total_distance = 0
list_length = len(numbers)
# Iterate through each bit position (0 to 31 for 32-bit integers)
for bit_position in range(32):
count_zeros = 0
# Count numbers with a '0' at the current bit position
for number in numbers:
if (number >> bit_position) & 1 == 0:
count_zeros += 1
# Calculates pairs with differing bits at this position.
count_ones = list_length - count_zeros
total_distance += count_zeros * count_ones
return total_distance| Case | How to Handle |
|---|---|
| Empty input array | Return 0, as there are no pairs to calculate Hamming distance for. |
| Array with a single element | Return 0, as a single element cannot form a pair. |
| Array with all elements being the same number | The Hamming distance will be 0 for all pairs, so the sum should remain 0, which our algorithm will correctly produce. |
| Large input array (performance concern) | Bit manipulation avoids nested loops, giving O(n*k) where n is array size and k is the number of bits in each number (typically 32), so performance is linear and scalable. |
| Input numbers with a wide range of values (potential for integer overflow when calculating total distance) | Use a 64-bit integer (long) to store the total Hamming distance to prevent overflow. |
| Negative numbers in the input array | The bit manipulation approach still works correctly with negative numbers as they are represented in two's complement. |
| Array containing a mix of small and very large numbers | The algorithm correctly compares all bits regardless of the magnitude of the numbers. |
| Array where all numbers have a single bit set in different positions (e.g., 1, 2, 4, 8) | Each pair will have a Hamming distance of 2, so the total should be `n * (n - 1)`, which our algorithm will compute. |