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):
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
c_even = sorted(list(Counter(v[::2]).items()) +
                [(-1, 0)], key=lambda x: x[1])[::-1]
c_odd = sorted(list(Counter(v[1::2]).items()) +
               [(-1, 0)], key=lambda x: x[1])[::-1]
if c_even[0][0] == c_odd[0][0]:
    ans = n - max(c_even[0][1]+c_odd[1][1], c_even[1][1]+c_odd[0][1])
else:
    ans = n - (c_even[0][1] + c_odd[0][1])
print(ans)

Solution (Python):
from collections import Counter


def inverse_lookup(d: dict, x):
    for k, v in d.items():

        if v == x:
            return k

    
    return None


def num_of_nth_often(lst, nth):
    counter = Counter(lst)
    counted_list = sorted(list(counter.values()), reverse=True)

    if len(counted_list) < nth:
        return 0, None

    else:
        return counted_list[nth - 1], inverse_lookup(counter, counted_list[nth - 1])

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

v_even = []
v_odd = []

for i in range(n):
    if i % 2 == 0:
        v_even.append(v[i])
    else:
        v_odd.append(v[i])

e1 = num_of_nth_often(v_even, 1)
e2 = num_of_nth_often(v_even, 2)
o1 = num_of_nth_often(v_odd, 1)
o2 = num_of_nth_often(v_odd, 2)

if e1[1] != o1[1]:
    rewrite_num = len(v_even) - e1[0] + len(v_odd) - o1[0]
    print(rewrite_num)
else:
    rewrite_num1 = len(v_even) - e1[0] + len(v_odd) - o2[0]
    rewrite_num2 = len(v_even) - e2[0] + len(v_odd) - o1[0]
    print(min(rewrite_num1, rewrite_num2))

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

count = 0
v1 = []
v2 = []
while count < n:
  v1.append(v[count])
  v2.append(v[count + 1])
  count += 2

c1 = collections.Counter(v1)
c2 = collections.Counter(v2)

num1, count1 = c1.most_common()[0]
num2, count2 = c2.most_common()[0]

if num1 != num2:
  min_count = (n // 2 - count1) + (n // 2 - count2)
elif count1 == count2 and count1 == n // 2:
  min_count = (n // 2)
else:
  if len(c1.most_common()) > 1 and len(c2.most_common()) > 1:
    num3, count3 = c1.most_common()[1]
    num4, count4 = c2.most_common()[1]
    a = (n // 2 - count1) + (n // 2 -count4)
    b = (n // 2 - count2) + (n // 2 -count3)
    min_count = min(a,b)
  else:
    if count1 > count2:
      num2, count2 = c2.most_common()[1]
      min_count = (n // 2 - count1) + (n // 2 - count2)
    else:
      num1, count1 = c1.most_common()[1]
      min_count = (n // 2 - count2) + (n // 2 - count1)
print(min_count)

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

count = 0
v1 = []
v2 = []
while count < n:
  v1.append(v[count])
  v2.append(v[count + 1])
  count += 2

c1 = collections.Counter(v1)
c2 = collections.Counter(v2)

num1, count1 = c1.most_common()[0]
num2, count2 = c2.most_common()[0]

if num1 != num2:
  min_count = (n // 2 - count1) + (n // 2 - count2)
elif count1 == count2 and count1 == n // 2:
  min_count = (n // 2)
else:
  if len(c1.most_common()) > 1 and len(c2.most_common()):
    num3, count3 = c1.most_common()[1]
    num4, count4 = c2.most_common()[1]
    a = (n // 2 - count1) + (n // 2 -count4)
    b = (n // 2 - count2) + (n // 2 -count3)
    min_count = min(a,b)
  else:
    if count1 > count2:
      num2, count2 = c2.most_common()[1]
      min_count = (n // 2 - count1) + (n // 2 - count2)
    else:
      num1, count1 = c1.most_common()[1]
      min_count = (n // 2 - count2) + (n // 2 - count1)
print(min_count)

Solution (Python):
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
A=V[::2]
B=V[1::2]
a=Counter(A)
b=Counter(B)
x=a.most_common(2)
y=b.most_common(2)
if x[0][0]!=y[0][0]:
  print((n//2-x[0][1])+(n//2-y[0][1]))
else:
  if len(x)>1 and len(y)>1:
    s=(n//2-x[0][1])+(n//2-y[1][1])
    t=(n//2-x[1][1])+(n//2-y[0][1])
    print(min(s,t))
  elif len(x)>1:
    print(n//2-x[1][1])
  elif len(y)>1:
    print(n//2-y[1][1])
  else:
    print(n//2)

Solution (Python):
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
A=V[::2]
B=V[1::2]
a=Counter(A)
b=Counter(B)
x=a.most_common(2)
y=b.most_common(2)
if x[0][0]!=y[0][0]:
  print((n//2-x[0][1])+(n//2-y[0][1]))
else:
  if len(x)>1 and len(y)>1:
    if x[0][1]>y[0][1]:
      print((n//2-x[0][1])+(n//2-y[1][1]))
    elif x[0][1]==y[0][1]:
      if x[1][1]>y[1][1]:
        print((n//2-x[1][1])+(n//2-y[0][1]))
      else:
        print((n//2-x[0][1])+(n//2-y[1][1]))
    else:
      print((n//2-x[1][1])+(n//2-y[0][1]))
  elif len(x)>1:
    print(n//2-x[1][1])
  elif len(y)>1:
    print(n//2-y[1][1])
  else:
    print(n//2)

Solution (Python):
from collections import Counter

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

even_cnt = Counter(V[0::2])
odd_cnt = Counter(V[1::2])

even_kv = [(k, v) for k, v in even_cnt.items()]
odd_kv = [(k, v) for k, v in odd_cnt.items()]

even_kv.append((None, 0))
odd_kv.append((None, 0))

even_kv.sort(key=lambda kv: -kv[1])
odd_kv.sort(key=lambda kv: -kv[1])

if even_kv[0][0] == odd_kv[0][0]:
    count = max(even_kv[0][1]+odd_kv[1][1],
                even_kv[1][1]+odd_kv[0][1])
else:
    count = even_kv[0][1] + odd_kv[0][1]
print(N-count)

Solution (Python):
from collections import Counter
n = int(input())
v = list(map(int, input().split()))

a = v[0::2]
b = v[1::2]

aa = Counter(a)
bb = Counter(b)

aaa = aa.most_common()
bbb = bb.most_common()

ans = 0

if aaa[0][0] != bbb[0][0]:
  ans = n - aaa[0][1] - bbb[0][1]

else:
  if len(aa) == len(bb) == 1:
    ans = min(aaa[0][1], bbb[0][1])
  else:
    aaa.append(('-', 0))
    bbb.append(('-', 0))
    ans = n - max(aaa[0][1]+bbb[1][1], aaa[1][1]+bbb[0][1])

print(ans)

Solution (Python):
a = [list(map(int, input().split())) for i in range(2)]

even = a[1][0::2]
odd = a[1][1::2]

evendic = {}
odddic = {}

evenmax = []
oddmax = []

ans = 0

for i in even:
    if i in evendic:
        evendic[i] += 1
    else:
        evendic[i] = 1
  
for i in odd:
    if i in odddic:
        odddic[i] += 1
    else:
        odddic[i] = 1

evennum = max(evendic.values())
oddnum = max(odddic.values())

for k, v in evendic.items():
    if v == evennum :
        evenmax.append(k)

for k, v in odddic.items():
    if v == oddnum:
        oddmax.append(k)

if len(set(even)) == 1 and len(set(odd)) == 1:
    if even == odd:
        ans = a[0][0] // 2

elif evennum == oddnum:
    if len(evenmax) >= 2 or len(oddmax) >= 2:
        ans = a[0][0] - evennum * 2
    elif max(evendic.items(), key = lambda x:x[1])[0] != max(odddic.items(), key = lambda x:x[1])[0]:
        ans = a[0][0] - evennum * 2
    else:
        ans = a[0][0] - evennum - max([sorted(odddic.values())[-2], sorted(evendic.values())[-2]])

else:
    if max(evendic.items(), key = lambda x:x[1])[0] != max(odddic.items(), key = lambda x:x[1])[0]:
        ans = a[0][0] - evennum - oddnum
    else:
        if evennum > oddnum:
            ans = a[0][0] - evennum - sorted(odddic.values())[-2]
        else:
            ans = a[0][0] - oddnum - sorted(evendic.values())[-2]

    
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
from collections import Counter
c0 = Counter(a[::2])
c1 = Counter(a[1::2])
kv0 = [(v,k) for k,v in c0.items()]
kv1 = [(v,k) for k,v in c1.items()]
kv0.sort()
kv1.sort()
if kv0[-1][1]!=kv1[-1][1]:
    ans = len(a[::2])-max(c0.values()) + len(a[1::2])-max(c1.values())
else:
    vals = []
    if len(kv0)>len(kv1):
        kv0, kv1 = kv1, kv0
    v0 = sum(item[0] for item in kv0)
    v1 = sum(item[0] for item in kv1)
    if len(kv0)==1:
        if len(kv1)==1:
            ans = min(v0, v1)
        else:
            ans = min(v0+v1-kv0[-1][0]+kv1[-2][0], v0+v1-kv1[-1][0])
    else:
        ans = min(v0+v1-kv0[-1][0]-kv1[-2][0], v0+v1-kv0[-2][0]-kv1[-1][0])
print(ans)

Solution (Python):
n=int(input())
e=[]
o=[]
v=list(map(int,input().split()))
for i in range(n):
  if i%2==0:
    o.append(v[i])
  else:
    e.append(v[i])
from collections import Counter
o=Counter(o)
e=Counter(e)
o=sorted(o.items(),key=lambda x:x[1],reverse=True)
e=sorted(e.items(),key=lambda x:x[1],reverse=True)

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

Solution (Python):
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
v_e = v[0::2]
v_o = v[1::2]
mc_e = Counter(v_e).most_common() + [(0, 0)]
mc_o = Counter(v_o).most_common() + [(0, 0)]

if mc_e[0][0] != mc_o[0][0]:
    print(n - mc_e[0][1] - mc_o[0][1])
else:
    print(n - max(mc_e[0][1] + mc_o[1][1], mc_e[1][1] + mc_o[0][1]))

Solution (Python):
from collections import deque, Counter








N = int(input())


A = list(map(int, input().split()))


o = []
e = []
for k in range(N):
  if k%2 == 0:
    e.append(A[k])
  else:
    o.append(A[k])
e = Counter(e)
o = Counter(o)
e = e.most_common()
o = o.most_common()
e.append(('1', 0))
o.append(('2', 0))
if e[0][0] != o[0][0]:
  ans = N - e[0][1] - o[0][1]
else:
  ans = N - e[0][1] - o[1][1]
  ans = min(ans, N - e[1][1] - o[0][1]) 
print(ans)

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

dic1 = {}
dic2 = {}
for i in range(n):
    if i % 2 == 0:
        if vs[i] in dic1:
            dic1[vs[i]] += 1
        else:
            dic1[vs[i]] = 1
    else:
        if vs[i] in dic2:
            dic2[vs[i]] += 1
        else:
            dic2[vs[i]] = 1

max1_1 = 0
max1_2 = 0
num1_1 = -1
num1_2 = -1
for key in dic1.keys():
    x = dic1[key]
    if x >= max1_1:
        tmp = max1_1
        max1_1 = x
        max1_2 = tmp
        tmp = num1_1
        num1_1 = key
        num1_2 = tmp

    elif x > max1_2:
        max1_2 = x
        num1_2 = key

max2_1 = 0
max2_2 = 0
num2_1 = -1
num2_2 = -1
for key in dic2.keys():
    x = dic2[key]
    if x >= max2_1:
        tmp = max2_1
        max2_1 = x
        max2_2 = tmp
        tmp = num2_1
        num2_1 = key
        num2_2 = tmp

    elif x > max2_2:
        max2_2 = x
        num2_2 = key

if num1_1 != num2_1:
    ans = n - max1_1 - max2_1
else:
    x = max1_1 + max2_2
    y = max1_2 + max2_1
    ans = min(n - x,n - y)
print(ans)

Solution (Python):
def main(): 
    n = int(input())
    v = list(map(int, input().split()))
    a = [0]*(10**5+7)
    b = [0]*(10**5+7)
    for i in v[::2]:
        a[i]+=1
    for i in v[1::2]:
        b[i]+=1
    ama = max(a)
    bma = max(b)
    
    if a.index(ama)!=b.index(bma):
        print(n-ama-bma)
    else:
        a[a.index(ama)] = 0
        ama2 = max(a)
        b[b.index(bma)] = 0
        bma2 = max(b)
        print(min(n-ama-bma2, n-ama2-bma))
    
    
    
    

    
main()

Solution (Python):
from collections import Counter
def main():
    n = int(input())
    V = list(map(int,input().split()))

    C0 = Counter(V[0:n:2])
    C1 = Counter(V[1:n:2])

    items0 = list(C0.items())
    items0.sort(key=lambda x:-x[1])
    items1 = list(C1.items())
    items1.sort(key=lambda x:-x[1])

    ans = 0
    if items0[0][0] == items1[0][0]:
        sec = n//2
        if len(items0)==1 and len(items1)==1:
            ans = n//2
        elif len(items0)==1:
            ans = items1[0][1]
        elif len(items1)==1:
            ans = items0[0][1]
        else:
            ans = n//2-items0[0][1] + n//2-max(items0[1][1],items1[1][1])
    else:
        ans = n -items0[0][1] - items1[0][1]

    print(ans)
main()

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

out=0
ki,gu=[], []
for i in range(n):
    if i%2==0:
        ki.append(v[i])
    else:
        gu.append(v[i])


if len(set(v)) ==1:  
    out=len(v)//2

else:
    kiC, guC = collections.Counter(ki), collections.Counter(gu)
    kiI, guI = list(kiC.keys()), list(guC.keys())
    kiV, guV = list(kiC.values()), list(guC.values())
    if max(kiC, key=kiC.get) == max(guC, key=guC.get):
        kiV, guV = sorted(kiV), sorted(guV)
        out = min(len(ki) - kiV[-1] +  len(gu) - guV[-2], len(ki) - kiV[-2] + len(gu) - guV[-1] )
    
    else:
        out+=len(ki) - max(kiV)
        out+=len(gu) - max(guV)

print(out)

Solution (Python):



import sys
from collections import Counter

n = int(input())

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

ans1 = 0

c1 = Counter(v[::2])

c1_max = max(c1, key=c1.get)

for i in v[::2]:
    if i != c1_max:
        ans1 += 1

c2 = Counter(v[1::2])

c2_max = max(c2, key=c2.get)

if c1_max == c2_max:
    if len(c2) > 1:
        c2.pop(c2_max)
        c2_max = max(c2, key=c2.get)
    else:
        c2_max += 1

for i in v[1::2]:
    if i != c2_max:
        ans1 += 1

ans2 = 0

c1 = Counter(v[1::2])

c1_max = max(c1, key=c1.get)

for i in v[1::2]:
    if i != c1_max:
        ans2 += 1

c2 = Counter(v[::2])

c2_max = max(c2, key=c2.get)

if c1_max == c2_max:
    if len(c2) > 1:
        c2.pop(c2_max)
        c2_max = max(c2, key=c2.get)
    else:
        c2_max += 1

for i in v[::2]:
    if i != c2_max:
        ans2 += 1

print(min(ans1, ans2))

Solution (Python):
from collections import Counter
N = int(input())
V = list(map(int,input().split()))
V1 = Counter(V[0:N:2])
V2 = Counter(V[1:N:2])

"""
まず大きく2つの場合分けができる
・V1とV2の最多数値が違う場合
・V1とV2の最多数値が同じ場合
前者の場合は、普通にNからそれぞれの最多数値を引けばよい。

後者の場合は、V1の方をセカンダリの数字に合わせるパターンと、V2の方をセカンダリの数字に合わせるパターンそれぞれ試して
変更回数が少ない方を採用する。
"""
V1 = [(v,k) for k,v in V1.items()]
V1.sort(reverse=True)
V1.append((0,10**5+1))
V2 = [(v,k) for k,v in V2.items()]
V2.sort(reverse=True)
V2.append((0,10**5+1))

if V1[0][1] != V2[0][1]:
    ans = N - V1[0][0] - V2[0][0]
else:
    candi1 = N - V1[1][0] - V2[0][0]
    candi2 = N - V1[0][0] - V2[1][0]
    ans = min(candi1,candi2)

print(ans)

Solution (Python):
n = int(input())
v = list(input().split())
from collections import Counter
x, y = list(Counter(v[::2]).most_common(3)), list(Counter(v[1::2]).most_common(3))
x.sort(key=lambda i:i[1], reverse=True)
y.sort(key=lambda i:i[1], reverse=True)
r = []
for a, b in x[:max(len(x), 3)]:
    for aa, bb in y[:max(len(y), 3)]:
        if a == aa:
            r.append(n-max(b, bb))
        else:
            r.append(n-b-bb)
print(min(r))

Solution (Python):


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

E=dict()
O=dict()

for i in range(n):
    a=v[i]
    if i%2==0:
        if a in E:
            E[a]+=1
        else: 
            E[a]=1
    if i%2==1:
        if a in O:
            O[a]+=1
        else: 
            O[a]=1

ans=n-max(E.values())-max(O.values())

Emax=max(E.values())
Ekeys=[]
for k in E.keys():
    if E[k] == Emax: Ekeys.append(k)

Omax=max(O.values())
Okeys=[]
for k in O.keys():
    if O[k] == Omax: Okeys.append(k)

if len(Ekeys)==1 and Ekeys==Okeys:
    if ans==0:
        ans=int(n/2)
    else:
        E1=E.copy()
        del E1[Ekeys[0]]
        ans=n-max(E1.values())-max(O.values())

        O1=O.copy()
        del O1[Okeys[0]]
        ans1=n-max(E.values())-max(O1.values())

        if ans>ans1: ans=ans1




print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = [0]*(10**5+3)
c = [0]*(10**5+3)
for i in range(n):
  if i%2==0:
    b[a[i]]+=1
  else:
    c[a[i]]+=1
bm=max(b)
cm=max(c)
if b.index(max(b))==c.index(max(c)):
  b[b.index(max(b))]=0
  c[c.index(max(c))]=0
  bm2=max(b)
  cm2=max(c)
  print(min(n-bm-cm2,n-cm-bm2))
else:
  print(n-bm-cm)

Solution (Python):
n = int(input())
lst = [int(i) for i in input().split()]
dic_odd = {}
dic_even = {}
for i in range(n):
  num = lst[i]
  if i % 2 == 0:
    if num not in dic_odd:
      dic_odd[num] = 1
    else:
      dic_odd[num] += 1
  else:
    if num not in dic_even:
      dic_even[num] = 1
    else:
      dic_even[num] += 1
lst_odd = sorted(dic_odd.items(), key=lambda x: x[1], reverse=True)
lst_even = sorted(dic_even.items(), key=lambda x: x[1], reverse=True)


if lst_odd[0][0] != lst_even[0][0]:
  ans = n - lst_odd[0][1] - lst_even[0][1]
else:
  if lst_odd[0][1] > lst_even[0][1]:
    if len(lst_even) > 1:
      ans = n - lst_odd[0][1] - lst_even[1][1]
    else:
      ans = n - lst_odd[0][1]
  elif lst_odd[0][1] < lst_even[0][1]:
    if len(lst_odd) > 1:
      ans = n - lst_odd[1][1] - lst_even[0][1]
    else:
      ans = n - lst_even[0][1]
  else:
    if len(lst_odd) == 1 or len(lst_even) == 1:
      ans = n - lst_odd[0][1]
    else:
      if lst_odd[1][1] > lst_even[1][1]:
        ans = n - lst_even[0][1] - lst_odd[1][1]
      else:
        ans = n - lst_even[1][1] - lst_odd[0][1]
print(ans)

Solution (Python):
n = int(input())
v = list(map(int,input().split()))
v0 = v[0:n-1:2]
v1 = v[1:n:2]
from collections import Counter
V0 = Counter(v0)
V1 = Counter(v1)
if V0.most_common()[0][0] != V1.most_common()[0][0]:
    a = V0.most_common()[0][1]
    b = V1.most_common()[0][1]
else:
    if V0.most_common()[0][1] > V1.most_common()[0][1]:
        if len(V1) > 1:
            a = V0.most_common()[0][1]
            b = V1.most_common()[1][1]
        else:
            a = V0.most_common()[0][1]
            b = 0
    elif V0.most_common()[0][1] < V1.most_common()[0][1]:
        if len(V0) > 1:
            a = V1.most_common()[0][1]
            b = V0.most_common()[1][1]
        else:
            a = 0
            b = V1.most_common()[0][1]
    else:
        if len(V0) == 1 and len(V1) == 1:
            a = 0
            b = V0.most_common()[0][1]
        elif len(V0) == 1:
            a = V0.most_common()[0][1]
            b = V1.most_common()[1][1]
        elif len(V1) == 1:
            a = V0.most_common()[1][1]
            b = V1.most_common()[0][1]
        else:
            a = V0.most_common()[0][1]
            b = max(V0.most_common()[1][1],V1.most_common()[1][1])
print(n-a-b)

Solution (Python):
import collections

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

v_list_1 = []
v_list_2 = []

for i in range(0,n//2):
    v_list_1.append(v_list[2*i])
    v_list_2.append(v_list[2*i+1])

v_1_c = collections.Counter(v_list_1)
v_2_c = collections.Counter(v_list_2)
v_1_cnt = v_1_c.most_common()[::-1]
v_2_cnt = v_2_c.most_common()[::-1]
ans = 0

ans_1_1 = n//2 -v_1_cnt[-1][1]
ans_2_1 = n//2 -v_2_cnt[-1][1]
if len(v_1_cnt) == 1 and len(v_2_cnt) == 1:
    if v_1_cnt[-1][0] != v_2_cnt[-1][0]:
        ans = 0
    else:
        ans = n//2
else:
    if v_1_cnt[-1][0] != v_2_cnt[-1][0]:
        ans = ans_1_1 + ans_2_1
    else:
        ans_1_2 = n//2 -v_1_cnt[-2][1]
        ans_2_2 = n//2 -v_2_cnt[-2][1]
        ans = ans_1_1 + min(ans_1_2, ans_2_2)

print(ans)

Solution (Python):




import collections






def solve(N, Vi):
    V1 = Vi[::2]
    V2 = Vi[1::2]
    c_V1 = dict(collections.Counter(V1))
    c_V2 = dict(collections.Counter(V2))
    c_V1 = sorted(c_V1.items(), key=lambda x: x[1], reverse=True)
    c_V2 = sorted(c_V2.items(), key=lambda x: x[1], reverse=True)
    c_V1.append((-1, 0))
    c_V2.append((-1, 0))
    ans = 0
    if c_V1[0][0] == c_V2[0][0]:
        ans = min(len(V1) - c_V1[0][1] + len(V2) - c_V2[1][1],
                  len(V1) - c_V1[1][1] + len(V2) - c_V2[0][1])
    else:
        ans = len(V1) - c_V1[0][1] + len(V2) - c_V2[0][1]
    print(ans)


if __name__ == '__main__':
    N = int(input())
    Vi = [int(i) for i in input().split()]
    solve(N, Vi)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict 
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  
  
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  






C=Counter(A[::2])
CC=Counter(A[1::2])
A=[[i,j] for i,j in C.items()]+[[None,0]]
AA=[[i,j] for i,j in CC.items()]+[[None,0]]
A=sorted(A, key=lambda x:x[1])
AA=sorted(AA, key=lambda x:x[1])
if A[-1][0]==AA[-1][0]:
    print(min(  sum(C.values())-A[-2][1]+sum(CC.values())-AA[-1][1] , sum(C.values())-A[-1][1]+sum(CC.values())-AA[-2][1]  ))
    exit()
print(sum(C.values())-max(C.values())+ sum(CC.values()) - max(CC.values()))

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

odd = dict()
even = dict()

for i in range(n):
    if i % 2 == 0:
        if even.get(v[i]) == None:
            even[v[i]] = 1
        else:
            even[v[i]] += 1
    else:
        if odd.get(v[i]) == None:
            odd[v[i]] = 1
        else:
            odd[v[i]] += 1


odd_max_v = max(odd.values())
odd_max_key = max(odd, key = odd.get)
even_max_v = max(even.values())
even_max_key = max(even, key = even.get)

ans = n
if odd_max_key != even_max_key:
    ans = n - odd_max_v - even_max_v
else:
    odd[odd_max_key] = 0
    even[even_max_key] = 0

    odd_max_v2 = max(odd.values())
    even_max_v2 = max(even.values())

    ans = min(n - odd_max_v - even_max_v2, n - odd_max_v2 - even_max_v)
print(ans)

Solution (Python):
import numpy as np
import collections
N = int(input())
u = list(map(int,input().split()))
ans = 0
test=range(0,N,2)
u_one = np.array(u)[test]
c_one=collections.Counter(u_one)
test=range(1,N,2)
u_two = np.array(u)[test]
c_two = collections.Counter(u_two)
d_one = c_one.most_common()
d_two = c_two.most_common()

if(len(d_one)!=1 and len(d_two)!=1):
    if(d_one[0][0]!=d_two[0][0]):
        print(N-d_one[0][1]-d_two[0][1])
    else:
        print(min(N-d_one[1][1]-d_two[0][1], N-d_one[0][1]-d_two[1][1]))
else:
    if(len(d_one)==1 and len(d_two)==1):
        if(d_one[0][0]==d_two[0][0]):
            print(min(N-d_two[0][1],N-d_one[0][1]))
        else:
            print(N-d_one[0][1]-d_two[0][1])
    elif(len(d_two)==1):
        print(min(N-d_one[0][1]-d_two[0][1],N-d_one[1][1]-d_two[0][1]))
    elif(len(d_one)==1):
        print(min(N-d_one[0][1]-d_two[0][1],N-d_one[0][1]-d_two[1][1]))

Solution (Python):
def r(a): 
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append([l,a[i]])
            l=1
    ll.append([l,a[-1]])
    return ll

n=int(input())
v=list(map(int,input().split()))
a,b=sorted(r(sorted(v[::2])),reverse=True),sorted(r(sorted(v[1::2])),reverse=True)
a.append([0,0])
b.append([0,0])
if a[0][1]!=b[0][1]:
    print(n-a[0][0]-b[0][0])
elif a[1][0]>=b[1][0]:
    print(n-a[1][0]-b[0][0])
elif a[1][0]<b[1][0]:
    print(n-a[0][0]-b[1][0])

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

a_odd = dict()
a_even = dict()
for i,x in enumerate(a):
  if i%2:
    a_odd[x] = 1 + (a_odd[x] if x in a_odd else 0)
  else:
    a_even[x] = 1 + (a_even[x] if x in a_even else 0)

odd_l = sorted(a_odd.items(), reverse=True, key=lambda x:x[1])
even_l = sorted(a_even.items(), reverse=True, key=lambda x:x[1])
odd_l.append((0,0))
even_l.append((0,0))

if odd_l[0][0] != even_l[0][0]:
  
  print(n - odd_l[0][1] - even_l[0][1])
elif odd_l[0][1] > even_l[0][1]:
  
  print(n - odd_l[0][1] - even_l[1][1])
elif odd_l[0][1] < even_l[0][1]:
  
  print(n - odd_l[1][1] - even_l[0][1])
else:
  
  if odd_l[1][1] < even_l[1][1]:
  	print(n - odd_l[0][1] - even_l[1][1])
  else:
    
    print(n - odd_l[1][1] - even_l[0][1])

Solution (Python):
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))

n = ini()
a = inl()
if len(set(a)) == n:
    print(n - 2)
elif len(set(a)) == 1:
    print(n//2)
else:
    f = {}
    s = {}
    for i in range(n):
        if i % 2 == 0:
            if a[i] not in f:
                f[a[i]] = 0
            f[a[i]] += 1
        else:
            if a[i] not in s:
                s[a[i]] = 0
            s[a[i]] += 1
    fsum = sum(f.values())
    ssum = sum(s.values())
    f = sorted(f.items(), key=lambda x: x[1], reverse=True)
    s = sorted(s.items(), key=lambda x: x[1], reverse=True)
    if f[0][0] == s[0][0]:
        tmp1 = fsum - f[0][1] + ssum - s[1][1]
        tmp2 = fsum - f[1][1] + ssum - s[0][1]
        print(min(tmp1, tmp2))
    else:
        print(fsum - f[0][1] + ssum - s[0][1])

Solution (Python):
import sys

n=int(input())
v=[int(x) for x in input().rstrip().split()]
  
dic1={}
dic2={}
if v.count(v[0])==n:
  print(n//2)
else:
  for i in range(n):

    if i%2==0:
      if v[i] in dic1:
        dic1[v[i]]+=1
      else:
        dic1[v[i]]=1

    else:
      if v[i] in dic2:
        dic2[v[i]]+=1
      else:
        dic2[v[i]]=1

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

  if dic1[0][0]==dic2[0][0]:
    print(min(n-(dic1[0][1]+dic2[1][1]),n-(dic1[1][1]+dic2[0][1])))
  else:
    print(n-(dic1[0][1]+dic2[0][1]))

Solution (Python):
from collections import Counter
A=list()
B=list()
N=int(input())
L=list(map(int,input().split()))
for i in range(N):
  if i%2==0:
    A.append(L[i])
  else:
    B.append(L[i])
c = Counter(A)
a = c.most_common()
c = Counter(B)
b = c.most_common()

if a[0][0]!=b[0][0]:
  print(N-a[0][1]-b[0][1])
  exit()

if len(a)==1 and len(b)==1:
  print(N//2)
  exit()
if len(a)==1:
  print(N-a[0][1]-b[1][1])
  exit()
if len(b)==1:
  print(N-a[1][1]-b[0][1])
  exit()
else:
  s=max(a[0][1]+b[1][1],a[1][1]+b[0][1])
  print(N-s)

Solution (Python):
import collections
n=int(input())
v=list(map(int, input().split()))
a=[]
b=[]
for i in range(n):
  if i%2==0:
    a.append(v[i])
  else:
    b.append(v[i])
a_cnt=collections.Counter(a)
b_cnt=collections.Counter(b)
a_cnt_sort=sorted(a_cnt.items(),key=lambda x:x[1],reverse=True)
b_cnt_sort=sorted(b_cnt.items(),key=lambda x:x[1],reverse=True)
if a_cnt_sort[0][0]!=b_cnt_sort[0][0]:
  print(n-a_cnt_sort[0][1]-b_cnt_sort[0][1])
else:
  if len(a_cnt_sort)>1 and len(b_cnt_sort)>1:
    ans1=n-a_cnt_sort[1][1]-b_cnt_sort[0][1]
    ans2=n-b_cnt_sort[1][1]-a_cnt_sort[0][1]
    print(min(ans1,ans2))
  elif len(a_cnt_sort)>1 and len(b_cnt_sort)==1:
    print(n-a_cnt_sort[1][1]-b_cnt_sort[0][1])
  elif len(a_cnt_sort)==1 and len(b_cnt_sort)>1:
    print(n-b_cnt_sort[1][1]-a_cnt_sort[0][1])
  else:
    print(n-min(a_cnt_sort[0][1],b_cnt_sort[0][1]))

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)
INF =  float("inf")
import bisect

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

e = defaultdict(int)
o = defaultdict(int)

e[INF] = 0
o[INF] = 0

ae = (N+1) // 2
ao = N - ae

for i in range(N):
    if i % 2 == 0:
        e[v[i]] += 1
    else:
        o[v[i]] += 1

e_list = sorted(e.items(), key=lambda x:x[1], reverse=True)
o_list = sorted(o.items(), key=lambda x:x[1], reverse=True)

if e_list[0][0] != o_list[0][0]:
    print(N-e_list[0][1]-o_list[0][1])
else:
    print(min(N-e_list[0][1]-o_list[1][1], N-e_list[1][1]-o_list[0][1]))

Solution (Python):
from collections import Counter

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

V_odd = [V[i] for i in range(1, N, 2)]
V_even = [V[i] for i in range(0, N, 2)]

C_odd = Counter(V_odd)
C_even = Counter(V_even)

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

if C_odd[0][0] != C_even[0][0]:
    ans = (N//2 - C_odd[0][1] + (N//2 - C_even[0][1]))
else:
    if len(C_odd) > 1:
        second_odd = C_odd[1][1]
    else:
        second_odd = 0
    if len(C_even) > 1:
        second_even = C_even[1][1]
    else:
        second_even = 0
    if second_odd > second_even:
        ans = (N//2 - second_odd) + (N//2 - C_even[0][1])
    else:
        ans = (N//2 - C_odd[0][1]) + (N//2 - second_even)

print(ans)

Solution (Python):

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

even=[[0,i] for i in range(10**5+1)]
odd=[[0,i] for i in range(10**5+1)]

for i in range(N):
    if i%2==0:
        even[V[i]][0]+=1
    else:
        odd[V[i]][0]+=1

even=sorted(even)
odd=sorted(odd)

n_even=even[-1][1]
n_odd=odd[-1][1]
if n_even!=n_odd:
    ans=N-(even[-1][0]+odd[-1][0])
    print(ans)

else:
    ans1=N-(even[-2][0]+odd[-1][0])
    ans2=N-(even[-1][0]+odd[-2][0])
    ans=min(ans1, ans2)
    print(ans)

Solution (Python):
import collections
n = int(raw_input())
ais = map(int, raw_input().split(' '))
cco = collections.Counter([ais[i] for i in range(len(ais)) if i % 2])
cce = collections.Counter([ais[i] for i in range(len(ais)) if i % 2 == 0])
cce = [(cce[k],k) for k in cce]
cce.sort(key = lambda x:-x[0])

t = n/2
if cce:
	m = t + t- cce[0][0]
else:
	m = 2*t
for colori in cco:
	for v,colorj in cce:
		if colorj != colori:
			m = min(m,t - cco[colori] + t - v)
			break

print m

Solution (Python):
n=int(input())
v=list(map(int,input().split()))
zero=dict()
one=dict()
for i in range(0,n,2):
    try:
        zero[v[i]]+=1
    except:
        zero[v[i]]=1
for i in range(1,n,2):
    try:
        one[v[i]]+=1
    except:
        one[v[i]]=1
x=list(zero.items())
y=list(one.items())
x.sort(reverse=True,key=lambda x:x[1])
y.sort(reverse=True,key=lambda x:x[1])
x+=[[10**6,0]]
y+=[[10**6,0]]
px=0
py=0
if x[0][0]!=y[0][0]:
    pass
elif x[1][1]+y[0][1]>x[0][1]+y[1][1]:
    px=1
else:
    py=1
print(n-x[px][1]-y[py][1])

Solution (Python):
n = int(input())
v = list(map(int, input().split()))
from collections import Counter
cnt = Counter(v)
ocnt = sorted(Counter(v[::2]).items(), key=lambda x: x[1], reverse=True)
ecnt = sorted(Counter(v[1::2]).items(), key=lambda x: x[1], reverse=True)
if len(cnt) == 1:
    print(n//2)
elif len(ocnt) == 1 and len(ecnt) == 1:
    print(0)
else:
    onum = ocnt[0][1]
    enum = ecnt[0][1]
    if ocnt[0][0] != ecnt[0][0]:
        print(n - onum - enum)
    else:
        print(min(n - onum - ecnt[1][1], n - enum - ocnt[1][1]))

Solution (Python):
n=int(input())
V=list(map(int,input().split()))
V2=[{},{}]

for i,v in enumerate(V):
  m = i%2
  V2[m][v]=V2[m].get(v,0)+1
v0n,v0o,v1n,v1o = [],[],[],[]
for v in V2[0]:
  v0n.append(v)
  v0o.append(V2[0][v])
for v in V2[1]:
  v1n.append(v)
  v1o.append(V2[1][v])







import numpy as np
v0a = np.array(v0o).argsort()[::-1]
v1a = np.array(v1o).argsort()[::-1]

s0 = sum(v0o)
s1 = sum(v1o)
total = s0+s1-v0o[v0a[0]]-v1o[v1a[0]]
if v0n[v0a[0]]!=v1n[v1a[0]]:
  print(total)
  exit()
else:
  if len(v0o) == 1:
    d0 = v0o[v0a[0]]
  else:
    d0 = v0o[v0a[0]] - v0o[v0a[1]]
  if len(v1o) == 1:
    d1 = v1o[v1a[0]]
  else:
    d1 = v1o[v1a[0]] - v1o[v1a[1]]
  print(total + min(d0,d1))