477. Total Hamming Distance
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the sum of Hamming distances between all possible pairs of numbers in a given array nums. The Hamming distance between two integers is the number of bit positions at which their corresponding bits are different.
Solution
Total Hamming Distance in Python
class Solution:
def totalHammingDistance(self, nums: list[int]) -> int:
total_distance = 0
n = len(nums)
# The constraint is nums[i] <= 10^9, which is less than 2^30.
# So, we only need to check up to the 30th bit. Looping 31 times covers bits 0 to 30.
for i in range(31):
# For each bit position, count how many numbers have this bit set.
ones = 0
for num in nums:
# Check if the i-th bit is set (1)
if (num >> i) & 1:
ones += 1
# The number of numbers with the i-th bit not set (0).
zeros = n - ones
# For the current bit i, the total distance is the number of pairs
# with different bits. This is calculated by (number of 0s) * (number of 1s).
total_distance += ones * zeros
return total_distance
Total Hamming Distance in Java
class Solution {
public int totalHammingDistance(int[] nums) {
int totalDistance = 0;
int n = nums.length;
// The constraint is nums[i] <= 10^9, which is less than 2^30.
// So, we only need to check up to the 30th bit. Looping 31 times covers bits 0 to 30.
for (int i = 0; i < 31; i++) {
// For each bit position, count how many numbers have this bit set.
int ones = 0;
for (int num : nums) {
// Check if the i-th bit is set (1)
if (((num >> i) & 1) == 1) {
ones++;
}
}
// The number of numbers with the i-th bit not set (0).
int zeros = n - ones;
// For the current bit i, the total distance is the number of pairs
// with different bits. This is calculated by (number of 0s) * (number of 1s).
totalDistance += ones * zeros;
}
return totalDistance;
}
}
Total Hamming Distance in C++
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int totalDistance = 0;
int n = nums.size();
// The constraint is nums[i] <= 10^9, which is less than 2^30.
// So, we only need to check up to the 30th bit. Looping 31 times covers bits 0 to 30.
for (int i = 0; i < 31; ++i) {
// For each bit position, count how many numbers have this bit set.
int ones = 0;
for (int num : nums) {
// Check if the i-th bit is set (1)
if ((num >> i) & 1) {
ones++;
}
}
// The number of numbers with the i-th bit not set (0).
int zeros = n - ones;
// For the current bit i, the total distance is the number of pairs
// with different bits. This is calculated by (number of 0s) * (number of 1s).
totalDistance += ones * zeros;
}
return totalDistance;
}
};
Total Hamming Distance in TypeScript
function totalHammingDistance(nums: number[]): number {
let totalDistance = 0;
const n = nums.length;
// The constraint is nums[i] <= 10^9, which is less than 2^30.
// So, we only need to check up to the 30th bit. Looping 31 times covers bits 0 to 30.
for (let i = 0; i < 31; i++) {
// For each bit position, count how many numbers have this bit set.
let ones = 0;
for (const num of nums) {
// Check if the i-th bit is set (1)
if ((num >> i) & 1) {
ones++;
}
}
// The number of numbers with the i-th bit not set (0).
const zeros = n - ones;
// For the current bit i, the total distance is the number of pairs
// with different bits. This is calculated by (number of 0s) * (number of 1s).
totalDistance += ones * zeros;
}
return totalDistance;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.