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: Time Limit Exceeded

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

result = []
for i in range(len(v)):
    count = 0
    a,b = v[i],v[(i+1)%n]
    if a == b:
        b = -1
    for j in range(i,i+len(v),2):
        if v[j%n] != a:
            count += 1
        if v[(j+1)%n] != b:
            count += 1
    result.append(count)

print(min(result))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v_e = [v[2 * i] for i in range(n // 2)]
v_o = [v[2 * i + 1] for i in range(n // 2)]
N = n/2
min1 = [N,N]
int1 = [-1,-1]
min2 = [N,N]
int2 = [-1,-1]
for i in set(v_e):
    tmp = N - v_e.count(i)
    if tmp < min1[0]:
        min1[1] = min1[0]
        int1[1] = int1[0]
        min1[0] = tmp
        int1[0] = i
    elif tmp < min1[1]:
        min1[1] = tmp
        int1[1] = i
for i in set(v_o):
    tmp = N - v_o.count(i)
    if tmp < min2[0]:
        min2[1] = min2[0]
        int2[1] = int2[0]
        min2[0] = tmp
        int2[0] = i
    elif tmp < min2[1]:
        min2[1] = tmp
        int2[1] = i
if int1[0] == int2[0]:
    print(int(min(min1[0]+min2[1],min1[1]+min2[0])))
else:
    print(int(min1[0]+min2[0]))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np


def get_histogram(arr):
    count_dic = {}
    appeared = []
    for i in arr:
        if i in appeared:
            count_dic[i] += 1
        else:
            appeared.append(i)
            count_dic[i] = 1
    return sorted(count_dic.items(), key=lambda x: x[1], reverse=True)


def count_replace(histogram, target):
    count = 0
    if target >= len(histogram):
        for h in histogram:
            count += h[1]
        return count
    for i, h in enumerate(histogram):
        if i != target:
            count += h[1]
    return count


def main(argv):
    n = int(input())
    nums_input = input()
    nums = list(map(lambda x: int(x), nums_input.split(' ')))

    hist_odd = get_histogram(nums[::2])
    hist_even = get_histogram(nums[1::2])

    if hist_even[0][0] != hist_odd[0][0]:
        print(count_replace(hist_even, 0) + count_replace(hist_odd, 0))
    else:
        if count_replace(hist_even, 1) > count_replace(hist_odd, 1):
            print(count_replace(hist_even, 0) + count_replace(hist_odd, 1))
        else:
            print(count_replace(hist_even, 1) + count_replace(hist_odd, 0))


if __name__ == "__main__":
    main(sys.argv[:1])

Submission status: Time Limit Exceeded

Solution code:
import sys


def get_histogram(arr):
    count = [[i, 0] for i in range(100000)]
    appeared = []
    for i in arr:
        count[i][1] += 1
        if not i in appeared:
            appeared.append(i)
    histogram = sorted(count, key=lambda x: x[1], reverse=True)
    return histogram  # [:len(appeared)]


def count_not_majority(historgram):
    count = 0
    for hist in historgram[1:]:
        count += hist[1]
    return count


def main(argv):
    n = int(input())
    nums_input = input()
    nums = list(map(lambda x: int(x), nums_input.split(' ')))

    hist_odd = get_histogram(nums[::2])
    count_odd = sum([hist[1] for hist in hist_odd])
    hist_even = get_histogram(nums[1::2])
    count_even = sum([hist[1] for hist in hist_even])

    # print(hist_even[:5], count_even, hist_odd[:5], count_odd)

    if hist_even[0][0] != hist_odd[0][0]:
        print(count_not_majority(hist_even) + count_not_majority(hist_odd))
    else:
        if count_even - hist_even[1][1] > count_odd - hist_odd[1][1]:
            print(count_not_majority(hist_even) + count_odd - hist_odd[1][1])
        else:
            print(count_not_majority(hist_odd) + count_even - hist_even[1][1])


if __name__ == "__main__":
    main(sys.argv[:1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a1 = [a[x*2] for x in range(n//2+n%2)]
a2 = [a[x*2+1] for x in range(n//2)]
a1set = []
a2set = []

for k in set(a1):
    a1set.append([k,a1.count(k)])
for k in set(a2):
    a2set.append([k,a2.count(k)])

a1set.sort(key=lambda x:x[1],reverse=True)
a2set.sort(key=lambda x:x[1],reverse=True)

if a1set[0][0] != a2set[0][0]:
    print(n-a1set[0][1]-a2set[0][1])
elif len(a1set) > 1 and len(a2set) > 1:
    if a1set[0][1] + a2set[1][1] > a2set[0][1] + a1set[1][1]:
        print(n - a2set[2][1] - a1set[0][1])
    else:
        print(n - a2set[0][1] - a1set[1][1])
elif len(a1set) > 1:
    print(n - a2set[0][1] - a1set[1][1])
elif len(a2set) > 1:
    print(n - a2set[2][1] - a1set[0][1])
else:
    print(n - a1set[0][1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a1 = [a[x*2] for x in range(n//2+n%2)]
a2 = [a[x*2+1] for x in range(n//2)]
sa1 = set(a1)
sa2 = set(a2)
a1set = []
a2set = []

for k in sa1:
    a1set.append([k,a1.count(k)])
for k in sa2:
    a2set.append([k,a2.count(k)])

a1set.sort(key=lambda x:x[1],reverse=True)
a2set.sort(key=lambda x:x[1],reverse=True)
while True:
    if a1set[0][0] != a2set[0][0]:
        print(n-a1set[0][1]-a2set[0][1])
        exit(0)
    elif len(a1set) > 1 and len(a2set) > 1:
        if a1set[0][1] + a2set[1][1] > a2set[0][1] + a1set[1][1]:
            a2set.pop(0)
        else:
            a1set.pop(0)
    elif len(a1set) > 1:
        a1set.pop(0)
    elif len(a2set) > 1:
        a2set.pop(0)
    else:
        print(n - a1set[0][1])
        exit(0)




print(a1, a2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
cnt = [0]*100010
for i in range(100010) :
  if(v.count(i) == n) :
    print(int(n/2))
    exit()
  cnt[i] = v.count(i)
a1 = max(cnt)
cnt.remove(a1)
a2 = max(cnt)
print(n-(a1+a2))

Submission status: Time Limit Exceeded

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


a = sorted(vlist[::2], key=vlist[::2].count, reverse=True)
b = sorted(vlist[1::2], key=vlist[1::2].count, reverse=True)


freq_a = a.count(a[0])
freq_b = b.count(b[0])


x = len(a) - freq_a
y = len(b) - freq_b


if len(set(vlist)) == 1:
    print(min(len(a), len(b)))
else:
    print(x+y)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
kr = []
gr = []
for i in range(a):
    if i % 2 == 1:
        kr.append(ar[i])
    else:
        gr.append(ar[i])
m1 = 0
m2 = 0
M1 = ""
M2 = ""
for r in kr:
    if ar.count(r) >= m1:
        m1 = ar.count(r)
        m2 = m1
        M1 = r
        M2 = M1
    elif ar.count(r) >= m2:
        m2 = ar.count(r)
        M2 = r
n1 = 0
n2 = 0
N1 = ""
N2 = ""
for r in gr:
    if ar.count(r) >= m1:
        n1 = ar.count(r)
        n2 = m1
        N1 = r
        N2 = M1
    elif ar.count(r) >= m2:
        n2 = ar.count(r)
        N2 = r
count = 0
if N1 != M1:
    count += len(kr) - m1
    count += len(gr) - n1
else:
    count += len(ar) - max(m1+n2,m2+n1)
if len(set(ar)) == 1:
    print(len(ar) // 2)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
total = int(input().rstrip())

data = input().rstrip()
num_list = data.split(" ")

even_list = []
odd_list = []

for i,j in enumerate(num_list):
    if (i % 2 == 0):
        even_list.append(int(j))
    else:
        odd_list.append(int(j))

even_count_list = []
odd_count_list = []

count = 0
even = []
odd = []
while(count < len(odd_list)):
    
    if even_list[count] not in even:
        
        even_count_list.append([even_list.count(even_list[count]), even_list[count]])
        even.append(even_list[count])
    
    if odd_list[count] not in odd:
        
        odd_count_list.append([odd_list.count(odd_list[count]), odd_list[count]])
        odd.append(odd_list[count])

    count += 1
    
even_count_list.sort(reverse=True)
odd_count_list.sort(reverse=True)
ans = 0

if even_count_list[0][1] == odd_count_list[0][1]: 
    
    if (len(even_count_list) == 1) and (len(odd_count_list) == 1):
        ans = int(total/2)
        
    if (len(even_count_list) >= 2) and (len(odd_count_list) == 1):
        ans = total - even_count_list[1][0] - odd_count_list[0][0]

    if (len(even_count_list) == 1) and (len(odd_count_list) >= 2):
        ans = total - even_count_list[0][0] - odd_count_list[1][0]
        
    if (len(even_count_list) >= 2) and (len(odd_count_list) >= 2):
        num_a = even_count_list[1][0] + odd_count_list[0][0]   
        num_b = even_count_list[0][0] + odd_count_list[1][0]  

        if num_a > num_b:
            ans = total - num_a
        elif num_a < num_b:
            ans = total - num_b
        else:
            ans = total - num_a

else:
    ans = total - even_count_list[0][0] - odd_count_list[0][0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# ARC103 C - /\/\/\/
# "https://beta.atcoder.jp/contests/arc103/tasks/arc103_a"

# 入力値nを格納
total = int(input().rstrip())

# 入力される数列をリストに格納
data = input().rstrip()
num_list = data.split(" ")

# 配列の末尾要素を数え、その要素及び要素数を返す
def item_count (n_list):
    n_list = n_list
    item = n_list.pop()
    i_count = n_list.count(item) + 1
    return [i_count, item]

# 文字列を奇数列と偶数列に分割する
even_list = []
odd_list = []

for i,j in enumerate(num_list):
    if (i % 2 == 0):
        even_list.append(int(j))
    else:
        odd_list.append(int(j))
        
# ２次元リスト odd_count_list　と even_count_list を用意
even_count_list = []
odd_count_list = []

# 以下1,2,3を奇数列と偶数列に対して、要素がなくなるまでループ処理する
while(len(even_list) !=0 or len(odd_list) !=0):
    
    # 1,配列に対して、要素を末尾から取り出し、カウントする
    #print(even_list)
    if len(even_list) !=0:
        even = item_count(even_list)
        
        # 2,取り出した要素とカウント数を[カウント数:要素]として２次元リストに格納する
        even_count_list.append(even)
        
        # 3,取り出した要素を列から削除する
        while (even[1] in even_list):
            even_list.remove(even[1])

    # 1,2,3を odd_listに対して行う
    if len(odd_list) !=0:
        odd = item_count(odd_list)
        
        odd_count_list.append(odd)
        
        while (odd[1] in odd_list):
            odd_list.remove(odd[1])
        
    even = []
    odd = []
    
# odd と even をカウント数で大きい順にソートする
even_count_list.sort(reverse=True)
odd_count_list.sort(reverse=True)

#print(even_count_list)
#print(odd_count_list)

# 解答を格納
ans = 0

# 以下条件1,2で分岐
# 1,それぞれの最大カウント数となる要素が同じだった場合　と　
# 2,それぞれの最大カウント数となる要素が同じでない場合　

if even_count_list[0][1] == odd_count_list[0][1]: 
    
    # 以下４条件で分岐
    # even と odd の要素数がどちらも１の場合
    if (len(even_count_list) == 1) and (len(odd_count_list) == 1):
        
        # 解答は total/2
        ans = int(total/2)
        
    # even の要素数が２以上、odd の要素数が１の場合
    if (len(even_count_list) >= 2) and (len(odd_count_list) == 1):
         
        # odd 側の最大カウント数と even 側次点のカウント数の合計を計算し、totalから減算する
        ans = total - even_count_list[1][0] - odd_count_list[0][0]
         
    # even の要素数が１、odd の要素数が２以上の場合
    if (len(even_count_list) == 1) and (len(odd_count_list) >= 2):
        
        # even 側の最大カウント数と odd 側次点のカウント数の合計を計算し、totalから減算する
        ans = total - even_count_list[0][0] - odd_count_list[1][0]
        
    # even と odd の要素数が どちらも2以上の場合
    if (len(even_count_list) >= 2) and (len(odd_count_list) >= 2):
        
        # a:odd 側の最大カウント数と even 側次点のカウント数の合計を計算する
        num_a = even_count_list[1][0] + odd_count_list[0][0]   
        # b:even 側の最大カウント数と odd 側次点のカウント数の合計を計算する
        num_b = even_count_list[0][0] + odd_count_list[1][0]  
        
        # aとbを比較し大きい方の数値をtotalから減算する、同じ値だった場合その値を減算する
        if num_a > num_b:
            ans = total - num_a
        elif num_a < num_b:
            ans = total - num_b
        else:
            ans = total - num_a

# 2,の場合　total からそれぞれの最大カウント数を引いた値が解答となる
else:
    ans = total - even_count_list[0][0] - odd_count_list[0][0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
n=int(input())
v=list(map(int,input().split()))
odd=v[1::2]
even=v[0::2]
a=[]
b=[]
lodd=list(set(odd))
leven=list(set(even))
for i in range(len(lodd)):
    a.append((odd.count(lodd[i]),lodd[i]))
for i in range(len(leven)):
    b.append((even.count(leven[i]),leven[i]))
if max(a)[1]!=max(b)[1]:
    print(n-max(a)[0]-max(b)[0])
elif max(a)[1]==max(b)[1]:
    if len(a)>1 and len(b)>1:
        print(min(n-max(a)[0]-heapq.nlargest(2,b)[1][0],n-heapq.nlargest(2,a)[1][0]-max(b)[0]))
    elif len(a)>1 and len(b)==1:
        print(n-heapq.nlargest(2,a)[1][0]-max(b)[0])
    elif len(a)==1 and len(b)>1:
        print(n-max(a)[0]-heapq.nlargest(2,b)[1][0])
    elif len(a)==1 and len(b)==1:
        print(n//2)

Submission status: Time Limit Exceeded

Solution code:
import heapq
n=int(input())
v=list(map(int,input().split()))
odd=v[1::2]
even=v[0::2]
a=[]
b=[]
lodd=list(set(odd))
leven=list(set(even))
for i in range(len(lodd)):
    a.append([odd.count(lodd[i]),lodd[i]])
for i in range(len(leven)):
    b.append([even.count(leven[i]),leven[i]])
if max(a)[1]!=max(b)[1]:
    print(n-max(a)[0]-max(b)[0])
elif max(a)[1]==max(b)[1]:
    if len(a)>1 and len(b)>1:
        print(min(n-max(a)[0]-heapq.nlargest(2,b)[1][0],n-heapq.nlargest(2,a)[1][0]-max(b)[0]))
    elif len(a)>1 and len(b)==1:
        print(n-heapq.nlargest(2,a)[1][0]-max(b)[0])
    elif len(a)==1 and len(b)>1:
        print(n-max(a)[0]-heapq.nlargest(2,b)[1][0])
    elif len(a)==1 and len(b)==1:
        print(n//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
odd=v[1::2]
even=v[0::2]
a=[]
b=[]
lodd=list(set(odd))
leven=list(set(even))
for i in range(len(lodd)):
    a.append([odd.count(lodd[i]),lodd[i]])
for i in range(len(leven)):
    b.append([even.count(leven[i]),leven[i]])
a.sort()
b.sort()
if a[-1][1]!=b[-1][1]:
    print(n-a[-1][0]-b[-1][0])
elif a[-1][1]==b[-1][1]:
    if len(a)>1 and len(b)>1:
        print(min(n-a[-1][0]-b[-2][0],n-a[-2][0]-b[-1][0]))
    elif len(a)>1 and len(b)==1:
        print(n-a[-2][0]-b[0][0])
    elif len(a)==1 and len(b)>1:
        print(n-a[0][0]-b[-2][0])
    elif len(a)==1 and len(b)==1:
        print(n//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
odd=v[1::2]
even=v[0::2]
a=[]
b=[]
lodd=list(set(odd))
leven=list(set(even))
for i in range(len(lodd)):
    a.append([odd.count(lodd[i]),lodd[i]])
for i in range(len(leven)):
    b.append([even.count(leven[i]),leven[i]])
a.sort()
b.sort()
if a[-1][1]!=b[-1][1]:
    print(n-a[-1][0]-b[-1][0])
if a[-1][1]==b[-1][1]:
    if len(a)>1 and len(b)>1:
        print(min(n-a[-1][0]-b[-2][0],n-a[-2][0]-b[-1][0]))
    if len(a)>1 and len(b)==1:
        print(n-a[-2][0]-b[-1][0])
    if len(a)==1 and len(b)>1:
        print(n-a[-1][0]-b[-2][0])
    if len(a)==1 and len(b)==1:
        print(n//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
odd=[]
even=[]
a=[]
b=[]
for i in range(n//2):
    odd.append(v[2*i+1])
    even.append(v[2*i])
lodd=list(set(odd))
leven=list(set(even))
for i in range(len(lodd)):
    a.append([odd.count(lodd[i]),lodd[i]])
for i in range(len(leven)):
    b.append([even.count(leven[i]),leven[i]])
a.sort()
b.sort()
if a[-1][1]!=b[-1][1]:
    print(n-a[-1][0]-b[-1][0])
if a[-1][1]==b[-1][1]:
    if len(a)>1 and len(b)>1:
        print(min(n-a[-1][0]-b[-2][0],n-a[-2][0]-b[-1][0]))
    if len(a)>1 and len(b)==1:
        print(n-a[-2][0]-b[-1][0])
    if len(a)==1 and len(b)>1:
        print(n-a[-1][0]-b[-2][0])
    if len(a)==1 and len(b)==1:
        print(n//2)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
vv = list(map(int, input().split()))
d1 = {}
d2 = {}

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

res = 10**7
for v1 in d1.keys():
    for v2 in d2.keys():
        if v1==v2:
            continue
        tmp = 0
        for k in d1.keys():
            if k!=v1:
                tmp += d1[k]
        for k in d2.keys():
            if k!=v2:
                tmp += d2[k]
        res = min(res, tmp)
if res==10**7:
    print(n//2)
else:
    print(res)

Submission status: Time Limit Exceeded

Solution code:
def given(_dummy=int(input())): return [int(_) for _ in input().split()]


def solve(ary):
    if len(set(ary)) < 2:
        return len(ary) // 2

    even, odd = [], []
    for i, _ in enumerate(ary):
        even.append(_) if i % 2 == 0 else odd.append(_)

    even_key, odd_key = list(set(even)), list(set(odd))

    res = len(ary)
    most = 0

    if len(even_key) != 1:
        l_cache = 0
        for n in even:
            curr = max(l_cache, even.count(n))
            if l_cache != curr:
                most = n
            l_cache = curr

        res -= l_cache
    else:
        res -= len(even)

    if len(odd_key) != 1:
        r_cache = 0
        for n in odd:
            if n == most:
                continue
            curr = max(r_cache, odd.count(n))
            r_cache = curr

        res -= r_cache
    else:
        res -= len(odd)

    return res


print(solve(given()))

Submission status: Time Limit Exceeded

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

a_odd=list([int(a[i]) for i in range(n) if i%2==1])
a_even=list([int(a[i]) for i in range(n) if i%2==0])

def count_change(a_sublist, i):
    return len([num for num in a_sublist if num!=i])

temp=n
for i in set(a_odd):
    for j in set(a_even):
        if i!=j:
            c=count_change(a_odd,i)+count_change(a_even,j)
            if temp>c:
                temp=c

if temp==n:
    print(len(a_odd))
else:
    print(temp)

Submission status: Time Limit Exceeded

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

lvalues = [v[i] for i in range(0, n, 2)]
rvalues = [v[i] for i in range(1, n, 2)]


def freq(values):
    nums = {}
    for v in values:
        if v in nums:
            nums[v] += 1
        else:
            nums[v] = 1
    return nums


lnums = freq(lvalues)
rnums = freq(rvalues)

r = -1

for lk in lnums:
    for rk in rnums:
        if lk == rk:
            continue
        if r < lnums[lk] + rnums[rk]:
            r = lnums[lk] + rnums[rk]

if r == -1:
    r = int(n / 2)

print(n - r)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
def f(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]
s= list(f(v, 2))
b=1
d=0
e=[0]
for i in range(1,len(s)):
    if s.count(s[i]) >s.count(s[i-1]):
        b=i
for q in range(0,len(s)):
    if s[i][0]==s[i][1]:
        for w in range(0,len(s)):
            if s[w][0]==s[w][1] and s[w][0]!=s[i][0]:
                e.append(s.count(s[w]))
        else:
            print(2*len(s)-max(e)-s.count(s[i]))
            break
    elif (s[i][0]!=s[q][0] and s[i][1]==s[q][1]) or(s[i][0]==s[q][0] and s[i][1]!=s[q][1]):
        d+=1
    elif s[i][0]!=s[q][0] and s[i][1]!=s[q][1]:
        d+=2
else:
    print(d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
def f(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]
s= list(f(v, 2))
b=1
d=0
for i in range(1,len(s)):
    if s.count(s[i]) >s.count(s[i-1]):
        b=i
for q in range(0,len(s)):
    if s[i][0]==s[i][1]:
        print(len(s))
        break
    elif (s[i][0]!=s[q][0] and s[i][1]==s[q][1]) or(s[i][0]==s[q][0] and s[i][1]!=s[q][1]):
        d+=1
    elif s[i][0]!=s[q][0] and s[i][1]!=s[q][1]:
        d+=2
else:
    print(d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v = list(map(int,input().split()))
od=[]
ev=[]
n_od=[]
n_ev=[]
k=int(n/2)

for i in range(k):
  od.append(v[2*i])
  ev.append(v[2*i-1])
  
od_set=set(od)
ev_set=set(ev)

for j in od_set:
  n_od.append([od.count(j),j])
for j in ev_set:
  n_ev.append([ev.count(j),j])
  
n_od.sort()
n_ev.sort()

if (len(od_set)==1) and (len(ev_set)==1) and (od[0]==ev[0]):
  e=k
  o=0
elif (n_od[-1][0]==n_ev[-1][0]) and (n_od[-1][1]==n_ev[-1][1]):
  e=n_ev[-1][0]
  o=max(n_od[-2][0],n_ev[-2][0])
else:
  e=n_ev[-1][0]
  o=n_od[-1][0]

print(n-e-o)

Submission status: Time Limit Exceeded

Solution code:
import sys

def DistanceFromVV(seq, el):
    """Return the numbers of edits required to make seq into a VV sequences
    using el as the first and second element, respectively.
    """

    distA = 0
    distB = 0
    for i in range(0, len(seq), 2):
        if seq[i] != el:
            distA += 1
    for j in range(1, len(seq), 2):
        if seq[j] != el:
            distB += 1
    return (distA, distB)

def CountElements(seq):
    """Count the number of occurrences of each element of seq, returning the 
    result as a dictionary.
    """

    count = {}
    for el in seq:
        if el in count:
            count[el] += 1
        else:
            count[el] = 1
    return count

def MinChanges(seq):
    """Return the minimum number of changes which must be made to the given 
    sequence in order to make it "VV".
    """

    element_count = CountElements(seq)
    elements_descending = sorted(element_count, 
                                 key=lambda key: element_count[key], 
                                 reverse=True)

    if (len(elements_descending) == 1):
        return len(seq) // 2

    best = [len(seq) + 1, len(seq) + 1]
    second_best = [len(seq) + 1, len(seq) + 1]
    best_elements = [-1, -1]
    for el in elements_descending:
        changes = DistanceFromVV(seq, el)
        for i in (0,1):
            if changes[i] <= best[i]:
                second_best[i] = best[i]
                best[i] = changes[i]
                best_elements[i] = el
            elif changes[i] < second_best[i]:
                second_best[i] = changes[i]

        if best_elements[0] != best_elements[1]:
            if best[0] <= len(seq)//4 and best[1] <= len(seq)//4:
                break
        elif (best[0] <= len(seq)//4 and second_best[1] <= len(seq)//4) or (best[1] <= len(seq)//4 and second_best[0] <= len(seq)//4):
            break

    if best_elements[0] != best_elements[1]:
        return sum(best)
    else:
        return min(best[0] + second_best[1], best[1] + second_best[0])

lines = sys.stdin.read().split('\n')
n = int(lines[0])
entries = lines[1].split(' ')
v = []
for e in entries:
    v += [int(e)]

print(MinChanges(v))

Submission status: Time Limit Exceeded

Solution code:
import sys

def DistanceFromVV(seq, elA, elB):
    """Return the number of edits required to make seq into a VV sequence
    using elA and elB as seq[0] and seq[1], respectively
    """

    dist = 0
    for i in range(0, len(seq), 2):
        if seq[i] != elA:
            dist += 1
    for j in range(1, len(seq), 2):
        if seq[j] != elB:
            dist += 1
    return dist

def CountElements(seq):
    count = {}
    for el in seq:
        if el in count:
            count[el] += 1
        else:
            count[el] = 1
    return count

def MinChanges(seq):
    """Return the minimum number of changes which must be made to the given 
    sequence in order to make it "VV"
    """

    element_count = CountElements(seq)
    elements_descending = sorted(element_count, 
                                 key=lambda key: element_count[key], 
                                 reverse=True)

    if (len(elements_descending) == 1):
        return len(seq) // 2

    best = len(seq) + 1
    for i in range(len(elements_descending)-1):
        for j in range(i+1, len(elements_descending)):
            changesA = DistanceFromVV(seq, elements_descending[i], 
                                      elements_descending[j])
            changesB = DistanceFromVV(seq, elements_descending[j], 
                                      elements_descending[i])
            best = min(best, changesA, changesB)
    return best

lines = sys.stdin.read().split('\n')
n = int(lines[0])
entries = lines[1].split(' ')
v = []
for e in entries:
    v += [int(e)]

print(MinChanges(v))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v_odd = v[0::2]
v_even = v[1::2]
chars_odd = list(set(v_odd))
chars_even = list(set(v_even))
odd_cnt = [[0, '_']]
for char in chars_odd:
    cnt_odd = v_odd.count(char)
    odd_cnt.append([cnt_odd, char])
odd_cnt.sort(reverse=True)
even_cnt = [[0, '_']]
for char in chars_even:
    cnt_even = v_even.count(char)
    even_cnt.append([cnt_even, char])
even_cnt.sort(reverse=True)
if odd_cnt[0][1] == even_cnt[0][1]:
    res = max(odd_cnt[0][0] + even_cnt[1][0], odd_cnt[1][0] + even_cnt[0][0])
else:
    res = odd_cnt[0][0] + even_cnt[0][0]
print(n - res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

se=set(e)
so=set(o)


if(len(se)==1 and len(so)==1):
    if(e[0]==o[0]):print(n//2)
    else:print(0)
    exit()

le=[]


for ve in se:
    a=len(e[e != ve])
    le.append([a,ve])

lo=[]
for vo in so:
    a=len(o[o != vo])
    lo.append([a,vo])
    
    
le.sort()
lo.sort()

if(len(se)==1):
    if(lo[0][1]!=le[0][1]):print(lo[0][0])
    else:print(lo[1][0])
elif(len(so)==1):
    if(lo[0][1]!=le[0][1]):print(le[0][0])
    else:print(le[1][0])
else:
    if(lo[0][1]!=le[0][1]):print(le[0][0]+lo[0][0])
    else:print(min(le[1][0]+lo[0][0],le[0][0]+lo[1][0]))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
c_even = sorted(list(Counter(v[::2]).items()) +
                [(-1, 0)], key=lambda x: x[1])[::-1]
c_odd = sorted(list(Counter(v[1::2]).items()) +
               [(-1, 0)], key=lambda x: x[1])[::-1]
if c_even[0][0] == c_odd[0][0]:
    ans = n - max(c_even[0][1]+c_odd[1][1], c_even[1][1]+c_odd[0][1])
else:
    ans = n - (c_even[0][1] + c_odd[0][1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def inverse_lookup(d: dict, x):
    for k, v in d.items():

        if v == x:
            return k

    # if x is not found
    return None


def num_of_nth_often(lst, nth):
    counter = Counter(lst)
    counted_list = sorted(list(counter.values()), reverse=True)

    if len(counted_list) < nth:
        return 0, None

    else:
        return counted_list[nth - 1], inverse_lookup(counter, counted_list[nth - 1])

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

e1 = num_of_nth_often(v_even, 1)
e2 = num_of_nth_often(v_even, 2)
o1 = num_of_nth_often(v_odd, 1)
o2 = num_of_nth_often(v_odd, 2)

if e1[1] != o1[1]:
    rewrite_num = len(v_even) - e1[0] + len(v_odd) - o1[0]
    print(rewrite_num)
else:
    rewrite_num1 = len(v_even) - e1[0] + len(v_odd) - o2[0]
    rewrite_num2 = len(v_even) - e2[0] + len(v_odd) - o1[0]
    print(min(rewrite_num1, rewrite_num2))

Submission status: Accepted

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

count = 0
v1 = []
v2 = []
while count < n:
  v1.append(v[count])
  v2.append(v[count + 1])
  count += 2

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

num1, count1 = c1.most_common()[0]
num2, count2 = c2.most_common()[0]

if num1 != num2:
  min_count = (n // 2 - count1) + (n // 2 - count2)
elif count1 == count2 and count1 == n // 2:
  min_count = (n // 2)
else:
  if len(c1.most_common()) > 1 and len(c2.most_common()) > 1:
    num3, count3 = c1.most_common()[1]
    num4, count4 = c2.most_common()[1]
    a = (n // 2 - count1) + (n // 2 -count4)
    b = (n // 2 - count2) + (n // 2 -count3)
    min_count = min(a,b)
  else:
    if count1 > count2:
      num2, count2 = c2.most_common()[1]
      min_count = (n // 2 - count1) + (n // 2 - count2)
    else:
      num1, count1 = c1.most_common()[1]
      min_count = (n // 2 - count2) + (n // 2 - count1)
print(min_count)

Submission status: Accepted

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

count = 0
v1 = []
v2 = []
while count < n:
  v1.append(v[count])
  v2.append(v[count + 1])
  count += 2

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

num1, count1 = c1.most_common()[0]
num2, count2 = c2.most_common()[0]

if num1 != num2:
  min_count = (n // 2 - count1) + (n // 2 - count2)
elif count1 == count2 and count1 == n // 2:
  min_count = (n // 2)
else:
  if len(c1.most_common()) > 1 and len(c2.most_common()):
    num3, count3 = c1.most_common()[1]
    num4, count4 = c2.most_common()[1]
    a = (n // 2 - count1) + (n // 2 -count4)
    b = (n // 2 - count2) + (n // 2 -count3)
    min_count = min(a,b)
  else:
    if count1 > count2:
      num2, count2 = c2.most_common()[1]
      min_count = (n // 2 - count1) + (n // 2 - count2)
    else:
      num1, count1 = c1.most_common()[1]
      min_count = (n // 2 - count2) + (n // 2 - count1)
print(min_count)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
A=V[::2]
B=V[1::2]
a=Counter(A)
b=Counter(B)
x=a.most_common(2)
y=b.most_common(2)
if x[0][0]!=y[0][0]:
  print((n//2-x[0][1])+(n//2-y[0][1]))
else:
  if len(x)>1 and len(y)>1:
    s=(n//2-x[0][1])+(n//2-y[1][1])
    t=(n//2-x[1][1])+(n//2-y[0][1])
    print(min(s,t))
  elif len(x)>1:
    print(n//2-x[1][1])
  elif len(y)>1:
    print(n//2-y[1][1])
  else:
    print(n//2)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
A=V[::2]
B=V[1::2]
a=Counter(A)
b=Counter(B)
x=a.most_common(2)
y=b.most_common(2)
if x[0][0]!=y[0][0]:
  print((n//2-x[0][1])+(n//2-y[0][1]))
else:
  if len(x)>1 and len(y)>1:
    if x[0][1]>y[0][1]:
      print((n//2-x[0][1])+(n//2-y[1][1]))
    elif x[0][1]==y[0][1]:
      if x[1][1]>y[1][1]:
        print((n//2-x[1][1])+(n//2-y[0][1]))
      else:
        print((n//2-x[0][1])+(n//2-y[1][1]))
    else:
      print((n//2-x[1][1])+(n//2-y[0][1]))
  elif len(x)>1:
    print(n//2-x[1][1])
  elif len(y)>1:
    print(n//2-y[1][1])
  else:
    print(n//2)

Submission status: Accepted

Solution code:
from collections import Counter

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

even_cnt = Counter(V[0::2])
odd_cnt = Counter(V[1::2])

even_kv = [(k, v) for k, v in even_cnt.items()]
odd_kv = [(k, v) for k, v in odd_cnt.items()]

even_kv.append((None, 0))
odd_kv.append((None, 0))

even_kv.sort(key=lambda kv: -kv[1])
odd_kv.sort(key=lambda kv: -kv[1])

if even_kv[0][0] == odd_kv[0][0]:
    count = max(even_kv[0][1]+odd_kv[1][1],
                even_kv[1][1]+odd_kv[0][1])
else:
    count = even_kv[0][1] + odd_kv[0][1]
print(N-count)