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] <= 109The key idea in #477 Total Hamming Distance is to compute the sum of Hamming distances for every pair of numbers in the array. A naive approach would compare every pair and count differing bits, but this leads to O(n^2) comparisons, which becomes inefficient for large inputs.
A more efficient strategy uses bit manipulation. Instead of comparing pairs directly, analyze each bit position (0–31 for standard integers). For a given bit position, count how many numbers have that bit set (1) and how many do not (0). Every pair consisting of one 1 and one 0 contributes exactly one to the Hamming distance. Therefore, the contribution for that bit is count_ones * count_zeros.
By repeating this calculation for all bit positions and summing the results, you obtain the total Hamming distance. This reduces the time complexity to O(n * B), where B is the number of bits (typically 32). The algorithm only uses a few counters, resulting in O(1) extra space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute Force Pair Comparison | O(n^2 * B) | O(1) |
| Bit Counting (Optimal) | O(n * B) | O(1) |
Knowledge Center
This approach involves calculating the Hamming distance for each pair of numbers by comparing their binary representations. This naive method checks each bit position from the least significant bit to the most significant bit for each pair.
Time Complexity: O(n^2 * k) where n is the number of numbers and k is the number of bits per integer (32).
Space Complexity: O(1)
1#include <iostream>
2#include <vector>
3
4int hammingDistance(int x, int y) {
5 int xorValue = x ^ y, count = 0;
6 while (xorValue) {
7 count += xorValue & 1;
8 xorValue >>= 1;
9 }
10 return count;
11}
12
13int totalHammingDistance(std::vector<int>& nums) {
14 int totalDistance = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
totalDistance += hammingDistance(nums[i], nums[j]);
}
}
return totalDistance;
}
int main() {
std::vector<int> nums = {4, 14, 2};
std::cout << totalHammingDistance(nums) << std::endl; // Output: 6
return 0;
}
This C++ code follows the same logic. It uses the hammingDistance function to count differing bits between two integers, and totalHammingDistance to compute the total distance.
For each bit position, count how many numbers have that bit set. The number of pairs from two sets, one having the bit set and the other not, can be computed directly. This reduces the complexity significantly.
Time Complexity: O(n * k) where n is the array size and k is 32 (number of bits).
Space Complexity: O(1)
1def
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of Hamming distance and bit manipulation problems are commonly discussed in technical interviews at FAANG and similar companies. This problem tests understanding of bitwise operations, optimization, and recognizing patterns that reduce pairwise comparisons.
Each differing bit between two numbers contributes exactly one to their Hamming distance. By counting how many numbers have 1s and 0s at a specific bit, you can determine how many pairs differ at that position. This avoids checking every pair individually.
The optimal approach uses bit manipulation by evaluating each bit position across all numbers. For every bit, count how many numbers have a 1 and how many have a 0, then multiply these counts to get the contribution. Summing this for all bits yields the total Hamming distance efficiently.
The optimal solution typically uses simple counters and bit operations rather than complex data structures. Arrays or variables track the number of set bits at each position, keeping the implementation lightweight and efficient.
This Python implementation counts the number of set bits at each position over all numbers. It calculates the total Hamming distance by the contribution from each bit position using bit_count * (n - bit_count).