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
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):
l = len(A)
s = 0
for i in range(len(A)):
for j in range(i+1,len(A)):
s = s + xor_count(A[i], A[j])
for i in range(32):
n1 = 0
for j in range(l):
n1 += A[j] & 1
A[j] = A[j] >> 1
n0 = l - n1
s += n1 * n0
return s