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 /\/\/\/.



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

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

even = []
odd = []
for i, v in enumerate(V):
	if i % 2 == 0: even.append(v)
	else: odd.append(v)

cnt_even = Counter(even).most_common(2)
cnt_odd = Counter(odd).most_common(2)

if cnt_even[0][0] != cnt_odd[0][0]:
	ans = len(V) - cnt_even[0][1] - cnt_odd[0][1]
else:
	if cnt_even[0][1] + cnt_odd[0][1] == len(V):
		ans = len(V) // 2

	elif len(cnt_odd) < 2 or len(cnt_even) < 2:
		ans = len(V) - cnt_even[0][1] - cnt_odd[0][1]
	else:
		ans1 = len(V) - cnt_even[0][1] - cnt_odd[1][1]
		ans2 = len(V) - cnt_even[1][1] - cnt_odd[0][1]
		ans = min(ans1, ans2)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = [l for l in input().split()]
o = s[1::2]
e = s[0::2]
od = {'0':0}
ed = {'0':0}
for i in o:
    if i not in od.keys():
        od[i] = 1
    else:
        od[i] += 1

for i in e:
    if i not in ed.keys():
        ed[i] = 1
    else:
        ed[i] += 1

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


if od[0][0] != ed[0][0]:
    print((len(o)-od[0][1])+(len(e)-ed[0][1]))
else:
    print(min(len(o)-od[0][1]+len(e)-ed[1][1],len(o)-od[1][1]+len(e)-ed[0][1]))

Submission status: Accepted

Solution code:
import collections

num = int(input())
values = [int(i) for i in input().split()]

odd = []  # kisuu
even = []  # gusu

for n, i in enumerate(values):
    if (n + 1) % 2 == 0:
        even.append(i)
    else:
        odd.append(i)

odd_counts = collections.Counter(odd)
even_counts = collections.Counter(even)

# print(odd_counts.most_common())
# print(even_counts.most_common())

odd_most_common = odd_counts.most_common()
even_most_common = even_counts.most_common()

half_num = int(num / 2)

if odd_most_common[0][0] != even_most_common[0][0]:
    odd_replace_num = half_num - odd_most_common[0][1]
    even_replace_num = half_num - even_most_common[0][1]
    print(odd_replace_num + even_replace_num)
else:
    answers = []
    odd_replace_num = half_num - odd_most_common[0][1]
    if len(even_most_common) == 1:
        even_replace_num = half_num
    else:
        even_replace_num = half_num - even_most_common[1][1]
    answers.append(odd_replace_num + even_replace_num)

    if len(odd_most_common) == 1:
        odd_replace_num = half_num
    else:
        odd_replace_num = half_num - odd_most_common[1][1]
    even_replace_num = half_num - even_most_common[0][1]
    answers.append(odd_replace_num + even_replace_num)

    print(min(answers))

Submission status: Accepted

Solution code:
import collections

n = int(input())
Vn = list(map(int, input().split(' ')))
Es, Os = Vn[0:n:2], Vn[1:n:2]

if (Vn.count(Vn[0]) == len(Vn)):
    print(n//2)
    exit()

# 各集合Es, Os の最頻値を val_Es_1, val_Os_1 とする
# また、最頻値の出現回数をそれぞれ、cnt_Es_1, cnt_Os_1 とする
val_Es_1, cnt_Es_1 = collections.Counter(Es).most_common()[0]
val_Os_1, cnt_Os_1 = collections.Counter(Os).most_common()[0]

if val_Es_1 != val_Os_1:
    print(n - (cnt_Es_1 + cnt_Os_1))
    exit()

# 各集合Es, Os の2番目の最頻値を val_Es_2, val_Os_2 とする
# また、最頻値の出現回数をそれぞれ、cnt_Es_2, cnt_Os_2 とする
val_Es_1, cnt_Es_2 = collections.Counter(Es).most_common()[1]
val_Os_1, cnt_Os_2 = collections.Counter(Os).most_common()[1]

print(min(n - (cnt_Es_1 + cnt_Os_2), n - (cnt_Es_2 + cnt_Os_1)))
exit()

Submission status: Accepted

Solution code:
import heapq

n=int(input())
ve=[0 for _ in range(100010)]
vo=[0 for _ in range(100010)]
v = list(map(int,input().split()))

for nn in range(n):
        vv = v[nn]
        if(nn%2==0):
                ve[vv]+=1
        else:
                vo[vv]+=1
hve = heapq.nlargest(2,range(len(ve)),key=ve.__getitem__)
hvo = heapq.nlargest(2,range(len(vo)),key=vo.__getitem__)

if hve[0]==hvo[0]:
        ans = min((n-ve[hve[0]]-vo[hvo[1]]),(n-ve[hve[1]]-vo[hvo[0]]))
else:
        ans = n-ve[hve[0]]-vo[hvo[0]]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = [int(i) for i in input().split()]
A0 = []
A1 = []
for i in range(0, n, 2):
    A0.append(A[i])
    A1.append(A[i + 1])
    
C0 = Counter(A0).most_common()
C1 = Counter(A1).most_common()

if C0[0][0] != C1[0][0]:
    ans = n - C0[0][1] - C1[0][1]
else:
    if len(C0) == 1 and len(C1) == 1:
        ans = n // 2
    elif len(C0) != 1 and len(C1) != 1:
        ans = min(n - C0[0][1] - C1[1][1], n - C0[1][1] - C1[0][1])
    elif len(C0) != 1 and len(C1) == 1:
        ans = n // 2 - C0[0][1]
    else:
        ans = n // 2 - C1[0][1]
        
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
v_even,v_odd =[[],[]]

for i in range(n//2):
    v_even.append(v[2*i])
    v_odd.append(v[2*i+1])

mc1 = Counter(v_even).most_common()
mc2 = Counter(v_odd).most_common()

k1,v1 = mc1[0]
k2,v2 = mc2[0]

if k1!=k2:
    print(n-v1-v2)
else:
    w1=0
    if len(mc1)>1:
        w1=mc1[1][1]
    w2=0
    if len(mc2)>1:
        w2=mc2[1][1]
    print(n-max(v1+w2,v2+w1))

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
cnt0 = [0 for i in range(100001)]
cnt1 = [0 for i in range(100001)]
for i, elem in enumerate(v):
    if i%2==0:
        cnt0[elem] += 1
    else:
        cnt1[elem] += 1

max_first0 = -1
argmax_first0 = 0
max_second0 = -1
argmax_second0 = 0
for i, elem in enumerate(cnt0):
    if elem>=max_second0:
        max_second0 = elem
        argmax_second0 = i
    if elem>=max_first0:
        max_second0 = max_first0
        argmax_second0 = argmax_first0
        max_first0 = elem
        argmax_first0 = i
max_first1 = -1
argmax_first1 = 0
max_second1 = -1
argmax_second1 = 0
for i, elem in enumerate(cnt1):
    if elem>=max_second1:
        max_second1 = elem
        argmax_second1 = i
    if elem>=max_first1:
        max_second1 = max_first1
        argmax_second1 = argmax_first1
        max_first1 = elem
        argmax_first1 = i
#print(argmax_first0, argmax_first1, argmax_second0, argmax_second1)
#print(max_first0, max_first1, max_second0, max_second1)

if argmax_first0 != argmax_first1:
    ans = n//2-max_first0 + n//2-max_first1
else:
    ans = min(n-max_first0-max_second1, n-max_first1-max_second0)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
v = list(map(int,input().split()))
cntnumeven = [0 for _ in range(10**5+1)]
cntnumodd = [0 for _ in range(10**5+1)]
veven = [v[2*i+1] for i in range(n//2)]
vodd = [v[2*i] for i in range(n//2)]
# for i in v:


for i in vodd:
    cntnumodd[i] += 1

for i in veven:
    cntnumeven[i] += 1
if np.argmax(cntnumeven) == np.argmax(cntnumodd):
    cntnumeven.sort(reverse=True)
    cntnumodd.sort(reverse=True)
    print(min(n - cntnumeven[1] - cntnumodd[0],n - cntnumeven[0] - cntnumodd[1]))
else:
    print(n - max(cntnumodd) - max(cntnumeven))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = [int(i) for i in input().split()]

dummy = [(-1, 0)]
(av1, ak1), (av2, ak2) = (collections.Counter(v[::2]).most_common(2) + dummy)[:2]
(bv1, bk1), (bv2, bk2) = (collections.Counter(v[1::2]).most_common(2) + dummy)[:2]

if av1 != bv1:
    print(n-ak1-bk1)
elif ak1 < bk1:
    print(n-ak2-bk1)
elif ak1 > bk1:
    print(n-ak1-bk2)
elif ak1+bk2 > bk1+ak2:
    print(n-ak1-bk2)
else:
    print(n-ak2-bk1)

Submission status: Accepted

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

a, b = dict(), dict()
for i in range(n):
    if i%2 == 0:
        a[v[i]] = a.get(v[i], 0) + 1
    else:
        b[v[i]] = b.get(v[i], 0) + 1
alst = sorted(list(a.items()), key=lambda x: x[1], reverse=True)
blst = sorted(list(b.items()), key=lambda x: x[1], reverse=True)

if alst[0][0] != blst[0][0]:
    print(n-alst[0][1]-blst[0][1])
elif len(alst) == 1 and len(blst) == 1:
    print(blst[0][1])    
elif len(alst) == 1:
    print(n-alst[0][1]-blst[1][1])
elif len(blst) == 1:
    print(n-alst[1][1]-blst[0][1])
elif blst[0][1]+alst[1][1] > alst[0][1]+blst[1][1]:
    print(n-alst[1][1]-blst[0][1])
else:
    print(n-alst[0][1]-blst[1][1])

Submission status: Accepted

Solution code:
n=input()
v=map(int,raw_input().split())

v0=[v[i] for i in range(n) if i%2==0]
v1=[v[i] for i in range(n) if i%2==1]
    
import collections
c0=collections.Counter(v0)
c1=collections.Counter(v1)

if c0.most_common()[0][0]!=c1.most_common()[0][0]:
    print n-c0.most_common()[0][1]-c1.most_common()[0][1]
else:
    if c0.most_common()[0][1]==n/2 and c1.most_common()[0][1]==n/2:
        print n/2
    elif c0.most_common()[0][1]==n/2:
        print n-c0.most_common()[0][1]-c1.most_common()[1][1]
    elif c1.most_common()[0][1]==n/2:
        print n-c0.most_common()[1][1]-c1.most_common()[0][1]
    else:            
        print min(n-c0.most_common()[1][1]-c1.most_common()[0][1]\
                  ,n-c0.most_common()[0][1]-c1.most_common()[1][1])

Submission status: Accepted

Solution code:
n = int(input())
vi = list(map(int, input().split()))

e = [0 for i in range(max(vi)+1)]
o = [0 for i in range(max(vi)+1)]

me = [0,0]
mo = [0,0]

me2 = [0,0]
mo2 = [0,0]

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

for i in range(len(e)):
    if e[i] > me[0]:
        me2[0] = me[0]
        me2[1] = me[1]
        me[0] = e[i]
        me[1] = i
    elif e[i] > me2[0]:
        me2[0] = e[i]
        me2[1] = i

for i in range(len(o)):
    if o[i] > mo[0]:
        mo2[0] = mo[0]
        mo2[1] = mo[1]
        mo[0] = o[i]
        mo[1] = i
    elif o[i] > mo2[0]:
        mo2[0] = o[i]
        mo2[1] = i

if me[1] == mo[1]:
    num = 0
    if me[0] + mo2[0] > me2[0] + mo[0]:
        num = me[0] + mo2[0]
    else:
        num = me2[0] + mo[0]
    print(n - num)
else:
    num = me[0] + mo[0]
    print(n - num)

Submission status: Accepted

Solution code:
import collections


if __name__ == "__main__":
    N = int(input())
    v_list = list(map(int, input().split()))
    even_list = [v_list[i] for i in range(N) if i % 2 == 0]
    odd_list = [v_list[i] for i in range(N) if i % 2 != 0]

    even_counter = collections.Counter(even_list)
    odd_counter = collections.Counter(odd_list)

    even_common = even_counter.most_common()
    odd_common = odd_counter.most_common()

    even_max = even_common[0][0]
    odd_max = odd_common[0][0]

    if even_max != odd_max:
        print((N - even_common[0][1] - odd_common[0][1]))
    else:
        if len(even_common) > 1:
            even_second_replace = N - even_common[1][1] - odd_common[0][1]
        else:
            even_second_replace = N - odd_common[0][1]

        if len(odd_common) > 1:
            odd_second_replace = N - even_common[0][1] - odd_common[1][1]
        else:
            odd_second_replace = N - even_common[0][1]
        print(min(even_second_replace, odd_second_replace))

Submission status: Accepted

Solution code:
N = int(input())
v = list(map(int,input().split()))
v_even = [0 for i in range(max(v)+1)]
v_odd = [0 for i in range(max(v)+1)]
sum_even = 0
sum_odd = 0
for i in range(N):
    if i%2 :
        v_odd[v[i]] += 1
        sum_odd += 1
    else:
        v_even[v[i]] += 1
        sum_even += 1
max_odd = 0
odd = 0
max_even = 0
even = 0
for i in range(len(v_odd)):
    if v_odd[i] > max_odd:
        max_odd = v_odd[i]
        odd = i

for i in range(len(v_even)):
    if v_even[i] > max_even:
        max_even = v_even[i]
        even = i

if odd != even:
    ans = sum_even - max_even + sum_odd - max_odd
else:
    v_odd[odd] = 0
    v_even[even] = 0
    ans = min((sum_even - max_even + sum_odd - max(v_odd)), sum_even - max(v_even) + sum_odd - max_odd)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
E = [A[i] for i in range(n) if i%2 == 1]
O = [A[i] for i in range(n) if i%2 == 0]
e = Counter(E).most_common(2)
o = Counter(O).most_common(2)
if e[0][0] != o[0][0]: ##Eの最頻値とOの最頻値が違う
    ans = (n//2-e[0][1])+(n//2-o[0][1])
    print(ans)
    ##以下、EとOの最頻値が同じ時の処理を書く
elif len(e)>=2 and len(o)>=2:
    ans = min((n//2-e[0][1])+(n//2-o[1][1]), (n//2-e[1][1])+(n//2-o[0][1]))
    print(ans)
else:
    print(n//2)

Submission status: Accepted

Solution code:
from collections import defaultdict
d0 = defaultdict(int)
d1 = defaultdict(int)
N=int(input())
v=list(map(int,input().split()))
for i in range(N):
    if i%2==0:
        d0[v[i]]+=1
    else:
        d1[v[i]]+=1
D0=sorted(d0.items(), key=lambda x:x[1],reverse=True)
D1=sorted(d1.items(), key=lambda x:x[1],reverse=True)
if (D0[0][1]+D1[0][1])==len(v) and D0[0][0]==D1[0][0]:
    print(len(v)//2)
else:
    i0=0
    i1=0
    while i0<=len(D0) or i1<=len(D1):
        if D0[i0][0]!=D1[i1][0]:
            print(len(v)-D0[i0][1]-D1[i1][1])
            break
        else:
            if D0[i0][1]>D1[i1][1]:
                i1+=1
            elif D0[i0][1]<D1[i1][1]:
                i0+=1
            else:
                if (len(D0)-1-i0)>0 and (len(D1)-1-i1)>0:
                    if D0[i0+1][1]>D1[i1+1][1]:
                        i0+=1
                    else:
                        i1+=1
                elif (len(D0)-1-i0)==0 :
                    i1+=1
                elif (len(D1)-1-i1)==0:
                    i0+=1

Submission status: Accepted

Solution code:
n = int(input())
v = [{}, {}]
for i, vi in enumerate(map(int, input().split())):
    j = i % 2
    v[j].update([(vi, v[j][vi] + 1)]) if vi in v[j] else v[j].update([(vi, 1)])
f1 = lambda v, i: max(v[i], key=v[i].get) if len(v[i]) > 0 else 0
f2 = lambda v, i, k: v[i].pop(k) if len(v[i]) > 0 else 0
kMax, vMax = [], []
for i in [0, 1]:
    kMax.append([f1(v, 0), f1(v, 1)])
    vMax.append([f2(v, 0, kMax[i][0]), f2(v, 1, kMax[i][1])])
if kMax[0][0] != kMax[0][1]:
    print(n - sum(vMax[0]))
else:
    print(n - max([vMax[0][0] + vMax[1][1], vMax[0][1] + vMax[1][0]]))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
V = list(map(int, input().split()))
X = Counter([V[i] for i in range(0, n, 2)]).most_common()
Y = Counter([V[i] for i in range(1, n, 2)]).most_common()

ans = 0

if X[0][0] != Y[0][0]:
    ans = n - X[0][1] - Y[0][1]
else:
    if len(X) == 1 and len(Y) == 1:
        ans = n // 2
    elif len(X) == 1 and len(Y) != 1:
        ans = n // 2 - Y[1][1]
    elif len(X) != 1 and len(Y) == 1:
        ans = n // 2 - X[1][1]
    else:
        a = n - X[0][1] - Y[1][1]
        b = n - X[1][1] - Y[0][1]
        ans = min(a, b)
        
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
a1 = []
a2 = []
for i in range(n):
    if i % 2 == 0:
        a1.append(v[i])
    else:
        a2.append(v[i])

a1 = Counter(a1)
a2 = Counter(a2)

a1 = a1.most_common()
a2 = a2.most_common()

if a1[0][0] == a2[0][0]:
    if len(a1) == 1 and len(a2) == 1:
        print(int(n/2))
    else:
        if a1[1][1] > a2[1][1]:
            print(n-a1[1][1]-a2[0][1])
        else:
            print(n-a1[0][1]- a2[1][1])
else:
    print(n - a1[0][1] - a2[0][1])

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int, input().split()))

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 100001
bug2 = [0] * 100001

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print(n - max(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2))
else:
    print(n - max_v_1 - max_v_2)

Submission status: Accepted

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

v_index_odds = []
v_index_evens = []

for i in range(0,n,2):
    v_index_evens.append(v[i])
    v_index_odds.append(v[i+1])

evens_counter = collections.Counter(v_index_evens).most_common()
odds_counter = collections.Counter(v_index_odds).most_common()

if evens_counter[0][0] != odds_counter[0][0]:
    ans = n - evens_counter[0][1] - odds_counter[0][1]
else:
    if n - evens_counter[0][1] - odds_counter[0][1] == 0:
        ans = n // 2
    else:
        if n // 2 - evens_counter[0][1] == 0:
            ans = n // 2 - evens_counter[1][1]
        elif n // 2 - odds_counter[0][1] == 0:
            ans = n // 2 - evens_counter[1][1]
        else:
            a = n - evens_counter[0][1] - odds_counter[1][1]
            b = n - evens_counter[1][1] - odds_counter[0][1]
            ans = min(a, b)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
num = list(map(int, input().split()))
num1 = []
num2 = []
ans = 0

for i in range(0, n, 2):
    num1.append(num[i])
    
for i in range(1, n, 2):
    num2.append(num[i])

num1 = Counter(num1).most_common()
num2 = Counter(num2).most_common()    

if num1[0][0] != num2[0][0]:
    ans = n - num1[0][1] - num2[0][1]
else:
    if n - num1[0][1] - num2[0][1] == 0:
        ans = n // 2
    else:
        if n // 2 - num1[0][1] == 0:
            ans = n // 2 - num2[1][1]
        elif n // 2 - num2[0][1] == 0:
            ans = n // 2 - num1[1][1]
        else:
            a = n - num1[0][1] - num2[1][1]
            b = n - num1[1][1] - num2[0][1]
            ans = min(a, b)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

if __name__ == '__main__':
    n = int(input())
    vs = [int(s) for s in input().split()]
    counter0 = list(Counter(vs[0::2]).items()) + [(100001, 0)]
    counter1 = list(Counter(vs[1::2]).items()) + [(100001, 0)]
    v0, c0 = max(counter0, key=lambda t: t[1])
    v1, c1 = max(counter1, key=lambda t: t[1])
    counter0.remove((v0, c0))
    counter1.remove((v1, c1))
    res = 0
    if v0 != v1:
        res = n - c0 - c1
    else:
        _, c02 = max(counter0, key=lambda t: t[1])
        _, c12 = max(counter1, key=lambda t: t[1])
        res = n - (c0 + c12 if c12 > c02 else c1 + c02)
    print(res)

Submission status: Accepted

Solution code:
import collections

N = int(input()) # N
V = list(map(int, input().split()))
Ve = V[0::2]
Vo = V[1::2]

Ve_ = collections.Counter(Ve)
Vo_ = collections.Counter(Vo)

Ve_1st = max(Ve_.items(), key=(lambda v:v[1]))
Vo_1st = max(Vo_.items(), key=(lambda v:v[1]))
Ve_.pop(Ve_1st[0])
Vo_.pop(Vo_1st[0])
if len(Ve_) == 0: Ve_ = {'-1':0}
if len(Vo_) == 0: Vo_ = {'-1':0}
Ve_2nd = max(Ve_.items(), key=(lambda v:v[1]))
Vo_2nd = max(Vo_.items(), key=(lambda v:v[1]))

if Ve_1st[0] != Vo_1st[0]:
    M = N - Ve_1st[1] - Vo_1st[1]
else:
    M = min(N - Ve_1st[1] - Vo_2nd[1], N - Ve_2nd[1] - Vo_1st[1])
print(M)

Submission status: Accepted

Solution code:
n = int(input())
v = [int(i) for i in input().split()]
even = [0] * int(1e5+1)
odd = [0] * int(1e5+1)

for i, k in enumerate(v):
    if (i & 1 == 0):
        even[k] += 1
    else:
        odd[k] += 1

if even.index(max(even)) == odd.index(max(odd)):
    even.sort(reverse=True)
    odd.sort(reverse=True)
    print(min(n - even[1] - odd[0],
              n - even[0] - odd[1]))
else:
    even.sort(reverse=True)
    odd.sort(reverse=True)
    print(n - even[0] - odd[0])

Submission status: Accepted

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

d1, d2 = {}, {}

for i, a in enumerate(v):
    if i % 2 == 0:
        d1[a] = d1.get(a, 0) + 1
    else:
        d2[a] = d2.get(a, 0) + 1

d1[-1] = 0
d2[-1] = 0

s = (sorted(d1.items(), key=lambda x: -x[1]))
t = (sorted(d2.items(), key=lambda x: -x[1]))

if (s[0][0] != t[0][0]):
    print (n-s[0][1]-t[0][1])
else:
    print( min( n-s[0][1]-t[1][1], n-s[1][1]-t[0][1]) )

Submission status: Accepted

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

d1, d2 = {}, {}

for i, a in enumerate(v):
    if i % 2 == 0:
        d1[a] = d1.get(a, 0) + 1

    if i % 2 == 1:
        d2[a] = d2.get(a, 0) + 1

d1[-1] = 0
d2[-1] = 0

s = (sorted(d1.items(), key=lambda x: x[1], reverse=True))
t = (sorted(d2.items(), key=lambda x: x[1], reverse=True))
#print (s)
#print (t)
if (s[0][0] != t[0][0]):
    print (n-s[0][1]-t[0][1])
else:
    print( min( n-s[0][1]-t[1][1], n-s[1][1]-t[0][1]) )

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int, input().split()))
v1 = collections.Counter([x for i, x in enumerate(v) if i % 2 == 0]).most_common()
v2 = collections.Counter([x for i, x in enumerate(v) if i % 2 == 1]).most_common()
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[0][1] - v2[1][1], n - v1[1][1] - v2[0][1]))

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int, input().split()))
v1 = collections.Counter([x for i, x in enumerate(v) if i % 2 == 0]).most_common()
v2 = collections.Counter([x for i, x in enumerate(v) if i % 2 == 1]).most_common()
v1.append((0, 0))
v2.append((0, 0))
max_v1 = v1[0][1]
max_v2 = v2[0][1]

if v1[0][0] == v2[0][0]:
    if max_v1 == max_v2:
        if v1[1][1] >= v2[1][1]:
            max_v1 = v1[1][1]
        else:
            max_v2 = v2[1][1]
    elif max_v1 > max_v2:
        max_v2 = v2[1][1]
    elif max_v1 < max_v2:
        max_v1 = v1[1][1]

ans = 0
for a in v1:
    ans += a[1]

for a in v2:
    ans += a[1]

print(ans - max_v1 - max_v2)

Submission status: Accepted

Solution code:
from collections import Counter
 
n = int(input())
v_array = list(map(int,input().split()))
 
v_odd = v_array[0::2]
v_even = v_array[1::2]
 
v_odd_common = Counter(v_odd).most_common()
v_even_common = Counter(v_even).most_common()

v_odd_common.append((0,0))
v_even_common.append((0,0))

if v_odd_common[0] != v_even_common[0]:
    print(n-v_odd_common[0][1]-v_even_common[0][1])
else:
    print(min(n-v_odd_common[0][1]-v_even_common[1][1],n-v_odd_common[1][1]-v_even_common[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
v_odd=Counter(v[0::2]).most_common()
v_even=Counter(v[1::2]).most_common()
v_odd.append([0,0])
v_even.append([0,0])
if v_odd[0][0]!=v_even[0][0]:
    print(n-v_odd[0][1]-v_even[0][1])
else:
    print(min((n-v_odd[0][1]-v_even[1][1]),n-v_odd[1][1]-v_even[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(input().split())
X = []
Y = []
ans = 0

for i in range(0, n-1, 2):
    X.append(V[i])
    Y.append(V[i + 1])

X = Counter(X).most_common()
Y = Counter(Y).most_common()

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

Submission status: Accepted

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

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

ma, mb = {None: 0}, {None: 0}
for vi in va:
    c = ma[vi] if vi in ma else 0
    ma[vi] = c + 1
for vi in vb:
    c = mb[vi] if vi in mb else 0
    mb[vi] = c + 1

# items() is [(k, m[k]) for k in m.keys()]
pa = list(ma.items())
pb = list(mb.items())
pa.sort(key=lambda x: x[1], reverse=True)
pb.sort(key=lambda x: x[1], reverse=True)

if pa[0][0] == pb[0][0]:
    if pa[0][1] < pb[0][1]:
        ans = n - pa[1][1] - pb[0][1]
    elif pa[0][1] > pb[0][1]:
        ans = n - pa[0][1] - pb[1][1]
    else:
        h = pa[0][1]
        if pa[1][1] < pb[1][1]:
            ans = n - h - pb[1][1]
        else:
            ans = n - h - pa[1][1]
else:
    ans = n - pa[0][1] - pb[0][1]
print(ans)

Submission status: Accepted

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

v1 = v[0::2]
v2 = v[1::2]

m1, m2 = {}, {}
for vi in v1:
    if vi in m1:
        m1[vi] += 1
    else:
        m1[vi] = 1
for vi in v2:
    c = m2[vi] if vi in m2 else 0
    m2[vi] = c + 1

p1 = [(k, m1[k]) for k in m1.keys()]
p2 = [(k, m2[k]) for k in m2.keys()]

p1.sort(key=lambda x: x[1], reverse=True)
p2.sort(key=lambda x: x[1], reverse=True)

if n == 2:
    if v1[0] == v2[0]:
        ans = 1
    else:
        ans = 0
else:
    if p1[0][0] == p2[0][0]:
        if p1[0][1] < p2[0][1]:
            ans = n - p2[0][1] - p1[1][1]
        elif p1[0][1] > p2[0][1]:
            ans = n - p1[0][1] - p2[1][1]
        else:
            h = p1[0][1]
            if len(p1) == 1:
                ans = n - h
            else:
                if p1[1][1] < p2[1][1]:
                    ans = n - h - p2[1][1]
                else:
                    ans = n - h - p1[1][1]
    else:
        ans = n - p1[0][1] - p2[0][1]
print(ans)

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
n = int(input())
v = list(map(int, input().split()))
vodd = []
dodd = {}
veve = []
deve = {}

kind = 0
inst_kinds = {}
for e,i in enumerate(v):
    if e % 2 == 0:
        # veve.append(i)
        if not i in deve:
            deve[i] = 1
        else:
            deve[i] += 1
    else:
        # vodd.append(i)
        if not i in dodd:
            dodd[i] = 1
        else:
            dodd[i] += 1


for k in deve:
    veve.append((deve[k], k))
veve.append((0,0))
veve.sort()
veve.reverse()
for k in dodd:
    vodd.append((dodd[k], k))
vodd.append((0,0))
vodd.sort()
vodd.reverse()
from functools import reduce
from operator import add

# sum = reduce(add, [k[0] for k in vodd])
sum = n
# sum += reduce(add, [k[0] for k in veve])


if vodd[0][1] != veve[0][1]:
    sum -= vodd[0][0] + veve[0][0]
else:
    sum = min(n - vodd[1][0] - veve[0][0], 
             n - vodd[0][0] - veve[1][0])

print(sum)

Submission status: Accepted

Solution code:
import collections

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

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

if len(o) == 1:
    o.append([0, 0])
if len(e) == 1:
    e.append([0, 0])


if o[0][0] != e[0][0]:
    ans = n - o[0][1] - e[0][1]
else:
    ans = min(n - o[0][1] - e[1][1], n - o[1][1] - e[0][1])

print(ans)

Submission status: Accepted

Solution code:
import collections

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

O = collections.Counter(v[0::2])
E = collections.Counter(v[1::2])

def get_candidate(F):
    c = [[0, 0], [0, 0]]
    for i, j in F.items():
        if j >= c[0][1]:
            c[1] = c[0]
            c[0] = [i, j]
        elif j >= c[1][1]:
            c[1] = [i, j]
    return c

o = get_candidate(O)
e = get_candidate(E)

if o[0][0] != e[0][0]:
    ans = n - o[0][1] - e[0][1]
else:
    ans = min(n - o[0][1] - e[1][1], n - o[1][1] - e[0][1])

print(ans)

Submission status: Accepted

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

O = {}
E = {}

for i in v[0::2]:
    if i in O:
        O[i] += 1
    else:
        O[i] = 1

for i in v[1::2]:
    if i in E:
        E[i] += 1
    else:
        E[i] = 1

o_candidate = [[0, 0], [0, 0]]
e_candidate = [[0, 0], [0, 0]]

for i, j in O.items():
    if j >= o_candidate[0][1]:
        o_candidate[1] = o_candidate[0]
        o_candidate[0] = [i, j]
    elif j >= o_candidate[1][1]:
        o_candidate[1] = [i, j]

for i, j in E.items():
    if j > e_candidate[0][1]:
        e_candidate[1] = e_candidate[0]
        e_candidate[0] = [i, j]
    elif j >= o_candidate[1][1]:
        o_candidate[1] = [i, j]

if o_candidate[0][0] != e_candidate[0][0]:
    ans = n - o_candidate[0][1] - e_candidate[0][1]
else:
    ans = min(n - o_candidate[0][1] - e_candidate[1][1],
              n - o_candidate[1][1] - e_candidate[0][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

even, odd = [sorted(Counter(V[i::2]).items(), key=lambda x: x[1], reverse=True) + [[None, 0]] for i in range(2)]
# print(even, odd)
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])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
L = list(input().split())
L0 = []
L1 = []
ans = 0

for i in range(0, n, 2):
    L0.append(L[i])
    L1.append(L[i + 1])
    
C0 = Counter(L0).most_common()
C1 = Counter(L1).most_common()

if C0[0][0] != C1[0][0]:
    ans = n  - C0[0][1] - C1[0][1]
else:
    if len(C0) == 1 and len(C1) == 1:
        ans = n // 2
    elif len(C0) == 1 and len(C1) != 1:
        ans = n // 2 - C1[1][1]
    elif len(C0) != 1 and len(C1) == 1:
        ans = n // 2 - C0[1][1]
    else:
        a = n - C0[0][1] - C1[1][1]
        b = n  - C0[1][1] - C1[0][1]
        ans = min(a, b)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
d = [[0 for _ in range(10**5+9)] for _ in range(2)]
for i in range(n):
    d[i%2][v[i]] += 1
s = sum(d[0])+sum(d[1])
d1,d2 = max(d[0]),max(d[1])
i1,i2 = d[0].index(max(d[0])),d[1].index(max(d[1]))

if i1 == i2:
    d[0][i1] = 0; d[1][i2] = 0
    dd1 = max(d[0]); dd2 = max(d[1])
    print(min(s-d1-dd2, s-dd1-d2))
else:
    print(s-d1-d2)

Submission status: Accepted

Solution code:
n = int(input())
v = input().split()
if len(set(v)) == 1:
    print(int(n/2))
    exit()
even = v[0::2]
odd = v[1::2]
deven = {i:0 for i in set(even)}
dodd = {i:0 for i in set(odd)}
for i in even:
    deven[i] += 1
for i in odd:
    dodd[i] += 1
deven = sorted(deven.items(), key = lambda x: x[1], reverse = True)
dodd = sorted(dodd.items(), key = lambda x: x[1], reverse = True)
res = 0
if deven[0][0] != dodd[0][0]:
    if deven[0][1] != int(n/2):
        res += int(n/2) - deven[0][1]
    if dodd[0][1] != int(n/2):
        res += int(n/2) - dodd[0][1]
else:
    if deven[0][1] == dodd[0][1]:
        res += n - deven[0][1] - max(deven[1][1], dodd[1][1])
    else:
        if deven[0][1] > dodd[0][1]:
            res += n - deven[0][1] - dodd[1][1]
        else:
            res += n - dodd[0][1] - deven[1][1] 
print(res)