Added H solution

This commit is contained in:
Claudio Maggioni 2019-04-04 20:25:17 +02:00
parent 5cb261deae
commit f843a4acc9

View file

@ -2,20 +2,16 @@
import sys import sys
def xor_count(a, b):
xor = a ^ b
i = 0
while xor != 0:
i = i + (xor & 1)
xor = xor >> 1
return i
def bitwise_difference(A): def bitwise_difference(A):
l = len(A)
s = 0 s = 0
for i in range(len(A)): for i in range(32):
for j in range(i+1,len(A)): n1 = 0
s = s + xor_count(A[i], A[j]) for j in range(l):
n1 += A[j] & 1
A[j] = A[j] >> 1
n0 = l - n1
s += n1 * n0
return s return s