Problem p03138 (python)

Fixed submission:
n,k = map(int,input().split())
a = list(map(int,input().split()))

len_a=len(str(bin(max(a))))-2
len_k=len(str(bin(k)))-2

if len_a >= len_k:
  length =len_a
  for i in range(n):
    a[i] = ("0"*length+str(bin(a[i]))[2:])[-len_a:] 
elif len_a < len_k:
  length =len_k
  for i in range(n):
    a[i] = ("0"*length+str(bin(a[i]))[2:])[-len_k:]
    
k = ("0"*length+str(bin(k))[2:])[-length:]

flag = False
for i in range(length):
  count_0 =0
  count_1 =0
  for j in range(n):
    if a[j][i]=="0":
      count_0 += 1
    else:
      count_1 += 1
  #print(count_0,count_1)
  if count_0 <= count_1 and k[i]=="1":
    if i==0:   
      k="0"+k[1:length]
    elif i == length - 1:
      k= k[0:i]+"0"
    else:
      k= k[0:i]+"0"+k[i+1:]
    flag = True
  elif count_0 > count_1 and k[i]=="0" and flag:
    if i==0:   
      k="1"+k[1:length]
    elif i == length - 1:
      k= k[0:i]+"1"
    else:
      k=k[0:i]+"1"+k[i+1:]
      
ans = 0
for i in range(n):
  for j in range(length):
    if a[i][j]!=k[j]:
      ans +=2**(length-1-j)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))

len_a=len(str(bin(max(a))))-2
len_k=len(str(bin(k)))-2

if len_a >= len_k:
  length =len_a
  for i in range(n):
    a[i] = ("0"*length+str(bin(a[i]))[2:])[-len_a:] 
elif len_a < len_k:
  length =len_k
  for i in range(n):
    a[i] = ("0"*length+str(bin(a[i]))[2:])[-len_k:]
    
k = ("0"*length+str(bin(k))[2:])[-length:]

flag = False
for i in range(length):
  count_0 =0
  count_1 =0
  for j in range(n):
    if a[j][i]=="0":
      count_0 += 1
    else:
      count_1 += 1
  #print(count_0,count_1)
  if count_0 <= count_1 and k[i]=="1":
    if i==0:   
      k="0"+k[1:i]
    elif i == length - 1:
      k= k[0:i]+"0"
    else:
      k= k[0:i]+"0"+k[i+1:]
    flag = True
  elif count_0 > count_1 and k[i]=="0" and flag:
    if i==0:   
      k="1"+k[1:i]
    elif i == length - 1:
      k= k[0:i]+"1"
    else:
      k=k[0:i]+"1"+k[i+1:]
      
ans = 0
for i in range(n):
  for j in range(length):
    if a[i][j]!=k[j]:
      ans +=2**(length-1-j)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))

len_a=len(str(bin(max(a))))-2
len_k=len(str(bin(k)))-2

if len_a >= len_k:
  length =len_a
  for i in range(n):
    a[i] = ("0"*1000+str(bin(a[i]))[2:])[-len_a:] 
elif len_a < len_k:
  length =len_k
  for i in range(n):
    a[i] = ("0"*1000+str(bin(a[i]))[2:])[-len_k:]
    
k = ("0"*1000+str(bin(k))[2:])[-length:]

flag = False
for i in range(length):
  count_0 =0
  count_1 =0
  for j in range(n):
    if a[j][i]=="0":
      count_0 += 1
    else:
      count_1 += 1
  #print(count_0,count_1)
  if count_0 <= count_1 and k[i]=="1":
    if i==0:   
      k="0"+k[1:i]
    elif i == length - 1:
      k= k[0:i]+"0"
    else:
      k= k[0:i]+"0"+k[i+1:]
    flag = True
  elif count_0 > count_1 and k[i]=="0" and flag:
    if i==0:   
      k="1"+k[1:i]
    elif i == length - 1:
      k= k[0:i]+"1"
    else:
      k=k[0:i]+"1"+k[i+1:]
ans = 0
for i in range(n):
  for j in range(length):
    if a[i][j]!=k[j]:
      ans +=2**(length-1-j)
print(ans)