Sum of Hamming distances

Write codeMedium20 min

Your task

The Hamming distance between two integers is the number of bit positions where they differ. Given an array nums of non-negative integers (each fits in 32 bits), return the sum of Hamming distances over all unordered pairs of elements. There can be up to 10^4 numbers, so an O(n^2) pairwise comparison is too slow.

Examples

total_hamming_distance(nums: list[int]) -> int
Example 1
Inputtotal_hamming_distance([4, 14, 2])
Output6