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:
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    V = string_to_int(inputs[0])
    N = len(V)
    N_HALF = N // 2
    counts = {
        0: {},
        1: {}
    }
    for i, v in enumerate(V):
        key = i % 2
        if v in counts[key]:
            counts[key][v] += 1
        else:
            counts[key][v] = 1

    counts_sorted = []
    for i in range(2):
        counts_sorted.append(
            sorted(list(counts[i].items()), key=lambda x: x[1], reverse=True)
        )

    def count_replace(first, first_i, second, second_i):
        replace_count = N_HALF - counts_sorted[first][first_i][1]
        replace_count += N_HALF - counts_sorted[second][second_i][1]
        return replace_count

    if counts_sorted[0][0] == counts_sorted[1][0]:
        len_0 = len(counts_sorted[0])
        len_1 = len(counts_sorted[1])
        if len_0 == 1 and len_1 == 1:
            return N_HALF
        elif len_0 == 1:
            return N_HALF - counts_sorted[1][1][1]
        elif len_1 == 1:
            return N_HALF - counts_sorted[0][1][1]
        else:
            cs = []
            cs.append(count_replace(0, 0, 1, 1))
            cs.append(count_replace(0, 1, 1, 0))
            return min(cs)

    else:
        return count_replace(0, 0, 1, 0)


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Accepted

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

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

lst_odd = []
lst_eve = []

for i in range(n):
    if i%2 == 0:
        lst_odd.append(lst[i])
    else:
        lst_eve.append(lst[i])

cnt = Counter(lst_odd)
mod = cnt.most_common()
e1 = mod[0][0]
if len(mod) > 1:
    e2 = mod[1][0]
else:
    e2 = 0

cnt = Counter(lst_eve)
mod = cnt.most_common()
o1 = mod[0][0]
if len(mod) > 1:
    o2 = mod[1][0]
else:
    o2 = 0

if e1 != o1:
    ans = n - lst_odd.count(e1) - lst_eve.count(o1)
else:
    ans = n - max((lst_odd.count(e1) + lst_eve.count(o2)),(lst_odd.count(e2) + lst_eve.count(o1)))

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

v_odd = v[1::2]
v_even = v[0::2]

v_odd_mc = Counter(v_odd).most_common()
v_even_mc = Counter(v_even).most_common()

v_odd_mc.append((0, 0))
v_even_mc.append((0, 0))

if v_odd_mc[0] != v_even_mc[0]:
    print(n-v_odd_mc[0][1]-v_even_mc[0][1])
else:
    print(n - max(v_odd_mc[0][1] + v_even_mc[1][1], v_odd_mc[1][1]+v_even_mc[0][1]))
    # print(min(n - v_odd_mc[0][1] - v_even_mc[1][1], n - v_odd_mc[1][1] - v_even_mc[0][1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
w=input().split()
a=Counter(w[::2]).most_common(2)
b=Counter(w[1::2]).most_common(2)
if a[0][0]!=b[0][0]:
	stay=a[0][1]+b[0][1]
else:
	if a[0][1]==b[0][1]==n//2:
		stay=a[0][1]
	elif a[0][1]>b[0][1]:
		stay=a[0][1]+b[1][1]
	elif a[0][1]<b[0][1]:
		stay=a[1][1]+b[0][1]
	else:
		if a[1][1]>=b[1][1]:
			stay=a[1][1]+b[0][1]
		else:
			stay=a[0][1]+b[1][1]
print(n-stay)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v_odd,v_eve = v[0::2],v[1::2]
lvo,lve = len(v_odd),len(v_eve)

v_odd_s = {}
for c in v_odd:v_odd_s[c] = v_odd_s.get(c,0) + 1
v_odd_l = []
for c in v_odd_s.keys():v_odd_l.append([c,v_odd_s[c]])
sorted_v_odd_l = sorted(v_odd_l, key=lambda x: x[1],reverse = True)

v_eve_s = {}
for c in v_eve:v_eve_s[c] = v_eve_s.get(c,0) + 1
v_eve_l = []
for c in v_eve_s.keys():v_eve_l.append([c,v_eve_s[c]])
sorted_v_eve_l = sorted(v_eve_l, key=lambda x: x[1],reverse = True)

if (len(sorted_v_odd_l) == 1) or (len(sorted_v_eve_l) == 1):
    if (len(sorted_v_odd_l) == 1) and (len(sorted_v_eve_l) == 1):
        if sorted_v_odd_l[0][0] != sorted_v_eve_l[0][0]:print(0)
        else:print(n//2)
    else:
        if len(sorted_v_odd_l) == 1:
            if sorted_v_odd_l[0][0] != sorted_v_eve_l[0][0]:print(n//2-sorted_v_eve_l[0][1])
            else:print(0 + n//2-sorted_v_eve_l[1][1])
        else:
            if sorted_v_odd_l[0][0] != sorted_v_eve_l[0][0]:print(n//2-sorted_v_odd_l[0][1]) 
            else:print(0 + n//2-sorted_v_odd_l[1][1])
else:
    if sorted_v_odd_l[0] == sorted_v_eve_l[0]:
        p1 = lvo + lve - sorted_v_eve_l[0][1] - sorted_v_odd_l[1][1]
        p2 = lvo + lve - sorted_v_eve_l[1][1] - sorted_v_odd_l[0][1]
        p3 = p4 = -1
    else:
        p1 = lvo + lve - sorted_v_eve_l[0][1] - sorted_v_odd_l[0][1]
        p2 = lvo + lve - sorted_v_eve_l[1][1] - sorted_v_odd_l[0][1]
        p3 = lvo + lve - sorted_v_eve_l[0][1] - sorted_v_odd_l[1][1]
        p4 = lvo + lve - sorted_v_eve_l[1][1] - sorted_v_odd_l[1][1]
    for i in sorted([p1,p2,p3,p4]):
        if i>=0:
            print(i)
            break

Submission status: Accepted

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

num = 100001
#num = 10

even = [[0,i] for i in range(num)]
odd = [[0,i] for i in range(num)]

for i in range(n):
    if i%2 == 0:
        even[v[i]][0] += 1
    else:
        odd[v[i]][0] += 1
        
odd.sort(key=lambda x: -x[0])
even.sort(key=lambda x: -x[0])

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

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = {}
for i in range(0, n, 2):
    v1.setdefault(v[i], 0)
    v1[v[i]] += 1
v2 = {}
for i in range(1, n, 2):
    v2.setdefault(v[i], 0)
    v2[v[i]] += 1
v1 = [(i, j) for i, j in v1.items()]
v2 = [(i, j) for i, j in v2.items()]
v1.sort(key = lambda x: x[1])
v2.sort(key = lambda x: x[1])
if v1[-1][0] != v2[-1][0]:
    print(n - v1[-1][1] - v2[-1][1])
else:
    if len(v1) == 1:
        if len(v2) == 1:
            print(n // 2)
        else:
            print(n - v1[-1][1] - v2[-2][1])
    elif len(v2) == 1:
        print(n - v1[-2][1] - v2[-1][1])
    else:
        print(min(n - v1[-1][1] - v2[-2][1], (n - v1[-2][1] - v2[-1][1])))

Submission status: Accepted

Solution code:
import collections
def main():


    N = int(input())
    V = list(map(int,input().split()))
    V1 = V[0::2]
    V2 = V[1::2]
    count = 0

    fre1 = collections.Counter(V1).most_common(2)
    fre2 = collections.Counter(V2).most_common(2)
    

    if len(list(set(V)))==1:
        print(int(N/2))
        exit()


    if fre1[0][0] != fre2[0][0]:

        count += len(V1)-fre1[0][1]
        count += len(V2)-fre2[0][1]
        print(count)
    elif len(V1)-fre1[0][1] == len(V2)-fre2[0][1]:
        bigger = max(fre1[1][1],fre2[1][1])
        count += len(V1)-bigger
        count += len(V1)-fre1[0][1]
        print(count)
    else:
        if len(V1)-fre1[0][1] < len(V2)-fre2[0][1]:
            count += len(V1)-fre1[0][1]
            count += len(V2)-fre2[1][1]
            print(count)
        else:
            count += len(V1)-fre1[1][1]
            count += len(V2)-fre2[0][1]
            print(count)

        






if __name__== "__main__":
    main()

Submission status: Accepted

Solution code:
# C
import collections
n = int(input())
lst = list(map(int, input().split()))
lst1 = lst[::2]
lst2 = lst[1::2]
dct1 = sorted(dict(collections.Counter(lst1)).items(), key=lambda x:-x[1])
dct2 = sorted(dict(collections.Counter(lst2)).items(), key=lambda x:-x[1])


ans = 0
if (len(dct1) == 1 and len(dct2) == 1):
	if (dct1[0][0] == dct2[0][0]):
		ans = dct1[0][1]
	else:
		ans = 0
elif (len(dct1) == 1 and len(dct2) > 1):
	if (dct1[0][0] == dct2[0][0]):
		ans = n // 2 - dct2[1][1]
	else:
		ans = n // 2 - dct2[0][1]
elif (len(dct2) == 1 and len(dct1) > 1):
	if (dct1[0][0] == dct2[0][0]):
		ans = n // 2 - dct1[1][1]
	else:
		ans = n // 2 - dct1[0][1]
elif (len(dct1) > 1 and len(dct2) > 1):
	if (dct1[0][0] == dct2[0][0]):
		ans = n - max(dct1[0][1] + dct2[1][1], dct1[1][1] + dct2[0][1])
	else:
		ans = n - dct1[0][1] - dct2[0][1]
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])

    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:
n = int(input())
v = list(map(int,input().split()))

set_even = set(v[0::2]) 
set_odd = set(v[1::2]) 
dic_even={i:0 for i in set_even}
dic_odd={i:0 for i in set_odd}

for i in range(n):
  if i%2==0:
    dic_even[v[i]]+=1
  else:
    dic_odd[v[i]]+=1
    
even = sorted(dic_even.items(),key = lambda x: x[1],reverse=True)
odd = sorted(dic_odd.items(),key = lambda x: x[1],reverse=True)
ans = 0

if len(even)==1 and len(odd)==1:
  if even[0][0] == odd[0][0]:
    ans = even[0][1]
  else:
    ans = 0
elif len(even)==1 and len(odd)>1:
  if even[0][0] == odd[0][0]:
    ans = n//2-odd[1][1]
  else:
    ans = n//2-odd[0][1]
elif len(even)>1 and len(even)==1:
  if even[0][0] == odd[0][0]:
    ans = n//2 -even[1][1]
  else:
    ans = n//2 -even[0][1]
else:
  if even[0][0] == odd[0][0]:    
    cdt_1 = even[0][1] + odd[1][1]
    cdt_2 = even[1][1] + odd[0][1]
    ans = n- max(cdt_1,cdt_2)
  else:
    ans = n - (even[0][1] + odd[0][1])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections


def solve(n: int, v: "List[int]"):
    o = v[1::2]
    e = v[::2]
    oc = collections.Counter(o).most_common()
    ec = collections.Counter(e).most_common()
    if len(oc) > 1:
        om1, om2 = oc[0], oc[1]
    else:
        om1, om2 = oc[0], (-1, 0)
    if len(ec) > 1:
        em1, em2 = ec[0], ec[1]
    else:
        em1, em2 = ec[0], (-1, 0)
    if om1[0] != em1[0]:
        print(n - om1[1] - em1[1])
    else:
        print(min(n-om1[1]-em2[1], n-om2[1]-em1[1]))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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 Counter

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

v1 = v[0::2]
v2 = v[1::2]
c1 = Counter(v1).most_common()
c2 = Counter(v2).most_common()
c1.append((0, 0))
c2.append((0, 0))

if c1[0][0] != c2[0][0]:
    ans = n // 2 - c1[0][1] + n // 2 - c2[0][1]
else:
    ans = min(n // 2 - c1[0][1] + n // 2 - c2[1][1], n // 2 - c1[1][1] + n // 2 - c2[0][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
v1 = v[::2]
v2 = v[1::2]
v1_N = Counter(v1).most_common()
v2_N = Counter(v2).most_common()
if len(v1_N) == 1 and len(v2_N) == 1 and v1_N[0][0] == v2_N[0][0]:
    print(int(n/2))
elif v1_N[0] == v2_N[0]:
    print(min(n-v1_N[0][1]-v2_N[1][1],n-v1_N[1][1]-v2_N[0][1]))
else:
    print(n-v1_N[0][1]-v2_N[0][1])

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//2):
	odd.append(v[2*i+1])
	even.append(v[2*i])

odd = sorted(odd)
even = sorted(even)
ac = Counter(odd).most_common(2)
bc = Counter(even).most_common(2)
def cnt(X):
	count = 0
	ans = 0
	for i in range(len(X)-1):
		if X[i+1] == X[i]:
			count += 1
			ans = max(ans,count)
		else:
			count = 0
	return ans+1
            
if ac[0][0] != bc[0][0]:
  print(len(v)-cnt(odd)-cnt(even))
elif len(ac) == 1 and len(bc) == 1:
	print(n//2)
else:
	print(n-max(ac[0][1]+bc[1][1],ac[1][1]+bc[0][1]))

Submission status: Accepted

Solution code:
import collections

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

odd_number = [i for index, i in enumerate(v) if index % 2 != 0]
even_number = [i for index, i in enumerate(v) if index % 2 == 0]

odd_count = collections.Counter(odd_number).most_common()
even_count = collections.Counter(even_number).most_common()
odd_count.append((10**6, 0))
even_count.append((10**6, 0))

if odd_count[0][0] != even_count[0][0]:
  answer = [odd_count[0][0], even_count[0][0]]
else: 
  answer = [odd_count[1][0], even_count[0][0]] if odd_count[1][1] + even_count[0][1] > odd_count[0][1] + even_count[1][1] else [odd_count[0][0], even_count[1][0]]

print(len([i for i in odd_number if i != answer[0]]) + len([i for i in even_number if i != answer[1]]))

Submission status: Accepted

Solution code:
# 2019/07/24

from collections import Counter

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

e=Counter(even).most_common(2)
if len(e)<2:e=[e[0][0]]
else:e=list([e[0][0]]+[e[1][0]])
o=Counter(odd).most_common(2)
if len(o)<2:o=[o[0][0]]
else:o=list([o[0][0]]+[o[1][0]])

ans=float('inf')
for i in e:
    for j in o:
        cnt=0
        if i==j:continue
        for k in range(n):
            if k%2==0 and v[k]!=i:
                cnt+=1
            elif k%2==1 and v[k]!=j:
                cnt+=1
        ans=min(cnt,ans)

if ans==float('inf'):ans=n//2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
arr = list(map(int,input().split()))
d1 = {}
d2 = {}

for i in range(n//2):
    if arr[i*2] in d1:
        d1[arr[i*2]]+=1
    else:
        d1[arr[i*2]] = 1
    if arr[i*2+1] in d2:
        d2[arr[i*2+1]]+=1
    else:
        d2[arr[i*2+1]] = 1

a1 = [[-1,0]]
a2 = [[-1,0]]

for key in d1.keys():
    a1.append([key,d1[key]])
for key in d2.keys():
    a2.append([key,d2[key]])

a1.sort(key = lambda x:x[1])
a2.sort(key = lambda x:x[1])

if a1[-1][0] == a2[-1][0]:
    temp1 = n-a1[-1][1]-a2[-2][1]
    temp2 = n-a1[-2][1]-a2[-1][1]
    print(min(temp1,temp2))
else:
    print(n-a1[-1][1]-a2[-1][1])

Submission status: Accepted

Solution code:
import collections
n = input()

li = list(map(int, input().split()))
odd_li = li[::2]
even_li = li[1::2]
odd_counter = collections.Counter(odd_li)
even_counter = collections.Counter(even_li)

odd_max = max(odd_counter.values())
even_max = max(even_counter.values())
odd_max_value = [key for key, value in odd_counter.items() if value == odd_max]
even_max_value = [key for key, value in even_counter.items() if value == even_max]

if len(odd_max_value) == 1 and odd_max_value == even_max_value:
    odd_counter.pop(odd_max_value[0])
    even_counter.pop(even_max_value[0])
    if len(odd_counter) == 0 and len(even_counter) == 0:
        print(len(odd_li)*2 - odd_max)
    elif len(odd_counter) == 0:
        print(len(odd_li)*2 - odd_max - max(even_counter.values()))
    elif len(even_counter) == 0:
        print(len(odd_li)*2 - odd_max - max(odd_counter.values()))
    else:
        second_max = max(max(odd_counter.values()),max(even_counter.values()) )
        print(len(odd_li)*2 - odd_max - second_max)
else:
    print(len(odd_li)*2 - odd_max - even_max)

Submission status: Accepted

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

v_even = []
v_odd = []

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

even_list = [0]*max(v_even)
odd_list = [0]*max(v_odd)
for j in range(len(v_even)):
    even_list[v_even[j]-1] += 1
    odd_list[v_odd[j]-1] += 1

even_list_sorted = sorted(even_list)
odd_list_sorted = sorted(odd_list)

if even_list.index(even_list_sorted[-1]) != odd_list.index(odd_list_sorted[-1]):
    ans = n - even_list_sorted[-1] - odd_list_sorted[-1]
elif len(even_list) == 1:
    ans = n // 2
elif even_list_sorted[-2] >= odd_list_sorted[-2]:
    ans = n - even_list_sorted[-2] - odd_list_sorted[-1]
else:
    ans = n - even_list_sorted[-1] - odd_list_sorted[-2]
    
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

co = Counter([v[i] for i in range(0, len(v), 2)])
ce = Counter([v[i] for i in range(1, len(v), 2)])
co1 = co.most_common()[0]
ce1 = ce.most_common()[0]
ans = n
if co1[0] == ce1[0]:
    if len(co) > 1:
        co2 = co.most_common()[1]
        ans = min(ans, n - co2[1] - ce1[1])
    if len(ce) > 1:
        ce2 = ce.most_common()[1]
        ans = min(ans, n - co1[1] - ce2[1])
    if len(co) == 1 and len(ce) == 1:
        ans = min(ans, n - max(co1[1], ce1[1]))
else:
    ans = min(ans, n - co1[1] - ce1[1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, deque
n=int(input())
V=deque(list(map(int,input().split())))
if len(set(V))==1:print(n//2)
else:
    Odd=[]
    Even=[]
    for _ in range(n//2):
        Odd.append(V.popleft())
        Even.append(V.popleft())
    O=Counter(Odd)
    E=Counter(Even)
    if len(set(Odd))==1:print(n//2-E.most_common()[0][1])
    elif len(set(Even))==1:print(n//2-O.most_common()[0][1])
    elif O.most_common()[0][0]==E.most_common()[0][0]:print(min(n-E.most_common()[0][1]-O.most_common()[1][1],n-E.most_common()[1][1]-O.most_common()[0][1]))
    else:print(n-E.most_common()[0][1]-O.most_common()[0][1])

Submission status: Accepted

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

evens = v[::2]
odds = v[1::2]


def means(arr):
    d = dict()
    for e in arr:
        if d.get(e) is None:
            d[e] = 1
        else:
            d[e] += 1
    m = sorted(d.items(), key=lambda e: e[1], reverse=True)
    m1, m1_cnt = m[0]
    m2, m2_cnt = m[1] if len(m) > 1 else (None, None)
    return (m1, m1_cnt), (m2, m2_cnt)


(e_m1, e_m1_cnt), (e_m2, e_m2_cnt) = means(evens)
(o_m1, o_m1_cnt), (o_m2, o_m2_cnt) = means(odds)

if e_m1 != o_m1:
    print(n - e_m1_cnt - o_m1_cnt)
else:
    if o_m2 is None and e_m2 is None:
        print(n // 2)
    else:
        candidate1 = n - e_m1_cnt - o_m2_cnt if o_m2_cnt is not None else float('inf')
        candidate2 = n - e_m2_cnt - o_m1_cnt if e_m2_cnt is not None else float('inf')
        print(min(candidate1, candidate2))

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(map(int, input().split()))
v1=v[0:n:2]
v2=v[1:n:2]
v10=collections.Counter(v1)
v11=v10.most_common()
v20=collections.Counter(v2)
v21=v20.most_common()
if v11[0][0]==v21[0][0] and v11[0][1]==n//2 and v21[0][1]==n//2:
    print(n//2)
elif v11[0][0]==v21[0][0]:
    x1=n//2-v11[0][1]
    x2=n//2-v11[1][1]
    y1=n//2-v21[0][1]
    y2=n//2-v21[1][1]
    print(min(x1+y2,x2+y1))
    
else:
    x1=n//2-v11[0][1]
    y1=n//2-v21[0][1]
    print(x1+y1)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = int(input())
    *v, = map(int, input().split())

    v0 = Counter(v[::2]).most_common()
    v1 = Counter(v[1::2]).most_common()

    if len(v0) == 1 and len(v1) == 1:
        if v0[0][0] == v1[0][0]:
            print(n // 2)
        else:
            print(0)
        return

    l0 = min(2, len(v0))
    l1 = min(2, len(v1))
    INF = 10 ** 9
    score = [[n - v0[i][1] - v1[j][1] if v0[i][0] != v1[j][0]
              else INF for j in range(l1)] for i in range(l0)]
    print(min([min(s) for s in score]))


main()

Submission status: Accepted

Solution code:
import collections


def main():
    n = int(input())
    *v, = map(int, input().split())

    v0 = count(v[::2])
    v1 = count(v[1::2])

    if len(v0) == 1 and len(v1) == 1:
        if v0[0][0] == v1[0][0]:
            print(n // 2)
        else:
            print(0)
        return

    l0 = min(2, len(v0))
    l1 = min(2, len(v1))

    INF = 10 ** 9
    score = [[0] * l1 for _ in range(l0)]
    for i in range(l0):
        for j in range(l1):
            score[i][j] = sum_v(v0, i) + sum_v(v1, j)
            if i >= l0 or j >= l1 or v0[i][0] == v1[j][0]:
                score[i][j] = INF
    print(min([min(s) for s in score]))


def count(v):
    return collections.Counter(v).most_common()


def sum_v(v, ii):
    return sum([vv[1] for i, vv in enumerate(v) if i != ii])


main()

Submission status: Accepted

Solution code:
from collections import Counter

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

odd = v[::2]
even = v[1::2]

ac = Counter(odd).most_common(2)
bc = Counter(even).most_common(2)

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

Submission status: Accepted

Solution code:
from collections import Counter

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

odd = v[::2]
even = v[1::2]

ac = Counter(odd).most_common(2)
bc = Counter(even).most_common(2)

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

Submission status: Accepted

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

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

import collections as co
ki_c = co.Counter(ki)
gu_c = co.Counter(gu)


ki_m = ki_c.most_common()
gu_m = gu_c.most_common()

ki_m.append((0,0))
gu_m.append((0,0))

if ki_m[0][0] != gu_m[0][0]:
  ans = n - ki_m[0][1] - gu_m[0][1]
else:
  ans1 = n - ki_m[0][1] - gu_m[1][1]
  ans2 = n - ki_m[1][1] - gu_m[0][1]
  ans = min(ans1, ans2)
print(ans)

Submission status: Accepted

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

import collections as col

x_e=[]
x_o=[]

for i in range(n):
	if i%2==0:
		x_e.append(x[i])
	else:
		x_o.append(x[i])

x_e_c=col.Counter(x_e).most_common()    
x_o_c=col.Counter(x_o).most_common()

if x_e_c[0][1]>x_o_c[0][1]:
	fst=x_e_c
	snd=x_o_c
else:
	if x_e_c[0][1]<x_o_c[0][1]:	
		fst=x_o_c
		snd=x_e_c
	else:
		if len(x_e_c)>1 and len(x_o_c)>1:
			if x_e_c[1][1]<=x_o_c[1][1]:	
				fst=x_e_c
				snd=x_o_c
			else:
				fst=x_o_c
				snd=x_e_c
		else:
			fst=x_o_c
			snd=x_e_c

res=0

res+=n//2-fst[0][1]
if fst[0][0]!=snd[0][0]:
	res+=n//2-snd[0][1]
else:
	if len(snd)>1:
		res+=n//2-snd[1][1]
	else:
		res+=n//2

print(res)

Submission status: Accepted

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

    v0 = assemble(v[::2])
    v1 = assemble(v[1::2])

    if len(v0) == 1 and len(v1) == 1:
        if v0[0][0] == v1[0][0]:
            print(n // 2)
        else:
            print(0)
        return
    elif len(v0) == 1:
        if v0[0][0] == v1[0][0]:
            print(sum_v(v1, 1))
        else:
            print(sum_v(v1, 0))
        return
    elif len(v1) == 1:
        if v0[0][0] == v1[0][0]:
            print(sum_v(v0, 1))
        else:
            print(sum_v(v0, 0))
        return

    score = [[0, 0], [0, 0]]
    for i in range(2):
        for j in range(2):
            score[i][j] = sum_v(v0, i) + sum_v(v1, j)
            if v0[i][0] == v1[j][0]:
                score[i][j] = 10 ** 9
    print(min([min(s) for s in score]))


def assemble(v):
    dct = {}
    for vel in v:
        if vel in dct:
            dct[vel] += 1
        else:
            dct[vel] = 1
    return sorted(list(dct.items()), key=lambda x: x[1], reverse=True)


def sum_v(v, ii):
    return sum([vv[1] for i, vv in enumerate(v) if i != ii])


main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
num1=Counter(v[0::2])
num2=Counter(v[1::2])
num1sort=sorted(num1.items(),key=lambda x:x[1],reverse=True)
num2sort=sorted(num2.items(),key=lambda x:x[1],reverse=True)
if num1sort[0][0] == num2sort[0][0]:
    if (len(num1) == 1 ):
        if(len(num2) == 1):
            print(n//2)
        else:
            print(n//2-num2sort[1][1])
    elif (len(num2) == 1):
        print(n//2-num1sort[1][1])
    else:
        p1 = num1sort[0][1] + num2sort[1][1]
        p2 = num1sort[1][1] + num2sort[0][1]
        if p1 > p2:
            print(n-p1)
        else:
            print(n-p2)
else:
    print(n-num1sort[0][1]-num2sort[0][1])

Submission status: Accepted

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

odd = {}
even = {}

for idx in range(N):
  if idx%2 == 0:
    if V[idx] in even:
      even[V[idx]] += 1
    else:
      even[V[idx]] = 1
  else:
    if V[idx] in odd:
      odd[V[idx]] += 1
    else:
      odd[V[idx]] = 1
sorted_odd = sorted(odd.items(), key=lambda x: -x[1])
sorted_even = sorted(even.items(), key=lambda x: -x[1])
fs_odd = sorted_odd[0][0]
fs_even = sorted_even[0][0]
if fs_odd == fs_even:
  if sorted_odd[0][1] > sorted_even[0][1]:
    fs_even = sorted_even[1][0]
    print(N-odd[fs_odd]-even[fs_even])
  elif sorted_odd[0][1] < sorted_even[0][1]:
    fs_odd = sorted_odd[1][0]
    print(N-odd[fs_odd]-even[fs_even])
  else:
    if len(sorted_odd) == 1:
      print(N//2)
    else:
      if sorted_odd[1][1] > sorted_even[1][1]:
        fs_odd = sorted_odd[1][0]
        print(N-odd[fs_odd]-even[fs_even])
      else:
        fs_even = sorted_even[1][0]
        print(N-odd[fs_odd]-even[fs_even])
else:
  print(N-odd[fs_odd]-even[fs_even])

Submission status: Accepted

Solution code:
import collections

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

s = len(set(v))
if s == 1:
    print(n // 2)
    exit()

odd = v[::2]
even = v[1::2]
mo = collections.Counter(odd).most_common(2)
me = collections.Counter(even).most_common(2)

if mo[0][0] != me[0][0]:
    print(n - mo[0][1] - me[0][1])
elif len(mo) == 1:
    print(n - mo[0][1] - me[1][1])
elif len(me) == 1:
    print(n - mo[1][1] - me[0][1])
else:
    print(n - max(mo[0][1] + me[1][1], mo[1][1] + me[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
a = list(map(int,input().split()))
even = [[0,0]] + sorted([v,k] for k,v in Counter(a[1::2]).items())
odd = [[0,0]] + sorted([v,k] for k,v in Counter(a[0::2]).items())

if even[-1][1] != odd[-1][1]:
    print(N - even[-1][0] - odd[-1][0])
else:
    print(min(N - even[-1][0] - odd[-2][0], N - even[-2][0] - odd[-1][0]))

Submission status: Accepted

Solution code:
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])
v1.sort()
v2.sort()

v1num = []
v2num = []
cnt1 = []
cnt2 = [] 
for x in v1:
    if x not in v1num:
        v1num.append(x)
        cnt1.append(1)
    else:
        cnt1[-1] += 1
for x in v2:
    if x not in v2num:
        v2num.append(x)
        cnt2.append(1)
    else:
        cnt2[-1] += 1
#print(v1num,v2num,cnt1,cnt2)

res = 0
m1 = max(cnt1)
m2 = max(cnt2)
maxc1ind = cnt1.index(m1)
maxc2ind = cnt2.index(m2)
maxc1 = v1num[maxc1ind]
maxc2 = v2num[maxc2ind]
if maxc1 != maxc2:
    res = n - m1 - m2
else:
    if len(cnt1) == 1 and len(cnt2) == 1:
        res = n // 2
    else:    
        cnt1.sort(reverse=True)
        cnt2.sort(reverse=True)
        x1 = cnt1[0] + cnt2[1]
        x2 = cnt1[1] + cnt2[0]
        res = n - max(x1,x2)
print(res)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

def main():
    n = int(input())
    v = np.array(list(map(int, input().split())))

    len1 = n//2 + n&1
    len2 = n//2

    d1 = {0:0}
    d2 = {0:0}
    for i in range(0,n,2):
        if v[i] in d1:
            d1[v[i]] += 1
        else:
            d1[v[i]] = 1
    for i in range(1,n,2):
        if v[i] in d2:
            d2[v[i]] += 1
        else:
            d2[v[i]] = 1

    a = sorted(d1.items(), key = lambda x: (x[1], x[0]) , reverse=True)
    b = sorted(d2.items(), key = lambda x: (x[1], -x[0]), reverse=True)
    a1_key, a1_val = a[0]
    a2_key, a2_val = a[1]
    b1_key, b1_val = b[0]
    b2_key, b2_val = b[1]
    if a1_key != b1_key:
        print(n - a1_val - b1_val)
    else:
        print(min(n - a1_val - b2_val, n - a2_val - b1_val))



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def larger(x, y):
	if x > y:
		return x
	return y

N = int(input())
V = list(map(int, input().split()))
even = []
odd = []
for i in range(0, N):
	if i % 2 == 0:
		even.append(V[i])
	else:
		odd.append(V[i])
even.sort()
odd.sort()
evencount = [[even[0], 1]]
oddcount = [[odd[0], 1]]
for i in range(1, N//2):
	if even[i] != even[i-1]:
		evencount.append([even[i], 1])
	else:
		evencount[-1][1] += 1
	if odd[i] != odd[i-1]:
		oddcount.append([odd[i], 1])
	else:
		oddcount[-1][1] += 1
evencount.sort(key = lambda v: v[1], reverse = True)
oddcount.sort(key = lambda v: v[1], reverse = True)
ans = N - evencount[0][1] - oddcount[0][1]
if evencount[0][0] == oddcount[0][0]:
	if len(evencount) == 1 or len(oddcount) == 1:
		ans = N//2
	else:
		ans = N - larger(evencount[0][1]+oddcount[1][1], evencount[1][1]+oddcount[0][1])
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=input().split()
a={"-1":0}
for i in range(0,len(l),2):
    if l[i] in a:
        a[l[i]]+=1
    else:
        a[l[i]]=1
b={"-1":0}  
for i in range(1,len(l),2):
    if l[i] in b:
        b[l[i]]+=1
    else:
        b[l[i]]=1
a = sorted(a.items(), key=lambda x:-x[1])
b = sorted(b.items(), key=lambda x:-x[1])
if a[0][0]==b[0][0]:
    m=max(a[0][1]+b[1][1],a[1][1]+b[0][1])
else:
    m=a[0][1]+b[0][1]
print(n-m)