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:
'''input
4
1 1 1 1
'''
N = int(input())

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

lyg = {}
nel = {}

for i in range(N):
	if i & 1:
		if sk[i] in nel:
			nel[sk[i]] += 1
		else:
			nel[sk[i]] = 1
	else:
		if sk[i] in lyg:
			lyg[sk[i]] += 1
		else:
			lyg[sk[i]] = 1


nel = sorted(nel.items(), key=lambda kv: kv[1]);
lyg = sorted(lyg.items(), key=lambda kv: kv[1]);

nel.reverse();
lyg.reverse();

nel.append((-12, 0))
lyg.append((-12, 0))

if nel[0][0] != lyg[0][0]:
	ats = (N+1)//2 - nel[0][1] + N//2 - lyg[0][1];
else:
	if N <= 3:
		ats = 1
	else:
		ats = min((N+1)//2 - nel[0][1] + N//2 - lyg[1][1], (N+1)//2 - nel[1][1] + N//2 - lyg[0][1])

print(ats)

Submission status: Accepted

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

odd = {}
even = {}

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

#ソートして一番目、二番目を見る
odd_sorted  = sorted(odd.items(), key = lambda x : -x[1])
even_sorted  = sorted(even.items(), key = lambda x : -x[1])


def count_rewrite(odd, even):
    ans = 0
    for i in range(len(v)):
        if i % 2:
            if v[i] != odd:
                ans += 1
        else:
            if v[i] != even:
                ans += 1
    return ans


ans = 1000002
#候補が同じ場合
if odd_sorted[0][0] == even_sorted[0][0]:
    #奇数も偶数も2番目が存在する？
    if len(odd.keys()) > 1 and len(even.keys()) > 1:
        ans = min(ans, count_rewrite(odd_sorted[0][0], even_sorted[1][0]))
        ans = min(ans, count_rewrite(odd_sorted[1][0], even_sorted[0][0]))
    #お互いに二番目が存在しない(ただし数は必ず一致するのでどちらか)
    elif len(odd.keys()) == 1 and len(even.keys()) == 1:
        ans = min(ans, count_rewrite(-1, even_sorted[0][0]))
    #奇数に2番目が存在しない
    elif len(odd.keys()):
        ans = min(ans, count_rewrite(odd_sorted[0][0], even_sorted[1][0]))
    #偶数に2番目が存在しない
    else: #len(even.keys()):
        ans = min(ans, count_rewrite(odd_sorted[1][0], even_sorted[0][0]))

else:
    ans = min(ans, count_rewrite(odd_sorted[0][0], even_sorted[0][0]))


print(ans)

Submission status: Accepted

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

odd = {}
even = {}

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

#ソートして一番目、二番目を見る
odd_sorted  = sorted(odd.items(), key = lambda x : -x[1])
even_sorted  = sorted(even.items(), key = lambda x : -x[1])




#候補が同じ場合
if odd_sorted[0][0] == even_sorted[0][0]:
    #奇数も偶数も2番目が存在する？
    if len(odd.keys()) > 1 and len(even.keys()) > 1:
        #数が大きい方を採用
        if odd_sorted[0][1] > even_sorted[0][1]:
            ans_odd = odd_sorted[0][0]
            ans_even = even_sorted[1][0]
        elif odd_sorted[0][1] < even_sorted[0][1]:
            ans_odd = odd_sorted[1][0]
            ans_even = even_sorted[0][0]
        else:
            #両者が一致した場合
            if odd_sorted[1][1] >= even_sorted[1][1]:
                ans_odd = odd_sorted[1][0]
                ans_even = even_sorted[0][0]
            elif odd_sorted[1][1] < even_sorted[1][1]:
                ans_odd = odd_sorted[0][0]
                ans_even = even_sorted[1][0]

    #お互いに二番目が存在しない(ただし数は必ず一致するのでどちらか)
    elif len(odd.keys()) == 1 and len(even.keys()) == 1:
        ans_odd = -1
        ans_even = even_sorted[0][0]
    #奇数に2番目が存在しない
    elif len(odd.keys()):
        ans_odd = odd_sorted[0][0]
        ans_even = even_sorted[1][0]
    #偶数に2番目が存在しない
    else: #len(even.keys()):
        ans_odd = odd_sorted[1][0]
        ans_even = even_sorted[0][0]

else:
    ans_odd = odd_sorted[0][0]
    ans_even = even_sorted[0][0]

ans = 0
for i in range(len(v)):
    if i % 2:
        if v[i] != ans_odd:
            ans += 1
    else:
        if v[i] != ans_even:
            ans += 1



print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import string
import sys
from collections import Counter
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    n = readi()
    v = readi(n)
    l1 = v[::2]
    l2 = v[1::2]

    mc1 = Counter(l1).most_common()
    mc2 = Counter(l2).most_common()
    if mc1[0][0] != mc2[0][0]:
        print((n // 2 - mc1[0][1]) + (n // 2 - mc2[0][1]))
    else:
        if len(mc1) > 1 and len(mc2) == 1:
            print((n // 2 - mc1[1][1]) + (n // 2 - mc2[0][1]))
        elif len(mc1) == 1 and len(mc2) > 1:
            print((n // 2 - mc1[0][1]) + (n // 2 - mc2[1][1]))
        elif len(mc1) > 1 and len(mc2) > 1:
            print(
                min(
                    (n // 2 - mc1[1][1]) + (n // 2 - mc2[0][1]),
                    (n // 2 - mc1[0][1]) + (n // 2 - mc2[1][1]),
                )
            )
        else:
            print(n // 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

dict1 = {}
dict2 = {}
flag = 1
for v in v_list:
  if flag > 0:
    if v in dict1:
      dict1[v] = dict1[v] + 1
    else:
      dict1[v] = 1
  else:
    if v in dict2:
      dict2[v] = dict2[v] + 1
    else:
      dict2[v] = 1
  flag = -1 * flag
list1 = list(dict1.items())
list2 = list(dict2.items())
list1_value = [t[0] for t in list1]
list1_num = [t[1] for t in list1]
list2_value = [t[0] for t in list2]
list2_num = [t[1] for t in list2]
list1_num_max = max(list1_num)
list2_num_max = max(list2_num)
if list1_value[list1_num.index(list1_num_max)] != list2_value[list2_num.index(list2_num_max)]:
  print(n - list1_num_max - list2_num_max)
else:
  ind1 = list1_num.index(list1_num_max)
  del list1_value[ind1]
  del list1_num[ind1]
  ind2 = list2_num.index(list2_num_max)
  del list2_value[ind2]
  del list2_num[ind2]
  list1_num_max_2nd = max(list1_num) if len(list1_num) > 0 else 0
  list2_num_max_2nd = max(list2_num) if len(list2_num) > 0 else 0
  if list1_num_max_2nd * list2_num_max_2nd > 0:
    print(min(n - list1_num_max - list2_num_max_2nd, 
             n- list2_num_max - list1_num_max_2nd))
  else:
    print(int(n/2))

Submission status: Accepted

Solution code:
N = int(input())
li=[int(it) for it in input().split()]
lia=li[::2]
lib=li[1::2]

import collections
ca = list(collections.Counter(lia).items())
cb = list(collections.Counter(lib).items())
na = len(lia)
nb = len(lib)
ca.sort(key=lambda x:-x[1])
cb.sort(key=lambda x:-x[1])
s = 0
if ( ca[0][0] != cb[0][0] ):
  s += na - ca[0][1]
  s += nb - cb[0][1]
else:
  if ( ca[0][1] > cb[0][1]   ):
    s += na - ca[0][1]
    if ( len(cb)>1 ):
      s += nb - cb[1][1]
    else:
      s += nb
  elif (ca[0][1] < cb[0][1]) :
    s += nb - cb[0][1]
    if ( len(ca)>1 ):
      s += na - ca[1][1]
    else:
      s += na
  else:
    p1 = cb[0][1]
    p2 = ca[0][1]
    if (len(ca)>1):
      p1+=ca[1][1]
    if (len(cb)>1):
      p2+=cb[1][1]
    s += na + nb - max(p1,p2)
    
print (s)

Submission status: Accepted

Solution code:
def basket_count(arr):
    basket = []
    tmp = arr[0]
    cnt_ele = 0
    for ele in arr:
        if ele == tmp:
            cnt_ele += 1
        else:
            basket.append([tmp, cnt_ele])
            # reset count for basket
            tmp = ele
            cnt_ele = 1
    basket.append([tmp, cnt_ele])
    return basket

def solve():
    n = int(raw_input())
    v = [int(_) for _ in raw_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()
    bask_v0 = basket_count(v0)
    bask_v0.sort(reverse = True, key = lambda x: x[1])
    bask_v1 = basket_count(v1)
    bask_v1.sort(reverse = True, key = lambda x: x[1])
    if bask_v0[0][0] != bask_v1[0][0]:
        ret = n - (bask_v0[0][1] + bask_v1[0][1])
    elif len(bask_v1) <= 1:
        if len(bask_v0) > 1:
            ret = n - (bask_v0[1][1] + bask_v1[0][1])
        else:
            ret = n - bask_v0[0][1]
    else: # len(bask_v1) > 1
        if len(bask_v0) <= 1:
            ret = n - (bask_v0[0][1] + bask_v1[1][1])
        else:
            ret = n - max(bask_v0[0][1] + bask_v1[1][1], \
                          bask_v0[1][1] + bask_v1[0][1])
    print str(ret)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

v_dict = [{} for _ in range(2)]
for i in range(n):
    if i % 2 == 0:
        if v[i] in v_dict[0]:
            v_dict[0][v[i]] += 1
        else:
            v_dict[0][v[i]] = 1
    else:
        if v[i] in v_dict[1]:
            v_dict[1][v[i]] += 1
        else:
            v_dict[1][v[i]] = 1

evens = sorted([(key, val) for key, val in v_dict[0].items()], key=lambda comb: comb[1])
odds = sorted([(key, val) for key, val in v_dict[1].items()], key=lambda comb: comb[1])

ans = 0
for _, val in evens[:-1]:
    ans += val
for _, val in odds[:-1]:
    ans += val
if evens[-1][0] == odds[-1][0]:
    if len(evens) > 1 and len(odds) > 1:
        if evens[-2][1] < odds[-2][1]:
            ans += odds[-1][1] - odds[-2][1]
        else:
            ans += evens[-1][1] - evens[-2][1]
    elif len(evens) > 1:
        ans += evens[-1][1] - evens[-2][1]
    elif len(odds) > 1:
        ans += odds[-1][1] - odds[-2][1]
    else:
        ans = n // 2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a1 = {'emp':0}
a2 = {'emp':0}

for i in range(0,n,2):
    # print(i)
    if str(a[i]) in a1.keys():
        a1[str(a[i])] += 1
    else :
        a1[str(a[i])] = 1
    if str(a[i+1]) in a2.keys():
        a2[str(a[i+1])] += 1
    else :
        a2[str(a[i+1])] = 1
# print(a1,a2)

a1 = sorted(a1.items(), key=lambda x: -x[1])
a2 = sorted(a2.items(), key=lambda x: -x[1])

# print(a1,a2)
if a1[0][0] == a2[0][0]:
    print(n-max(a1[0][1]+a2[1][1],a1[1][1]+a2[0][1]))
else :
    print(n-a1[0][1]-a2[0][1])

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
from collections import Counter
even=Counter(arr[::2])
even[0]=0
odd=Counter(arr[1::2])
odd[0]=0
even1,even2=even.most_common(2)
odd1,odd2=odd.most_common(2)
if even1[0]!=odd1[0]:
  print(n-(even1[1]+odd1[1]))
else:
  print(n-max(even1[1]+odd2[1],even2[1]+odd1[1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

# Return the list of 2 most common numbers and each appearance
e = Counter(v[::2]);e[0] = 0
o = Counter(v[1::2]);o[0] = 0

e1,e2 = e.most_common(2)
o1,o2 = o.most_common(2)

if e1[0] != o1[0]:
    print(n - (e1[1] + o1[1]))
else:
    print(n - max(e1[1]+o2[1],e2[1]+o1[1]))

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
V=[int(v) for v in input().split()]

M=max(V)
count_odd=np.array([0 for i in range(M+1)])
count_even=np.array([0 for i in range(M+1)])

for i in range(len(V)):
	if(i%2==0):
		count_even[V[i]]+=1
	if(i%2==1):
		count_odd[V[i]]+=1

if(np.argmax(count_odd)!=np.argmax(count_even)):
	print(n-max(count_odd)-max(count_even))
else:
	count_odd.sort()
	count_even.sort()
	print(min(n-count_odd[-1]-count_even[-2],n-count_odd[-2]-count_even[-1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
INF = float("inf")


def solve(n: int, v: "List[int]"):
    c0 = Counter(v[0::2])
    c1 = Counter(v[1::2])

    # print(c0)
    # print(c1)
    c0[0] = 0
    c1[0] = 0

    mc0 = c0.most_common(2)
    mc1 = c1.most_common(2)

    v0, v1 = -INF, -INF

    if mc0[0][0] == mc1[0][0]:
        if mc0[0][1] + mc1[1][1] < mc0[1][1] + mc1[0][1]:
            v0 = mc0[1][1]
            v1 = mc1[0][1]
        else:
            v0 = mc0[0][1]
            v1 = mc1[1][1]
    else:
        v0 = mc0[0][1]
        v1 = mc1[0][1]

    print(n - v0-v1)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    v = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    solve(n, v)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

v1, v2 = defaultdict(int), defaultdict(int)
for i, vv in enumerate(v):
    if i%2 == 0:
        v1[vv] += 1
    else:
        v2[vv] += 1
v1 = sorted([(c, vv) for vv, c in v1.items()], reverse = True)
v2 = sorted([(c, vv) for vv, c in v2.items()], reverse = True)

if v1[0][1] == v2[0][1]:
    if len(v1) == 1 and len(v2) == 1:
        ans = n//2
    elif v1[1][0] > v2[1][0]:
        ans = n - v1[1][0] - v2[0][0]
    else:
        ans = n - v1[0][0] - v2[1][0]
else:
    ans = n - v1[0][0] - v2[0][0]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

evens = defaultdict(int)
odds = defaultdict(int)

for i in range(n):
    if i % 2 == 0:
        evens[vs[i]] += 1
    else:
        odds[vs[i]] += 1

evens = sorted(list(evens.items()), key=lambda x: x[1], reverse=True)
evens.append((0, 0))
odds = sorted(list(odds.items()), key=lambda x: x[1], reverse=True)
odds.append((0, 0))

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
v_even = [ele for i, ele in enumerate(v) if i%2==0]
v_odd  = [ele for i, ele in enumerate(v) if i%2==1]
counter_even = Counter(v_even).most_common(2)
counter_odd  = Counter(v_odd).most_common(2)

if counter_even[0][0] != counter_odd[0][0]:
    print(n - counter_even[0][1] - counter_odd[0][1])
else:
    if len(counter_even) > 1 and len(counter_odd) > 1:
        if counter_even[0][1] > counter_odd[0][1]:
            print(n - counter_even[0][1] - counter_odd[1][1])
        elif counter_even[0][1] < counter_odd[0][1]:
            print(n - counter_odd[0][1] - counter_even)
        else:
            if counter_even[1][1] > counter_odd[1][1]:
                print(n - counter_odd[0][1] - counter_even[1][1])
            else:
                print(n - counter_even[0][1] - counter_odd[1][1])

    elif len(counter_even) == 1 and len(counter_odd) > 1:
        print(int(n/2) - counter_odd[1][1])
    elif len(counter_even) > 1 and len(counter_odd) == 1:
        print(int(n/2) - counter_even[1][1])
    else:
        print(int(n/2))

Submission status: Accepted

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

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


def search_mode(list):
    dict = {}
    mode = 0
    for l in list:
        if l in dict.keys():
            dict[l] += 1
            if mode < dict[l]:
                mode = dict[l]
        else:
            dict[l] = 1
    dict = sorted(dict.items(), key=lambda x: -x[1])
    return dict


even_dict = search_mode(even)
odd_dict = search_mode(odd)

if len(even_dict) == 1 and len(odd_dict) == 1:
    if even_dict[0][0] == odd_dict[0][0]:
        print(n // 2)
    else:
        print(0)
elif len(even_dict) == 1:
    if even_dict[0][0] == odd_dict[0][0]:
        print(len(odd) - odd_dict[1][1])
    else:
        print(len(odd) - odd_dict[0][1])
elif len(odd_dict) == 1:
    if odd_dict[0][0] == even_dict[0][0]:
        print(len(even) - even_dict[1][1])
    else:
        print(len(even) - even_dict[0][1])
elif even_dict[0][0] == odd_dict[0][0]:
    eo01 = n - even_dict[0][1] - odd_dict[1][1]
    eo10 = n - even_dict[1][1] - odd_dict[0][1]
    max_count = min(eo01, eo10)
    print(max_count)
else:
    print(n - even_dict[0][1] - odd_dict[0][1])

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = [int(i) for i in input().split()]
v_even = [v[2*i] for i in range(int(n/2))]
v_odd = [v[2*i-1] for i in range(int(n/2))]
c_even = collections.Counter(v_even)
c_odd = collections.Counter(v_odd)
if(c_even.most_common()[0][0]!=c_odd.most_common()[0][0]):
    print(n - c_even.most_common()[0][1] - c_odd.most_common()[0][1])
elif(len(collections.Counter(v))==1):
    print(int(n/2))
else:
    if(c_even.most_common()[0][1]>c_odd.most_common()[0][1]):
        print(n-c_even.most_common()[0][1] - c_odd.most_common()[1][1])
    elif(c_even.most_common()[0][1]<c_odd.most_common()[0][1]):
        print(n - c_odd.most_common()[0][1] - c_even.most_common()[1][1])
    else:
        print(n - c_odd.most_common()[0][1] - max(c_odd.most_common()[1][1],c_even.most_common()[1][1]))

Submission status: Accepted

Solution code:
import collections

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

c1 = collections.Counter(v[::2]).most_common()
c2 = collections.Counter(v[1::2]).most_common()

if c1[0][0] != c2[0][0]:
  ans = n - c1[0][1] - c2[0][1]
elif len(c1) >=2 and len(c2) >= 2:
  if c1[1][1] > c2[1][1]:
    ans = n - c1[1][1] - c2[0][1]
  else:
    ans = n - c1[0][1] - c2[1][1]
elif len(c1) >= 2 and len(c2) == 1:
  ans = n - c1[0][1]
elif len(c1) == 1 and len(c2) >= 2:
  ans = n - c2[0][1]
else:
  ans = n - c1[0][1]

print(ans)

Submission status: Accepted

Solution code:
def get_top2_dict(dict1):
  numlist=tuple(dict1.items())
  best=[(0,0),(0,0)]
  for x in numlist:
    if x[1]>=best[0][1]:
      best[1]=best[0]
      best[0]=x
    elif x[1]>=best[1][1]:
      best[1]=x
  return best

n=int(input())
numlist=[int(x) for x in input().split()]
even=0
odd=1
evenlist={}
oddlist={}
while even<n:
  try:
    evenlist[numlist[even]]
  except:
    evenlist[numlist[even]]=0
  evenlist[numlist[even]]+=1
  even+=2

while odd<n:
  try:
    oddlist[numlist[odd]]
  except:
    oddlist[numlist[odd]]=0
  oddlist[numlist[odd]]+=1
  odd+=2

besteven=get_top2_dict(evenlist)
bestodd=get_top2_dict(oddlist)

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

Submission status: Accepted

Solution code:
#coding: utf-8
from collections import Counter
N = int(input())
l = list(map(int,input().split()))
if len(set(l)) > 1:
  o, e = [], []
  for i in range(N):
    if i % 2==0:
      e.append(l[i])
    else:
      o.append(l[i])
  oc, oe = Counter(o).most_common(2), Counter(e).most_common(2)
  print(len(l) - oc[0][1] - oe[0][1] if oc[0][0] != oe[0][0] else len(l) - max(oc[0][1] + oe[1][1], oc[1][1] + oe[0][1]))

else:
  print(len(l) // 2)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
od = [0]
ev = [0]
for i in range(n):
    if i % 2 == 0:
        od.append(v[i])
    else:
        ev.append(v[i])
        
o1, o2 = collections.Counter(od).most_common(2)
e1, e2 = collections.Counter(ev).most_common(2)
if o1[0] == e1[0]:
    if o1[1] + e1[1] == n:
        print(n//2)
        exit()
    print(n - max(o1[1] + e2[1], o2[1] + e1[1]))
else:
    print(n - (o1[1] + e1[1]))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
e=Counter(a[::2]);e[0]=0
o=Counter(a[1::2]);o[0]=0
e1,e2=e.most_common(2)
o1,o2=o.most_common(2)
if e1[0]!=o1[0]:
    print(n-(e1[1]+o1[1]))
else:
    print(n-max(e1[1]+o2[1],e2[1]+o1[1]))

Submission status: Accepted

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
odd =Counter([v for k,v in enumerate(a) if k%2==0]).most_common()
even=Counter([v for k,v in enumerate(a) if k%2==1]).most_common()


if len(odd)==len(even)==1:
    if odd[0][0]==even[0][0]:
        print(len(a)//2)
    else:
        print(0)
elif odd[0][0]!=even[0][0]:
    print(sum(t for(s,t)in odd[1:])+sum(t for(s,t)in even[1:]))
else:
    a=odd[0][1]+sum(t for(s,t)in odd[2:])+sum(t for(s,t)in even[1:])
    b=even[0][1]+sum(t for(s,t)in even[2:])+sum(t for(s,t)in odd[1:])
    print(min(a,b))

Submission status: Accepted

Solution code:
""" AtCoder """

import collections

n = int(input())
v = list(map(int, input().split()))
a = []
b = []
for i in range(0, n, 2):
    a.append(v[i])
    b.append(v[i + 1])

aa = collections.Counter(a)
bb = collections.Counter(b)

ans = aa.most_common()[0][1] + bb.most_common()[0][1]

if aa.most_common()[0][0] == bb.most_common()[0][0]:
    if len(aa.most_common()) == 1:
        ans = aa.most_common()[0][1]
    elif len(bb.most_common()) == 1:
        ans = bb.most_common()[0][1]
    else:
        ans = max(aa.most_common()[1][1] + bb.most_common()[0][1], aa.most_common()[0][1] + bb.most_common()[1][1])

print(len(a) + len(b) - ans)

Submission status: Accepted

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

v_even = defaultdict(int)
v_odd = defaultdict(int)

for k in range(n//2):
  v_even[V[2*k]] += 1
  v_odd[V[2*k + 1]] += 1

even_max = 0
even_max2 = 0
for k, v in v_even.items():
  if v > even_max:
    even_max2 = even_max
    even_max = v
    even_num = k
  else:
    even_max2 = max(even_max2, v)

odd_max = 0
odd_max2 = 0
for k, v in v_odd.items():
  if v > odd_max:
    odd_max2 = odd_max
    odd_max = v
    odd_num = k
  else:
    odd_max2 = max(odd_max2, v)

if  even_num != odd_num:
  print(n - even_max - odd_max)
else:
  print(n - max(even_max + odd_max2, even_max2 + odd_max))

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
a=v[0::2]
b=v[1::2]
c=dict()
d=dict()

for i in a:
  if i in c:
    c[i]+=1
  else:
    c[i]=1
for j in b:
  if j in d:
    d[j]+=1
  else:
    d[j]=1

e=sorted([[w,v] for v,w in c.items()],reverse=True)
f=sorted([[w,v] for v,w in d.items()],reverse=True)

if len(set(a))==len(set(b))==1 and e[0][1]==f[0][1] and e[0][0]==f[0][0]:
  print(min(len(a),len(b)))
elif e[0][1]!=f[0][1]:
  print(n-e[0][0]-f[0][0])
elif e[0][0]>f[0][0]:
  print(n-e[0][0]-f[1][0])
elif e[0][0]<f[0][0]:
  print(n-e[1][0]-f[0][0])
elif e[0][0]==f[0][0] and e[0][1]==f[0][1]:
  if e[1][0]>f[1][0]:
    print(n-e[1][0]-f[0][0])
  else:
    print(n-e[0][0]-f[1][0])

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    n = I()
    v = LI()
    d = [defaultdict(int) for i in range(2)]
    for i in range(n):
        d[i%2][v[i]] += 1
    if len(d[0].keys()) == len(d[1].keys()) == 1:
        if d[0].keys() == d[1].keys():
            ans = n//2
        else:
            d = [list(d[i].values()) for i in range(2)]
            ans = sum(d[0])-max(d[0])+sum(d[1])-max(d[1])
    else:
        d = [sorted(list(d[i].items()),key = lambda x:-x[1]) for i in range(2)]
        if d[0][0][0] != d[1][0][0]:
            ans = -d[0][0][1]-d[1][0][1]
            for i in range(len(d[0])):
                ans += d[0][i][1]
            for i in range(len(d[1])):
                ans += d[1][i][1]
        else:
            ans = -d[0][0][1]-d[1][1][1]
            for i in range(len(d[0])):
                ans += d[0][i][1]
            for i in range(len(d[1])):
                ans += d[1][i][1]
            ans2 = -d[0][1][1]-d[1][0][1]
            for i in range(len(d[0])):
                ans2 += d[0][i][1]
            for i in range(len(d[1])):
                ans2 += d[1][i][1]
            ans = min(ans,ans2)
    print(ans)
#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    n = II()
    v = LI()
    d1 = defaultdict(int)
    d0 = defaultdict(int)
    for num, a in enumerate(v):
        if num % 2:
            d1[a] += 1
        else:
            d0[a] += 1
    A = list(d0.items())
    A.sort(key = lambda x: x[1], reverse = True)
    A = A[:2]
    A.append((0,0))
    B = list(d1.items())
    B.sort(key = lambda x: x[1], reverse = True)
    B = B[:2]
    B.append((0, 0))
    a = n // 2
    b = (n + 1) // 2
    if A[0][0] == B[0][0]:
        ans = min(a - A[0][1] + b - B[1][1], a - A[1][1] + b - B[0][1])
        print(ans)
    else:
        #print(A,B)
        A = A[0][1]
        B = B[0][1]
        print(a+b-A-B)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

Solution code:
from collections import Counter

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

if len(set(odd)) == len(set(even)) == 1 and set(odd) == set(even):
    print(n//2)
else:
    o = Counter(odd)
    e = Counter(even)
    mode_odd = o.most_common(2)
    mode_even = e.most_common(2)
    if mode_odd[0][0] != mode_even[0][0]:
        cnt = n - mode_odd[0][1] - mode_even[0][1]
    else:
        if mode_odd[0][1] == mode_even[0][1]:
            cnt = n - max(mode_odd[1][1], mode_even[1][1]) - mode_odd[0][1]
        elif mode_odd[0][1] > mode_even[0][1]:
            cnt = n - mode_odd[0][1] - mode_even[1][1]
        else:
            cnt = n - mode_even[0][1] - mode_odd[1][1]
    print(cnt)

Submission status: Accepted

Solution code:
def read():
    return raw_input()


def write(message):
    import sys
    print message
    sys.stdout.flush()


if __name__ == '__main__':
    n = int(read())
    v = map(int, read().split())
    from collections import Counter
    counts_odd = Counter(v[::2])
    counts_even = Counter(v[1::2])
    co = counts_odd.most_common(2) + [(0, 0)]
    ce = counts_even.most_common(2) + [(0, 0)]

    ok = None
    if co[0][0] == ce[0][0]:
        ok = max(co[0][1] + ce[1][1], co[1][1] + ce[0][1])
    else:
        ok = co[0][1] + ce[0][1]
    write(n - ok)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
A = [int(x) for x in input().split()]
even = A[0::2]
odd = A[1::2]
EC = defaultdict(int)
EC[0] = 0
OC = defaultdict(int)
OC[0] = 0
for e in even:
  EC[e] += 1
for o in odd:
  OC[o] += 1
E = sorted([[v, k] for k, v in EC.items()], reverse=True)
O = sorted([[v, k] for k, v in OC.items()], reverse=True)
if E[0][1] != O[0][1]:
  print(n - E[0][0] - O[0][0])
elif E[1][0] >= O[1][0]:
  print(n - E[1][0] - O[0][0])
else:
  print(n - E[0][0] - O[1][0])

#sample code.

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
el = [int(i) for i in input().split()]
odd_l = Counter(el[::2])
even_l = Counter(el[1::2])
a1, b1 = odd_l.most_common(1)[0]
a2, b2 = even_l.most_common(1)[0]
if a1 != a2:
    ans = n - b1 - b2
else:
    if len(odd_l) > 1:
        b3 = odd_l.most_common(2)[1][1]
    else:
        b3 = 0
    if len(even_l) > 1:
        b4 = even_l.most_common(2)[1][1]
    else:
        b4 = 0
    ans = min(n - b1 - b4, n - b3 - b2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
element = [int(i) for i in input().split()]
ans = 0
odd_l = Counter(element[::2])
even_l = Counter(element[1::2])
a1,b1= odd_l.most_common(1)[0]
a2,b2 = even_l.most_common(1)[0]
if a1 != a2:
    ans = n - b1 - b2
else:
    if len(odd_l) > 1:
        b3 = odd_l.most_common(2)[1][1]
    else:
        b3=0
    if len(even_l) > 1:
        b4 = even_l.most_common(2)[1][1]
    else:
        b4 = 0
    ans = min(n-b1-b4,n-b3-b2)
print(ans)