Tap here to switch tabs
Problem
Submissions

Problem: Total Hamming Distance

Total Hamming Distance requires returning the sum of bit differences for every unordered pair of integers in an array, where each differing binary position adds one.

Statement

Given an integer array nums, calculate the total Hamming distance between every pair of distinct elements.

The Hamming distance between two integers is the number of bit positions at which their binary representations differ.

Return the sum of the Hamming distances for all unordered pairs of elements in nums.

Constraints:

  • 1≤1 \leq nums.length ≤104\leq 10^4

  • 0≤0 \leq nums[i] ≤109\leq 10^9

  • The result fits in a 3232 bit integer.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Total Hamming Distance

Total Hamming Distance requires returning the sum of bit differences for every unordered pair of integers in an array, where each differing binary position adds one.

Statement

Given an integer array nums, calculate the total Hamming distance between every pair of distinct elements.

The Hamming distance between two integers is the number of bit positions at which their binary representations differ.

Return the sum of the Hamming distances for all unordered pairs of elements in nums.

Constraints:

  • 1≤1 \leq nums.length ≤104\leq 10^4

  • 0≤0 \leq nums[i] ≤109\leq 10^9

  • The result fits in a 3232 bit integer.