Skip to main content
LeetCode 477, Medium. Topics: Array, Math, Bit Manipulation. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

The <a href=“https://en.wikipedia.org/wiki/Hamming_distance” target=“_blank”>Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different. Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.

Examples

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^9
  • The answer for the given input will fit in a 32-bit integer.
Follow up: Could you solve this problem with a linear runtime?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026