> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Total Hamming Distance Python Solution

> Tested Python solution for LeetCode 477 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 477, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/total-hamming-distance/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 477   # by problem number
lcpy gen -s total_hamming_distance   # by problem name
```

## Problem

The \<a href="[https://en.wikipedia.org/wiki/Hamming\_distance](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

```
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.
```

```
Input: nums = [4,14,4]
Output: 4
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/total_hamming_distance/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/total_hamming_distance/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * b) where b is the number of bits (32)
    # Space: O(1)
    def total_hamming_distance(self, nums: list[int]) -> int:
        total = 0
        for bit in range(32):
            ones = sum((num >> bit) & 1 for num in nums)
            total += ones * (len(nums) - ones)
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n \* b) where b is the number of bits (32) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.