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:
#!/usr/bin/env python3

import collections
import itertools
import sys


INVALID = -1
INF = sys.maxsize // 2


def solve(n, vs):
    up_vals = vs[::2]
    up_ctr = collections.Counter(up_vals)
    up_ctr[INVALID] = 0
    down_vals = vs[1::2]
    down_ctr = collections.Counter(down_vals)
    down_ctr[INVALID] = 0
    up_mcs = up_ctr.most_common(3)
    down_mcs = down_ctr.most_common(3)
    cost = INF
    for (k_up, v_up), (k_down, v_down) in itertools.product(up_mcs, down_mcs):
        if k_up != k_down:
            new_cost = n - v_up - v_down
            cost = min(new_cost, cost)
    return cost


def main():
    n = int(input())
    vs = [int(v) for v in input().split()]
    print(solve(n, vs))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#

"""
ARC103 C
"""

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

evli = vli[::2]
ovli = vli[1::2]

from collections import Counter
Cevli = Counter(evli).most_common(2)
Covli = Counter(ovli).most_common(2)

vem,iem=Cevli[0]
vom,iom=Covli[0]

if vem!=vom:
    ans = n-iem-iom
elif iem == n//2 and iom == n//2:
    ans = n//2
elif iem == n//2:
    vom2,iom2=Covli[1]
    ans = n-n//2-iom2
elif iom == n//2:
    vem2,iem2=Cevli[1]
    ans = n-n//2-iem2
else:
    vem2,iem2=Cevli[1]
    vom2,iom2=Covli[1]
    ans1 = n-iem-iom2
    ans2 = n-iem2-iom
    ans = min(ans1,ans2)

print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

# even
if count_even.most_common()[0][0] != count_odd.most_common()[0][0]:
    ans = n - count_even.most_common()[0][1] -count_odd.most_common()[0][1]
else:
    if len(count_even) == 1 and len(count_odd) == 1:
        ans = n//2
    elif len(count_even) == 1:
        ans = n//2 - count_odd.most_common()[1][1]
    elif len(count_odd) == 1:
        ans = (n+1)//2 - count_even.most_common()[1][1]
    else:
        ans_tmp1 = n - count_even.most_common()[0][1] - count_odd.most_common()[1][1]
        ans_tmp2 = n - count_even.most_common()[1][1] - count_odd.most_common()[0][1]
        ans = min(ans_tmp1, ans_tmp2)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
ecost = [n//2 for _ in range(100001)]
ocost = [n//2 for _ in range(100001)]
for i in range(n):
    if i%2 == 0:
        ecost[v[i]] -= 1
    else:
        ocost[v[i]] -= 1
e1 = ecost[1]
ve1 = 1
e2 = ecost[2]
ve2 = 2
o1 = ocost[1]
vo1 = 1
o2 = ocost[2]
vo2 = 2
for i in range(3,100001):
    ecand = ecost[i]
    ocand = ocost[i]
    if ecand <= e1:
        e2 = e1
        e1 = ecand
        ve2 = ve1
        ve1 = i
    elif ecand <= e2:
        e2 = ecand
        ve2 = i
    if ocand <= o1:
        o2 = o1
        o1 = ocand
        vo2 = vo1
        vo1 = i
    elif ocand <= o2:
        o2 = ocand
        vo2 = i
if vo1 != ve1:
    ans = o1 + e1
else:
    ans = e1 + min(o2,e2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt0 = Counter(vs[0::2])
cnt1 = Counter(vs[1::2])

cnt0[0] = 0
cnt1[0] = 0

ans = n
for v0, num0 in cnt0.most_common(2):
    for v1, num1 in cnt1.most_common(2):
        if v0 != v1:
            ans = min(ans, n - (num0 + num1))

print(ans)

Submission status: Accepted

Solution code:
from statistics import mode
from collections import Counter
# S = input()
n = int(input())
# N, K = map(int, input().split())
v = list(map(int, input().split()))
# LR = [list(map(int, input().split())) for _ in range(M)]

guu = []
kii = []

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

counterK = Counter(guu)
guuMaxCount = counterK.most_common()[0][1]

counterG = Counter(kii)
kiiMaxCount = counterG.most_common()[0][1]

if counterK.most_common()[0][0] == counterG.most_common()[0][0]:
    if n == 2:
        kiiMaxCount = 0
    elif n == 3:
        kiiMaxCount = 0
    elif n >= 4:
        if len(counterK.most_common()) >= 2 and len(counterG.most_common()) >= 2:
            if kiiMaxCount - counterK.most_common()[1][1] >= guuMaxCount - counterG.most_common()[1][1]:
                guuMaxCount = counterG.most_common()[1][1]
            else:
                kiiMaxCount = counterK.most_common()[1][1]
        elif len(counterK.most_common()) >= 2 and len(counterG.most_common()) == 1:
            kiiMaxCount = kiiMaxCount = counterK.most_common()[1][1]
        elif len(counterK.most_common()) == 1 and len(counterG.most_common()) >= 2:
            guuMaxCount = counterG.most_common()[1][1]
        else:
            kiiMaxCount = 0

ans = n



print(ans - guuMaxCount - kiiMaxCount)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import Counter

n = ni()
a = list(li())

even = Counter(a[::2])
odd = Counter(a[1::2])

es = [(0,0)]
for ek,ev in even.items():
    es.append((ev,ek))
    
es.sort()
    
    
os = [(0,0)]
for ok,ov in odd.items():
    os.append((ov,ok))
    
os.sort()
    

if es[-1][1] != os[-1][1]:
    print((n//2-es[-1][0]) + (n//2-os[-1][0]))
else:
    print(min((n//2-es[-1][0]) + (n//2-os[-2][0]),
              (n//2-es[-2][0]) + (n//2-os[-1][0])))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import collections

n=int(input())
a = [int(i) for i in input().split()]

ae = [a[i] for i in range(n) if i%2==0]
ao = [a[i] for i in range(n) if i%2==1]

aedic = collections.Counter()

aodic = collections.Counter()

for i in ae:
    aedic[i]+=1
for i in ao:
    aodic[i]+=1

p=aedic.most_common(2)
q=aodic.most_common(2)

#print(p,q)

if p[0][0] != q[0][0]:
#    print("a")
    print(n-p[0][1]-q[0][1])
elif len(p)==1 and len(q)!=1:
#    print("b")
    print(n-p[0][1]-q[1][1])
elif len(p)!=1 and len(q)==1:
#    print("c")
    print(n-p[1][1]-q[0][1])
elif len(p)==1 and len(q)==1:
#    print("c")
    print(n//2)
else:
#    print("d")
    a=min(n-p[-1][1]-q[0][1], n-p[0][1]-q[-1][1])
    print(a)

Submission status: Accepted

Solution code:
from collections import Counter

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

C1 = list(dict(Counter(A[::2])).items())
C2 = list(dict(Counter(A[1::2])).items())

C1.append([-1, 0])
C2.append([-1, 0])
C1.sort(key=lambda x: x[1], reverse=True)
C2.sort(key=lambda x: x[1], reverse=True)

if C1[0][0] != C2[0][0]:
    print(N - C1[0][1] - C2[0][1])
else:
    ans1 = N - C1[0][1] - C2[1][1]
    ans2 = N - C1[1][1] - C2[0][1]
    print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
w=sorted(a for a in v[0::2])
x=sorted(a for a in v[1::2])
a=[[0,i]for i in range(10**5+1)]
b=[[0,i]for i in range(10**5+1)]
for t in w:
    a[t][0]+=1
for t in x:
    b[t][0]+=1
c=sorted(a)[::-1]
d=sorted(b)[::-1]
if c[0][1]==d[0][1]:
    t=max(c[1][0],d[1][0])
    print(n-c[0][0]-t)
else:
    print(n-c[0][0]-d[0][0])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    vs = [int(s) for s in input().split()]

    print(solve(vs))

def solve(vs):
    c1 = count(vs[0::2])
    c2 = count(vs[1::2])

    a1 = c1[0]
    a2 = c1[1] if len(c1) > 1 else (0, 0)
    b1 = c2[0]
    b2 = c2[1] if len(c2) > 1 else (0, 0)

    if a1[0] != b1[0]:
        return len(vs) - a1[1] - b1[1]

    if a1[1] + b2[1] > a2[1] + b1[1]:
        return len(vs) - a1[1] - b2[1]
    else:
        return len(vs) - a2[1] - b1[1]

def count(vs):
    c = dict()

    for v in vs:
        c[v] = c.get(v, 0) + 1

    cs = sorted(c.items(), key=lambda x: x[1], reverse=True)
    return cs

main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, V):
    v1 = [v for v in V[::2]]
    v2 = [v for v in V[1::2]]

    v1c = Counter(v1)
    v2c = Counter(v2)
    v1 = v1c.most_common(2)
    v2 = v2c.most_common(2)
    v1.append((-1, 0))
    v2.append((-1, 0))
    v1_1, v1_2 = v1[:2]
    v2_1, v2_2 = v2[:2]

    if v1_1[0] != v2_1[0]:
        return (N//2 - v1_1[1]) + (N//2 - v2_1[1])

    return min(
        (N//2 - v1_1[1]) + (N//2 - v2_2[1]),
        (N//2 - v1_2[1]) + (N//2 - v2_1[1]),
    )


def main():
    N = read_int()
    V = read_int_n()
    print(slv(N, V))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
v=[int(i) for i in input().split()]
if N==2:
    if v[0]==v[1]:
        print(1)
        exit()
    else:
        print(0)
        exit()
A=dict()
B=dict()
L=0
R=0
def inc(D,k):
    if k in D:
        D[k]+=1
    else:
        D[k]=1
for i in range(N):
    if i%2==0:
        L+=1
        inc(A,v[i])
    else:
        R+=1
        inc(B,v[i])
X=[(L,-1)]
Y=[(R,-2)]
for i in A:
    X.append((L-A[i],i))
for i in B:
    Y.append((R-B[i],i))
X.sort(reverse=True)
Y.sort(reverse=True)
while(True):
    if X[-1][1]!=Y[-1][1]:
        print(X[-1][0]+Y[-1][0])
        exit()
    else:
        vl=X[-1][0]+Y[-2][0]
        vr=X[-2][0]+Y[-1][0]
        if vl<vr:
            Y.pop()
            R-=1
        else:
            X.pop()
            L-=1

Submission status: Accepted

Solution code:
from collections import Counter as C
n = int(input())
v = [int(a) for a in input().split()]

k1 = C(v[::2]).most_common()[0][0]
v1 = C(v[::2]).most_common()[0][1]

if v1 == n/2:
    k2 = 0
    v2 = 0
else:
    k2 = C(v[::2]).most_common()[1][0]
    v2 = C(v[::2]).most_common()[1][1]

k3 = C(v[1::2]).most_common()[0][0]
v3 = C(v[1::2]).most_common()[0][1]

if v3 == n/2:
    k4 = 0
    v4 = 0
else:
    k4 = C(v[1::2]).most_common()[1][0]
    v4 = C(v[1::2]).most_common()[1][1]

if k1 != k3:
    print (n - v1 - v3)
else:
    print (min(n - v1 - v4, n - v2 - v3))

Submission status: Accepted

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

v0 = collections.Counter([v[2 * i] for i in range(n // 2)])
v1 = collections.Counter([v[2 * i + 1] for i in range(n // 2)])

if v0.most_common()[0][0] != v1.most_common()[0][0]:
    ans = n - v0.most_common()[0][1] - v1.most_common()[0][1]
else:
    if len(v0) == 1:
        if len(v1) == 1:
            ans = n // 2
        else:
            ans = n - v1.most_common()[1][1]
    else:
        if len(v1) == 1:
            ans = n - v0.most_common()[1][1]
        else:
            ans = min(n - v0.most_common()[0][1] - v1.most_common()[1][1], n - v0.most_common()[1][1] - v1.most_common()[0][1])

print(ans)

Submission status: Accepted

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

c = {0:{}, 1:{}}
for i,vi in enumerate(v):
    r = i % 2
    if vi not in c[r]:
        c[r][vi] = 0
    c[r][vi] += 1

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

if c0[0][0] == c1[0][0]:
    if len(c0) < 2:
        c0.append((-1,0))
    if len(c1) < 2:
        c1.append((-1,0))
    print(n - max(c0[0][1] + c1[1][1], c0[1][1] + c1[0][1]))
else:
    print(n - c0[0][1] - c1[0][1])

Submission status: Accepted

Solution code:
import sys

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

check=0
for i in range(n):
    if V[i]!=V[i-1]:
        check=1
        break

if check==0:
    print(n//2)
    sys.exit()


G=[]
K=[]
for i in range(n):
    if i%2==0:
        G.append(V[i])
    else:
        K.append(V[i])

G.sort()
K.sort()

GV=[]
count=1

for i in range(1,len(G)):
    if G[i]==G[i-1]:
        count+=1
    else:
        GV.append((count,G[i-1]))
        count=1

GV.append((count,G[i-1]))

count=1
KV=[]
for i in range(1,len(K)):
    if K[i]==K[i-1]:
        count+=1
    else:
        KV.append((count,K[i-1]))
        count=1

KV.append((count,K[i-1]))

GV.sort(reverse=True)
KV.sort(reverse=True)

if GV[0][1]!=KV[0][1]:
    ANS=len(G)+len(K)-GV[0][0]-KV[0][0]

else:
    ANS=min(len(G)+len(K)-GV[1][0]-KV[0][0],len(G)+len(K)-GV[0][0]-KV[1][0])

print(ANS)

Submission status: Accepted

Solution code:
N = int(input())
lis = [int(i) for i in input().split()]
i = 0
a = {}
b = {}
for nums in lis:
  if i%2==0:
    if nums in a.keys():
      a[nums] += 1
    else:
      a[nums] = 1
  else:
    if nums in b.keys():
      b[nums] += 1
    else:
      b[nums] = 1
  i += 1
a1 = sorted(a.items(), key=lambda x: -x[1])
b1 = sorted(b.items(), key=lambda x: -x[1])
if a1[0][0]!=b1[0][0]:
  print(N-a1[0][1]-b1[0][1])
else:
  if len(a1)>=2 and len(b1)>=2:
    print(min(N-a1[1][1]-b1[0][1],N-a1[0][1]-b1[1][1]))
  elif len(a1)==1 and len(b1)==1:
    print(min(N-a1[0][1],N-b1[0][1]))
  elif len(a1)==1:
    print(min(N-a1[0][1]-b1[1][1],N-b1[0][1]))
  else:
    print(min(N-b1[0][1]-a1[1][1],N-a1[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int( input())
V = list( map( int, input().split()))
oddV = [ V[2*i+1] for i in range(n//2)]
evenV = [ V[2*i] for i in range(n//2)]
CoddV = Counter(oddV).most_common()
CevenV = Counter(evenV).most_common()
p, q = CoddV[0]
a, b = CevenV[0]
if p != a:
    ans = n-b-q
elif n == 2:
    ans = 1
else:
    if q == n//2 and b == n//2:
        ans = n//2
    elif q == n//2:
        ans = n//2 - CevenV[1][1]
    elif b == n//2:
        ans = n//2 - CoddV[1][1]
    else:
        r, s = CoddV[1]
        c, d = CevenV[1]
        if q + d >= s + b:
            ans = n-q-d
        else:
            ans = n-s-b
print(ans)

Submission status: Accepted

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

b=[a[i] for i in range(0,n,2)]
c=[a[i] for i in range(1,n,2)]
from collections import Counter
xb=list(Counter(b).items())
xc=list(Counter(c).items())
xb=sorted(xb, key=lambda x:-x[1])
xc=sorted(xc, key=lambda x:-x[1])
ans=[]
if xb[0][0]==xc[0][0]:
    if len(xc)>1:
        ans.append(n//2-xb[0][1]-(-n//2)-xc[1][1])
    if len(xb)>1:
        ans.append(n//2-xb[1][1]-(-n//2)-xc[0][1])
    if len(xc)==1 and len(xb)==1:
        ans.append(n//2)
else:
    ans.append(n//2-xb[0][1]-(-n//2)-xc[0][1])
print(min(ans))

Submission status: Accepted

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

l0 = [0 for i in range(10**5+1)]
l1 = [0 for i in range(10**5+1)]

a0 = [v[i] for i in range(0, n, 2)]
a1 = [v[i] for i in range(1, n, 2)]

for i in a0:
    l0[i] += 1
for i in a1:
    l1[i] += 1

first0 = 0
index0 = 0
for i in range(len(l0)):
    if l0[i] > first0:
        first0 = l0[i]
        index0 = i

l0[index0] = 0
second0 = max(l0)

first1 = 0
index1 = 0
for i in range(len(l1)):
    if l1[i] > first1:
        first1 = l1[i]
        index1 = i

l1[index1] = 0
second1 = max(l1)

if index0 != index1:
    print(n - first0 - first1)
else:
    print(min(n - first0 - second1, n - first1 - second0))

Submission status: Accepted

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

l1 = [l[i] for i in range(0, n, 2)]
l2 = [l[i] for i in range(1, n, 2)]
c1 = sorted(Counter(l1).items(), key = lambda x: x[1], reverse = True) + [(0, 0)]
c2 = sorted(Counter(l2).items(), key = lambda x: x[1], reverse = True) + [(0, 0)]

res = c1[0][1] + c2[0][1]
if c1[0][0] == c2[0][0]:
    res = max((c1[1][1] + c2[0][1]), (c1[0][1] + c2[1][1]))

# print(c1, c2)
print(n - res)

Submission status: Accepted

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

v_cnt = [[0] * 100001 for _ in range(2)]

for i in range(n):
    v_cnt[i%2][v[i]] += 1

from collections import Counter

c = [sorted(enumerate(v_cnt[i]), reverse=True, key=lambda x: x[1])[:2] for i in range(2)]

#print(c)
ans = 1e9
for i, i_cnt in c[0]:
    for j, j_cnt in c[1]:
        if i != j:
            ans = min(ans, n//2 - i_cnt + (n+1)//2 - j_cnt)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from collections import Counter

s_in = lambda: stdin.readline()[:-1] # s = s_in()
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()

n = d_in()
v_arr = ds_in()

e_arr = []
o_arr = []
for i in range(n//2):
    e_arr.append(v_arr[2*i])
    o_arr.append(v_arr[2*i+1])

e_c = Counter(e_arr)
o_c = Counter(o_arr)

if len(e_c) == 1:
    e_m = e_c.most_common(n=1)
    if len(o_c) == 1:
        o_m = o_c.most_common(n=1)
        if e_m[0] == o_m[0]:
            print(n // 2)
        else:
            print(0)
    else:
        o_m = o_c.most_common(n=2)
        if e_m[0] == o_m[0]:
            print(n//2 - o_m[1][1])
        else:
            print(n//2 - o_m[0][1])
else:
    e_m = e_c.most_common(n=2)
    if len(o_c) == 1:
        o_m = o_c.most_common(n=1)
        if e_m[0] == o_m[0]:
            print(n//2 - e_m[1][1])
        else:
            print(n//2 - e_m[0][1])
    else:
        o_m = o_c.most_common(n=2)
        if e_m[0] != o_m[0]:
            print(n - e_m[0][1] - o_m[0][1])
        else:
            print(n - e_m[0][1] - max(e_m[1][1], o_m[1][1]))

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    n = input()
    vv = map(int, raw_input().split())
    d1 = defaultdict(int)
    for x in vv[::2]:
        d1[x] += 1
    d2 = defaultdict(int)
    for x in vv[1::2]:
        d2[x] += 1
    d1 = sorted([[c, x] for x, c in d1.items()], reverse=True)
    d2 = sorted([[c, x] for x, c in d2.items()], reverse=True)
    if d1[0][1] != d2[0][1]:
        print(n - d1[0][0] - d2[0][0])
        return
    if len(d1) == len(d2) == 1:
        print(n // 2)
        return
    if len(d1) == 1:
        print(n - d1[0][0] - d2[1][0])
        return
    if len(d2) == 1:
        print(n - d1[1][0] - d2[0][0])
        return
    print(n - max(d1[0][0] + d2[1][0], d1[1][0] + d2[0][0]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Cnt = [0]*100001
for v in V1:
    Cnt[v] += 1
a1 = N//2-max(Cnt)
arg1 = argmax(Cnt)
Cnt[arg1] = 0
a11 = N//2-max(Cnt)


Cnt = [0]*100001

for v in V2:
    Cnt[v] += 1
a2 = N//2-max(Cnt)
arg2 = argmax(Cnt)
Cnt[arg2] = 0
a22 = N//2-max(Cnt)
#print(arg1, arg2, a1, a11, a2, a22)
if arg1==arg2:
    print(min(a1+a22, a2+a11))
else:
    print(a1+a2)

Submission status: Accepted

Solution code:
from collections import Counter

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

one = sorted(Counter([v[i] for i in range(0, n, 2)]).items(), key=lambda x: x[1], reverse=True)
two = sorted(Counter([v[i] for i in range(1, n, 2)]).items(), key=lambda x: x[1], reverse=True)

#print(one, two)
if one[0][0] != two[0][0]:
	print(n - one[0][1] - two[0][1])
else:
	if len(one) == 1 and len(two) == 1:
		print(n // 2)
	elif len(one) == 1:
		print(n // 2 - two[1][1])
	elif len(two) == 1:
		print(n // 2 - one[1][1])
	else:
		print(min(n - one[0][1] - two[1][1], n - one[1][1] - two[0][1]))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = a[::2]
    c = a[1::2]

    retB = {}
    for i in b:
        retB[i] = retB.get(i, 0) + 1

    retC = {}
    for i in c:
        retC[i] = retC.get(i, 0) + 1

    retB = sorted(retB.items(), key=lambda x: x[1], reverse=True)
    retC = sorted(retC.items(), key=lambda x: x[1], reverse=True)
    if retB[0][0] != retC[0][0]:
        tmpB = len(b) - retB[0][1]
        tmpC = len(c) - retC[0][1]
        print(tmpC + tmpB)
    else:
        if len(retB) == 1 and len(retC) == 1:
            print(len(c))
            return
        if len(retB) == 1:
            tmpB = len(b) - retB[0][1]
            tmpC = len(c) - retC[1][1]
            print(tmpB + tmpC)
            return
        if len(retC) == 1:
            tmpB = len(b) - retB[1][1]
            tmpC = len(c) - retC[0][1]
            print(tmpB + tmpC)
            return
        tmpB = len(b) - retB[1][1]
        tmpC = len(c) - retC[0][1]
        ret = [tmpB + tmpC]
        tmpB = len(b) - retB[0][1]
        tmpC = len(c) - retC[1][1]
        ret.append(tmpB + tmpC)
        print(min(ret))


main()

Submission status: Accepted

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

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

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

numList = list(reversed(sorted(numList)))
numList2 = list(reversed(sorted(numList2)))

if numList[0][1] == numList2[0][1]:
    print(n - max(numList[0][0] + numList2[1][0],numList[1][0] + numList2[0][0]))
else:
    print(n - numList[0][0] - numList2[0][0])

Submission status: Accepted

Solution code:
# encoding:utf-8
from collections import Counter

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

	v1 = [vs[i] for i in range(n) if i % 2 == 0]
	v2 = [vs[i] for i in range(n) if i % 2 == 1]

	v1c = Counter(v1)
	v2c = Counter(v2)

	mc1 = v1c.most_common(2)
	mc2 = v2c.most_common(2)
	
	if len(mc1) == 1:
		mc1.append((0, 0))

	if len(mc2) == 1:
		mc2.append((0, 0))

	if mc1[0][0] == mc2[0][0]:
		if mc1[0][1] - mc1[1][1] >= mc2[0][1] - mc2[1][1]:
			print(n - mc1[0][1] - mc2[1][1])
		else:
			print(n - mc1[1][1] - mc2[0][1])
	else:
		print(n - mc1[0][1] - mc2[0][1])


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n = int(input())
t = list(map(int,input().split(" ")))
dic1 = {}
dic2 = {}
for i in range(n):
    v = t[i]
    if i % 2 == 1:
        if v in dic1:
            dic1[v] += 1
        else:
            dic1[v] = 1
    else:
        if v in dic2:
            dic2[v] += 1
        else:
            dic2[v] = 1


a = list(dic1.items())
b = list(dic2.items())
a.sort(key=lambda x:x[1])
b.sort(key=lambda x:x[1]) 

if a[-1][0] != b[-1][0]:
    m1 = a[-1][1]
    m2 = b[-1][1]

elif len(a) == 1 and len(b) == 1:
    m1 = a[-1][1]
    m2 = 0

elif  len(a) == 1:
    m1 = a[-1][1]
    m2 = b[-2][1]

elif len(b) == 1:
    m1 = a[-2][1]
    m2 = b[-1][1]

else:
    m = [ a[-1][1],a[-2][1],b[-2][1]]
    m.sort()
    m1 = m[-1]
    m2 = m[-2]

print(n - m1 - m2)

Submission status: Accepted

Solution code:
import numpy as np

n = int(raw_input())
v = map(int,raw_input().split())

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


for i in range(n/2):
    seta[v[2*i]-1]+=1
    setb[v[2*i+1]-1]+=1
seta = np.array(seta)
setb = np.array(setb)

argseta=seta.argsort()[::-1]
argsetb=setb.argsort()[::-1]


if argseta[0]!=argsetb[0]:
    print n - max(seta) - max(setb)
else:
    if setb[argseta[0]]-seta[argseta[1]]<setb[argsetb[0]]-setb[argsetb[1]]:
        print n - max(setb) - seta[argseta[1]]
    else:
        print n - max(seta) - setb[argsetb[1]]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
from collections import Counter
n = int(input())
A = inpl()

C1 = Counter(A[::2])
C2 = Counter(A[1::2])

S1 = sorted(C1.items(), key=lambda x: x[1], reverse=True) + [[0, 0]]
S2 = sorted(C2.items(), key=lambda x: x[1], reverse=True) + [[0, 0]]

if S1[0][0] != S2[0][0]:
    print(n - S1[0][1] - S2[0][1])
else:
    print(n - max(S1[1][1] + S2[0][1], S1[0][1] + S2[1][1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import Counter

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

odd = [x for (i, x) in enumerate(v) if i % 2 == 1]
even = [x for (i, x) in enumerate(v) if i % 2 == 0]

odd_c = Counter(odd)
even_c = Counter(even)

odd_c = sorted(odd_c.items(), key=lambda x: x[1], reverse=True) + [(-1, 0)]
even_c = sorted(even_c.items(), key=lambda x: x[1], reverse=True) + [(-1, 0)]

if odd_c[0][0] != even_c[0][0]:
    print(n - odd_c[0][1] - even_c[0][1])
elif odd_c[0][1] + even_c[1][1] >= odd_c[1][1] + even_c[0][1]:
    print(n - odd_c[0][1] - even_c[1][1])
else:
    print(n - odd_c[1][1] - even_c[0][1])

Submission status: Accepted

Solution code:
import sys
n = int(raw_input())
nums = list(map(int,raw_input().split()))
cnt1 = [0 for x in range(100001)]
cnt2 = [0 for x in range(100001)]
for i in range(n):
    if i%2:
        cnt1[nums[i]] += 1
    else:
        cnt2[nums[i]] += 1
a1 = -1
a2 = -1
n1 = 0
n2 = 0
b1 = -1
b2 = -1
m1 = 0
m2 = 0
for i in range(100001):
    if cnt1[i]>a1:
        a2 = a1
        a1 = cnt1[i]
        n2 = n1
        n1 = i
    elif cnt1[i]>a2:
        a2 = cnt1[i]
        n2 = i
    if cnt2[i]>b1:
        b2 = b1
        b1 = cnt2[i]
        m2 = m1
        m1 = i
    elif cnt2[i]>b2:
        b2 = cnt2[i]
        m2 = i
if m1!=n1:
    print n-a1-b1
else:
    if a2+b1<a1+b2:
        print n-a1-b2
    else:
        print n-a2-b1

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
mae = []
usi = []
for i in range(0,n,2):
  mae.append(a[i])
for i in range(1,n,2):
  usi.append(a[i])
mae = collections.Counter(mae)
usi = collections.Counter(usi)
mae = mae.most_common()
usi = usi.most_common()
if mae[0][0] != usi[0][0]:
  print(n-mae[0][1]-usi[0][1])
else:
  try:
    t = mae[0][1] + usi[1][1]
  except:
    t = 0
  try:
    s = mae[1][1] + usi[0][1]
  except:
    s = 0
  u = mae[0][1]
  print(n-max(t,s,u))

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

n = read()
v = reads()

evens = sorted(((v, k) for k, v in Counter(v[::2]).items()), reverse=True) + [(0, -1)]
odds = sorted(((v, k) for k, v in Counter(v[1::2]).items()), reverse=True) + [(0, -1)]

if evens[0][1] != odds[0][1]:
  ans = n - evens[0][0] - odds[0][0]
else:
  ans = n - max(evens[0][0] + odds[1][0], evens[1][0] + odds[0][0])

print(ans)

Submission status: Accepted

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

ctr = Counter(src)
if len(ctr) == 1:
    print(N//2)
    exit()

odd = Counter([a for i,a in enumerate(src) if i%2])
even = Counter([a for i,a in enumerate(src) if i%2==0])

ev = even.most_common()[:2]
ans = N
for k,v in odd.most_common():
    k1,v1 = ev[0]
    if k != k1:
        ans = min(ans, N-v1-v)
    if len(ev) < 2: continue
    k2,v2 = ev[1]
    if k != k2:
        ans = min(ans, N-v2-v)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=input().split()

b=Counter(a[::2])
c=Counter(a[1::2])

bn=0
b1=('0',0)
b2=('0',0)
c1=('0',0)
c2=('0',0)
for i in b.most_common():
  if bn==0:
    b1=i
  if bn==1:
    b2=i
  bn+=1
cn=0  
for i in c.most_common():
  if cn==0:
    c1=i
  if cn==1:
    c2=i
  cn+=1
if b1[0]!=c1[0]:
  print(n-b1[1]-c1[1])
elif b1[1]+c2[1]>c1[1]+b2[1]:
  print(n-b1[1]-c2[1])
else:
  print(n-b2[1]-c1[1])

Submission status: Accepted

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

last = 0

odd = [0 for _ in range(10**5+1)]
even = [0 for _ in range(10**5+1)]

even_max = 0
odd_max = 0
even_max_index = 0
odd_max_index = 0
for i in range(N):
    if i%2 == 0:
        even[v[i]] += 1
        if even[v[i]] > even_max:
            even_max = even[v[i]]
            even_max_index = v[i]
    else:
        odd[v[i]] += 1
        if odd[v[i]] > odd_max:
            odd_max = odd[v[i]]
            odd_max_index = v[i]

ans = even_max + odd_max
if even_max_index == odd_max_index:
    even[even_max_index] = 0
    odd[odd_max_index] = 0
    ans = max(max(even)+odd_max, max(odd)+even_max)

print(N-ans)

Submission status: Accepted

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

from collections import Counter

odd = Counter(A[::2])
even = Counter(A[1::2])

odd = sorted(((c,v) for v,c in odd.items()), reverse=True)
even = sorted(((c,v) for v,c in even.items()), reverse=True)

odd.append((0,None))
even.append((0,None))

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

Submission status: Accepted

Solution code:
from collections import Counter
import sys
N=int(input())
v=[int(i) for i in input().split()]
A=[]
B=[]
for i in range(N):
    if i%2==0:
        A.append(v[i])
    else:
        B.append(v[i])
S=Counter(A).most_common(2)
T=Counter(B).most_common(2)
if S[0][0]!=T[0][0]:
    print(N-S[0][1]-T[0][1])
    sys.exit()
if len(S)==1 and len(T)==1:
    print(N-S[0][1])
    sys.exit()
if len(S)==1:
    print(N-S[0][1]-T[1][1])
    sys.exit()
if(len(T)==1):
    print(N-S[1][1]-T[0][1])
    sys.exit()    

a=S[0][1]+T[1][1]
b=S[1][1]+T[0][1]
c=max(a,b)
print(N-c)