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):

"""
Created on Sat Sep 29 16:33:51 2018
ABC111C
@author: maezawa
"""

import collections

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

v0 = [v[i] for i in range(0,n,2)]
v1 = [v[i] for i in range(1,n,2)]

v0cnt = collections.Counter(v0)
v1cnt = collections.Counter(v1)

v0most = v0cnt.most_common()[0]
v1most = v1cnt.most_common()[0]
if len(v0cnt) > 1:
    v0next = v0cnt.most_common()[1]
if len(v1cnt) > 1:
    v1next = v1cnt.most_common()[1]
if v0most[0] != v1most[0]:
    ans = n - v0most[1] -v1most[1]
else:
    if v0most[1] == v1most[1]:
        if v0most[1] == n//2:
            ans = n//2
        else:
            if v0next[1] > v1next[1]:
                ans = n - v1most[1] - v0next[1]
            else:
                ans = n - v0most[1] - v1next[1]            
    elif v0most[1] > v1most[1]:
        ans = n - v0most[1] - v1next[1]
    else:
        ans = n - v1most[1] - v0next[1]

print(ans)

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = n
b = {}
c = {}
for i in range(0,len(a)):
    if i%2 == 0:
        b[a[i]] = b.get(a[i],0) + 1
    else:
        c[a[i]] = c.get(a[i],0) + 1
b = sorted(b.items(), key=lambda x: x[1])
c = sorted(c.items(), key=lambda x: x[1])

bk,bv = b[-1]
ck,cv = c[-1]
if ck != bk:
    ans = n-cv-bv
else:
    if bv > cv:
        ans -= bv
        cv = 0
        if len(c) > 1:
            ck,cv= c[-2]
        ans -= cv
    if bv < cv:
        ans -= cv
        bv = 0
        if len(b) > 1:
            bk,bv= b[-2]
        ans -= bv
    if bv == cv:
        ans -= cv
        if len(b) > 1 and len(c) > 1:
            bk,bv = b[-2]
            ck,cv = c[-2]
            ans -= max(bv,cv)
        else:
            if len(b) > 1:
                bk,bv = b[-2]
                ans -= bv
            elif len(c) > 1:
                ck,cv = c[-2]
                ans -= cv
print(ans)

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

res = ''

ht1 = dict()
ht2 = dict()

for i in range(0, n - 1, 2):
    if v[i] not in ht1:
        ht1[v[i]] = 1
    else:
        ht1[v[i]] += 1


for j in range(1, n, 2):
    if v[j] not in ht2:
        ht2[v[j]] = 1
    else:
        ht2[v[j]] += 1

sorted1 = sorted(ht1.items(), key=lambda x: x[1])
sorted2 = sorted(ht2.items(), key=lambda x: x[1])

if (sorted1[-1][0] != sorted2[-1][0]):
    res = n - sorted1[-1][1] - sorted2[-1][1]
else:
    if len(sorted1) == 1 and len(sorted2) == 1:
        res = int(n / 2)
    elif len(sorted1) == 1:
        print('aaa')
        res = n - sorted2[-1][1]
    elif len(sorted2) == 1:
        print('bbb')
        res = n - sorted1[-1][1]
    elif sorted1[-2][1] < sorted2[-2][1]:
        res = n - sorted1[-1][1] - sorted2[-2][1]
    else:
        res = n - sorted2[-1][1] - sorted1[-2][1]

print(res)

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

if max(v)==min(v):
	print(int(n/2))
else:
	dic1 = {}
	dic2 = {}
	
	for i in range(n):
		if i % 2 == 1:
			if str(v[i]) in dic1.keys():
				dic1[str(v[i])] = dic1[str(v[i])] + 1
			else:
				dic1[str(v[i])] = 1
	
		if i % 2 == 0:
			if str(v[i]) in dic2.keys():
				dic2[str(v[i])] = dic2[str(v[i])] + 1
			else:
				dic2[str(v[i])] = 1
	
	max1 = max(dic1, key=dic1.get)
	max2 = max(dic2, key=dic2.get)
	p1 = dic1[max1]
	p2 = dic2[max2]
	if max1 == max2:
		dic1.pop(max1)
		dic2.pop(max2)
		if dic1 == {}:
			p3 = 0
		else:
			max3 = max(dic1, key=dic1.get)
			p3 = dic1[max3]
		if dic2 == {}:
			p4 = 0
		else:
			max4 = max(dic2, key=dic2.get)
			p4 = dic2[max4]
		if p1 + p4 > p2 + p3:
			p2 = p4
		else:
			p1 = p3
	
	print(n-p1-p2)

Solution (Python):
import collections

def get_max_value(dict1, dict2):
    max_dict1=max(dict1, key=dict1.get)
    max_dict2=max(dict2, key=dict2.get)
    return max_dict1, max_dict2

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

if len(collections.Counter(a))==1:
    print(int(n/2))
else:
    a_even=[a[i] for i in range(0,n,2)]
    a_odd=[a[i]for i in range(1,n,2)]

    a_even_c=collections.Counter(a_even)
    a_odd_c=collections.Counter(a_odd)

    while(True):
        max_a_even, max_a_odd=get_max_value(a_even_c, a_odd_c)
        if max_a_even!=max_a_odd:
            replace_count=n-a_even_c[max_a_even]-a_odd_c[max_a_odd]
            break
        else:
            if a_even_c[max_a_even]>a_odd_c[max_a_odd]:
                del a_odd_c[max_a_odd]
            elif a_even_c[max_a_even]==a_odd_c[max_a_odd]:
                temp=a_even_c[max_a_even]
                del a_odd_c[max_a_odd]
                del a_even_c[max_a_even]
                even_2, odd_2=get_max_value(a_even_c, a_odd_c)
                if a_even_c[even_2]>=a_odd_c[odd_2]:
                    a_odd_c[max_a_odd]=temp
                else:
                    a_even_c[max_a_even]=temp
            else:
                del a_even_c[max_a_even]

    print(replace_count)

Solution (Python):
import collections
n=int(input())
v=[int(i) for i in input().split()]
p=v
u=v[0::2]
q=p[1::2]
U= collections.Counter(u)
Q= collections.Counter(q)
a=n/2-U.most_common()[0][1]
b=n/2-Q.most_common()[0][1]
if(a==0)and(b==0)and(U.most_common()[0][0]==Q.most_common()[0][0]):
  print(int(n/2))
else:
  if(U.most_common()[0][0]==Q.most_common()[0][0]):
    if(U.most_common()[1][1]<Q.most_common()[1][1]):
      b=n/2-Q.most_common()[1][1]
    else:
      a=n/2-U.most_common()[1][1]
  print(int(a+b))

Solution (Python):
from collections import Counter
from collections import deque

if __name__ == '__main__':
    n = int(input())
    vs0 = []
    vs1 = []
    for i,v in enumerate(map(int, input().split())):
        if i % 2 == 0:
            vs0.append(v)
        else:
            vs1.append(v)
    vs0 = deque(sorted(Counter(vs0).items(), key=lambda t: t[1]))
    vs1 = deque(sorted(Counter(vs1).items(), key=lambda t: t[1]))
    t0 = vs0.pop()
    t1 = vs1.pop()
    if t0[0] != t1[0]:
        vs0 = (t[1] for t in vs0)
        vs1 = (t[1] for t in vs1)
        print(sum(vs0) + sum(vs1))
    else:
        if len(vs1) == 0:
            if len(vs0) == 0:
                print(t1[1])
            else:
                vs0 = (t[1] for t in vs0)
                print(sum(vs0))
        elif len(vs0) == 0:
            vs1 = (t[1] for t in vs1)
            print(sum(vs1))
        else:
            t00 = vs0.pop()
            t11 = vs1.pop()
            a = t00[1] + t1[1]
            b = t11[1] + t0[1]
            vs0 = (t[1] for t in vs0)
            vs1 = (t[1] for t in vs1)
            print(sum(vs0) + sum(vs1) + (a if a < b else b))

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

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

a = {}
b = {}
for i in range(n):
	if i % 2 == 1:
		if v[i] in a:
			a[v[i]] += 1
		else:
			a[v[i]] = 1
	else:
		if v[i] in b:
			b[v[i]] += 1
		else:
			b[v[i]] = 1
a[0] = 0
b[0] = 0
a = sorted(a.items(), key=lambda x:x[1])
b = sorted(b.items(), key=lambda x:x[1])
a.reverse()
b.reverse()
c = a[0]
d = b[0]
e = a[1]
f = b[1]
if c[0] == d[0]:
    g = max(c[1]+f[1],d[1]+e[1])
    print(n-g)
else:
    print(n-c[1]-d[1])

Solution (Python):
from collections import Counter
n = int(input())
vn = list(map(int,input().split()))
v0 = [0]*(n//2)
v1 = [0]*(n//2)
for i in range(n//2):
  v0[i] = vn[i*2+0]
  v1[i] = vn[i*2+1]
v0 = Counter(v0)
v1 = Counter(v1)
v0c0 = v0.most_common()[0]
v1c0 = v1.most_common()[0]
v0c1 = [0,0] if (len(v0.most_common()) == 1) else v0.most_common()[1]
v1c1 = [0,0] if (len(v1.most_common()) == 1) else v1.most_common()[1]

if v0c0[0] != v1c0[0]:
  print(n-v0c0[1]-v1c0[1])
elif v0c0[1] < v1c0[1]:
  print(n-v0c1[1]-v1c0[1])
elif v0c0[1] > v1c0[1]:
  print(n-v0c0[1]-v1c1[1])
elif v0c1[1] < v1c1[1]:
  print(n-v0c0[1]-v1c1[1])
elif v0c1[1] > v1c1[1]:
  print(n-v0c1[1]-v1c0[1])
else:
  print(n-v0c0[1]-v1c1[1])

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
d1={}
d2={}
r=len(a)
sum=0
maxs=0
maxs2=0
d1[maxs]=d2[maxs2]=0
for i in range(r):
    if i%2:
        if a[i] in d1:
            d1[a[i]]+=1
            if d1[a[i]]>d1[maxs]:
                maxs=a[i]
        else:
            d1[a[i]]=1
            sum+=1
            if d1[a[i]]>d1[maxs]:
                maxs=a[i]
    else:
        if a[i] in d2:
            d2[a[i]]+=1
            if d2[a[i]]>d2[maxs2]:
                maxs2=a[i]
        else:
            d2[a[i]]=1
            sum+=1
            if d2[a[i]]>d2[maxs2]:
                maxs2=a[i]
if maxs!=maxs2:
    print(n-d1[maxs]-d2[maxs2])
else:
    mmmm=0
    for i in d2:
        if i!=maxs2 and d2[i]>mmmm:
            mmmm=d2[i]
    mmm=0
    for i in d1:
        if i!=maxs and d1[i]>mmm:
            mmm=d1[i]
    print(min(n - d1[maxs] - mmmm,n-mmm-d2[maxs2]))

Solution (Python):
import collections
def solve():
    _ = input()
    v = list(map(int, input().split()))
    if v.count(v[0]) == len(v):
        print(len(v) // 2)
        return
    even = v[0::2]
    odd = v[1::2]
    even_cnt = list(sorted(collections.Counter(even).items(), key=lambda x: x[1], reverse=True))
    odd_cnt = list(sorted(collections.Counter(odd).items(), key=lambda x: x[1], reverse=True))
    ev = even_cnt[0][0]
    od = odd_cnt[0][0]
    if even_cnt[0] == odd_cnt[0]:
        if len(even_cnt) == 1:
            ev = even_cnt[1][0]
        elif len(odd_cnt) == 1:
            od = odd_cnt[1][0]
        elif even_cnt[1][1] > odd_cnt[1][1]:
            ev = even_cnt[1][0]
        else:
            od = odd_cnt[1][0]
    print(len(even) - even.count(ev) + len(odd) - odd.count(od))

if __name__ == '__main__':
    solve()

Solution (Python):
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=a[0::2]
c=a[1::2]
x=Counter(b)
y=Counter(c)
x1=list(x.most_common(1)[0])
y1=list(y.most_common(1)[0])

if x1[0]==y1[0]:
  if x1[1]==len(b):
    if y1[1]==len(c):
      print(len(c))
    else:
      y2=list(y.most_common(2)[1])
      print(len(c)-y2[1])
  elif y1[1]==len(c):
    x2=list(x.most_common(2)[1])
    print(len(b)-x2[1])
  
  else:
    x2=list(x.most_common(2)[1])
    y2=list(y.most_common(2)[1])   
    if x1[1]+y2[1]>=x2[1]+y1[1]:
      print(len(a)-x1[1]-y2[1])
    else:
      print(len(a)-y1[1]-x2[1])
  
else:
  xc=len(b)-x1[1]
  yc=len(c)-y1[1]
  print(xc+yc)

Solution (Python):

import numpy as np
import collections
N = 1000000
_ = int(input())
ls = list(map(int,input().split()))
ls.append(N)
ls.append(N)
array = np.array(ls).reshape(-1,2).T
c_even = collections.Counter(array[0])
c_odd = collections.Counter(array[1])
if len(set(ls))==2:
    print (int((len(ls)-2)/2))
elif c_even.most_common()[0][0] != c_odd.most_common()[0][0]:
    print (len(ls)-c_even.most_common()[0][1]-c_odd.most_common()[0][1]-2)
else:
    print (min(len(ls)-c_even.most_common()[0][1]-c_odd.most_common()[1][1]-2,len(ls)-c_even.most_common()[1][1]-c_odd.most_common()[0][1]-2))

Solution (Python):
import sys
from collections import OrderedDict
n = int(input())
v = list(map(int, input().split()))
if len(list(set(v))) == 1:
    print(n // 2)
    sys.exit()
e = {}
for i in v[::2]:
    if i in e:
        e[i] += 1
    else:
        e[i] = 1
o = {}
for i in v[1::2]:
    if i in o:
        o[i] += 1
    else:
        o[i] = 1
e = OrderedDict(sorted(e.items(), key = lambda x:-x[1]))
o = OrderedDict(sorted(o.items(), key = lambda x:-x[1]))
ans = 0
if not list(e.keys())[0] == list(o.keys())[0]:
    ans += (n // 2 - max(list(e.values())))
    ans += (n // 2 - max(list(o.values())))
else:
    ans1 = 0
    ans2 = 0
    if not len(list(set(list(o.keys())))) == 1:
        ans1 += (n // 2 - list(e.values())[0])
        ans1 += (n // 2 - list(o.values())[1])
    else:
        ans1 = 10 ** 10
    if not len(list(set(list(e.keys())))) == 1:
        ans2 += (n // 2 - list(o.values())[0])
        ans2 += (n // 2 - list(e.values())[1])
    else:
        ans2 = 10 ** 10
    ans = min(ans1, ans2)
print(ans)

Solution (Python):
import collections

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

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

A = collections.Counter(a).most_common()
B = collections.Counter(b).most_common()

sa, na = A[0]
sb, nb = B[0]
if sa == sb:
    if na == n // 2 and nb == n // 2:
        print(na)
    else:
        if len(A) == 1:
            _, nb = B[1]
            print(n - na - nb)
        elif len(B) == 1:
            _, na = A[1]
            print(n - na - nb)
        else:
            _, nna = A[1]
            _, nnb = B[1]
            if na + nnb < nb + nna:
                print(n - nb - nna)
            else:
                print(n - na - nnb)
else:
    print(n - na - nb)

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


if len(set(list(A))) == 1:
    print(n // 2)
    sys.exit(0)
O = A[0::2]
E = A[1::2]
mco = Counter(O).most_common()
mce = Counter(E).most_common()

l = len(O) 
io = 0
io_max = len(mco) - 1
ie = 0
ie_max = len(mce) - 1
while(mco[io][0] == mce[ie][0]):
    if io_max <= io + 1:
        ie += 1
        continue
    if ie_max <= ie + 1:
        io += 1
        continue
    v1 = mco[io][1] + mce[ie+1][1]
    v2 = mco[io+1][1] + mce[ie][1]
    if v1 >= v2:
        ie += 1
    else:
        io += 1

print(l*2-mco[io][1]-mce[ie][1])

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

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

v1 = [v for i,v in enumerate(vl) if i%2 ==0]
v2 = [v for i,v in enumerate(vl) if i%2 ==1]

table1 = {}
table2 = {}

for x in v1:
    table1.setdefault(x,0)
    table1[x] += 1

for x in v2:
    table2.setdefault(x,0)
    table2[x] += 1

v1 = [(k,table1[k]) for k in table1]
v2 = [(k,table2[k]) for k in table2]

v1 = sorted(v1,key= lambda v: v[1],reverse=True)
v2 = sorted(v2,key= lambda v: v[1],reverse=True)

size= int(n/2)
ans=0
if v1[0][0] != v2[0][0]:
    ans += size - v1[0][1]
    ans += size - v2[0][1]
else:
    if len(v1) == 1 and len(v2) == 1:
        print(size)
        exit()
    elif len(v1) == 1 and len(v2) != 1:
        print(size - v2[1][1])
        exit()
    elif len(v1) != 1 and len(v2) == 1:
        print(size - v1[1][1])
        exit()   


    if v1[1][1] == v1[0][1] or v2[1][1] == v2[0][1]:
        ans += size - v1[0][1]
        ans += size - v2[0][1]
    elif v1[1][1] < v2[1][1]:
        ans += size - v1[0][1]
        ans += size - v2[1][1]
    else:
        ans += size - v1[1][1]
        ans += size - v2[0][1]

print(ans)

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

d = defaultdict(int)
rd = defaultdict(int)
d[0] = 0
rd[0] = 0

ll = (n) // 2
rr = (n) // 2

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

d = list(reversed(sorted(d.items(), key=lambda x: x[1])))
rd= list(reversed(sorted(rd.items(), key=lambda x: x[1])))

i, j = 0, 0

while d[i][0] == rd[j][0] and i < len(d)-1 and j < len(rd)-1:
    if (ll - d[i][1] + rr - rd[j+1][1] < ll - d[i+1][1] + rr - rd[j][1]):
        j += 1
    else:
        i += 1


print(ll - d[i][1] + rr - rd[j][1])

Solution (Python):
import collections

def main():
    n = input()
    a = map(int, raw_input().split())
    odd = [a[2 * i] for i in range(n/2)]
    even = [a[2*i + 1] for i in range(n/2)]
    codd = collections.Counter(odd)
    ceven = collections.Counter(even)
    coddmostitem = codd.most_common()[0][0]
    coddmosttimes = codd.most_common()[0][1]
    cevenmostitem = ceven.most_common()[0][0]
    cevenmosttimes = ceven.most_common()[0][1]
    if coddmostitem == cevenmostitem:
        if coddmosttimes > cevenmosttimes:
            if len(ceven) == 1:
                cevennoreplace = 0
            else:
                cevennoreplace = ceven.most_common()[1][1]
            coddnoreplace = codd.most_common()[0][1]
        if coddmosttimes < cevenmosttimes:
            if len(codd) == 1:
                coddnoreplace = 0
            else:
                coddnoreplace = codd.most_common()[1][1]
            cevennoreplace = ceven.most_common()[0][1]
        else:
            if len(ceven) == 1:
                cevennoreplace = 0
                coddnoreplace = coddmosttimes
            elif len(codd) == 1:
                cevennoreplace = cevenmosttimes
                coddnoreplace = 0
            elif codd.most_common()[1][1] >= ceven.most_common()[1][1]:
                cevennoreplace = ceven.most_common()[0][1]
                coddnoreplace = codd.most_common()[1][1]
            elif codd.most_common()[1][1] < ceven.most_common()[1][1]:
                cevennoreplace = ceven.most_common()[1][1]
                coddnoreplace = codd.most_common()[0][1]
    else:
        coddnoreplace = coddmosttimes
        cevennoreplace = cevenmosttimes
    coddreplace = n/2 - coddnoreplace
    cevenreplace = n/2 - cevennoreplace
    print(coddreplace+cevenreplace)

if __name__ == '__main__':
    main()

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

d = [{-1: 0},{-1: 0}]
for i in range(n):
    d[i%2][v[i]] = d[i%2].get(v[i],0)+1

odd_max = max(d[0], key=d[0].get)
even_max = max(d[1],key=d[1].get)

if odd_max != even_max:
    ans = n - d[0][odd_max] - d[1][even_max]
else:
    odd_max_num = d[0].pop(odd_max)
    odd_second_num = max(d[0].values())
    even_max_num = d[1].pop(even_max)
    even_second_num = max(d[1].values())
    ans = n - max(odd_max_num+even_second_num,odd_second_num+even_max_num)

print(ans)

Solution (Python):
import itertools

n = int(input())
v = [int(w) for w in input().split()]
assert len(v) == n

def elem_count(ls):
    '''Return the list of (num of elem, elem).'''
    ls.sort()
    ls = itertools.groupby(ls)
    cs = [(len(list(s)), e) for e, s in ls]
    cs.sort(reverse=True)
    return cs

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

e = elem_count(e)
o = elem_count(o)

if e[0][1] != o[0][1]:
    ans = n - (e[0][0] + o[0][0])

elif len(e) == len(o) == 1:
    ans = n - e[0][0]

elif len(e) == 1:
    ans = n - (e[0][0] + o[1][0])

elif len(o) == 1:
    ans = n - (e[1][0] + o[0][0])

elif e[0][0] + o[1][0] > e[1][0] + o[0][0]:
    ans = n - (e[0][0] + o[1][0])
else:
    ans = n - (e[1][0] + o[0][0])

print(ans)

Solution (Python):
import numpy as np

def count(V, V_dic):
    for v in V:
        if v in V_dic.keys():
            V_dic[v] += 1
        else:
            V_dic[v] = 1
    return

N = int(input())
V = [int(i) for i in input().split(' ')]
V1 = []
V2 = []
for i in range(N//2):
    V1.append(V[i*2])
    V2.append(V[i*2+1])
V1_dic = {1000000:0}
V2_dic = {1000000:0}
count(V1, V1_dic)
count(V2, V2_dic)
sort_V1 = sorted(V1_dic.items(), key=lambda V: V[1], reverse=True)
sort_V2 = sorted(V2_dic.items(), key=lambda V: V[1], reverse=True)

if sort_V1[0][0] == sort_V2[0][0]:
    im1 = sort_V1[0][1] + sort_V2[1][1]
    im2 = sort_V1[1][1] + sort_V2[0][1]
    if im1 >= im2:
        print(N - im1)
    else:
        print(N - im2)
else:
    print(N - (sort_V1[0][1] + sort_V2[0][1]))

Solution (Python):
import collections

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

v_odd = []
for i in range(0, n, 2):
    v_odd.append(v[i])
v_even = []
for i in range(1, n, 2):
    v_even.append(v[i])

odd_dic = collections.Counter(v_odd)
odd_sort = sorted(odd_dic.items(), key=lambda x: -x[1])
odd_maxkey = odd_sort[0][0]
odd_cnt1 = odd_sort[0][1]
if len(odd_sort) > 1:
    odd_cnt2 = odd_sort[1][1]
else:
    odd_cnt2 = 0

even_dic = collections.Counter(v_even)
even_sort = sorted(even_dic.items(), key=lambda x: -x[1])
even_maxkey = even_sort[0][0]
even_cnt1 = even_sort[0][1]
if len(even_sort) > 1:
    even_cnt2 = even_sort[1][1]
else:
    even_cnt2 = 0

ans = len(v_odd) + len(v_even)
if odd_maxkey == even_maxkey:
    ans -= max(odd_cnt1 + even_cnt2, odd_cnt2 + even_cnt1)
else:
    ans -= odd_cnt1 + even_cnt1







print(ans)

Solution (Python):
iN = int(input())
iM = iN // 2
aV = [int(x) for x in input().split()]

aV0={}
aV1={}
for i in range(iN):
    iA = aV[i]
    if i % 2 == 0:
        if iA in aV0:
            aV0[iA] += 1
        else:
            aV0[iA] = 1
    else:
        if iA in aV1:
            aV1[iA] += 1
        else:
            aV1[iA] = 1
aV0[0]=0
aV1[0]=0

aV0dec = sorted(aV0,key=aV0.get,reverse=True)
aV1dec = sorted(aV1,key=aV1.get,reverse=True)

iMax1aV0 = aV0dec[0]
iMax1aV1 = aV1dec[0]

if iMax1aV0 != iMax1aV1:
    print(iN - aV0[iMax1aV0] - aV1[iMax1aV1])
else:
    iMax1aV0sum =aV0[iMax1aV0]
    iMax1aV1sum =aV0[iMax1aV1]
    
    
    
    
    
    print(iN - max(iMax1aV0sum,iMax1aV1sum) - max(aV0[aV0dec[1]],aV1[aV1dec[1]]))

Solution (Python):
import collections

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

a = []
b = []
a_f = 0
b_f = 0
a_s = 0
b_s = 0
for i in range(N):
    if i%2 == 0:
        a.append(l[i])
    else:
        b.append(l[i])
        
c_a = collections.Counter(a)
c_b = collections.Counter(b)

a_f = c_a.most_common()[0]
b_f = c_b.most_common()[0]

if a_f[0] != b_f[0]:
    print(N - a_f[1] - b_f[1])

elif len(set(a)) == 1 and len(set(b)) == 1:
    print(int(N/2))
elif len(set(a)) == 1:
    print(int(N/2 - c_b.most_common()[1][1]))
elif len(set(b)) == 1:
    print(int(N/2 - c_a.most_common()[1][1]))
elif a_f[1] == b_f[1]:
    second_a = c_a.most_common()[1][1]
    second_b = c_b.most_common()[1][1]
    if second_a >= second_b:
        print(int(N - b_f[1] - second_a))
    else:
        print(int(N - a_f[1] - second_b))
elif a_f[1] > b_f[1]:
    print(int(N - a_f[1] - c_b.most_common()[1][1]))
else:
    print(int(N - b_f[1] - c_a.most_common()[1][1]))

Solution (Python):
from collections import defaultdict

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

def f(v):
    d = defaultdict(int)
    d[0] = 0

    for i in v:
        d[i] += 1
    return sorted(d.items(), key=lambda x: x[1], reverse=True)

t1 = f(v[::2])
t2 = f(v[1::2])

if t1[0][0] == t2[0][0]:
    if t1[0][1] + t2[1][1] > t1[1][1] + t2[0][1]:
        print(n - t1[0][1] - t2[1][1])
    else:
        print(n - t1[1][1] - t2[0][1])
else:
    print(n - t1[0][1] - t2[0][1])

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

M0=[]
M1=[]

for i in range(n):
	if i%2==0: M0.append(A[i])
	else: M1.append(A[i])

C0=Counter(M0)
C1=Counter(M1)

L0=sorted(C0.items(), key=lambda x:x[1], reverse=True)
L1=sorted(C1.items(), key=lambda x:x[1], reverse=True)

a0=None
a1=None

if L0[0][0]!=L1[0][0]:
	a0=L0[0][0]
	a1=L1[0][0]
	b0=L0[0][0]
	b1=L1[0][0]
elif L0[0][0]==L1[0][0]:
	a0=L0[0][0]
	if len(L1)==1:
		a1=-1
	else:
		a1=L1[1][0]

	b1=L1[0][0]
	if len(L0)==1:
		b0=-1
	else:
		b0=L0[1][0]

ans0=0
ans1=0
for x in M0:
	if x!=a0:
		ans0+=1

for x in M1:
	if x!=a1:
		ans0+=1

ans1=0
for x in M0:
	if x!=b0:
		ans1+=1

for x in M1:
	if x!=b1:
		ans1+=1

print min(ans0, ans1)

Solution (Python):

import collections

n = int(input())
v = [int(s) for s in input().strip().split(" ")]
el = [0] * (n // 2)
ol = [0] * (n // 2)
ans = 0

for i in range(n):
    if i % 2 == 0:
        el[i//2] = v[i]
    else:
        ol[(i-1)//2] = v[i]
        
ec = collections.Counter(el)
oc = collections.Counter(ol)
e_num = ec.most_common()[0][0]
e_count = ec.most_common()[0][1]
o_num = oc.most_common()[0][0]
o_count = oc.most_common()[0][1]

if len(set(v)) == 1:
    ans = n //2
elif e_num != o_num:
    ans = n - e_count - o_count
elif e_count > o_count:
    
    o_count = oc.most_common()[1][1]
    ans = n - e_count - o_count
elif e_count < o_count:
    
    e_count = ec.most_common()[1][1]
    ans = n - e_count - o_count
elif e_count == o_count:
    if ec.most_common()[1][1] >= oc.most_common()[1][1]:
        e_count = ec.most_common()[1][1]
    else:
        o_count = oc.most_common()[1][1]
    ans = n - e_count - o_count

print(ans)

Solution (Python):


input()
numbers = tuple(map(int, input().split()))

evenNumberToCount = dict()

oddNumberToCount = dict()

for i, n in enumerate(numbers):
    if i % 2 == 0:
        evenNumberToCount[n] = evenNumberToCount.get(n, 0) + 1
    else:
        oddNumberToCount[n] = oddNumberToCount.get(n, 0) + 1

evenCountToNumber = list()
for n, i in evenNumberToCount.items():
    evenCountToNumber.append((i, n))
evenCountToNumber.sort(reverse=True)

oddCountToNumber = list()
for n, i in oddNumberToCount.items():
    oddCountToNumber.append((i, n))
oddCountToNumber.sort(reverse=True)

if oddCountToNumber[0][1] == evenCountToNumber[0][1]:
    if len(numbers) // 2 == oddCountToNumber[0][0] == evenCountToNumber[0][0]:
        print(len(numbers) // 2)
    elif oddCountToNumber[0][0] < evenCountToNumber[0][0]:
        print(len(numbers) - oddCountToNumber[1][0] - evenCountToNumber[0][0])
    elif oddCountToNumber[0][0] > evenCountToNumber[0][0]:
        print(len(numbers) - oddCountToNumber[0][0] - evenCountToNumber[1][0])
    else:
        first = len(numbers) - oddCountToNumber[0][0] - evenCountToNumber[1][0]
        second = len(numbers) - oddCountToNumber[1][0] - evenCountToNumber[0][0]
        print(min(first, second))
else:
    print(len(numbers) - oddCountToNumber[0][0] - evenCountToNumber[0][0])

Solution (Python):
import sys
Input = sys.stdin.readline

n = int(Input())
v = list(map(int, Input().split()))
mi = float("inf")
count1 = 0
count2 = 0

v1 = v[0::2]
v1_count = {}
for i in v1:
    if i not in v1_count:
        v1_count[i] = 1
    else:
        v1_count[i] += 1

v1_count = sorted(v1_count.items(), key=lambda i: i[1], reverse=True)
count1 += len(v1) - v1_count[0][1]

v2 = v[1::2]
v2_count = {}
for i in v2:
    if i not in v2_count:
        v2_count[i] = 1
    else:
        v2_count[i] += 1

v2_count = sorted(v2_count.items(), key=lambda i: i[1], reverse=True)
count2 += len(v2) - v2_count[0][1]

if v1_count[0][0] != v2_count[0][0]:
    mi = count1 + count2
else:
    lv2 = len(v2)
    for k, i in v2_count[1:]:
        result = count1 + lv2 - i
        if mi > result:
            mi = result
    else:
        if mi > count1 + lv2:
            mi = count1 + lv2

    lv1 = len(v1)
    for k, i in v1_count[1:]:
        result = count2 + lv1 - i
        if mi > result:
            mi = result
    else:
        if mi > count2 + lv1:
            mi = count2 + lv1

print(mi)

Solution (Python):
import collections

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


gu = v[0::2]
ki = v[1::2]


gc = collections.Counter(gu)
kc = collections.Counter(ki)

guc = gc.most_common()
kic = kc.most_common()

gv = gc.values()
kv = kc.values()

if guc[0][0] != kic[0][0]:
    gcount = sum(gv) - max(gv)
    kcount = sum(kv) - max(kv)
    print(gcount+kcount)
else:
    if len(guc) == 1 and len(kic) == 1 :
        print(str(guc[0][1]))
    else:
        if len(guc) == 1 and len(kic) != 1 :
            print(str(sum(gv)+sum(kv)-max(gv)-kic[1][1]))
        elif len(guc) != 1 and len(kic) == 1 :
            print(str(sum(gv)+sum(kv)-max(kv)-guc[1][1]))
        else:
            print(str(sum(gv)+sum(kv)-max((guc[1][1] + kic[0][1]), (guc[0][1] + kic[1][1]))))

Solution (Python):
n = int(input())
v = list(map(int, input().split()))
ev = [0]*(max(v)+1)
od = [0]*(max(v)+1)
for i in range(n):
    if i % 2 == 0:
        ev[v[i]-1] += 1
    else:
        od[v[i] - 1] += 1
e = sorted(ev)
o = sorted(od)
if ev.index(e[-1]) == od.index(o[-1]) and e[-2] != e[-1] and o[-2] != o[-1]:
    print(n-max(e[-1]+o[-2], e[-2]+o[-1]))
else:
    print(n-e[-1]-o[-1])

Solution (Python):
import math
n = int(input())
lis = list(map(int,input().split()))
lisa = lis[0::2]
lisb = lis[1::2]
  
cou = [0 for i in range(10 ** 5+1)]
co = [0 for i in range(10 ** 5+1)]
for i in range(len(lisa)):
  cou[lisa[i]] += 1
for i in range(len(lisb)):
  co[lisb[i]] += 1
if cou.index(max(cou)) != co.index(max(co)):
  print(n -(max(cou) + max(co)))
else:
  num = max(cou)
  cou[cou.index(max(cou))] = 0
  co[co.index(max(co))] = 0
  ans = cou + co
  print(n -(num + max(ans)))

Solution (Python):
from collections import Counter

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

count_odd = Counter(v[1::2]).most_common()
count_even = Counter(v[0::2]).most_common()


if count_odd[0][0] != count_even[0][0]:
    ans = n - count_odd[0][1] - count_even[0][1]
else:
    if len(count_odd) > 1 and len(count_even) > 1:
        if count_odd[1][1] >= count_even[1][1]:
            ans = n - count_odd[1][1] - count_even[0][1]
        else:
            ans = n - count_odd[0][1] - count_even[1][1]
    elif len(count_odd) == 1 and len(count_even) == 1:
        ans = n/2
    else:
        if len(count_odd) == 1:
            ans = n - count_odd[0][1] - count_even[1][1]
        elif len(count_even) == 1:
            ans = n - count_odd[1][1] - count_even[0][1]

print(int(ans))