Problem: 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.

Submission attempts (in order):
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])


candidate_odd = []
candidate_even = []

candidate_odd.append



def count_rewrite(odd, even):
    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
    return ans


ans = 1000002

if odd_sorted[0][0] == even_sorted[0][0]:
    
    if len(odd.keys()) > 1 and len(even.keys()) > 1:
        ans = min(ans, count_rewrute(odd_sorted[0][0], even_sorted[1][0]))
        ans = min(ans, count_rewrute(odd_sorted[1][0], even_sorted[0][0]))
    
    elif len(odd.keys()) == 1 and len(even.keys()) == 1:
        ans = min(ans, count_rewrute(-1, even_sorted[0][0]))
    
    elif len(odd.keys()):
        ans = min(ans, count_rewrute(odd_sorted[0][0], even_sorted[1][0]))
    
    else: 
        ans = min(ans, count_rewrute(odd_sorted[1][0], even_sorted[0][0]))

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


print(ans)

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]:
    
    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]))
    
    elif len(odd.keys()):
        ans = min(ans, count_rewrite(odd_sorted[0][0], even_sorted[1][0]))
    
    else: 
        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)

Verdicts: Runtime Error

Accepted