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):
'''
問題文
	数列 a1,a2,...,an が以下の条件を満たすとき、 /\/\/\/ と呼ぶことにします。
	・各 i=1,2,..,n−2 について、ai=ai+2
	・数列に現れる数はちょうど 2 種類
	偶数長の数列 v1,v2,...,vn が与えられます。 
	要素をいくつか書き換えることでこの数列を /\/\/\/ にしたいです。 書き換える要素の数は最小でいくつになるか求めてください。

制約
2 ≤ n ≤ 10^5
n は偶数
1 ≤ vi ≤ 10^5
vi は整数

入力
入力は以下の形式で標準入力から与えられる。
n
v1 v2 ... vn

出力
書き換える要素数の最小値を出力してください。

入力例 1 
4
3 1 3 2
出力例 1 
1
数列 
3,1,3,2 は /\/\/\/ ではありませんが、
1 要素書き換えることで /\/\/\/ にすることができます。 例えば、4 要素目を書き換えて 3,1,3,1 とすればよいです。

入力例 2 
6
105 119 105 119 105 119
出力例 2 
0
数列 
105,119,105,119,105,119 は /\/\/\/ です。

入力例 3 
4
1 1 1 1
出力例 3 
Copy
2
数列 
1,1,1,1 は 1 種類の数からなる数列であるため、 /\/\/\/ ではありません。
'''

import collections

def calcChange(even_list, odd_list) :
	"""
	/\/\/\/にするために変更する要素数を算出

	Parameters
	----------
	even_list : list
			偶数番目の要素のリスト
	odd_list : list
			奇数番目の要素のリスト
	
	Returns
	-------
	change : int
			/\/\/\/にするために変更する要素数
			
	"""	




 
 
	
	change = 0

	
	even_counter = collections.Counter(even_list)
	odd_counter = collections.Counter(odd_list)
	
	
	most_even = even_counter.most_common()
	most_odd = odd_counter.most_common()

	
	if most_even[0][0] != most_odd[0][0] :
		even_change = len(even_list) - most_even[0][1]
		odd_change  = len(odd_list)  - most_odd[0][1]
		change = even_change + odd_change
	
	else :
		if len(most_even)==1 and len(most_odd)==1 :
			change = len(even_list)
		else : 
			even_1 = (len(even_list) - most_even[0][1]) + (len(odd_list)  - most_odd[1][1])
			odd_1  = (len(even_list) - most_even[1][1]) + (len(odd_list)  - most_odd[0][1])
			change = min(even_1, odd_1)


	return change



def main():
	'''
	メイン関数
	'''
	
	n = int(input())
	
	v = list(map(int, input().split()))

	
	v_e = [ v[i] for i in range(0,n) if i%2==0 ]

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

	print(calcChange(v_e, v_o))


if __name__ == '__main__':
	main()

Solution (Python):
from collections import Counter

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

V1 = [V[i] for i in range(0, len(V), 2)]
V2 = [V[i] for i in range(1, len(V), 2)]

x = Counter(V1).most_common(2)
y = Counter(V2).most_common(2)

if x[0][1] < y[0][1]:
    x, y = y, x
elif len(x)>1 and len(y) > 1 and x[0][1] == y[0][1] and x[1][1] > y[1][1]:
    x, y = y, x

l = int(N / 2)
res = l - x[0][1]
if y[0][0] == x[0][0]:
    res += l - (y[1][1] if len(y) > 1 else 0)
else:
    res += l - y[0][1]

print(res)

Solution (Python):
def get_first(even,even_max_ind,even_second_ind):
    even_max=0
    second=0
    for i in range(1,10**5+1):
        if(even[i]==0):
            continue
        if(even_max<even[i]):
            even_second_ind[0]=even_max_ind.pop()
            second=even[even_second_ind[0]]
            even_max_ind.clear()
            even_max_ind.append(i)
            even_max=even[i]
        elif(even_max==even[i]):
            even_max_ind.append(i)
        elif(second < even[i]):
            even_second_ind[0]=i
            second=even[even_second_ind[0]]


n=int(input())
e_max=0
o_max=0
even=[0]*(10**5+1)
odd=[0]*(10**5+1)
retu=list(map(int,input().split()))
for i in range(n):
    if(i%2==0):
        even[retu[i]]+=1
    else:
        odd[retu[i]]+=1

even_max_ind=[0]
even_second_ind=[0]
odd_max_ind=[0]
odd_second_ind=[0]

get_first(even,even_max_ind,even_second_ind)
get_first(odd,odd_max_ind,odd_second_ind)

if(len(even_max_ind)>=2 or len(odd_max_ind)>=2 or even_max_ind[0]!=odd_max_ind[0]):
    print(n-even[even_max_ind[0]] - odd[odd_max_ind[0]])
else:
    print(n-max([even[even_second_ind[0]] + odd[odd_max_ind[0]],even[even_max_ind[0]] + odd[odd_second_ind[0]]]))

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

dd_even = defaultdict(int)
dd_odd  = defaultdict(int)


for i in range(0, n, 2):
    dd_even[v[i]] += 1

l_even = list(dd_even.items())
l_even.sort(key= lambda x:x[1])
top_even = l_even[-1][0]



for i in range(1, n, 2):
    dd_odd[v[i]] += 1

l_odd = list(dd_odd.items())
l_odd.sort(key= lambda x:x[1])

top_odd = l_odd[-1][0]

if top_even == top_odd:
    if len(l_odd) == 1: 
        res = int(n / 2)
        print(res)
        sys.exit()
    else:
        
        l_odd_1 = [l for l in l_odd if l[0] != l_odd[-2][0]]
        sum_odd_1 = sum(l[1] for l in l_odd_1)
        l_even_1 = [l for l in l_even if l[0] != top_even]
        sum_even_1 = sum(l[1] for l in l_even_1)
        sum_1 = sum_odd_1 + sum_even_1

        l_odd_2 = [l for l in l_odd if l[0] != top_odd]
        sum_odd_2 = sum(l[1] for l in l_odd_2)
        l_even_2 = [l for l in l_even if l[0] != l_even[-2][0]]
        sum_even_2 = sum(l[1] for l in l_even_2)
        sum_2 = sum_odd_2 + sum_even_2

        if sum_1 < sum_2:
            print(sum_1)
        else:
            print(sum_2)
        sys.exit()
else:
    l_odd = [l for l in l_odd if l[0] != top_odd]

sum_odd = sum(l[1] for l in l_odd)


l_even = [l for l in l_even if l[0] != top_even]
sum_even = sum(l[1] for l in l_even)


res = sum_odd + sum_even
print(res)

Solution (Python):
import heapq
n=int(input().strip())
nu=list(map(int, input().strip().split()))
nu1=[]
nu2=[]
nu1memo=[0]*100001
nu2memo=[0]*100001
for i in range(0,n//2):
    nu1.append(nu[2*i])
    nu2.append(nu[2*i+1])
    nu1memo[nu[2*i]]+=1
    nu2memo[nu[2*i+1]]+=1
    

mi1=heapq.nlargest(2, range(len(nu1memo)), key=nu1memo.__getitem__)

mi2=heapq.nlargest(2, range(len(nu2memo)), key=nu2memo.__getitem__)
ans=0
if mi1[0]!=mi2[0]:
    ans=n-nu1memo[mi1[0]]-nu2memo[mi2[0]]
elif mi1[0]==mi2[0]:
    ans=min(n-nu1memo[mi1[0]]-nu2memo[mi2[1]],n-nu1memo[mi1[1]]-nu2memo[mi2[0]])
    
print(ans)

Solution (Python):
import collections

N = int(input())
a = list(int(i) for i in input().split())

b = a[::2]
c = a[1::2]

b_ =collections.Counter(b)
c_ =collections.Counter(c)

baa = b_.most_common()
caa = c_.most_common()

baa.append([0,0])
caa.append([0,0])

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

Solution (Python):
n=int(input())
t=list(map(int,input().split()))
import collections
t_set=list(set(t))
half_n=n//2
even_list=[]
odd_list=[]
if len(t_set)==1:
    print(half_n)
    exit()
for i in range(0,n,2):
    even_list.append(t[i])
for i in range(1,n,2):
    odd_list.append(t[i])
even_list=collections.Counter(even_list)
odd_list=collections.Counter(odd_list)
if even_list.most_common()[0][0]!=odd_list.most_common()[0][0]:
    ans=(half_n-even_list.most_common()[0][1])+(half_n-odd_list.most_common()[0][1])
else:
    ans=(half_n-even_list.most_common()[0][1])+(half_n-odd_list.most_common()[1][1])
    ans_2=(half_n-even_list.most_common()[1][1])+(half_n-odd_list.most_common()[0][1])
    ans=min(ans,ans_2)
print(ans)

Solution (Python):
n = int(input())

c = [int(x) for x in input().split()]

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

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

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

even = sorted(even,key = lambda x:-x[1])
odd = sorted(odd,key = lambda x:-x[1])

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

Solution (Python):
n = int(input())

c = [int(x) for x in input().split()]

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

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

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

even = sorted(even,key = lambda x:-x[1])
odd = sorted(odd,key = lambda x:-x[1])

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

Solution (Python):
def count_char_occurrence(seq):
    
    ret = {}
    for char in seq:
        if char in ret:
            ret[char] += 1
        else:
            ret[char] = 1
    return ret


def accum_char_frequency(seq):
    """
    accumulate frequency of each number in a inputted sequence
    Example:
        in: [a,a,a,a,b,b,b,b,c]
        out: { 4 times: [a,b] , 1 times: [c] }
    """
    frequencies = count_char_occurrence(seq)

    ret = {}
    for char, count in frequencies.items():
        if count in ret:
            ret[count].append(char)
        else:
            ret[count] = [char]
    return ret


def main():
    _ = input()
    seq = list(map(int, input().split()))

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

    if len(set(seq)) == 1:  
        print(len(even))
        return

    no_freq_odd = accum_char_frequency(odd)
    freq_odd = sorted(no_freq_odd.items(), reverse=True)
    
    no_freq_even = accum_char_frequency(even)
    freq_even = sorted(no_freq_even.items(), reverse=True)

    if len(freq_odd[0][1]) == 1 and len(freq_even[0][1]) == 1 and freq_odd[0][1] == freq_even[0][1]:
        min_change_even = len(seq)
        if len(freq_even) != 1:
            min_change_even = (len(odd) - freq_odd[0][0]) + (len(even) - freq_even[1][0])

        min_change_odd = len(seq)
        if len(freq_odd) != 1:
            min_change_odd = (len(odd) - freq_odd[1][0]) + (len(even) - freq_even[0][0])

        print(min(min_change_even, min_change_odd))
        return
    else:
        min_change = (len(odd) - freq_odd[0][0]) + (len(even) - freq_even[0][0])
        print(min_change)
        return

main()

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

a = [arr[i] for i in range(0,n,2)]
b = [arr[y] for y in range(1,n,2)]

import collections
acnt = collections.Counter(a)
bcnt = collections.Counter(b)

lsacnt = sorted(acnt.items(), key=lambda x: x[1],reverse=True)
lsbcnt = sorted(bcnt.items(), key=lambda j: j[1],reverse=True)

def val(abcnt, ablscnt):
	if len(list(abcnt)) < 2:
		return 0
	else:
		return ablscnt[1][1]

if lsacnt[0][0] != lsbcnt[0][0]:
	ans = n - lsacnt[0][1] - lsbcnt[0][1]
elif lsacnt[0][0] == lsbcnt[0][0]:
	if lsacnt[0][1] > lsbcnt[0][1]:
		ans = n - lsacnt[0][1] - val(bcnt,lsbcnt)
	elif lsacnt[0][1] < lsbcnt[0][1]:
		ans = n - val(acnt,lsacnt) - lsbcnt[0][1]
	elif lsacnt[0][1] == lsbcnt[0][1]:
		ans = n - lsacnt[0][1] - max(val(acnt,lsacnt),val(bcnt,lsbcnt))

print(ans)

Solution (Python):
import collections
n = int(input())
v = list(int(i) for i in input().split())

a = v[:n:2]
b = v[1:n:2]
c = collections.Counter(a)
d = collections.Counter(b)

if len(c) ==1 and len(d) == 1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        print(n//2)

if len(c) ==1 and len(d) !=1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[1][1])

if len(d) ==1 and len(c) !=1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        print(n-c.most_common()[1][1]-d.most_common()[0][1])

if len(c) != 1 and len(d) != 1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        if (c.most_common()[0][1]+d.most_common()[1][1]) >= (c.most_common()[1][1]+d.most_common()[0][1]):
            print(n-c.most_common()[0][1]-d.most_common()[1][1])
        else:
            print(n-c.most_common()[1][1]-d.most_common()[0][1])

Solution (Python):


n = input()
array = map(int, raw_input().split())
e_list = [array[i] for i in range(0, n, 2)]
o_list = [array[i] for i in range(1, n+1, 2)]


def process(arr):
    arr.sort()
    num = arr[0]
    counter = 0
    amt_arr = []
    arr.append(0)
    max = 0
    max_amt_num = []
    for i in arr:
        if i == num:
            counter += 1
        else:
            if counter > max:
                max_amt_num = [num]
                max = counter
            elif counter == max:
                max_amt_num.append(num)
                max = counter
            num = i
            amt_arr.append(counter)
            counter = 1
    if len(amt_arr) == 1:
        amt_arr.append(0)
    if len(max_amt_num) > 1:
        max_amt_num = "haha"
    amt_arr.sort()

    return amt_arr[-1], amt_arr[-2], max_amt_num
v1, v2, a1 = process(e_list)
v3, v4, a2 = process(o_list)
if a1 != a2 and a1 != "haha":
    result = len(array) - v1 - v3
else:
    result = len(array) - max(v1+v4, v2+v3)
print result

Solution (Python):


n = input()
array = map(int, raw_input().split())
e_list = [array[i] for i in range(0, n, 2)]
o_list = [array[i] for i in range(1, n+1, 2)]


def process(arr):
    arr.sort()
    num = arr[0]
    counter = 0
    amt_arr = []
    arr.append(0)
    max = 0
    max_amt_num = []
    for i in arr:
        if i == num:
            counter += 1
        else:
            if counter > max:
                max_amt_num = [num]
                max = counter
            elif counter == max:
                max_amt_num.append(num)
                max = counter
            num = i
            amt_arr.append(counter)
            counter = 1
    if len(amt_arr) == 1:
        amt_arr.append(0)
    if len(max_amt_num) > 1:
        max_amt_num = "haha"
    amt_arr.sort()

    return amt_arr[-1], amt_arr[-2], max_amt_num
v1, v2, a1 = process(e_list)
v3, v4, a2 = process(o_list)
if a1 != a2 and a1 != "haha":
    result = len(array) - v1 - v3
else:
    result = len(array) - max(v1+v4, v2+v3)
print result

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

Solution (Python):
import collections
n=int(input())
half_n=n//2
t=list(map(int,input().split()))
even_lis=[]
odd_lis=[]
set_t=list(set(t))
if len(set_t)==1:
    print(n//2)
    exit()
for i in range(0,n,2):
    even_lis.append(t[i])
for i in range(1,n,2):
    odd_lis.append(t[i])
even_count=collections.Counter(even_lis)
odd_count=collections.Counter(odd_lis)
if even_count.most_common()[0][0]!=odd_count.most_common()[0][0]:
    ans=(half_n-even_count.most_common()[0][1])+(half_n-odd_count.most_common()[0][1])
else:
    ans=(half_n-even_count.most_common()[0][1])+(half_n-odd_count.most_common()[1][1])
    ans_2=(half_n-even_count.most_common()[1][1])+(half_n-odd_count.most_common()[0][1])
    ans=min(ans,ans_2)
print(ans)

Solution (Python):


from collections import Counter


def solve(n, seq):
    odd = seq[::2]  
    even = seq[1::2]  

    
    

    
    
    odd = sorted([(k, v) for k, v in Counter(odd).items()], key=lambda x: x[1], reverse=True)
    even = sorted([(k, v) for k, v in Counter(even).items()], key=lambda x: x[1], reverse=True)

    
    

    sol = 0
    if odd[0][0] == even[0][0]:
        if len(odd) == 1 and len(even) == 1:  
            sol = int(n // 2)  
        else:
            if len(odd) >= 2 and len(even) >= 2:
                sol = min(n - odd[0][1] - even[1][1], n - odd[1][1] - even[0][1])
            else:
                if odd[0][1] > even[1][1]:
                    
                    sol = (n // 2 - odd[0][1]) + (n // 2 - even[1][1])
                else:
                    
                    sol = (n // 2 - odd[1][1]) + (n // 2 - even[0][1])
    else:
        
        sol = (n // 2 - odd[0][1]) + (n // 2 - even[0][1])

    return sol


if __name__ == "__main__":
    n = int(input())
    seq = list(map(int, input("").split(" ")))
    print(solve(n, seq))

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

O=[[int(x),0] for x in range(10**5+1)]
E=[[int(x),0] for x in range(10**5+1)]

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

O=sorted(O,key=lambda x:-x[1])
E=sorted(E,key=lambda x:-x[1])

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

Solution (Python):
from collections import Counter

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

n1=[]
n2=[]
for i in range(N//2):
    n1.append(n[i*2])
    n2.append(n[i*2-1])



cm1=Counter(n1).most_common(2)
cm2=Counter(n2).most_common(2)



cm1.append([0,0])
cm2.append([0,0])




if cm1[0][0] != cm2[0][0]:
    ans=N-cm1[0][1]-cm2[0][1]
elif cm1[0][1]-cm1[1][1] > cm2[0][1]-cm2[1][1]:
    ans=N-cm1[0][1]-cm2[1][1]
else:
    ans=N-cm1[1][1]-cm2[0][1]
print(ans)

Solution (Python):
N = int(input())
*V, = map(int, input().split())
def calc(V):
    A = {-1: 0}
    for c in V:
        A[c] = A.get(c, 0) + 1
    return sorted(A.items(), key=lambda x:(x[1],x[0]), reverse=1)
P = calc(V[::2])
Q = calc(V[1::2])
if P[0][0] == Q[0][0]:
    print(N-max(P[0][1]+Q[1][1], P[1][1]+Q[0][1]))
else:
    print(N-P[0][1]-Q[0][1])

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


class Node:
    def __init__(self, x):
        self.data  = x
        self.left  = None
        self.right = None
        self.count = 1

class BST:
    def __init__(self, number_list): 
        self.root = None 
        for node in number_list: 
            self.insert(node) 

    def insert(self, data):
        n = self.root 
        if n == None: 
            self.root = Node(data) 
            return 
        else: 
            while True: 
                entry = n.data 
                if data < entry: 
                    if n.left is None: 
                        n.left = Node(data) 
                        return 
                    n = n.left 
                elif data > entry: 
                    if n.right is None: 
                        n.right = Node(data) 
                        return 
                    n = n.right 
                else: 
                    n.data = data 
                    n.count+=1
                    return 


    def traverse(self,node,func,len):
        if node is not None:
            len.append([node.count,node.data])
            self.traverse(node.right,func,len)
            self.traverse(node.left,func,len)
        return len


def print_data(x): 
    print(x)

def append_data(l,x):
    l.append(x)



n1=[]
n2=[]
for i in range(N//2):
    n1.append(n[i*2])
    n2.append(n[i*2-1])



tree1=BST(n1)
tree2=BST(n2)



cn1=[]
cn1=tree1.traverse(tree1.root,append_data,cn1)
cn2=[]
cn2=tree2.traverse(tree2.root,append_data,cn2)



cn1.append([0,0])
cn2.append([0,0])



    
sum1=0
sum2=0
if cn1[0][0]>=cn1[1][0]:
    max11=cn1[0][0]
    num11=cn1[0][1]

    max12=cn1[1][0]
    num12=cn1[1][1]
else:
    max11=cn1[1][0]
    num11=cn1[1][1]

    max12=cn1[0][0]
    num12=cn1[0][1]


if cn2[0][0]>=cn2[1][0]:
    max21=cn2[0][0]
    num21=cn2[0][1]

    max22=cn2[1][0]
    num22=cn2[1][1]
else:
    max21=cn2[1][0]
    num21=cn2[1][1]

    max22=cn2[0][0]
    num22=cn2[0][1]
    

for i in range(2,len(cn1)):
    if max12<cn1[i][0]:
        if max11<cn1[i][0]: 
            sum1+=max12

            max12=max11
            num12=num11

            max11=cn1[i][0]
            num11=cn1[i][1]
        else:                
            sum1+=max12

            max12=cn1[i][0]
            num12=cn1[i][1]
    else:
        sum1+=cn1[i][0]


for i in range(2,len(cn2)):
    if max22<cn2[i][0]:
        if max21<cn2[i][0]: 
            sum2+=max22

            max22=max21
            num22=num21

            max21=cn2[i][0]
            num21=cn2[i][1]
        else:               
            sum2+=max22

            max22=cn2[i][0]
            num22=cn2[i][1]
    else:
        sum2+=cn2[i][0]



if num11 != num21:
    ans=sum1+sum2+max12+max22
elif max11-max12 > max21-max22:
    ans=sum1+sum2+max12+max21
else:
    ans=sum1+sum2+max11+max22
print(ans)

Solution (Python):
import math as M
n=int(input())
Numbers=list(map(int,input().split(' ')))
Evens={}
Odds={}
for i in range(n):
    if i%2==0:
        if Numbers[i] in Evens:
            Evens[Numbers[i]]=Evens[Numbers[i]]+1
        else:
            Evens[Numbers[i]]=1

    if i%2!=0:
        if Numbers[i] in Odds:
            Odds[Numbers[i]]=Odds[Numbers[i]]+1
        else:
            Odds[Numbers[i]]=1

Evens=sorted(Evens.items(),key=lambda x:x[1])
Odds=sorted(Odds.items(),key=lambda x:x[1])
Evens.reverse()
Odds.reverse()

if Evens[0][0]==Odds[0][0]:
    if len(Evens)==1:
        Evens.append([Evens[0][0]+1,0])
    if len(Odds) == 1:
        Odds.append([Odds[0][0] + 1, 0])
    print(min(n,n-Evens[0][1]-Odds[1][1],n - Evens[1][1] - Odds[0][1]))
else:
    print(n-Evens[0][1]-Odds[0][1])

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

v_even, v_odd = v_list[::2], v_list[1::2]

from collections import Counter

so_ev = Counter(v_even).most_common(2)
so_od = Counter(v_odd).most_common(2)

if so_ev[0][0] == so_od[0][0]:
    if (len(so_ev) == 1) and (len(so_od) == 1):
        print(n - so_ev[0][1])
    else:
        print(min(n - so_ev[1][1] - so_od[0][1], n - so_ev[0][1] - so_od[1][1]))
else:
    print(n - so_ev[0][1] - so_od[0][1])

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

even = []
odd = []

for index, i in enumerate(li):
    if index % 2 == 0:
        even.append(i)
    elif index % 2 != 0:
        odd.append(i)

from collections import Counter
so_ev = Counter(even).most_common(2)
so_od = Counter(odd).most_common(2)

if so_ev[0][0] == so_od[0][0]:
    if len(so_ev) == 1 and len(so_od) == 1:
        print(n - so_ev[0][1])
    else:
        print(min(n - so_ev[1][1] - so_od[0][1], n - so_ev[0][1] - so_od[1][1]))
else:
    print(n - so_ev[0][1] - so_od[0][1])

Solution (Python):
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
even1 = v[0::2]
odd1 = v[1::2]
even2 = Counter(even1)
odd2 = Counter(odd1)
a = even2.most_common()[0][0]
b = odd2.most_common()[0][0]
c = even2.most_common()[0][1]
d = odd2.most_common()[0][1]
if a != b:
 print(len(even1)-c+len(odd1)-d)
else:
 if len(list(set(even1))) == 1 and len(list(set(odd1))) == 1:
  print(len(even1))
 elif len(list(set(even1))) == 1:
  print(len(odd1)-odd2.most_common()[1][1])
 elif len(list(set(odd1))) == 1:
  print(len(even1)-even2.most_common()[1][1])
 else:
  e = even2.most_common()[1][1]
  f = odd2.most_common()[1][1]
  if c-e >= d-f:
   print(len(even1)*2-c-f)
  else:
   print(len(even1)*2-d-e)

Solution (Python):
import numpy as np

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

m = int(n / 2)
p = max(v)

evens = np.array([0] * p)
odds = np.array([0] * p)

for i in range(0, m):
    evens[v[2 * i] - 1] += 1
    odds[v[2 * i + 1] - 1] += 1

if len(evens) == 1 and len(odds) == 1:
    if evens.argsort()[-1] == odds.argsort()[-1]:
        print(m)
    else:
        print(0)
elif len(evens) ==1:
    mode_e1 = evens.argsort()[-1]
    mode_o2, mode_o1 = odds.argsort()[-2:]
    if mode_e1 == mode_o1:
        print(m - odds[mode_o2])
    else:
        print(m - odds[mode_o1])
elif len(odds) == 1:
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o1 = odds.argsort()[-1]
    if mode_e1 == mode_o1:
        print(m - evens[mode_e2])
    else:
        print(m - evens[mode_e1])
else:
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o2, mode_o1 = odds.argsort()[-2:]

    if mode_e1 == mode_o1:
        print(min(n - evens[mode_e1] - odds[mode_o2], n - evens[mode_e2] - odds[mode_o1]))
    else:
        print(n - evens[mode_e1] - odds[mode_o1])

Solution (Python):
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[::2]).most_common() 
b=Counter(v[1::2]).most_common()
if len(a)==1:
    a.append([0,0])
if len(b)==1:
    b.append([0,0])
if a[0][0]==b[0][0]:
    print(min(n-a[1][1]-b[0][1],n-a[0][1]-b[1][1]))
else:
    print(n-a[0][1]-b[0][1])

Solution (Python):
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
a = Counter(v[::2]).most_common()
b = Counter(v[1::2]).most_common()
if len(a) ==1:
  a.append([0,0])
if len(b) == 1:
  b.append([0,0])
if a[0][0] == b[0][0]:
  if a[1][1]>b[1][1]:
    print(n-a[1][1] - b[0][1])
  else:
    print(n-a[0][1]-b[1][1])
else:
  print(n-a[0][1]-b[0][1])

Solution (Python):
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
a = Counter(v[::2]).most_common()
b = Counter(v[1::2]).most_common()
if len(a) == 1:
    a.append([0, 0])
if len(b) == 1:
    b.append([0, 0])
if a[0][0] == b[0][0]:
    if a[1][1] > b[1][1]:
        print(n - a[1][1] - b[0][1])
    else:
        print(n - a[0][1] - b[1][1])
else:
    print(n- a[0][1] - b[0][1])

Solution (Python):

import collections

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

e = [vs[i*2] for i in range(n//2)]
o = [vs[i*2+1] for i in range(n//2)]

e_counter = collections.Counter(e).most_common()
o_counter = collections.Counter(o).most_common()

e1, e1_count = e_counter[0]
try:
    e2, e2_count = e_counter[1]
except IndexError:
    e2_count = 0

o1, o1_count = o_counter[0]
try:
    o2, o2_count = o_counter[1]
except IndexError:
    o2_count = 0

if e1 != o1:
    keep_num_count = e1_count + o1_count
else:
    keep_num_count = e1_count + o2_count
    if keep_num_count < e2_count + o1_count:
        keep_num_count = e2_count + o1_count

print(n - keep_num_count)

Solution (Python):
_ = input()
seq = list(map(int,input().split()))
 
seq1 = seq[::2]
seq2 = seq[1::2]
 
if len(set(seq)) == 1:
    print(len(seq2))
    exit()
    
def count_dictonary(seq):
    ret_dict={}
    for item in seq:
        if item not in ret_dict:
            ret_dict[item] = 1
        else:
            ret_dict[item]+=1
    return ret_dict
 
def modes(counting_dict):
    
    ret={}
    for item in counting_dict.items():
        no, count = item
        if counting_dict[no] in ret:
            ret[count].append(no)
        else:
            ret[count]=[no]
            
    return ret
 
 
counting1 = count_dictonary(seq1)
counting2 = count_dictonary(seq2)
 
modes1 = sorted(modes(counting1).items(),reverse=True)
modes2 = sorted(modes(counting2).items(),reverse=True)
 
min_change=len(seq)
if modes1[0][1] != modes2[0][1] or len(modes1[0][1]) >=2 or len(modes2[0][1]) >=2:
    min_change = (len(seq1) - modes1[0][0]) + (len(seq2) - modes2[0][0])
else:
    m01=len(seq)
    m10=len(seq)
    if len(modes2) !=1:
        m01 =  (len(seq1) - modes1[0][0]) + (len(seq2) - modes2[1][0])
    if len(modes1) !=1:
        m10 =  (len(seq1) - modes1[1][0]) + (len(seq2) - modes2[0][0])
    min_change = min(m01,m10)
    
print(min_change)

Solution (Python):

import collections


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

    va = []
    vb = []
    for i in range(0, n, 2):
        va.append(v[i])
        vb.append(v[i + 1])

    if va == vb and len(set(va)) == 1:
        print(len(va))
        return

    c1mc = collections.Counter(va).most_common()
    c2mc = collections.Counter(vb).most_common()

    if c1mc[0][0] != c2mc[0][0]:
        print(n - c1mc[0][1] - c2mc[0][1])
        return

    else:
        a = n - c1mc[1][1] - c2mc[0][1]
        b = n - c1mc[0][1] - c2mc[1][1]

        if a < b:
            print(a)
        else:
            print(b)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
n = int(input())
V = list(map(int,input().split()))
V_odd = V[::2]
V_eve = V[1::2]
odd_max = Counter(V_odd).most_common(1)
even_max = Counter(V_eve).most_common(1)

result = n - odd_max[0][1] - even_max[0][1]
if odd_max[0][0] == even_max[0][0]:
    odd_least,even_least = 0,0
    if Counter(V_odd).most_common(1) != Counter(V_odd).most_common(2):
        odd_least = (Counter(V_odd).most_common(2))[1][1]
    if Counter(V_eve).most_common(1) != Counter(V_eve).most_common(2):
        even_least = (Counter(V_eve).most_common(2))[1][1]
    if Counter(V_odd).most_common(1) == Counter(V_odd).most_common(2) and Counter(V_eve).most_common(1) == Counter(V_eve).most_common(2):
        result = int(n/2)
    else:
        result = min(n - odd_least - even_max[0][1],n - even_least - odd_max[0][1])

print(result)

Solution (Python):
N = int(input())
a = [[],[]]
[a[i%2].append(t) for i, t in enumerate(map(int, input().split()))]

a_cnt = [{},{}]
for i in range(2):
    for j in range(len(a[i])):
        if a[i][j] in a_cnt[i]:
            a_cnt[i][a[i][j]] += 1
        else:
            a_cnt[i][a[i][j]] = 1

for i in range(2):
    
    a_cnt[i] = list(sorted(list(a_cnt[i].items()),\
        key=(lambda x:x[1]), reverse=True))[:2]


if a_cnt[0][0][0] != a_cnt[1][0][0]:
    print(N -a_cnt[0][0][1] -a_cnt[1][0][1])
else:
    if len(a_cnt[0])==1 and len(a_cnt[1])==1:
        print(N//2)
    else:
        ans1 = N -a_cnt[0][0][1] -a_cnt[1][1][1]
        ans2 = N -a_cnt[0][1][1] -a_cnt[1][0][1]
        print(min(ans1, ans2))

Solution (Python):
n=int(input())
vlis=[int(i) for i in input().split()]





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



import collections
odd_max=collections.Counter(odd).most_common(2)
even_max=collections.Counter(even).most_common(2)


if odd_max[0][0]==even_max[0][0]:
    
    if len(odd_max)==1 and len(even_max)==1:
        print(int(len(vlis)/2))
    
    elif len(even_max)==1:
        print(len(vlis)-even_max[0][1]-odd_max[1][1])
    
    elif len(odd_max)==1:
        print(len(vlis)-odd_max[0][1]-even_max[1][1])
    else:

        if odd_max[0][1]+even_max[1][1]>=odd_max[1][1]+even_max[0][1]:
            print(len(vlis)-odd_max[0][1]-even_max[1][1])
        else:
            print(len(vlis)-odd_max[1][1]-even_max[0][1])
else:
    print(len(vlis)-odd_max[0][1]-even_max[0][1])

Solution (Python):
n=int(input())
vlis=[int(i) for i in input().split()]
odd=vlis[0::2]
even=vlis[1::2]
import collections 
odd_=collections.Counter(odd).most_common(2)
even_=collections.Counter(even).most_common(2)
 
if odd_[0][0]==even_[0][0]:
    if len(odd_)==1 and len(even_)==1:
        print(int(len(vlis)/2))
    elif len(even_)==1:
        print(len(vlis)-even_[0][1]-odd_[1][1])
    elif len(odd_)==1:
        print(len(vlis)-odd_[0][1]-even_[1][1])
    else:
        if odd_[0][1]+even_[1][1]>=odd_[1][1]+even_[0][1]:
            print(len(vlis)-odd_[0][1]-even_[1][1])
        else:
            print(len(vlis)-odd_[1][1]-even_[0][1])
else:
    print(len(vlis)-odd_[0][1]-even_[0][1])