Detailed Solution in Python


#1

All the numbers in array are 32-bit integers
We check the number of elements in array having bit at ith position = 1 --> x
We check the number of elements in array having bit at ith position = 0 --> y ( intuitively len(A)-x )
Now , for that ith bit we can say that each of the x numbers can be combined with y numbers ( they will have different bit at same position ) .Similarly ,each of the y numbers can be combined with x numbers ( again , because different bit at same position) --> giving us -->xy + yx .
Calculate this for all the 32 bits.

def hammingDistance(self, A):

        ans=0
        for i in range(0,32):
            x=0
            for j in range(len(A)):
                if int(A[j] & 1<<i):
                    x= x+ 1
            y=len(A)-x
            ans= ans + x*y*2
        return ans%1000000007

#2

elegant piece of code


#3

Amazing Code. Love it