Added H solution
This commit is contained in:
parent
5cb261deae
commit
f843a4acc9
1 changed files with 8 additions and 12 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
Reference in a new issue