<p>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.</p>

<p>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>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,14,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,14,4]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li>
</ul>

class Solution:
    def totalHammingDistance(self, nums: List[int]) -> int:
        cnt = 0
        getbinary = lambda x, n: format(x, 'b').zfill(n)
        bi = []
        for i in range(len(nums)):
            a = getbinary(nums[i], 32)
            bi.append(a)
        
        for i in range(32):
            zero, one = 0, 0
            for b in bi:
                if b[i] == '1':
                    one += 1
                else:
                    zero += 1
            cnt += one * zero
        return cnt 
        
        '''
        cnt = 0

        for i in range(len(nums)-1):
            for k in range(i+1, len(nums)):
                prev = self.getBinary(nums[i])
                curr = self.getBinary(nums[k])
                longone = curr if len(curr) > len(prev) else prev
                shortone = curr if len(curr) < len(prev) else prev

                shortone = '0'*(len(longone) - len(shortone)) + shortone
                for j in range(0, len(longone)):
                    if longone[j] != shortone[j]:
                        cnt += 1
                prev = curr

        return cnt 

    def getBinary(self, num: int):
        res = ''
        for i in range(int(math.sqrt(num)), -1, -1):
            if num >= 2**i:
                res += '1'
                num -= 2**i
            else:
                res += '0'
        return res 
        '''
        
18400