Problem p03244 (python)

Fixed submission:
from collections import Counter
import heapq

N = int(input())
Vs = list(map(int,input().split()))

if len(set(Vs)) == 1:
  print(N//2)
  exit()

Veve, Vodd = [],[]
idx = 0
for v in Vs:
  if idx%2 == 0: Veve.append(v)
  else: Vodd.append(v)
  idx += 1

Veve_cnt, Vodd_cnt = [], []
[heapq.heappush(Veve_cnt, [-num,val]) for val, num in Counter(Veve).items()]
[heapq.heappush(Vodd_cnt, [-num,val]) for val, num in Counter(Vodd).items()]

neg_num, val = heapq.heappop(Veve_cnt)
eve_val = val
eve_num = -neg_num

neg_num, val = heapq.heappop(Vodd_cnt)
odd_val = val
odd_num = -neg_num

if eve_val != odd_val:
  print(N-eve_num-odd_num)
else:
  if len(Vodd_cnt) != 0:odd_num2 = -heapq.heappop(Vodd_cnt)[0]
  else:odd_num2 = -float('inf')

  if len(Veve_cnt) != 0:eve_num2 = -heapq.heappop(Veve_cnt)[0]
  else:eve_num2 = -float('inf')

  print(min(N-eve_num2-odd_num,N-eve_num-odd_num2))

Buggy submission (verdict: Runtime Error):
N = int(input())
V = list(map(int, input().split()))

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

even_idx_cnt = sorted(even_idx_cnt.items(), key=lambda  x:x[1], reverse=True)
odd_idx_cnt = sorted(odd_idx_cnt.items(), key=lambda  x:x[1], reverse=True)

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
elif len(even_idx_cnt[0][0]) == 1:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
elif len(odd_idx_cnt[0][0]) == 1:
    print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
else:
    if even_idx_cnt[1][0] > odd_idx_cnt[1][0]:
        print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
    else:
        print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])

Buggy submission (verdict: Runtime Error):
N = int(input())
V = list(map(int, input().split()))

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

even_idx_cnt = sorted(even_idx_cnt.items(), key=lambda  x:x[1], reverse=True)
odd_idx_cnt = sorted(odd_idx_cnt.items(), key=lambda  x:x[1], reverse=True)

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
elif even_idx_cnt[0][0] > odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
else:
    print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
N = int(input())
V = list(map(int, input().split()))

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

even_idx_cnt = sorted(even_idx_cnt.items(), key=lambda  x:x[1], reverse=True)
odd_idx_cnt = sorted(odd_idx_cnt.items(), key=lambda  x:x[1], reverse=True)

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
elif len(even_idx_cnt[0][0]) == 1:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
elif len(odd_idx_cnt[0][0]) == 1:
    print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
else:
    if even_idx_cnt[1][0] > odd_idx_cnt[1][0]:
        print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
    else:
        print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])

Buggy submission (verdict: Runtime Error):
N = int(input())
V = list(map(int, input().split()))

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

even_idx_cnt = sorted(even_idx_cnt.items(), key=lambda  x:x[1], reverse=True)
odd_idx_cnt = sorted(odd_idx_cnt.items(), key=lambda  x:x[1], reverse=True)

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
elif even_idx_cnt[0][0] > odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
else:
    print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
N = int(input())
V = list(map(int, input().split()))

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

even_idx_cnt = sorted(even_idx_cnt.items(), key=lambda  x:x[1], reverse=True)
odd_idx_cnt = sorted(odd_idx_cnt.items(), key=lambda  x:x[1], reverse=True)

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
else:
    if even_idx_cnt[1][0] > odd_idx_cnt[1][0]:
        print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
    else:
        print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])

Buggy submission (verdict: Runtime Error):
from collections import Counter
import heapq

def find_key_from_value(input_dict, target_value):
  return  [key for key, value in input_dict.items() if value == target_value][0]

N = int(input())
V= list(map(int,input().split()))
eve, odd  = Counter([V[i] for i in range(0,N,2)]), Counter([V[i] for i in range(1,N,2)])
if len(eve) == len(odd):
  print(N//2)
  exit()

eve_largests = heapq.nlargest(2,eve.values())
odd_largests = heapq.nlargest(2,odd.values())

if eve_largests[0] != odd_largests[0]:
  eve_largest, odd_largest = eve_largests[0], odd_largests[0]
else:
  if len(eve_largests[0]) == 1 and len(odd_largests[0]) == 2:
    eve_largest, odd_largest = eve_largests[0], odd_largests[1]
  elif len(eve_largests[0]) == 2 and len(odd_largests[0]) == 1:
    eve_largest, odd_largest = eve_largests[1], odd_largests[0]
  else:
    if eve_largests[1] >= odd_largests[1]:
      eve_largest, odd_largest = eve_largests[1], odd_largests[0]
    else:
       eve_largest, odd_largest = eve_largests[0], odd_largests[1]
    
eve_key = find_key_from_value(eve, eve_largest)
odd_key = find_key_from_value(odd, odd_largest)  
  
print(N-eve[eve_key]-odd[odd_key])