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 = input().split()
v1 = []
v2 = []

for k in range(n):
    if k%2 == 0:
        v1.append(v[k])
    else:
        v2.append(v[k])

v1Counter = Counter(v1)
v2Counter = Counter(v2)
a1, b1 = zip(*v1Counter.most_common(2))
a2, b2 = zip(*v2Counter.most_common(2))

if a1[0] != a2[0]:
    print(n-b1[0]-b2[0])

else:
    if b1[0]==n/2 and b2[0]==n/2:
        print(int(n/2))
    elif b1[0]==n/2:
        print(n-b1[0]-b2[1])
    elif b2[0]==n/2:
        print(n-b2[0]-b1[1])
    else:
        c = max(b1[0]+b2[1],b1[1]+b2[0])
        print(n-c)

Submission status: Accepted

Solution code:
import collections
from operator import itemgetter

N = int(input())

up = []
down = []
for i, v in enumerate(list(map(int, input().split()))):
    if i % 2 == 0:
        up.append(v)
    else:
        down.append(v)

ud = collections.Counter(up)
dd = collections.Counter(down)

ul = []
for u, c in ud.items():
    ul.append([u, c])
dl = []
for d, c in dd.items():
    dl.append([d, c])

ul.sort(key=itemgetter(1), reverse=True)
dl.sort(key=itemgetter(1), reverse=True)

if ul[0][0] != dl[0][0]:
    print(N - ul[0][1] - dl[0][1])
elif len(ul) == 1 and len(dl) > 1:
    print(N - ul[0][1] - dl[1][1])
elif len(ul) > 1 and len(dl) == 1:
    print(N - ul[1][1] - dl[0][1])
elif len(ul) == 1 and len(dl) == 1:
    print(N//2)
else:
    print(N - max(ul[0][1]+dl[1][1], ul[1][1]+dl[0][1]))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ev=[0]*100010
od=[0]*100010

for i in range(n):
    if i%2==0:
        ev[int(A[i])]+=1
    else:
        od[int(A[i])]+=1

if ev.index(max(ev))==od.index(max(od)):
    ev.sort(reverse=True)
    od.sort(reverse=True)
    if ev[0]!=ev[1] and od[0]!=od[1]:
        if ev[1]>=od[1]:
            print(n//2-ev[1]+n//2-od[0])
            exit(0)
        else:
            print(n // 2 - ev[0] + n // 2 - od[1])
            exit(0)

ev.sort(reverse=True)
od.sort(reverse=True)
#print(ev)
#print(od)

print(n//2-ev[0]+n//2-od[0])

Submission status: Accepted

Solution code:
from collections import Counter

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

ac = Counter(ls[::2]).most_common(2)
bd = Counter(ls[1::2]).most_common(2)

if ac[0][0] != bd[0][0]:
    print(n-ac[0][1]-bd[0][1])
else:
    if len(ac) == 1 or len(bd) == 1:
        print(n//2)
    else:
        print(n-max(ac[0][1] + bd[1][1], ac[1][1] + bd[0][1]))

Submission status: Accepted

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

def collect(a):
  adic = {}
  for ai in a:
    if ai in adic.keys():
      adic[ai] += 1
    else:
      adic[ai] = 1
  adic = sorted(adic.items(), key=lambda x:x[1])[::-1]
  adic.append((0, 0))
  return adic

od = collect(an[::2])
ev = collect(an[1::2])

if od[0][0] == ev[0][0]:
  maxi = max(od[0][1] + ev[1][1], od[1][1] + ev[0][1])
else:
  maxi = od[0][1] + ev[0][1]
print(str(n-maxi))

Submission status: Accepted

Solution code:
n1 = [[j, 0] for j in range(100005)]
n2 = [[j, 0] for j in range(100005)]
n = int(input())
v = list(map(int, input().split()))
for i in range(n):
    if(i % 2 == 0):
        n1[v[i]][1] += 1
    else:
        n2[v[i]][1] += 1
n1 = sorted(n1, key=lambda x:x[1], reverse=True)
n2 = sorted(n2, key=lambda x:x[1], reverse=True)

if(n1[0][0] == n2[0][0]):
    ans1 = n//2 - n1[0][1] + n//2 - n2[1][1]
    ans2 = n//2 - n1[1][1] + n//2 - n2[0][1]
    ans = min(ans1, ans2)
    print(ans)
else:
    ans = n//2 - n1[0][1] + n//2 - n2[0][1]
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC111 C

from operator import itemgetter
import collections

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

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

sc = collections.Counter(s)
tc = collections.Counter(t)
sc = list(sc.items())
tc = list(tc.items())
ls = len(sc)
lt = len(tc)
sc.sort(key = itemgetter(1))
tc.sort(key = itemgetter(1))
if sc[-1][0] == tc[-1][0]:
    if ls == 1 and lt == 1:
        print(n - sc[-1][1])
    elif ls == 1 and lt != 1:
        print(n - sc[-1][1] - tc[-2][1])
    elif ls != 1 and lt == 1:
        print(n - sc[-2][1] - tc[-1][1])
    else:
        ma = max(sc[-2][1],tc[-2][1])
        print(n - sc[-1][1] - ma)
else:
    print(n - sc[-1][1] - tc[-1][1])

Submission status: Accepted

Solution code:
import collections

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

o=[]
e=[]
for i in range(n):
    if i%2==0:
        e.append(v[i])
    else:
        o.append(v[i])
count_o=collections.Counter(o)
count_e=collections.Counter(e)
count_o=list(count_o.items())
count_e=list(count_e.items())
count_o=sorted(count_o,key=lambda x:x[1])
count_e=sorted(count_e,key=lambda x:x[1])
if count_o[-1][0]==count_e[-1][0]:
    if len(count_e)==1:
        addnum_e=0
    else:
        addnum_e=count_e[-2][1]
    if len(count_o)==1:
        addnum_o=0
    else:
        addnum_o=count_o[-2][1]
    oe=count_o[-1][1]+addnum_e
    eo=addnum_o+count_e[-1][1]
    total=max(oe,eo)
else:
    total=count_o[-1][1]+count_e[-1][1]
print(n-total)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

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

evens = Counter(v[::2])
odds = Counter(v[1::2])

evens = list(sorted(evens.items(), key=itemgetter(1), reverse=True))
odds = list(sorted(odds.items(), key=itemgetter(1), reverse=True))
# 出現数降順ソート

ans = n
for even_num, even_cnt in evens[:2] + [(-1, 0)]:
    for odd_num, odd_cnt in odds[:2] + [(-1, 0)]:
        if even_num == odd_num: continue
        ans = min(ans, n - even_cnt - odd_cnt)
# evensとoddsの書き換え先候補として、出現率上位2数を調べる
# 同じ数は使用できない
# 1111のようにevensとoddsがともに同じ数字しかない場合に備え、
# vの要素に含まれない値-1が0個あると考える(片方の-1をすべて書き換えると考える）

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = {}
for i in range(a):
    if str(b[i]) in c:
        c[str(b[i])] += 1
    else:
        c[str(b[i])] = 1
c = sorted(c.items(), key=lambda x: x[1], reverse=True)
if len(c) <= 1:
    print(a // 2)
elif len(c) == 2:
    e = [int(c[0][0]), int(c[1][0])] + b
    f = [int(c[1][0]), int(c[0][0])] + b
    ec = 0
    fc = 0
    for i in range(2, len(e)):
        if e[i] != e[i-2]:
            e[i] = e[i-2]
            ec += 1
        if f[i] != f[i-2]:
            f[i] = f[i-2]
            fc += 1
    print(min(ec, fc))
elif len(c) >= 3:
    e = [int(c[0][0]), int(c[1][0])] + b
    f = [int(c[1][0]), int(c[0][0])] + b
    g = [int(c[0][0]), int(c[2][0])] + b
    h = [int(c[1][0]), int(c[2][0])] + b
    j = [int(c[2][0]), int(c[0][0])] + b
    k = [int(c[2][0]), int(c[1][0])] + b

    ec = 0
    fc = 0
    gc = 0
    hc = 0
    jc = 0
    kc = 0
    for i in range(2, len(e)):
        if e[i] != e[i-2]:
            e[i] = e[i-2]
            ec += 1
        if f[i] != f[i-2]:
            f[i] = f[i-2]
            fc += 1
        if g[i] != g[i-2]:
            g[i] = g[i-2]
            gc += 1
        if h[i] != h[i-2]:
            h[i] = h[i-2]
            hc += 1
        if j[i] != j[i-2]:
            j[i] = j[i-2]
            jc += 1
        if k[i] != k[i-2]:
            k[i] = k[i-2]
            kc += 1
    print(min(ec, fc, gc, hc, jc, kc))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

evens = defaultdict(lambda: 0)
odds = defaultdict(lambda: 0)
for i in range(0, n, 2):
    evens[numbers[i]] += 1
    odds[numbers[i + 1]] += 1

evens = list(evens.items())
odds = list(odds.items())

evens.sort(key=lambda pair: pair[1], reverse=True)
odds.sort(key=lambda pair: pair[1], reverse=True)

if len(evens) == 1 and len(odds) == 1:
    if evens[0][0] == odds[0][0]:
        print(n // 2)
    else:
        print(0)
    exit()

if len(evens) == 1 or len(odds) == 1:
    pairs = evens if len(evens) > len(odds) else odds
    if evens[0][0] == odds[0][0]:
        del pairs[1]
    else:
        del pairs[0]
    print(sum([v for _, v in pairs]))
    exit()

if evens[0][0] == odds[0][0]:
    if evens[0][1] == odds[0][1]:
        if evens[1][1] > odds[1][1]:
            del evens[1]
            del odds[0]
        else:
            del evens[0]
            del odds[1]
    elif evens[0][1] > odds[0][1]:
        del evens[0]
        del odds[1]
    else:
        del evens[1]
        del odds[0]
else:
    del evens[0]
    del odds[0]
print(sum([v for _, v in evens]) + sum([v for _, v in odds]))

Submission status: Accepted

Solution code:
import collections
N=int(input())
L=list(map(int,input().split()))
E=[i for i in L[::2]]
O=[i for i in L[1::2]]
e = collections.Counter(E).most_common()
o = collections.Counter(O).most_common()
if e[0][0] != o[0][0]:
    print(N - e[0][1] - o[0][1])
else:
    if len(e) == 1 or len(o) == 1:
        print(N // 2)
    else:
        a = N - e[1][1] - o[0][1]
        b = N - e[0][1] - o[1][1]
        print(min(a, b))

Submission status: Accepted

Solution code:
import collections

n = int(input())
value = input().split()

odd = [int(value[i]) for i in range(n) if i % 2]
even = [int(value[i]) for i in range(n) if not i % 2]

odd_c = collections.Counter(odd)
even_c = collections.Counter(even)
# 出現数の多い順にリストをつくる
# 要素は値と出現頻度のペア（tuple）
freq_odd = odd_c.most_common()
freq_even = even_c.most_common()

if len(set(value)) == 1:  # valuleがすべて同じ数の場合（例外処理）
  freq = n // 2
elif freq_odd[0][0] != freq_even[0][0]:
  freq = freq_odd[0][1] + freq_even[0][1]
else:
  if len(freq_odd) == 1:
    freq = freq_odd[0][1] + freq_even[1][1]
  elif len(freq_even) == 1:
    freq = freq_odd[1][1] + freq_even[0][1]
  else:
    # 最頻値が一致してしまう場合、その次に出現数の多い要素を決め直す
    choice_a = freq_odd[0][1] + freq_even[1][1]
    choice_b = freq_odd[1][1] + freq_even[0][1]
    # 出現数の多いほど、書き直す部分は少なくて済む
    freq = max(choice_a, choice_b)

print(n - freq)

Submission status: Accepted

Solution code:
n = int(input())
v = [int(x) for x in input().split()]
big = 10**5+1
even = [0] * big
odd = [0] * big
for i in range(big):
    even[i] = [0] * 2
    odd[i] = [0]*2
    even[i][1] = i
    odd[i][1] = i

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

even = sorted(even, reverse=True)
odd = sorted(odd, reverse=True)

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

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v0 = []
v1 = []
for i in range(n):
    if i % 2 == 0:
        v0.append(v[i])
    else:
        v1.append(v[i])
v0.sort()
v1.sort()
l = [[0,-2]]
r = [[0,-1]]
hoge = v0[0]
count = 0
for i in v0:
    if i == hoge:
        count += 1
    else:
        l.append([count,hoge])
        hoge = i
        count = 1
l.append([count,hoge])
hoge = v1[0]
count = 0
for i in v1:
    if i == hoge:
        count += 1
    else:
        r.append([count,hoge])
        hoge = i
        count = 1
r.append([count,hoge])
l.sort(reverse = True)
r.sort(reverse = True)
if l[0][1] != r[0][1]:
    print(n-l[0][0]-r[0][0])
else:
    print(min(n-l[1][0]-r[0][0],n-l[0][0]-r[1][0]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
lst = [x for x in input().split()]

lst1 = Counter(lst[0::2]).most_common()
lst2 = Counter(lst[1::2]).most_common()

if lst1[0][0] != lst2[0][0]:
    print(n - lst1[0][1] - lst2[0][1])
else:
    if len(lst1) == 1 or len(lst2) == 1:
        print(n // 2)
    else:
        a = n - lst1[1][1] - lst2[0][1]
        b = n - lst1[0][1] - lst2[1][1]
        print(min(a, b))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
gu, ki = [], []
for i in range(n):
	if i % 2 == 0:
		ki.append(v[i])
	else:
		gu.append(v[i])
ki_flag, gu_flag = False, False
kict = Counter(ki)
guct = Counter(gu)
kict_mc = kict.most_common()
guct_mc = guct.most_common()
if len(kict_mc) > 1:
	ki_flag = True
if len(guct_mc) > 1:
	gu_flag = True
ki_first = kict_mc[0][0]
if ki_flag:
	ki_second = kict_mc[1][0]
gu_first = guct_mc[0][0]
if gu_flag:
	gu_second = guct_mc[1][0]
if ki_flag & gu_flag:
	if ki_first == gu_first:
		print(min([n - kict_mc[0][1] - guct_mc[1][1], n - kict_mc[1][1] - guct_mc[0][1]]))
	else:
		print(n - kict_mc[0][1] - guct_mc[0][1])
elif ki_flag:
	if ki_first == gu_first:
		print(n - kict_mc[1][1] - guct_mc[0][1])
	else:
		print(n - kict_mc[0][1] - guct_mc[0][1])
elif gu_flag:
	if ki_first == gu_first:
		print(n - kict_mc[0][1] - guct_mc[1][1])
	else:
		print(n - kict_mc[0][1] - guct_mc[0][1])
else:
	if ki_first == gu_first:
		print(int(n / 2))
	else:
		print(n - kict_mc[0][1] - guct_mc[0][1])

Submission status: Accepted

Solution code:
from collections import defaultdict
dic1=defaultdict(int)
dic2=defaultdict(int)
n=int(input())
l=list(map(int,input().split()))
for i in range(0,n-1,2):
  dic1[l[i]]+=1
for i in range(1,n,2):
  dic2[l[i]]+=1
max11=0
i1=0
max12=0
i2=0
max21=0
i3=0
max22=0
i4=0
for i in dic1:
  if dic1[i]>=max11:
    max12=max11
    max11=dic1[i]
    i2=i1
    i1=i
  elif dic1[i]>max12:
    max12=dic1[i]
    i2=i
for i in dic2:
  if dic2[i]>=max21:
    max22=max21
    max21=dic2[i]
    i4=i3
    i3=i
  elif dic2[i]>max22:
    max22=dic2[i]
    i4=i
if i1!=i3:
  print(n-max11-max21)
elif i1==i3:
  print(min(n-max11-max22,n-max21-max12))

Submission status: Accepted

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


from collections import defaultdict
d1 = defaultdict(int)
d2 = defaultdict(int)

for i in range(n):
    if i%2==0:
        d1[ls[i]]+=1
    else:
        d2[ls[i]]+=1


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


if d1[0][0] != d2[0][0]:
    print(n-d1[0][1]-d2[0][1])
else:
    if len(d1)==1 and len(d2)==1:
        print(min(n-d1[0][1],n-d2[0][1]))
    elif len(d1)==1 and len(d2)==2:
        print(min(n-d1[0][1]-d2[1][1],n-d2[0][1]))
    elif len(d1)==2 and len(d2)==1:
        print(min(n-d1[1][1]-d2[0][1],n-d1[0][1]))
    else:
        print(min(n-d1[1][1]-d2[0][1],n-d1[0][1]-d2[1][1]))

Submission status: Accepted

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

li0 = li[::2]
li1 = li[1::2]
c0 = col.Counter(li0)
c1 = col.Counter(li1)
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 len(set(li0)) == 1:
    if len(set(li1)) == 1:
      print(n - len(li0))
    else:
      print(n - len(li0) - c1.most_common()[1][1])
  else:
    if len(set(li1)) == 1:
      print(n - len(li1) - c0.most_common()[1][1])
    else:
      if c0.most_common()[1][1] <= c1.most_common()[1][1]:
        print(n - c0.most_common()[0][1]-c1.most_common()[1][1])
      else:
        print(n - c1.most_common()[0][1]-c0.most_common()[1][1])

Submission status: Accepted

Solution code:
import collections

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

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

c1_sort = sorted(c1.items(), key=lambda x:x[1], reverse=True)
c2_sort = sorted(c2.items(), key=lambda x:x[1], reverse=True)

if c1_sort[0][0] != c2_sort[0][0]:
    ans = n - c1_sort[0][1] - c2_sort[0][1]
else:
    if len(c1_sort) == 1 and len(c2_sort) == 1:
        ans = n // 2
    else:
        ans = n - max(c1_sort[0][1] + c2_sort[1][1], c1_sort[1][1] + c2_sort[0][1])
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
dat_a = list(map(int, input().split()))
dat_a_e = []
dat_a_o = []
for i in range(n//2):
    dat_a_e.append(dat_a[2*i])
    dat_a_o.append(dat_a[2*i+1])
#print(dat_a_e)
#print(dat_a_o)
c_e = collections.Counter(dat_a_e).most_common(2)
c_o = collections.Counter(dat_a_o).most_common(2)
if len(c_e) == 1:
    c_e.append((0,0))
if len(c_o) == 1:
    c_o.append((0,0))

if c_o[0][0] != c_e[0][0]:
    print(n//2 - c_e[0][1] + n//2 - c_o[0][1])
else:
    x = n//2 - c_e[0][1] + n//2 - c_o[1][1]
    y = n//2 - c_e[1][1] + n//2 - c_o[0][1]
    print(min(x, y))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

L=II()
INL=LI()

ODDS=INL[::2]
EVNS=INL[1::2]

MP_ODDS = {}
MP_EVNS = {}

if L == 2:
    print(0)
    exit()

for i in ODDS:
    if not i in MP_ODDS:
        MP_ODDS[i] = 0
    MP_ODDS[i] += 1

for i in EVNS:
    if not i in MP_EVNS:
        MP_EVNS[i] = 0
    MP_EVNS[i] += 1

ODDSC = []
EVNSC = []
for i,j in MP_ODDS.items():
    ODDSC.append((j,i))
for i,j in MP_EVNS.items():
    EVNSC.append((j,i))
ODDSC.sort(reverse=True)
EVNSC.sort(reverse=True)
EVNSC.append((0, -1))
ODDSC.append((0, -1))

# print(ODDSC, L)
if ODDSC[0][1] == EVNSC[0][1]:
    print(min(L-ODDSC[1][0]-EVNSC[0][0], L-ODDSC[0][0]-EVNSC[1][0]))    
else:
    print(L - ODDSC[0][0] - EVNSC[0][0])

Submission status: Accepted

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

from collections import Counter

v = Counter(V).most_common()

if len(v)==1:
    print(n//2)
    
else:
    even = [V[2*i] for i in range(n//2)]
    odd = [V[2*j+1] for j in range(n//2)]
    
    e = Counter(even).most_common()
    o = Counter(odd).most_common()
    
    if e[0][0]!=o[0][0]:
        print(n-e[0][1]-o[0][1])
    else:
        sec = v[1][0]
        esec = even.count(sec)
        osec = odd.count(sec)
        print(n-max(esec+o[0][1], osec+e[0][1]))

Submission status: Accepted

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

from collections import Counter

v = Counter(V).most_common()

if len(v)==1:
    print(n//2)
    
else:
    even = [V[2*i] for i in range(n//2)]
    odd = [V[2*j+1] for j in range(n//2)]
    
    e = Counter(even).most_common()
    o = Counter(odd).most_common()
    
    if e[0][0]!=o[0][0]:
        print(n-e[0][1]-o[0][1])
    else:
        sec = v[1][0]
        esec = even.count(sec)
        osec = odd.count(sec)
        if esec>osec:
            print(n-esec-o[0][1])
        else:
            print(n-osec-e[0][1])

Submission status: Accepted

Solution code:
def count_nums(v):
    counter = {}
    for x in v:
        if x not in counter:
            counter[x] = 0
        counter[x] += 1
    retval = list(counter.items())
    retval.sort(lambda a: -a[1])
    # i : index
    # retval[i][0] : number
    # retval[i][1] : count
    # retval is sorted by count
    return retval


def main():
    n = int(input())
    v = list(map(int, input().split()))
    v1 = v[::2]
    v2 = v[1::2]

    cv1 = count_nums(v1)
    cv2 = count_nums(v2)

    if cv1[0][0] != cv2[0][0]:
        print(n - (cv1[0][1] + cv2[0][1]))
    elif len(cv1) == 1 and len(cv2) == 1:
        print(len(v2))
    elif len(cv1) == 1:
        print(len(v2) - cv2[1][1])
    elif len(cv2) == 1:
        print(len(v1) - cv1[1][1])
    else:
        print(n - max(cv1[0][1]+cv2[1][1], cv1[1][1]+cv2[0][1]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter


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


lst_o = lst_v[::2]
lst_e = lst_v[1::2]
cnt_o = Counter(lst_o).most_common() + [(0, 0)]
cnt_e = Counter(lst_e).most_common() + [(0, 0)]


if cnt_o[0][0] == cnt_e[0][0]:
    rem_number = max(cnt_o[0][1] + cnt_e[1][1], cnt_o[1][1] + cnt_e[0][1])
else:
    rem_number = cnt_o[0][1] + cnt_e[0][1]
ans = n - rem_number


print(ans)

Submission status: Accepted

Solution code:

#################################
#配列を二つに分ける
#引数
# values: 配列
# a,b   : 配列の格納先
#戻り値
# なし
#################################
def separate(values,a,b):
    for v in values:
        a.append(v)
        a,b = b,a


##############################
#登場回数の多い数と登場回数を返す
#引数
# values: ソートされた配列
#戻り値
# 辞書型配列
##############################
def getCount(values):
    currentNum=0
    currentCount=0
    ret={'First' :{'Num':[],'Cnt':0},
         'Second':{'Num':[],'Cnt':0}}
    for v in values:
        if v==currentNum:
            currentCount+=1
        else:
            #別の数字が表れた場合
            if ret['First']['Cnt'] < currentCount:
                #最大値より大きい場合、それが新たな最大値になる
                ret['Second']=ret['First'].copy()
                ret['First']={'Num':[currentNum],'Cnt':currentCount}
            elif ret['First']['Cnt'] == currentCount:
                #最大値タイ
                ret['First']['Num'].append(currentNum)
            elif ret['Second']['Cnt'] < currentCount:
                #2番目より大きい場合、それが新たな2番目になる。
                ret['Second']={'Num':[currentNum],'Cnt':currentCount}
            elif ret['Second']['Cnt'] == currentCount:
                #2位タイ
                ret['Second']['Num'].append(currentNum)

            currentNum=v
            currentCount=1

    #配列の最後まで来た場合
    if ret['First']['Cnt'] < currentCount:
        ret['Second']=ret['First'].copy()
        ret['First']={'Num':[currentNum],'Cnt':currentCount}
    elif ret['First']['Cnt'] == currentCount:
        ret['First']['Num'].append(currentNum)
    elif ret['Second']['Cnt'] < currentCount:
        ret['Second']={'Num':[currentNum],'Cnt':currentCount}
    elif ret['Second']['Cnt'] == currentCount:
        ret['Second']['Num'].append(currentNum)

    return ret


#############################
# メイン
#############################
n = input()
vars = input ()
a=[]
b=[]
varArray=vars.split(" ")
separate(varArray,a,b)
a.sort()
b.sort()
a_result=getCount(a)
b_result=getCount(b)
if (len(a_result['First']['Num'])>1 or
    len(b_result['First']['Num'])>1):
    #最頻値が複数ある場合は素直に計算可
    xa=len(a)-a_result['First']['Cnt']
    xb=len(b)-b_result['First']['Cnt']
    x=xa+xb
else:
    #abどちらも最頻値が1種類の場合
    if a_result['First']['Num'][0]!=b_result['First']['Num'][0]:
        #abの最頻値が異なる場合は素直に計算可
        xa=len(a)-a_result['First']['Cnt']
        xb=len(b)-b_result['First']['Cnt']
        x=xa+xb
    else:
        #abの最頻値が同じ値の場合、どちらかは2番目の値を採用する必要あり
        xaa=len(a)-a_result['First']['Cnt']
        xab=len(b)-b_result['Second']['Cnt']
        xa=xaa+xab
        xba=len(a)-a_result['Second']['Cnt']
        xbb=len(b)-b_result['First']['Cnt']
        xb=xba+xbb
        x=xa if xa<xb else xb

print(x)

Submission status: Accepted

Solution code:
from collections import Counter


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


lst_o = lst_v[::2]
lst_e = lst_v[1::2]
cnt_o = Counter(lst_o).most_common() + [(0, 0)]
cnt_e = Counter(lst_e).most_common() + [(0, 0)]


if cnt_o[0][0] == cnt_e[0][0]:
    rem_number = max(cnt_o[0][1] + cnt_e[1][1], cnt_o[1][1] + cnt_e[0][1])
else:
    rem_number = cnt_o[0][1] + cnt_e[0][1]
ans = n - rem_number


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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



if len(set(lst_v)) == 1:
    ans = n // 2
else:
    lst_o = lst_v[::2]
    lst_e = lst_v[1::2]
    cnt_o = Counter(lst_o).most_common()
    cnt_e = Counter(lst_e).most_common()

    if cnt_o[0][0] == cnt_e[0][0]:
        rem_number = max(cnt_o[0][1] + cnt_e[1][1], cnt_o[1][1] + cnt_e[0][1])
    else:
        rem_number = cnt_o[0][1] + cnt_e[0][1]
    ans = n - rem_number


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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



if len(set(lst_v)) == 1:
    ans = n // 2
else:
    lst_o = lst_v[::2]
    lst_e = lst_v[1::2]
    cnt_o = sorted(Counter(lst_o).items(), key=lambda x: -x[1])
    cnt_e = sorted(Counter(lst_e).items(), key=lambda x :-x[1])


    if cnt_o[0][0] == cnt_e[0][0]:
        rem_number = max(cnt_o[0][1] + cnt_e[1][1], cnt_o[1][1] + cnt_e[0][1])
    else:
        rem_number = cnt_o[0][1] + cnt_e[0][1]
    ans = n - rem_number


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, OrderedDict


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


if len(set(lst_v)) == 1:
    ans = n // 2
else:
    lst_odd = lst_v[::2]
    lst_even = lst_v[1::2]
    cnt_odd = sorted(Counter(lst_odd).items(), key=lambda x: -x[1])
    cnt_even = sorted(Counter(lst_even).items(), key=lambda x :-x[1])

    if cnt_odd[0][0] == cnt_even[0][0]:
        rem_number = max(cnt_odd[0][1] + cnt_even[1][1], cnt_odd[1][1] + cnt_even[0][1])
    else:
        rem_number = cnt_odd[0][1] + cnt_even[0][1]
    sum_odd = sum(t[1] for t in cnt_odd)
    sum_even = sum(t[1] for t in cnt_even)
    ans = sum_odd + sum_even - rem_number


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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


if len(set(lst_v)) == 1:
    ans = n // 2
else:
    lst_odd = lst_v[::2]
    lst_even = lst_v[1::2]
    cnt_odd = sorted(Counter(lst_odd).items(), key=lambda x: -x[1])
    cnt_even = sorted(Counter(lst_even).items(), key=lambda x :-x[1])
    o1 = cnt_odd[0]
    e1 = cnt_even[0]

    if o1[0] == e1[0]:
        o2 = cnt_odd[1] if cnt_odd[1] else (0, 0)
        e2 = cnt_even[1] if cnt_even[1] else (0, 0)
        rem_number = max(o1[1] + e2[1], o2[1] + e1[1])
    else:
        rem_number = o1[1] + e1[1]
    sum_odd = sum(t[1] for t in cnt_odd)
    sum_even = sum(t[1] for t in cnt_even)
    ans = sum_odd + sum_even - rem_number


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, OrderedDict


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


if len(set(lst_v)) == 1:
    ans = n // 2
else:
    lst_odd = [lst_v[i] for i in range(n) if i%2==0]
    lst_even = [lst_v[i] for i in range(n) if i%2!=0]
    cnt_odd = sorted(Counter(lst_odd).items(), key=lambda x: -x[1])
    cnt_even = sorted(Counter(lst_even).items(), key=lambda x :-x[1])

    if cnt_odd[0][0] == cnt_even[0][0]:
        rem_number = max(cnt_odd[0][1] + cnt_even[1][1], cnt_odd[1][1] + cnt_even[0][1])
    else:
        rem_number = cnt_odd[0][1] + cnt_even[0][1]
    sum_odd = sum(t[1] for t in cnt_odd)
    sum_even = sum(t[1] for t in cnt_even)
    ans = sum_odd + sum_even - rem_number


print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import Counter
 
N = int(input())
V = [int(x) for x in input().split()]
EV_counter = Counter(V[::2])
OD_counter = Counter(V[1::2])
 
# 1種類しかないとき対策
EV_counter[-1] = 0
OD_counter[-2] = 0
 
# 上位2種しか使う必要がない
EV = EV_counter.most_common(2)
OD = OD_counter.most_common(2)
 
answer = N
for (k1,v1), (k2,v2) in itertools.product(EV,OD):
    if k1 == k2:
        continue
    x = (N - v1 - v2)
    if answer > x:
        answer = x

print(answer)

Submission status: Accepted

Solution code:
import collections 

n = int(input())
lst = list(map(int,input().split()))
lst_mode_odd = collections.Counter(lst[::2]).most_common()
l_odd = len(lst_mode_odd)
n_odd = lst_mode_odd[0][1]
num_odd = lst_mode_odd[0][0]
lst_mode_even = collections.Counter(lst[::-2]).most_common()
l_even = len(lst_mode_even)
n_even = lst_mode_even[0][1]
num_even = lst_mode_even[0][0]
if num_odd == num_even:
    if l_odd == l_even ==1:
        print(int(len(lst)/2))
        exit()
    elif l_odd == 1:
        n_even = lst_mode_even[1][1]
    elif l_even == 1:
        n_odd = lst_mode_odd[1][1]
    else:
        res_odd = lst_mode_odd[0][1]- lst_mode_odd[1][1]
        res_even = lst_mode_even[0][1]- lst_mode_even[1][1]
        if res_odd >= res_even:
            n_even = lst_mode_even[1][1]
        else:n_odd = lst_mode_odd[1][1]
    
ans = len(lst) - n_odd -n_even
print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import Counter

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

# 1種類しかないとき対策
EV_counter[-1] = 0
OD_counter[-2] = 0

# 上位2種しか使う必要がない
EV = EV_counter.most_common(2)
OD = OD_counter.most_common(2)

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

print(answer)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
nums=list(map(int,input().split()))
a=set(nums)
if len(a)==1:
  print(n//2)
else:
  odd=nums[::2]
  even=nums[1::2]
  o=Counter(odd)
  e=Counter(even)
  ans=n-o.most_common()[0][1]-e.most_common()[0][1]
  if o.most_common()[0][0]==e.most_common()[0][0]:
    print(ans+min(o.most_common()[0][1]-o.most_common()[1][1],e.most_common()[0][1]-e.most_common()[1][1]))
  else:  
    print(ans)

Submission status: Accepted

Solution code:
import collections

n=int(input())
v=list(map(int,input().split()))
v1=collections.Counter(v[0::2]).most_common()
v2=collections.Counter(v[1::2]).most_common()
v1.append((0,0))
v2.append((0,0))

if v1[0][0]!=v2[0][0]:
    ans=len(v) - v1[0][1] - v2[0][1]
else:
    ans=min(len(v) - v1[0][1] - v2[1][1],len(v) - v1[1][1] - v2[0][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
tmp = []
for i in range(0, n, 2):
    tmp.append(v[i])
c = Counter(tmp)
c1 = c.most_common(2)
tmp = []
for i in range(1, n, 2):
    tmp.append(v[i])
c = Counter(tmp)
c2 = c.most_common(2)
if c1[0][0] != c2[0][0]:
    print(n - (c1[0][1] + c2[0][1]))
    exit()
else:
    c1.append((-1, 0))
    c2.append((-1, 0))
    ans = min(n-c1[0][1]-c2[1][1], n-c1[1][1]-c2[0][1])
    print(ans)
    exit()