class Solution:
def totalHammingDistance(self, nums):
total = 0
n = len(nums)
for i in range(32):
bit_count = 0
for num in nums:
bit_count += (num >> i) & 1
total += bit_count * (n - bit_count)
return total
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int total = 0, n = nums.size();
for (int i = 0; i < 32; ++i) {
int bitCount = 0;
for (int num : nums) {
bitCount += (num >> i) & 1;
}
total += bitCount * (n - bitCount);
}
return total;
}
};
class Solution {
public int totalHammingDistance(int[] nums) {
int total = 0, n = nums.length;
for (int i = 0; i < 32; ++i) {
int bitCount = 0;
for (int num : nums) {
bitCount += (num >> i) & 1;
}
total += bitCount * (n - bitCount);
}
return total;
}
}
var totalHammingDistance = function(nums) {
let total = 0, n = nums.length;
for (let i = 0; i < 32; ++i) {
let bitCount = 0;
for (let num of nums) {
bitCount += (num >> i) & 1;
}
total += bitCount * (n - bitCount);
}
return total;
};
You are given an array of integers nums
. The Hamming distance between two integers is the number of positions at which the corresponding bits are different in their binary representations.
Your task is to compute the total Hamming distance between all pairs of integers in the array. That is, for every pair of indices (i, j)
with i < j
, calculate the Hamming distance between nums[i]
and nums[j]
, and return the sum of all these distances.
At first glance, you might think to simply compare every pair of numbers in nums
and compute their Hamming distance by converting them to binary and counting the differing bits. However, with up to 10,000 elements, this would require checking nearly 50 million pairs, which is too slow.
Instead, let's look for patterns. The Hamming distance between two numbers is the count of positions where their bits differ. If we examine each bit position across all numbers, we can count how many numbers have a 1 in that position and how many have a 0. Each pair where one number has a 1 and the other has a 0 at the same position contributes 1 to the total Hamming distance.
By focusing on bit positions instead of pairs, we can reduce the problem from O(n2) comparisons to O(n) per bit position, which is much faster.
nums
have a 1 at that position.ones
be the count of numbers with a 1, and zeros
be the rest (n - ones
).ones * zeros
. Add this to the running total.This method is efficient because it only looks at each bit in each number once, leading to O(32n) time complexity, which is effectively O(n) for practical purposes.
Let's consider the input nums = [4, 14, 2]
.
Summing up: 0 (bit 0) + 2 (bit 1) + 2 (bit 2) + 2 (bit 3) = 6. So the answer is 6.
The optimized approach is much faster and suitable for large inputs.
To efficiently compute the total Hamming distance among all pairs in an array, we avoid comparing every pair directly. Instead, we count the number of ones and zeros at each bit position and use their product to sum the differing pairs. This clever bitwise counting reduces the time complexity from quadratic to linear, making it both fast and elegant.