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):
n=int(input())
nn=n//2
vlist = [int(i) for i in input().split()]

vlista=[]
vlistb=[]

for i in range(n):
    if i%2==0:
        vlista.append(vlist[i])
    else:
        vlistb.append(vlist[i])

vlista=sorted(vlista)
vlistb=sorted(vlistb)

mae=0
ulista=[]
ulistb=[]

for i in range(nn):
    if vlista[i]==mae:
        ulista[-1][1]+=1
    else:
        ulista.append([vlista[i], 1])
    mae=vlista[i]

mae=0

for i in range(nn):
    if vlistb[i]==mae:
        ulistb[-1][1]+=1
    else:
        ulistb.append([vlistb[i], 1])
    mae=vlistb[i]


ulista.sort(key=lambda dict: dict[1], reverse=True)
ulistb.sort(key=lambda dict: dict[1], reverse=True)

if len(ulista)==1 and len(ulistb)==1:
    if ulista[0][0]==ulistb[0][0]:
        print(nn)
    else:
        print(0)
else:
    if ulista[0][0]==ulistb[0][0]:
        if len(ulista)==1:
            print(n-ulista[0][1]-ulistb[1][1])
        elif len(ulistb)==1:
            print(n-ulista[1][1]-ulistb[0][1])
        else:
            print(min(n-ulista[1][1]-ulistb[0][1], n-ulista[0][1]-ulistb[1][1]))
    else:
        print(n-ulista[0][1]-ulistb[0][1])

Solution (Python):
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    do = collections.defaultdict(int)
    de = collections.defaultdict(int)
    n = int(input().rstrip('\n'))
    v = list(map(int, input().rstrip('\n').split()))
    for i, vv in enumerate(v):
        if i % 2 == 0:
            de[vv] += 1
        else:
            do[vv] += 1
    do = sorted(do.items(), key=lambda x: x[1], reverse=True)
    de = sorted(de.items(), key=lambda x: x[1], reverse=True)
    if do[0][0] == de[0][0]:
        if len(do) == len(de) == 1:
            print(min(de[0][1], do[0][1]))
        else:
            m = 10 ** 20
            if len(do) != 1:
                m = min(m, n // 2 - do[1][1] + n // 2 - de[0][1])
            if len(de) != 1:
                m = min(m, n // 2 - do[0][1] + n // 2 - de[1][1])
            print(m)
    else:
        print(n // 2 - do[0][1] + n // 2 - de[0][1])


if __name__ == '__main__':
    slove()

Solution (Python):
from collections import defaultdict

def getlist():
	return list(map(int, input().split()))

n = int(input())
v = getlist()

D1 = defaultdict(int)
D2 = defaultdict(int)
for i in range(n):
	if i % 2 == 0:
		D1[v[i]] += 1
	else:
		D2[v[i]] += 1

v1 = []
v2 = []
for i in D1:
	v1.append([D1[i], i])
for i in D2:
	v2.append([D2[i], i])

v1 = sorted(v1)
v2 = sorted(v2)

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

Solution (Python):
import collections
import sys

input = sys.stdin.readline

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

v_e = []
v_o = []

for i in range(0,n,2):
    v_e.append(v[i])
for j in range(1,n,2):
    v_o.append(v[j])

if len(collections.Counter(v)) == 1:
    print (n//2)
else:
    v_e_1 = collections.Counter(v_e)
    v_o_1 = collections.Counter(v_o)

    V_E = v_e_1.most_common()
    V_O = v_o_1.most_common()

    if V_E[0][0] == V_O[0][0]:
        a = V_O[1][1] + V_E[0][1]
        b = V_O[0][1] + V_E[1][1]
        print(min([n-a,n-b]))
    else:
        print(n-V_E[0][1]-V_O[0][1])

Solution (Python):
from collections import Counter


def main():
    n = int(input())
    v = list(map(int, input().split()))
    if Counter(v).most_common()[0][1] == n:
        print(n // 2)
        return
    o, e = v[::2], v[1::2]
    oc = Counter(o)
    ec = Counter(e)
    om = oc.most_common()
    em = ec.most_common()
    if om[0][0] == em[0][0]:
        a = om[0][1] + em[1][1]
        b = em[0][1] + om[1][1]
        print(n - max(a, b))
    else:
        print(n - (om[0][1] + em[0][1]))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
c1=Counter(v[::2])
c2=Counter(v[1::2])
c1[0]=0
c2[0]=0
c10,c11=c1.most_common(2)
c20,c21=c2.most_common(2)
if c10[0]!=c20[0]:
    print(n-(c10[1]+c20[1]))
else:
    print(n-max(c10[1]+c21[1],c11[1]+c20[1]))

Solution (Python):
import collections

def inverse_lookup(d, x):
    for k,v in d.items():
        if x == v:
            return k

n = int(input())
v_list = list(map(int, input().split()))
v_0_list = v_list[::2]
v_1_list = v_list[1::2]

v_0_count_dict = collections.Counter(v_0_list)
v_0_count_list = list(v_0_count_dict.values())
v_0_count_list.sort()
v_0_max_count = v_0_count_list[-1]
v_0_max = inverse_lookup(v_0_count_dict, v_0_max_count)

v_1_count_dict = collections.Counter(v_1_list)
v_1_count_list = list(v_1_count_dict.values())
v_1_count_list.sort()
v_1_max_count = v_1_count_list[-1]
v_1_max = inverse_lookup(v_1_count_dict, v_1_max_count)

ans = 0
if v_0_max == v_1_max:
    if len(v_0_count_list) == 1:
        if len(v_1_count_list) == 1:
            v_0_max_count = 0
            v_1_max_count = len(v_1_list)
        else:
            v_1_max_count = v_1_count_list[-2]
    else:
        if len(v_1_count_list) == 1:
            v_0_max_count = v_0_count_list[-2]
        else:
            if v_0_count_list[-1] - v_0_count_list[-2] < v_1_count_list[-1] - v_1_count_list[-2]:
                v_0_max_count = v_0_count_list[-2]
            elif v_0_count_list[-1] - v_0_count_list[-2] >= v_1_count_list[-1] - v_1_count_list[-2]:
                v_1_max_count = v_1_count_list[-2]
    ans += len(v_0_list) - v_0_max_count
    ans += len(v_1_list) - v_1_max_count
else:
    ans += len(v_0_list) - v_0_max_count
    ans += len(v_1_list) - v_1_max_count

print(ans)

Solution (Python):
n=int(input())
v=list(map(int,input().split()))
v_odd={}
v_even={}
for i,x in enumerate(v):
  if i%2==1:
    if x in v_odd:
      v_odd[x]+=1
    else:
      v_odd[x]=1
  else:
    if x in v_even:
      v_even[x]+=1
    else:
      v_even[x]=1

v_odd_top = sorted(v_odd.items(), key=lambda x:x[1],reverse=True)[:2]
v_even_top = sorted(v_even.items(), key=lambda x:x[1],reverse=True)[:2]
 
if v_odd_top[0][0]!=v_even_top[0][0]:
  v_odd_ans=v_odd_top[0][1]
  v_even_ans=v_even_top[0][1]
else:
  if len(v_odd_top)>1 and len(v_even_top)>1:
    if v_odd_top[0][1]+v_even_top[1][1]>=v_odd_top[1][1]+v_even_top[0][1]:
      v_odd_ans=v_odd_top[0][1]
      v_even_ans=v_even_top[1][1]
    else:
      v_odd_ans=v_odd_top[1][1]
      v_even_ans=v_even_top[0][1]
  elif len(v_odd_top)==1 and len(v_even_top)>1:
    v_odd_ans=v_odd_top[0][1]
    v_even_ans=v_even_top[1][1]
  elif len(v_odd_top)>1 and len(v_even_top)==1:
    v_odd_ans=v_odd_top[1][1]
    v_even_ans=v_even_top[0][1]
  elif len(v_odd_top)==1 and len(v_even_top)==1:
    v_odd_ans=v_odd_top[0][1]
    v_even_ans=0
            
sum_freq = sum(v_odd.values())+sum(v_even.values())
ans = sum_freq - ( v_odd_ans + v_even_ans )
print(ans)

Solution (Python):
import collections

N = int(input())
V = list(map(int, input().split()))
odd = V[::2]
even = V[1::2]
odd_count =  collections.Counter(odd).most_common()
even_count = collections.Counter(even).most_common()

if odd_count[0] == even_count[0]:
  if len(odd_count) == 1:
    ans = N//2
  elif odd_count[1][1] == odd_count[0][1] or even_count[1][1] == even_count[0][1]:
    ans = N-(odd_count[0][1] + even_count[0][1])
  else:
    ans = N-max(odd_count[0][1] + even_count[1][1], odd_count[1][1]+ even_count[0][1])
else:
  ans = N-(odd_count[0][1] + even_count[0][1])

print(ans)

Solution (Python):
from collections import Counter

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

tmp = n // 2

tmp1 = []
tmp2 = []

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

tmp1 = Counter(tmp1).most_common(2)
tmp2 = Counter(tmp2).most_common(2)

ans = n

for i in range(len(tmp1)):
    for j in range(len(tmp2)):
        if tmp1[i][0] == tmp2[j][0]:
            continue
        else:
            ans = min(ans, n - tmp1[i][1] - tmp2[j][1])

if ans == n and len(Counter(v)) == 1:
    print(tmp)
else:
    print(ans)

Solution (Python):
import itertools
from collections import Counter

N = int(input())
V = [int(x) for x in input().split()]
EV_counter = Counter(V[::2])
OD_counter = Counter(V[1::2])


EV_counter[-1] = 0
OD_counter[-2] = 0


EV = EV_counter.most_common(2)
OD = OD_counter.most_common(2)

answer = N
for (k1,v1), (k2,v2) in itertools.product(EV,OD):
  if k1 == k2:
    continue
  x = (N - v1 - v2)
  if answer > x:
    answer = x

print(answer)

Solution (Python):
import collections
n = int(input())
v = list(map(int, input().split(' ')))
a = v[::2]
b = v[1::2]

a_count = []
b_count = []

a_count = collections.Counter(a).most_common(3)
b_count = collections.Counter(b).most_common(3)

if a_count[0][0] != b_count[0][0]:
    print(n-int(a_count[0][1])-int(b_count[0][1]))
else:
    if len(a_count)!= 1 and len(b_count)!= 1:
        print(min(n-int(a_count[0][1])-int(b_count[1][1]),n-int(a_count[1][1])-b_count[0][1]))
    elif len(a_count) == 1 and len(b_count)!=1:
        print(n-int(a_count[0][1])-int(b_count[1][1]))
    elif len(b_count)==1 and len(a_count)!= 1:
        print(n-int(a_count[1][1])-int(b_ocunt[0][1]))
    else:
        print(int(n/2))

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

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

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

v_even_s=sorted(v_even,key=lambda x:x[1],reverse=True)
v_odd_s=sorted(v_odd,key=lambda x:x[1],reverse=True)

sum1=0
sum2=0

i=0
f=True
ans=0
while f:
    if v_even_s[i][0]!=v_odd_s[i][0]:
        if v_even_s[i][1]>v_odd_s[i][1]:
            v_even_s.pop(i)
            v_odd_s.pop(0)
        else:
            v_even_s.pop(0)
            v_odd_s.pop(i)
        f=False
    else:
        ans+=v_even_s[i][1]
        if ans==n//2:
            v_even_s.pop(0)
            v_odd_s.pop(1)
            f=False
        i+=1

for i in range(n-1):
    sum1+=v_even_s[i][1]
    sum2+=v_odd_s[i][1]
ans=sum1+sum2
print(ans)

Solution (Python):
import collections
N = int(input())
A = [int(x) for x in input().split()]

a = []
b = []
for i in range(N//2):
	a.append(int(A[i*2]))
	b.append(int(A[i*2+1]))

a = collections.Counter(a)
b = collections.Counter(b)

if a.most_common()[0][0] == b.most_common()[0][0] 	:
	if len(a) == 1:
		print(N//2)
	elif a.most_common()[0][1] == b.most_common()[0][1] 	:
		if a.most_common()[1][1] > b.most_common()[1][1]:
			print(N - a.most_common()[1][1] - b.most_common()[0][1])
		else:
		 	print(N - a.most_common()[0][1] - b.most_common()[1][1])
	elif a.most_common()[0][1] > b.most_common()[0][1]:
		print(N - a.most_common()[0][1] - b.most_common()[1][1]) 
	else:
		print(N - a.most_common()[1][1] - b.most_common()[0][1])
else:
	print(N - a.most_common()[0][1] - b.most_common()[0][1])

Solution (Python):
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
a = Counter(v[::2])
b = Counter(v[1::2])
count = a.most_common()
count1 = b.most_common()
if len(set(v)) == 1:
    print(n//2)
    exit()
if count[0][0] == count1[0][0]:
    print(n- count[0][1] - (max(count[1][1], count1[1][1])))

else:
    print(n - count[0][1] - count1[0][1])

Solution (Python):
import sys
import collections

N=int(sys.stdin.readline().rstrip())
VList=list(map(int,sys.stdin.readline().split()))

evenList=[]
oddList=[]
for i in range(0, N-1, 2):
  evenList.append(VList[i])
  oddList.append(VList[i+1])
  
c1=collections.Counter(evenList)
c2=collections.Counter(oddList)

values1, counts1 = zip(*c1.most_common())
values2, counts2 = zip(*c2.most_common())

if len(c1)==1 and len(c2)==1:
  if values1[0] == values2[0]:
    print(len(oddList))
    exit()
  else:
    print(0)
    exit()    

ans=0
if values1[0] != values2[0]:
  ans = len(evenList)-counts1[0]
  ans += len(oddList)-counts2[0]
else:
  ansTmp1 = len(evenList)-counts1[1]
  ansTmp1 += len(oddList)-counts2[0]
  ansTmp2 = len(evenList)-counts1[0]
  ansTmp2 += len(oddList)-counts2[1]  
  ans = min(ansTmp1, ansTmp2)
  
print(ans)

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

Onum = {}
Enum = {}

for i in range(n):
    if i%2 == 1:
        if v[i] in Onum:
            Onum[v[i]] += 1
        else:
            Onum[v[i]] = 1
    else:
        if v[i] in Enum:
            Enum[v[i]] += 1
        else:
            Enum[v[i]] = 1

Onum = sorted(Onum.items(), key = lambda x:-x[1])
Enum = sorted(Enum.items(), key = lambda x:-x[1])


if Onum[0][0] != Enum[0][0]:
    ans = n - (Onum[0][1] + Enum[0][1])
elif Onum[0][1] == n // 2:
    ans = n // 2
else:
    ans = n - max(Onum[0][1] + Enum[1][1], Onum[1][1] + Enum[0][1])
print(ans)

Solution (Python):

n = int(input())
v = list(map(int,input().split()))
E = [[0,i] for i in range(1,10**5+1)]
O = [[0,i] for i in range(1,10**5+1)]
for j in range(n):
  if j % 2 == 0:
    E[v[j]-1][0] += 1
  else:
    O[v[j]-1][0] += 1
E.sort()
E.reverse()
O.sort()
O.reverse()
if E[0][1] != O[0][1]:
  print( n - E[0][0] - O[0][0] )
else:
  can = [ n - E[1][0] - O[0][0] , n - E[0][0] - O[1][0] ]
  print(min(can))

Solution (Python):
n = int(input())
v = list(map(int,input().split()))
do = [0 for i in range(100001)]
de = [0 for i in range(100001)]
for i in range(n):
    if i % 2 == 0:
        de[v[i]]+=1
    else:
        do[v[i]]+=1
if do.index(max(do))==de.index(max(de)):
    do.sort(reverse=True)
    de.sort(reverse=True)
    print(n-max(do[0]+de[1],do[1]+de[0]))
else:
    print(n-max(do)-max(de))

Solution (Python):

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline

import math
import heapq

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

dict0 = defaultdict(int)
dict1 = defaultdict(int)

for i in range(n//2):
	dict0[v[2*i]] += 1
	dict1[v[2*i + 1]] += 1

list0 = [(0, 0)]
list1 = [(0, 0)]
for num in dict0:
	bisect.insort(list0, (dict0[num], num))
for num in dict1:
	bisect.insort(list1, (dict1[num], num))

if list1[-1][1] != list0[-1][1]:
	print(n - list1[-1][0] - list0[-1][0])
else:
	print(min(n - list1[-1][0] - list0[-2][0], n - list1[-2][0] - list0[-1][0]))

Solution (Python):

import sys
from collections import deque, defaultdict
import copy
import bisect


import math
import heapq

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

dict0 = defaultdict(int)
dict1 = defaultdict(int)

for i in range(n//2):
	dict0[v[2*i]] += 1
	dict1[v[2*i + 1]] += 1

list0 = [(0, 0)]
list1 = [(0, 0)]
for num in dict0:
	bisect.insort(list0, (dict0[num], num))
for num in dict1:
	bisect.insort(list1, (dict1[num], num))

if list1[-1][1] != list0[-1][1]:
	print(n - list1[-1][0] - list0[-1][0])
else:
	print(min(n - list1[-1][0] - list0[-2][0], n - list1[-2][0] - list0[-1][0]))

Solution (Python):
from collections import defaultdict
N = int(input())
V = list(map(int,input().split()))
V1 = []
V2 = []
for i in range(N):
    if i % 2 == 0:
        V1.append(V[i])
    else:
        V2.append(V[i])

dic1 = defaultdict(int)
for v in V1:
    dic1[v] += 1
dic1 = sorted(dic1.items(), key=lambda x:x[1], reverse=True)

dic2 = defaultdict(int)
for v in V2:
    dic2[v] += 1
dic2 = sorted(dic2.items(), key=lambda x:x[1], reverse=True)

if dic1[0][0] != dic2[0][0]:
    ans = (N//2 - dic1[0][1]) + (N//2 - dic2[0][1])
else:
    ans1 = N//2 - dic1[0][1]
    if len(dic2) >= 2:
        ans1 += N//2 - dic2[1][1]
    else:
        ans1 += N//2

    ans2 = N//2 - dic2[0][1]
    if len(dic1) >= 2:
        ans2 += N//2 - dic1[1][1]
    else:
        ans2 += N//2

    ans = min(ans1, ans2)

print(ans)

Solution (Python):

import collections

n = int(input())
v = list(input().split())
v1 = v[0::2]
v2 = v[1::2]





l1 = list(collections.Counter(v1).most_common())
l2 = list(collections.Counter(v2).most_common())


if l1[0][0] != l2[0][0]:
    tmp = l1[0][1] + l2[0][1]
else:
    if len(l1) > 1 and len(l2) > 1: 
        tmp = max(l1[1][1] + l2[0][1], l1[0][1] + l2[1][1])
    elif len(l1) == 1 and len(l2) > 1:
        tmp = max(l2[0][1], l1[0][1] + l2[1][1])
    elif len(l2) == 1 and len(l1) > 1:
        tmp = max(l1[1][1] + l2[0][1], l1[0][1])
    else:
        tmp = max(l2[0][1], l1[0][1])

ans = n - tmp
print(ans)

Solution (Python):

import collections

n = int(input())
v = list(input().split())
v1 = v[0::2]
v2 = v[1::2]





l1 = list(collections.Counter(v1).items())
l2 = list(collections.Counter(v2).items())


l1 = sorted(l1, key=lambda x: x[1], reverse=True) 
l2 = sorted(l2, key=lambda x: x[1], reverse=True) 


if l1[0][0] != l2[0][0]:
    tmp = l1[0][1] + l2[0][1]
else:
    if len(l1) > 1 and len(l2) > 1: 
        tmp = max(l1[1][1] + l2[0][1], l1[0][1] + l2[1][1])
    elif len(l1) == 1 and len(l2) > 1:
        tmp = max(l2[0][1], l1[0][1] + l2[1][1])
    elif len(l2) == 1 and len(l1) > 1:
        tmp = max(l1[1][1] + l2[0][1], l1[0][1])
    else:
        tmp = max(l2[0][1], l1[0][1])

ans = n - tmp
print(ans)

Solution (Python):
from collections import Counter

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

V1=V[0::2]
V2=V[1::2]

V1c=Counter(V1).most_common()
V2c=Counter(V2).most_common()



if V1c[0][0]==V2c[0][0]:
  if len(V1c)==1 and len(V2c)==1:
    print(n//2)
  elif len(V1c)==1:
    print(V2c[0][1])
  elif len(V2c)==1:
    print(V1c[0][1])   
  else:
    print(n-max(V1c[0][1]+V2c[1][1],V1c[1][1]+V2c[0][1]))
else:
  print(n-V1c[0][1]-V2c[0][1])

Solution (Python):
from numpy import argmax as mode
from numpy import bincount as bc
N = int(input())
V = list(map(int, input().split()))
a = [V[2 * i] for i in range(N // 2)]
b = [V[2 * i + 1] for i in range(N // 2 + N % 2)]
bca = bc(a)
bcb = bc(b)

if mode(bca) == mode(bcb):
  print(N - a.count(mode(bca)) - max(sorted(bca)[-2], sorted(bcb)[-2]))
else:
  print(N - a.count(mode(bca)) - b.count(mode(bcb)))

Solution (Python):
from collections import Counter

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

V1=V[0::2]
V2=V[1::2]

V1c=Counter(V1).most_common()
V2c=Counter(V2).most_common()


if V1c[0][0]==V2c[0][0]:
  if len(V1c)==1 and len(V2c)==1:
    print(n//2)
  elif len(V1c)==1:
    print(V2c[0][1])
  elif len(V2c)==1:
    print(V1c[0][1])   
  else:
    print(n-max(V1c[0][1]+V2c[1][1],V1c[1][1]+V2c[0][1]))
else:
  print(n-V1c[0][1]-V2c[0][1])

Solution (Python):
def count_rewrite(v):
  ct = {}
  for x in v:
    if x in ct:
      ct[x] += 1
    else:
      ct[x] = 1
  total = 0
  for x in ct:
    total += ct[x]
  best = 0
  for x in ct:
    if ct[x] > best:
      best = ct[x]
      maxv = x
  del ct[maxv]
  second = 0
  for x in ct:
    if ct[x] > second:
      second = ct[x]
  return maxv, total - best, total - second
n = int(input())
v = [int(e) for e in input().split()]
r1 = count_rewrite(v[::2])
r2 = count_rewrite(v[1::2])
if r1[0] != r2[0]:
  print(r1[1] + r2[1])
else:
  if r1[2] >= r2[2]:
    print(r1[1] + r2[2])
  else:
    print(r1[2] + r2[1])

Solution (Python):
a = int(input())
b = list(map(int, input().split()))
c = {}
d = {}
for i in range(a):
    if i % 2 == 0:
        if b[i] not in c:
            c[b[i]] = 1
        else:
            c[b[i]] += 1
    else:
        if b[i] not in d:
            d[b[i]] = 1
        else:
            d[b[i]] += 1
e = sum(c.values()) + sum(d.values())
c = sorted(c.items(), key=lambda x: x[1], reverse=True)
d = sorted(d.items(), key=lambda x: x[1], reverse=True)
if c[0][0] == d[0][0]:
    if len(c) == len(d) == 1:
        print(c[0][1])
    elif len(c) == 1:
        print(e - c[0][1] - d[1][1])
    elif len(d) == 1:
        print(e - c[1][1] - d[0][1])
    else:
        if c[0][1] - c[1][1] > d[0][1] - d[1][1]:
            print(e - c[0][1] - d[1][1])
        else:
            print(e - c[1][1] - d[0][1])
else:
    if len(c) == len(d) == 1:
        print(0)
    else:
        print(e - c[0][1] - d[0][1])

Solution (Python):
from itertools import groupby
N = int(input())
v_list = list(map(int, input().split()))
v1 = [v_list[2*i] for i in range(N//2)]
v2 = [v_list[2 * i + 1] for i in range(N // 2)]

v1.sort()
first = 0
second = 0
firstkey = 0
secondkey = 0
for key, group in groupby(v1):
    count = len(list(group))
    
    if count >= first:
        first, second = count, first
        firstkey, secondkey = key, firstkey
    elif count >= second:
        second = count
        secondkey = key
v1_dict = {firstkey: first, secondkey: second}

v2.sort()
first = 0
second = 0
firstkey = 0
secondkey = 0
for key, group in groupby(v2):
    count = len(list(group))
    
    if count >= first:
        first, second = count, first
        firstkey, secondkey = key, firstkey
    elif count >= second:
        second = count
        secondkey = key
v2_dict = {firstkey: first, secondkey: second}



v1_dict.pop(0, "")
v2_dict.pop(0, "")




if len(v1_dict) == len(v2_dict) == 1 and v1_dict == v2_dict:  
    print(N // 2)
else:
    min_change = 10**10
    for key1, value1 in v1_dict.items():
        for key2, value2 in v2_dict.items():
            
            if key1 == key2:
                continue
            rest = N-value1-value2
            if value1 > N // 2:
                temp = rest + value1 - N // 2
            elif value2 > N // 2:
                temp = rest + value2 - N // 2
            else:
                temp = rest

            if temp < min_change:
                min_change = temp
    print(min_change)

Solution (Python):
import collections
n = int(input())
v = list(map(int,input().split()))
l = []
r = []
for i in range(0,n//2):
    l.append(v[2*i])
    r.append(v[2*i+1])
       

lc = collections.Counter(l).most_common()
rc = collections.Counter(r).most_common()

if lc[0][0] == rc[0][0]:
    if len(lc)==1:
        print(n//2)
    else:
        print(n-max(lc[0][1]+rc[1][1],lc[1][1]+rc[0][1]))
else:
    print(n-lc[0][1]-rc[0][1])

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_c=collections.Counter(a)
b_c=collections.Counter(b)
    
a1=a_c.most_common()[0][0]
b1=b_c.most_common()[0][0]

x1=a_c[a1]
y1=b_c[b1]

if a1!=b1:
  print(n-x1-y1)
else:
  if len(a_c)==1:
    x2=0
  else:
    a2=a_c.most_common()[1][0]
    x2=a_c[a2]
  if len(b_c)==1:
    y2=0
  else:
    b2=b_c.most_common()[1][0]
    y2=b_c[b2]
  print(n-max(x1+y2,y1+x2))

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

count = [
    {s: 0 for s in set(V[::2])},
    {s: 0 for s in set(V[1::2])},
]

for i, v in enumerate(V):
    count[i % 2][v] += 1

count[0] = sorted(count[0].items(), key=lambda x: -x[1])
count[1] = sorted(count[1].items(), key=lambda x: -x[1])

a = count[0][0]
b = count[1][0]

if a[0] == b[0]:
    try:
        if count[0][1][1] > count[1][1][1]:
            a = count[0][1]
        else:
            b = count[1][1]
    except IndexError:
        a = (-1, 0)

print(N - a[1] - b[1])

Solution (Python):
import itertools
from collections import Counter

N = int(input())
V = [int(x) for x in input().split()]
EV_counter = Counter(V[::2])
OD_counter = Counter(V[1::2])


EV_counter[-1] = 0
OD_counter[-2] = 0


EV = EV_counter.most_common(2)
OD = OD_counter.most_common(2)

answer = N
for (k1,v1), (k2,v2) in itertools.product(EV,OD):
  if k1 == k2:
    continue
  x = (N - v1 - v2)
  if answer > x:
    answer = x

print(answer)

Solution (Python):


from collections import Counter
import sys
N = int(input())
v = [int(i) for i in input().split()]
A = []
B = []
for i in range(N):
    if i % 2 == 0:
        A.append(v[i])
    else:
        B.append(v[i])
S = Counter(A).most_common(2) + [(0,0)]
T = Counter(B).most_common(2) + [(0,0)]
if S[0][0] != T[0][0]:
    print(N - S[0][1] - T[0][1])
    sys.exit()
a = S[0][1] + T[1][1]
b = S[1][1] + T[0][1]
x = max(a, b)
print(N - x)

Solution (Python):
from collections import Counter

N = int(input())
V = [int(i) for i in input().split()]
V1 = V[1::2]
V2 = V[0::2]

v1 = Counter(V1).most_common(2)
v2 = Counter(V2).most_common(2)

v1.append((0,0))
v2.append((0,0))


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

Solution (Python):
from collections import defaultdict
n = int(input())
v = list(map(int, input().split()))
d1 = defaultdict(int)
d2 = defaultdict(int)
for i in range(0, n, 2):
  d1[v[i]]+=1
  d2[v[i+1]]+=1
l1 = list(d1.items())
l2 = list(d2.items())
l1.sort(key=lambda x: -x[1])
l2.sort(key=lambda x: -x[1])
t1 = l1[0][1]
t2 = l2[0][1]
t3 = l1[1][1] if len(l1)>1 else 0
t4 = l2[1][1] if len(l2)>1 else 0
if l1[0][0]==l2[0][0]:
  ans = min(n-t1-t4, n-t2-t3)
else:
  ans = n-t1-t2
print(ans)

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

a = []
b = []

for i in range(n):
    if i % 2 == 0:
        a.append(l[i])
    else:
        b.append(l[i])

def createMyList(li):
    d = {-1:(0,-1)}
    for e in li:
        if e not in d:
            d[e] = (1,e)
        else:
            d[e] = (d[e][0] + 1, d[e][1])
    return list(reversed(sorted(list(d.values()))))

la = createMyList(a)
lb = createMyList(b)



if la[0][1] == lb[0][1]:
    ans = n- max(lb[0][0] + la[1][0], la[0][0] +lb[1][0])
else:
    ans = n - (la[0][0] + lb[0][0])
print(ans)

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

from collections import defaultdict

d0=defaultdict(int)
d1=defaultdict(int)
for i in range(n):
    if i%2==0:
        d0[num[i]]+=1
    else:
        d1[num[i]]+=1

l0=[(k,d0[k]) for k in d0]
l1=[(k,d1[k]) for k in d1]

l0=sorted(l0, key=lambda d:d[1],reverse=True)
l1=sorted(l1, key=lambda d:d[1],reverse=True)

l0.append(("wild card",0))
l1.append(("wild card",0))

if l0[0][0]!=l1[0][0]: 
    print(len(num)-l0[0][1]-l1[0][1])
elif n==2:
    print(1)
else:
    print(n-max(l0[0][1]+l1[1][1],l0[1][1]+l1[0][1]))

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

if n == 2:
    if l[0] == l[1]:
        print(1)
    else:
        print(0)
else:
    a = []
    b = []
    for i in range(n):
        if i % 2 == 0:
            a.append(l[i])
        else:
            b.append(l[i])

    da = {}
    for e in a:
        if e not in da:
            da[e] = (1,e)
        else:
            da[e] = (da[e][0]+1,da[e][1])

    db = {}
    for e in b:
        if e not in db:
            db[e] = (1,e)
        else:
            db[e] = (db[e][0]+1,db[e][1])

    da[-1] = (0,-1)
    db[-1] = (0,-1)

    la = list(reversed(sorted(list(da.values()))))
    lb = list(reversed(sorted(list(db.values()))))
    
    



    if la[0][1] == lb[0][1]:
        ans = n- (la[0][0] + max(la[1][0],lb[1][0]))
    else:
        ans = n - (la[0][0] + lb[0][0])
    print(ans)

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

a = []
b = []
for i in range(n // 2):
    a.append(v[(i+1)*2-1])
    b.append(v[(i+1)*2-2])

a = collections.Counter(a)
b = collections.Counter(b)
 
 
if a.most_common()[0][0] != b.most_common()[0][0]:
    print(n - a.most_common()[0][1] - b.most_common()[0][1])
elif len(a.most_common()) == 1 and len(b.most_common()) == 1:
    print(n//2)
elif a.most_common()[1][1] + b.most_common()[0][1] > a.most_common()[0][1] + b.most_common()[1][1]:
    print(n - a.most_common()[1][1] - b.most_common()[0][1])
else:
    print(n - a.most_common()[0][1] - b.most_common()[1][1])

Solution (Python):
import collections
n = int(input())
v = list(map(int, input().split()))
a = []
b = []
for i in range(n // 2):
    a.append(v[(i+1)*2-1])
    b.append(v[(i+1)*2-2])
a = collections.Counter(a)
b = collections.Counter(b)
 
 
if a.most_common()[0][0] != b.most_common()[0][0]:
    print(n - a.most_common()[0][1] - b.most_common()[0][1])
elif len(a.most_common()) == 1 and len(b.most_common()) == 1:
    print(n//2)
elif a.most_common()[1][1] + b.most_common()[0][1] > a.most_common()[0][1] + b.most_common()[1][1]:
    print(n - a.most_common()[1][1] - b.most_common()[0][1])
else:
    print(n - a.most_common()[0][1] - b.most_common()[1][1])

Solution (Python):
import collections
n = int(input())
v = list(map(int, input().split()))
a = []
b = []
for i in range(n // 2):
    a.append(v[(i+1)*2-1])
    b.append(v[(i+1)*2-2])
a = collections.Counter(a)
b = collections.Counter(b)


if a.most_common()[0][0] != b.most_common()[0][0]:
    print(n - a.most_common()[0][1] - b.most_common()[0][1])
elif len(a.most_common()) == 1 and len(b.most_common()) == 1:
    print(n//2)
elif a.most_common()[1][1] + b.most_common()[0][1] > a.most_common()[0][1] + b.most_common()[1][1]:
    print(n - a.most_common()[1][1] - b.most_common()[0][1])
else:
    print(n - a.most_common()[0][1] - b.most_common()[1][1])

Solution (Python):
import collections
n = int(input())
v = list(map(int, input().split()))
even = [v[2*i] for i in range(n//2)]
odd = [v[2*i+1] for i in range(n//2)]
dEven = collections.Counter(even).most_common()
dOdd = collections.Counter(odd).most_common()
if dEven[0][0] != dOdd[0][0]:
    ans = n - dEven[0][1]  - dOdd[0][1]
else:
    if len(dEven) >=2 and len(dOdd) >= 2:
        ans = n - max(dEven[0][1]+dOdd[1][1], dEven[1][1]+dOdd[0][1])
    elif len(dEven) < len(dOdd):
        ans = n - (dEven[0][1]+dOdd[1][1])
    elif len(dEven) > len(dOdd):
        ans = n - (dEven[1][1]+dOdd[0][1])
    else:
        ans = n//2
print(ans)

Solution (Python):
n=input()
a=[]

a=map(int,raw_input().split())

b=[0]*100001
c=0
while c <= n-2:
    b[a[c]]+=1
    c+=2

d=[0]*100001
e=1
while e <= n-1:
    d[a[e]]+=1
    e+=2

f=sorted(b)[-2]
g=sorted(d)[-2]



if max(b) == n/2 and max(d) == n/2 and b.index(max(b)) == d.index(max(d)):
    print(n/2)
elif max(b) == n/2 and max(d) == n/2 and b.index(max(b)) != d.index(max(d)):
    print(0)
elif b.index(max(b)) == d.index(max(d)):
    print(n-max(b)-max(f,g))
else:
    print(n-max(b)-max(d))

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


v1, v2 = {}, {}

for v in V[::2]:
	if not v in v1:
		v1[v] = 1
	else:
		v1[v] += 1

for v in V[1::2]:
	if not v in v2:
		v2[v] = 1
	else:
		v2[v] += 1

a1 = sorted(v1, key=lambda x:-v1[x])
a2 = sorted(v2, key=lambda x:-v2[x])




r = []
for k1 in a1[:2]:
	for k2 in a2[:2]:
		if k1 != k2:
			r += [N - v1[k1] - v2[k2]]

if not r:
	r += [N // 2]


print(min(r))