Problem:
 Score : 300 points    Problem Statement A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:   For each i = 1,2,..., n-2 , a_i = a_{i+2} .  Exactly two different numbers appear in the sequence.   You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.      Constraints  2 \leq n \leq 10^5  n is even.  1 \leq v_i \leq 10^5  v_i is an integer.         Input Input is given from Standard Input in the following format:  n  v_1  v_2  ...  v_n       Output Print the minimum number of elements that needs to be replaced.        Sample Input 1 4
3 1 3 2      Sample Output 1 1  The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1 .       Sample Input 2 6
105 119 105 119 105 119      Sample Output 2 0  The sequence 105,119,105,119,105,119 is /\/\/\/.       Sample Input 3 4
1 1 1 1      Sample Output 3 2  The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.

Solution (Python):

import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(n: int, v: "List[int]"):
    e = v[0::2]
    o = v[1::2]

    ec = sorted([(vv, k) for k, vv in Counter(e).items()], reverse=True) + [(0, None)]
    oc = sorted([(vv, k) for k, vv in Counter(o).items()], reverse=True) + [(0, None)]

    ee1 = ec[0]
    oo1 = oc[1] if oc[0][1] == ee1[1] else oc[0]
    ret1 = len(v) - ee1[0] - oo1[0]

    oo2 = oc[0]
    ee2 = ec[1] if ec[0][1] == oo2[1] else ec[0]
    ret2 = len(v) - ee2[0] - oo2[0]
   
    return min(ret1, ret2)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    v = [ int(next(tokens)) for _ in range(n) ]  
    result = solve(n, v)
    print(result)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
v = list(map(int, input().split()))
odd_dic = {}
even_dic = {}
for a in range(n):
    b = v[a]
    if a % 2 == 0:
        dic = odd_dic
    else:
        dic = even_dic
    if b in dic:
        dic[b] += 1
    else:
        dic[b] = 1
answer = n
odd_dic = sorted(odd_dic.items(), key=lambda x:x[1])
even_dic = sorted(even_dic.items(), key=lambda x:x[1])
if odd_dic[-1][0] == even_dic[-1][0]:
    answer -= odd_dic[-1][1]
    tmp = 0
    if len(even_dic) > 1:
        tmp = even_dic[-2][1]
    if len(odd_dic) > 1:
        tmp = max(odd_dic[-2][1], tmp)
    answer -= tmp
else:
    answer -= odd_dic[-1][1]
    answer -= even_dic[-1][1]
print(answer)

Solution (Python):
from collections import Counter
_ = input().strip()
V = list(map(int, input().strip().split(' ')))
odd_c = Counter(V[0::2]).most_common()
even_c = Counter(V[1::2]).most_common()
if odd_c[0][0] != even_c[0][0]:
  print(len(V)-odd_c[0][1]-even_c[0][1])
else:
  if len(odd_c) == 1 and len(even_c) == 1:
    print(len(V)//2)
  elif len(odd_c) == 1 and len(even_c) != 1:
    print(len(V)//2-even_c[1][1])
  elif len(odd_c) != 1 and len(even_c) == 1:
    print(len(V)//2-odd_c[1][1])
  else:
    print(len(V)-max(odd_c[0][1] + even_c[1][1], odd_c[1][1] + even_c[0][1]))

Solution (Python):
import collections

n = int(input())
v = [int(i) for i in input().split()] 

odd=v[1::2]
even=v[::2]

if len(collections.Counter(v))==1:
    max_eq = max(len(odd),len(even))
    print(n - max_eq)
    exit()

odd_most = collections.Counter(odd).most_common(2)
even_most = collections.Counter(even).most_common(2)

max_count=0
for odd_top in odd_most:
    for even_top in even_most:
        if odd_top[0] == even_top[0]:
            continue
        max_count = max(max_count,odd_top[1]+even_top[1])
print(n-max_count)

Solution (Python):


from collections import Counter

n = int(input())
v = [int(s) for s in input().split()]

even = v[::2]
odd = v[1::2]

even_c = Counter(even)
odd_c = Counter(odd)
even_list = even_c.most_common()
odd_list = odd_c.most_common()

if even_list[0][0] != odd_list[0][0]:
    print(n - even_list[0][1] - odd_list[0][1])
else:
    if len(even_list) > 1 and len(odd_list) > 1:
        print(min(n - even_list[0][1] - odd_list[1][1], n - even_list[1][1] - odd_list[0][1]))
    elif len(even_list) == 1 and len(odd_list) == 1:
        print(int(n / 2))
    elif len(even_list) == 1:
        print(n - even_list[0][1] - odd_list[1][1])
    else:
        print(n - even_list[1][1] - odd_list[0][1])

Solution (Python):
from collections import Counter

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

a1 = list(Counter(V[::2]).items())
a2 = list(Counter(V[1::2]).items())

a1.sort(key=lambda a: a[1], reverse=True)
a2.sort(key=lambda a: a[1], reverse=True)

if a1[0][0] != a2[0][0]:
    print(N - a1[0][1] - a2[0][1])
else:
    if len(a1) == 1 and len(a2) == 1:
        print(N // 2)
    elif len(a1) == 1:
        print(N - a2[1][1])
    elif len(a2) == 1:
        print(N - a1[1][1])
    else:
        print(min(N - a1[1][1] - a2[0][1], N - a1[0][1] - a2[1][1]))

Solution (Python):
import math
N = int( input())
temp = list(map(int,input().split(" ")))
ans = 10**20
odd =[v for i,v in enumerate(temp) if i % 2 == 1]
even =[v for i,v in enumerate(temp) if i % 2 == 0]
dicodd = {}
diceven = {}
for i in temp:
    if i not in dicodd:
        dicodd[i] = 0
    if i not in diceven:
        diceven[i] = 0
for o in odd:
    dicodd[o] += 1
for e in even:
    diceven[e] += 1


maxOdd = max(dicodd.items(),key= lambda x:x[1])
dicodd.pop(maxOdd[0])

max2Odd = (0,0)
if len(dicodd) != 0:
    max2Odd = max(dicodd.items(),key= lambda x:x[1])

maxEven = max(diceven.items(),key= lambda x:x[1])
diceven.pop(maxEven[0])

max2Even = (0,0)
if len(diceven) != 0:
    max2Even = max(diceven.items(),key= lambda x:x[1])

if maxOdd[0] != maxEven[0]:
    print(N - maxOdd[1] - maxEven[1])
else:
    ans1 = N - maxOdd[1] - max2Even[1]
    ans2 = N - max2Odd[1] - maxEven[1]
    print(min(ans1,ans2))

Solution (Python):
import collections

n=int(input())
v=list(map(int,input().split(' ')))

v_2=[]
v_1=[]
for i,t in enumerate(v):
  if i%2==0:
    v_2.append(t)
  else:
    v_1.append(t)

v_2=collections.Counter(v_2)
v_1=collections.Counter(v_1)

if v_2.most_common()[0][0] != v_1.most_common()[0][0]:
    print(len(v)-v_2.most_common()[0][1]-v_1.most_common()[0][1])
else:
    if v_2.most_common()[0][1] > v_1.most_common()[0][1]:
      print(len(v)-v_2.most_common()[0][1]-v_1.most_common()[1][1])
    elif v_2.most_common()[0][1] < v_1.most_common()[0][1]:
      print(len(v)-v_2.most_common()[1][1]-v_1.most_common()[0][1])
    else:
        if v_2.most_common()[0][1]==len(v)/2:
            print(int(len(v)/2))
        else:
            if v_2.most_common()[1][1]>=v_1.most_common()[1][1]:
                print(len(v)-v_2.most_common()[1][1]-v_1.most_common()[0][1])
            else:
                print(len(v)-v_2.most_common()[0][1]-v_1.most_common()[1][1])

Solution (Python):
import collections

n = int(input())
n_half = int(n / 2)
v = list(map(int, input().split()))

odd = []
even = []
for i in range(n_half):
  odd.append(v[i*2])
  even.append(v[i*2+1])

oc = collections.Counter(odd)
ec = collections.Counter(even)
ocm = oc.most_common()
ecm = ec.most_common()
if ocm[0][0] != ecm[0][0]:
  print(n - ocm[0][1] - ecm[0][1])
else:
  if ocm[0][1] == n_half and ecm[0][1] == n_half:
    print(n_half)
  elif ocm[0][1] == n_half:
    print(n_half - ecm[1][1])
  elif ecm[0][1] == n_half:
    print(n_half - ocm[1][1])
  else:
    print(min(n - ocm[0][1] - ecm[1][1], n - ocm[1][1] - ecm[0][1]))

Solution (Python):

from collections import defaultdict

def calc(data):
    count = defaultdict(int)
    for num in data:
        count[num] += 1

    count_data = []
    for i, j in count.items():
        count_data.append([i, j])

    count_data.sort(key=lambda x: -x[1])
    return count_data


def main():
    num = int(input())
    data = list(map(int, input().split()))

    data_e = data[::2]
    data_o = data[1::2]

    count_e = calc(data_e)
    count_o = calc(data_o)

    if count_e[0][0] != count_o[0][0]:
        ans = num - count_e[0][1] - count_o[0][1]
    elif len(count_e) == 1 and len(count_o) == 1:
        ans = num - count_e[0][1]
    elif len(count_e) == 1:
        ans1 = num - count_o[0][1]
        ans2 = num - count_e[0][1] - count_o[1][1]
        ans = min(ans1, ans2)
    elif len(count_o) == 1:
        ans1 = num - count_e[0][1]
        ans2 = num - count_o[0][1] - count_e[1][1]
        ans = min(ans1, ans2)

    else:
        ans1 = num - count_e[0][1] - count_o[1][1]
        ans2 = num - count_e[1][1] - count_o[0][1]
        ans = min(ans1, ans2)

    print(ans)




if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(n: int, v: "List[int]"):
    counts = [{}, {}]
    for i in range(n):
        if v[i] in counts[i % 2]:
            counts[i % 2][v[i]] += 1
        else:
            counts[i % 2][v[i]] = 1
    l1 = list(counts[0].items()) + [(-1, 0)]
    l2 = list(counts[1].items()) + [(-1, 0)]
    l1.sort(reverse=True, key=lambda x: x[1])
    l2.sort(reverse=True, key=lambda x: x[1])
    
    
    tmp = 0
    if l1[0][0] == l2[0][0]:
        tmp = max(l1[0][1] + l2[1][1], l1[1][1] + l2[0][1])
    else:
        tmp = l1[0][1] + l2[0][1]
    ret = n - tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    v = [ int(next(tokens)) for _ in range(n) ]  
    solve(n, v)

if __name__ == '__main__':
    main()

Solution (Python):
import collections , sys



n = int(input())
v = [int(i) for i in input().split()]


c2 = collections.Counter(v[::2])
c1 = collections.Counter(v[1::2])

if len(list(set(v))) == 1:
    print(n//2)
    sys.exit()

ceven = c2.most_common()[0][:]
codd = c1.most_common()[0][:]

if ceven[0] == codd[0]:
    if ceven[1] > codd[1]:
        codd = c1.most_common()[1][:]
    elif ceven[1] < codd[1]:
        ceven = c2.most_common()[1][:]
    else:
        ceven2 = c2.most_common()[1][:]
        codd2 = c1.most_common()[1][:]
        if  ceven2[1] > codd2[1]:
            ceven = ceven2
        else :
            codd = codd2
            
ans = n - codd[1] - ceven[1]

print(ans)

Solution (Python):
n, = (int(x) for x in input().split())
vi_arr = [int(x) for x in input().split()]
odd_dict = {"a":0}
even_dict = {"a":0}
for i, vi in enumerate(vi_arr):
    if i%2 == 0:
        idict = odd_dict
    else:
        idict = even_dict
    idict.setdefault(vi, 0)
    idict[vi] += 1

odd_hist = sorted(odd_dict.items(), key=lambda x:x[1], reverse=True)
even_hist = sorted(even_dict.items(), key=lambda x:x[1], reverse=True)

max_odd = odd_hist[0][1]
max_even = even_hist[0][1]
if odd_hist[0][0] == even_hist[0][0]:
    if odd_hist[0][1]+even_hist[1][1] > even_hist[0][1]+odd_hist[1][1]:
        max_even = even_hist[1][1]
    else:
        max_odd = odd_hist[1][1]

print(len(vi_arr)-max_odd-max_even)

Solution (Python):
from collections import defaultdict

n = int(input())
V = list(map(int, input().split()))
 
def mode(d):
    return sorted(d.items(), key=lambda x: x[1], reverse=True)
 
if len(set(V)) == 1:
    ans = n // 2
else:
    o = defaultdict(int)
    e = defaultdict(int)
    for i in range(n):
        if i % 2 == 0:
            e[V[i]] += 1
        else:
            o[V[i]] += 1
    mode_o = mode(o)
    mode_e = mode(e)
    if mode_o[0][0] != mode_e[0][0]:
        ans = n - mode_o[0][1] - mode_e[0][1]
    else:
        ans = min(n - mode_o[0][1] - mode_e[1][1], n - mode_o[1][1] - mode_e[0][1])
print(ans)

Solution (Python):
n=int(input())
s=list(map(int,input().split()))
d1=dict()
d2=dict()
for i in range(0,n,2):
  if s[i] not in d1:
    d1[s[i]]=1
  else:
    d1[s[i]]+=1
for i in range(1,n,2):
  if s[i] not in d2:
    d2[s[i]]=1
  else:
    d2[s[i]]+=1
dmk,dm,dmmk,dmm=[],0,[],0
for i in d1.keys():
  if dm<d1[i]:
    dmm=dm
    dmmk=dmk
    dm=d1[i]
    dmk=[i]
  elif d1==d1[i]:
    dmk+=[i]
  elif dmm<d1[i]:
    dmm=d1[i]
    dmmk=[i]
  elif dmm==d1[i]:
    dmmk+=[i]
d1=[dm,dmk,dmm,dmmk]
dmk,dm,dmmk,dmm=[],0,[],0
for i in d2.keys():
  if dm<d2[i]:
    dmm=dm
    dmmk=dmk
    dm=d2[i]
    dmk=[i]
  elif d2==d2[i]:
    dmk+=[i]
  elif dmm<d2[i]:
    dmm=d2[i]
    dmmk=[i]
  elif dmm==d2[i]:
    dmmk+=[i]
d2=[dm,dmk,dmm,dmmk]
if len(d1[1])>1 or len(d2[1])>1 or d1[1]!=d2[1]:
  print(n-d1[0]-d2[0])
else:
  print(n-max(d1[0]+d2[2],d1[2]+d2[0]))

Solution (Python):
import collections


def split_even_odd_by_index(target_list):
    odd = []
    even = []
    for i in range(len(target_list)):
        if i % 2 == 0:
            even.append(target_list[i])
        else:
            odd.append(target_list[i])

    return [even, odd]


if __name__ == '__main__':
    n = int(input())
    number_list = list(map(int, input().split()))
    split_list = split_even_odd_by_index(number_list)
    even_list = split_list[0]
    odd_list = split_list[1]

    even_dic = collections.Counter(even_list).most_common()
    odd_dic = collections.Counter(odd_list).most_common()

    even_first = even_dic[0]
    if len(even_dic) != 1:
        even_second = even_dic[1]
    else:
        even_second = (0, 0)

    odd_first = odd_dic[0]
    if len(odd_dic) != 1:
        odd_second = odd_dic[1]
    else:
        odd_second = (0, 0)

    if even_first[0] != odd_first[0]:
        print(len(even_list) - even_first[1] + len(odd_list) - odd_first[1])
    else:
        print(min(len(even_list) - even_first[1] + len(odd_list) - odd_second[1], len(even_list) - even_second[1] + len(odd_list) - odd_first[1]))

Solution (Python):
from collections import Counter as ct

n = int(input())
s = input().split()
a = ct(s[::2])
b = ct(s[1::2])
sa = sorted(a.items(), key=lambda x: -x[1])
sb = sorted(b.items(), key=lambda x: -x[1])
if sa[0][0] == sb[0][0]:
    if len(sa)*len(sb)==1:
        dt=n//2
    elif sa[0][1] - sa[1][1] < sb[0][1] - sb[1][1] or len(sb)==1:
        dt = sa[1][1] + sb[0][1]
    else:
        dt = sa[0][1] + sb[1][1]
else:
    dt = sa[0][1] + sb[0][1]
print(sum([x for x in a.values()]+[y for y in b.values()])-dt)

Solution (Python):
import collections
n=int(input())
v=list(map(int,input().split()))
vo=v[0::2]
ve=v[1::2]
co=collections.Counter(vo)
ce=collections.Counter(ve)
cmo=co.most_common()
cme=ce.most_common()
ans=n-cmo[0][1]-cme[0][1]
if cmo[0][0]==cme[0][0]:
    if cmo[0][1]>cme[0][1]:
       if cmo[0][1]!=n//2:
           ans=n-cmo[0][1]-cme[1][1]
    elif cmo[0][1]<cme[0][1]:
        if cme[0][1]!=n//2:
            ans=n-cmo[1][1]-cme[0][1]
    else:
        if cmo[0][1]==n//2:
            ans=n//2
        else:
            ans=n-cmo[0][1]-max(cmo[1][1],cme[1][1])
print(ans)

Solution (Python):
n = int(input())
v = list(map(int, input().split()))

vv = [[], []]
for i in range(n):
    vv[i % 2].append(v[i])

import collections
items0 = sorted(collections.Counter(vv[0]).items(), key=lambda x: -x[1])
items1 = sorted(collections.Counter(vv[1]).items(), key=lambda x: -x[1])
key0 = [x[0] for x in items0]
key1 = [x[0] for x in items1]
val0 = [x[1] for x in items0]
val1 = [x[1] for x in items1]

ans = n - val0[0] - val1[0]

if key0[0] == key1[0]:
    tmp0=tmp1=1000000000000000
    if len(items0) > 1:
        tmp0 = n - val0[1] - val1[0]
    if len(items1) > 1:
        tmp1 = n - val0[0] - val1[1]
    ans = min(tmp0, tmp1)
    if len(items0) == 1 and len(items1) == 1:
        ans=n//2
print(ans)

Solution (Python):
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

va = v[::2]
vb = v[1::2]

va_ = Counter(va).most_common()
vb_ = Counter(vb).most_common()

if va_[0][0] == vb_[0][0]:
    if len(va_) == 1 or len(vb_) == 1:
        if len(va_) == 1 and len(vb_) == 1:
            print(len(v) // 2)
        elif len(va_) == 1:
            if va_[0][0] == vb_[0][0]:
                print(len(vb) - vb_[1][1])
            else:
                print(len(vb) - vb_[0][1])
        else:
            if va_[0][0] == vb_[0][0]:
                print(len(va) - va_[1][1])
            else:
                print(len(va) - va_[0][1])
        exit()
    if va_[0][1] + vb_[1][1] > va_[1][1] + vb_[0][1]:
        print(len(va) - va_[0][1] + len(vb) - vb_[1][1])
    else:
        print(len(va) - va_[1][1] + len(vb) - vb_[0][1])
else:
    print(len(va) - va_[0][1] + len(vb) - vb_[0][1])

Solution (Python):
n = int(input())
v = list(map(int, input().split()))

array1 = v[::2]
array2 = v[1::2]

dict1 = {}
dict2 = {}
for i in array1:
  if i not in dict1:
    dict1[i] = 1
  else:
    dict1[i] = dict1[i] + 1

for i in array2:
  if i not in dict2:
    dict2[i] = 1
  else:
    dict2[i] = dict2[i] + 1

sort_array1 = []
sort_array2 = []
for k, v in sorted(dict1.items(), key=lambda x: -x[1]):
    sort_array1.append({k:v})
for k, v in sorted(dict2.items(), key=lambda x: -x[1]):
    sort_array2.append({k:v})

one_num1 = 0
one_num2 = 0

for k1,v1 in sort_array1[0].items():
  if len(sort_array1) != 1:
    one_num1 = len(array1) - v1
  for k2,v2 in sort_array2[0].items():
    if len(sort_array2) != 1:
      one_num2 = len(array2) - v2
    if k1 != k2:
      print(one_num1 + one_num2)
      break

    if len(sort_array1) == 1 and len(sort_array2) == 1:
      print(len(array1))
      break
    if len(sort_array1) == 1:
      for k,v in sort_array2[1].items():
        two_num2 = v
      print(1+two_num2)
      break
    if len(sort_array2) == 1:
      for k,v in sort_array1[1].items():
        two_num1 = v
      print(1+two_num1)
      break

    for k,v in sort_array1[1].items():
      two_num1 = len(array1) - v
    for k,v in sort_array2[1].items():
      two_num2 = len(array2) - v
    if two_num1 <= two_num2:
      print (one_num2 + two_num1)
    else:
      print (one_num1 + two_num2)

Solution (Python):
import collections as col
n = int(input())
s = list(map(int,input().split()))
t = s[0::2]
r = s[1::2]
a = col.Counter(t)
b = col.Counter(r)
a = sorted(a.items(), key=lambda x:x[1], reverse=True)
b = sorted(b.items(), key=lambda x:x[1], reverse=True)
a.append((-1,0))
b.append((-1,0))
if a[0][0] != b[0][0]:
    print(n - a[0][1] - b[0][1])
else:
    if a[1][1] > b[1][1]:
        print(n - a[1][1] - b[0][1])
    else:
        print(n - a[0][1] - b[1][1])

Solution (Python):
n = int(input())
v_list = list(map(int, input().split()))
nums_even = {-1: 0}
nums_odd = {-1: 0}
for i, v in enumerate(v_list):
    if i % 2 == 0:
        nums_even.setdefault(v, 0)
        nums_even[v] += 1
    else:
        nums_odd.setdefault(v, 0)
        nums_odd[v] += 1
even_max = -1
even_max2 = -1
for key, val in nums_even.items():
    if val > nums_even[even_max]:
        even_max2 = even_max
        even_max = key
    elif val > nums_even[even_max2]:
        even_max2 = key
odd_max = -1
odd_max2 = -1
for key, val in nums_odd.items():
    if val > nums_odd[odd_max]:
        odd_max2 = odd_max
        odd_max = key
    elif val > nums_odd[odd_max2]:
        odd_max2 = key
if even_max != odd_max:
    print(n - nums_even[even_max] - nums_odd[odd_max])
else:
    m = max(nums_even[even_max] + nums_odd[odd_max2], nums_even[even_max2] + nums_odd[odd_max])
    print(n - m)

Solution (Python):
from collections import Counter
n = int(input())
list = list(map(int,input().split()))
list_o = list[::2]
list_e = list[1::2]

cnt_o = Counter(list_e).most_common(2)
cnt_e = Counter(list_o).most_common(2)

if cnt_o[0][0] == cnt_e[0][0]:
    if len(cnt_o) == 1 and len(cnt_e) == 1:
        print(int(n/2))
    else:
        print(n-cnt_o[0][1]-max(cnt_o[1][1],cnt_e[1][1]))
else:
    print(n-cnt_o[0][1]-cnt_e[0][1])

Solution (Python):


def solve():
    n = int(input())
    V = [None]
    V.extend(list(map(int, input().split(" "))))
    
    
    tmp = V[1]
    flag = True
    for i in range(2, n+1):
        if tmp == V[i]:
            continue
        flag = False
        break
    if flag:
        print(int(n/2))
        return

    
    odd_dict = {}
    for i in range(1, n+1, 2):
        
        if V[i] in odd_dict.keys():
            odd_dict[V[i]] += 1
            continue
        odd_dict[V[i]] = 1

    
    even_dict = {}
    for i in range(2, n+1, 2):
        
        if V[i] in even_dict.keys():
            even_dict[V[i]] += 1
            continue
        even_dict[V[i]] = 1

    sorted_odds = sorted(odd_dict.items(), key=lambda x: x[1], reverse=True)
    sorted_evens = sorted(even_dict.items(), key=lambda x: x[1], reverse=True)

    
    if sorted_evens[0][0] == sorted_odds[0][0]:
        if len(sorted_odds) == 1:
            ans = n - sorted_odds[0][1] - sorted_evens[1][1]
        elif len(sorted_evens) == 1:
            ans = n - sorted_odds[1][1] - sorted_evens[0][1]
        else:
            ans1 = n - sorted_odds[0][1] - sorted_evens[1][1]
            ans2 = n - sorted_odds[1][1] - sorted_evens[0][1]
            ans = min(ans1, ans2)
    else:
        ans = n - sorted_evens[0][1] - sorted_odds[0][1]

    print(ans)

if __name__ == "__main__":
    solve()

Solution (Python):
from collections import Counter as c

N = int(input())
V = list(map(int,input().split()))
ev = c(V[::2]).most_common(2)
od = c(V[1::2]).most_common(2)

if ev[0][0] == od[0][0]:
    if len(ev) == 1 and len(od) == 1:
        print(N//2)
    else:
        print(N-max(ev[0][1]+od[1][1],ev[1][1]+od[0][1]))
else:
    print(N-ev[0][1]-od[0][1])

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left

def main():
	N = int(input())
	V = input().split(" ")
	
	K = []
	G = []
	for i, val in enumerate(V):
		if i % 2 == 0:
			G.append(val)
		else:
			K.append(val)
	
	k = collections.Counter(K)
	k = sorted(k.items(), key=lambda x:x[1], reverse=True)
	g = collections.Counter(G)
	g = sorted(g.items(), key=lambda x:x[1], reverse=True)
	
	aaa, bbb = k[0]
	ccc, ddd = g[0]
	eee = -1
	fff = -1
	ggg = -1
	hhh = -1
	if len(k) > 1:
		eee, fff = k[1]
	if len(g) > 1:
		ggg, hhh = g[1]
	
	if aaa == ccc:
		if bbb > ddd and ggg != -1:
			g[0] = (ggg, hhh)
			g[1] = (ccc, ddd)
		elif bbb < ddd and eee != -1:
			k[0] = (eee, fff)
			k[1] = (aaa, bbb)
		elif ggg != -1:
			if fff >= hhh:
				k[0] = (eee, fff)
				k[1] = (aaa, bbb)
			else:
				g[0] = (ggg, hhh)
				g[1] = (ccc, ddd)
	
	
	
		
	ans = 0
	sono1 = 0
	sono2 = 0
	for a, b in enumerate(k):
		i, j = b
		if a == 0:
			sono1 = i
			continue
		ans += j
	
	for a, b in enumerate(g):
		i, j = b
		if a == 0:
			sono2 = i
			continue
		ans += j
		
	if ans > 0:
		print(ans)
	elif sono1 != sono2:
		print(ans)
	else:
		print(len(K))
		
		
	
if __name__ == "__main__":
	main()

Solution (Python):
n=int(input())
a=[0]*10**5
b=[0]*10**5
v=list(map(int,input().split()))
v1=[]
v2=[]
if len(set(v))==1:
    print(n//2)
else:
    for i in range(n):
        if i%2==0:
            a[v[i]-1]+=1
            v1.append(v[i])
        else:
            b[v[i]-1]+=1
            v2.append(v[i])
    max1=0
    max2=0
    for j in range(len(a)):
        if max1<a[j]:
            max1=a[j]
            c1=j
    for k in range(len(b)):
        if max2<b[k]:
            max2=b[k]
            c2=k
    if c1!=c2:
        print(len(v1)+len(v2)-max1-max2)
    else:
        a=sorted(a)
        b=sorted(b)
        print(min(n-max1-b[-2],n-max2-a[-2]))

Solution (Python):
n = int(input())
V = list(map(int,input().split()))
a = [0]*100000
b = [0]*100000
v1=[]
v2=[]

if len(set(V)) == 1:
    print(n//2)

else:
    for i in range(n):
        if i%2 == 0:
            a[V[i]-1] += 1
            v1.append(V[i])
        else:
            b[V[i]-1] += 1
            v2.append(V[i])

    max1 = 0
    max2 = 0

    for j in range(len(a)):
        if max1 < a[j]:
            max1 = a[j]
            c1 = j
    for k in range(len(b)):
        if max2 < b[k]:
            max2=b[k]
            c2=k

    if c1 != c2:
        print(n-max1-max2)
    else:
        a.sort()
        b.sort()
        print(min(n-max1-b[-2],n-max2-a[-2]))

Solution (Python):
from collections import Counter
from sys import stdin
n = int(stdin.readline().rstrip())
vl = [int(_) for _ in stdin.readline().rstrip().split()]
odds=Counter(vl[0::2]).most_common()
evens=Counter(vl[1::2]).most_common()
odd_f = odds[0]
even_f = evens[0]
if odd_f != even_f:
    print(n-odd_f[1]-even_f[1])
else:
    try:
        odd_se = odds[1]
    except:
        odd_se = [0,0]
    try:
        even_se = evens[1]
    except:
        even_se = [1,0]
    print(n - max(odd_f[1]+even_se[1], odd_se[1]+even_f[1]))

Solution (Python):
n=int(input())
v=input().split()

m=[{"even":0},{"odd":0}]

for i in range(0, len(v)):
	if v[i] not in m[i%2]:
		m[i%2][v[i]] = 1
	else:
		m[i%2][v[i]] += 1

l = [sorted(z.items(), key=lambda x: x[1], reverse=True) for z in m]

if l[0][0][0] == l[1][0][0]:
	print(min(n-l[0][0][1]-l[1][1][1], n-l[0][1][1]-l[1][0][1]))
else:
	print(n-l[0][0][1]-l[1][0][1])

Solution (Python):
import collections

n = int(input())
v = input().split()

e = [v[i] for i in range(n) if i % 2 == 0]
o = [v[i] for i in range(n) if i % 2 == 1]

c_e = collections.Counter(e).most_common()
c_o = collections.Counter(o).most_common()

e_0 = c_e[0]
o_0 = c_o[0]

if e_0 != o_0:
    print(len(e) - e_0[1] + len(o) - o_0[1])
else:
    if len(c_e) == 1:
        e_1 = [0, 0]
    else:
        e_1 = c_e[1]
    if len(c_o) == 1:
        o_1 = [0, 0]
    else:
        o_1 = c_o[1]
    print(min(len(e)-e_0[1]+len(o)-o_1[1], len(e)-e_1[1]+len(o)-o_0[1]))

Solution (Python):
def larger(x, y):
	if x > y: return x
	else: return y

n = int(raw_input())
v = map(int, raw_input().split())
lst1 = []
lst2 = []
for i in range(0, n):
	if i % 2 == 1: lst1.append(v[i])
	else: lst2.append(v[i])
lst1.sort()
lst2.sort()
firstnumber1 = 0
firstcount1 = 0
secondnumber1 = 0
secondcount1 = 0
firstnumber2 = 0
firstcount2 = 0
secondnumber2 = 0
secondcount2 = 0
tmp1 = lst1[0]
cnt1 = 1
tmp2 = lst2[0]
cnt2 = 1

for i in range(1, n/2):
	if tmp1 != lst1[i]:
		if firstcount1 < cnt1:
			secondnumber1 = firstnumber1
			secondcount1 = firstcount1
			firstnumber1 = tmp1
			firstcount1 = cnt1
		elif secondcount1 < cnt1:
			secondnumber1 = tmp1
			secondcount1 = cnt1
		tmp1 = lst1[i]
		cnt1 = 1
	else: cnt1 += 1
	if tmp2 != lst2[i]:
		if firstcount2 < cnt2:
			secondnumber2 = firstnumber2
			secondcount2 = firstcount2
			firstnumber2 = tmp2
			firstcount2 = cnt2
		elif secondcount2 < cnt2:
			secondnumber2 = tmp2
			secondcount2 = cnt2
		tmp2 = lst2[i]
		cnt2 = 1
	else: cnt2 += 1
if firstcount1 < cnt1:
	secondnumber1 = firstnumber1
	secondcount1 = firstcount1
	firstnumber1 = tmp1
	firstcount1 = cnt1
elif secondcount1 < cnt1:
	secondnumber1 = tmp1
	secondcount1 = cnt1
if firstcount2 < cnt2:
	secondnumber2 = firstnumber2
	secondcount2 = firstcount2
	firstnumber2 = tmp2
	firstcount2 = cnt2
elif secondcount2 < cnt2:
	secondnumber2 = tmp2
	secondcount2 = cnt2

if firstnumber1 == firstnumber2:
	print n - larger(secondcount1+firstcount2, firstcount1+secondcount2)
else:
	print n - firstcount1 - firstcount2

Solution (Python):
import collections

N = int(input())
v = list(map(int,input().rstrip().split()))
list_odd = v[0::2]
list_even = v[1::2]

odd_count = collections.Counter(list_odd).most_common()
even_count = collections.Counter(list_even).most_common()

if len(odd_count) == 1 and len(even_count) == 1:
    if odd_count[0][0] == even_count[0][0]:
        print(N//2)
    else:
        print(0)
elif odd_count[0][0] != even_count[0][0]:
    print(N-odd_count[0][1]-even_count[0][1])
elif len(odd_count) == 1:
    print(N//2-even_count[1][1])
elif len(even_count) == 1:
    print(N//2-odd_count[1][1])
else:
    print(min(N-odd_count[0][1]-even_count[1][1], N-even_count[0][1]-odd_count[1][1]))

Solution (Python):
import collections
n = int(input())
vs = list(map(int, input().split()))
veven = vs[0:n:2]
vodd = vs[1:n:2]
mins = []
flag = False
if veven == vodd and len(set(vodd)) == 1:
    print(n //2)
else:
    ce = collections.Counter(veven)
    co = collections.Counter(vodd)
    cem = ce.most_common()
    com = co.most_common()
    for i, j in cem:
        for k, h in com:
            if i != k:
                mins.append(n-j-h)
                flag = True
                break
        if flag:
            break
    flag = False
    for i, j in com:
        for k, h in cem:
            if i != k:
                mins.append(n-j-h)
                flag = True
                break
        if flag:
            break
    print(min(mins))

Solution (Python):


import collections
n = int(input())
lst = list(map(int,input().split()))
odd = [0]*(n//2)
even = [0]*(n//2)
leng = 0
for i in range(n//2):
    odd[i]=lst[leng]
    leng+=1
    even[i]=lst[leng]
    leng+=1
o1 = collections.Counter(odd).most_common()[0]
e1 = collections.Counter(even).most_common()[0]
if o1[0] != e1[0]:
    ans = n - (o1[1] + e1[1])
else:
    try:
        o2 = collections.Counter(odd).most_common()[1]
    except:
        o2 = [0,0]
    try:
        e2 = collections.Counter(even).most_common()[1]
    except:
        e2 = [1,0]
    ans = n - max(o1[1]+e2[1],o2[1]+e1[1])

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

n = INT()
v = LIST()

v_odd = [v[i] for i in range(n) if i % 2 == 1]
v_even = [v[i] for i in range(n) if i % 2 == 0]

count_odd = Counter(v_odd)
count_even = Counter(v_even)

if count_odd.most_common()[0][0] != count_even.most_common()[0][0]:
	print(int(n/2-count_odd.most_common()[0][1] + n/2 - count_even.most_common()[0][1]))
else:
	if len(count_odd) == 1 and len(count_even) == 1:
		print(int(n/2))
	else:
		if len(count_odd) == 1:
			print(int(n/2 - count_even.most_common()[1][1]))
		elif len(count_even) == 1:
			print(int(n/2 - count_odd.most_common()[1][1]))
		else:
			tmp1 = int(n/2-count_odd.most_common()[1][1] + n/2 - count_even.most_common()[0][1])
			tmp2 = int(n/2-count_odd.most_common()[0][1] + n/2 - count_even.most_common()[1][1])
			print(min(tmp1, tmp2))

Solution (Python):
import collections
def solve():
  n = int(input())
  odd = []
  even = []
  a = list(int(i) for i in input().split())
  for i in range(n):
    if i%2 == 0:
      odd.append(a[i])
    else:
      even.append(a[i])
  ans = 0
  c1 = collections.Counter(odd)
  c2 = collections.Counter(even)
  n1 = len(odd)
  n2 = len(even)
  if len(set(a)) == 1:
    print(n//2)
    exit()
  ans = 10**12
  if c1.most_common()[0][0]  ==   c2.most_common()[0][0]:
    if not len(c2) ==1:
     ans = min((n-c1.most_common()[0][1]-c2.most_common()[1][1]),ans)
    if not len(c1) ==1:
     ans = min((n-c2.most_common()[0][1]-c1.most_common()[1][1]),ans)
  else:
         ans = min(ans,(n1-c1.most_common()[0][1]+n2-c2.most_common()[0][1]))
  print(ans)
                  
                  
solve()

Solution (Python):
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

e = Counter(v[::2]).most_common()
o = Counter(v[1::2]).most_common()

if len(set(v)) == 1:
    ans = n // 2
elif e[0][0] != o[0][0]:
    ans = n - e[0][1] - o[0][1]
else:
    ans = min(n - e[0][1] - o[1][1],
              n - e[1][1] - o[0][1])
print(ans)