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):
def main(n:int,lst:list)->int:
    lst1 = lst[::2]
    lst2 = lst[1::2]
    ans1 =count2(lst1)
    ans2 =count2(lst2)
    if ans1[0][0] == ans2[0][0]:
        if ans1[1] and ans2[1]:
            return n - max(ans1[0][1] + ans2[1],ans2[0][1] + ans1[1])
        else:
            return n//2
    else:
        return n - ans1[0][1] - ans2[0][1]
def count2(lst):
    lst.sort()
    ans = [["",0],0]
    last =lst[0]
    a = 0
    for e in lst:
        if last == e:
            a +=1
        else:
            ans = check(a,ans)
            last = e
            a = 1
    return check(a,ans)
def check(a,ans):
    if a > ans[0][1]:
        ans[1] = ans[0][1]
        ans[0][0] = e
        ans[0][1] = a
    elif a > ans[1]:
        ans[1] = a
    return ans
n = int(input())
lst = input().split()
print(main(n,lst))


def count2(lst):
    ans = [["",0],0]
    def check():
        nonlocal ans
        if a > ans[0][1]:
            ans[1] = ans[0][1]
            ans[0][0] = last
            ans[0][1] = a
        elif a > ans[1]:
            ans[1] = a
    lst.sort()
    last =lst[0]
    a = 0
    for e in lst:
        if last == e:
            a +=1
        else:
            check()
            last = e
            a = 1
    check()
    return ans

def main(lst:list)->int:
    n = len(lst)
    ans1 =count2(lst[::2])
    ans2 =count2(lst[1::2])
    if ans1[0][0] == ans2[0][0]:
        if ans1[1] and ans2[1]:
            return n - max(ans1[0][1] + ans2[1],ans2[0][1] + ans1[1])
        else:
            return n//2
    else:
        return n - ans1[0][1] - ans2[0][1]

def main(n:int,lst:list)->int:
    b = True
    lst1 = []
    lst2 = []
    for e in lst:
        if b:
            lst1.append(e)
        else:
            lst2.append(e)
        b = not b
    lst1.sort()
    lst2.sort()
    c1 = [["",0],0]
    c2 = [["",0],0]
    last =lst1[0]
    a = 0
    for e in lst1:
        if last == e:
            a +=1
        else:
            if a >= c1[0][1]:
                c1[1] = c1[0][1]
                c1[0][0] = e
                c1[0][1] = a
            elif a > c1[1]:
                c1[1] = a
            last = e
            a = 1
    if a >= c1[0][1]:
        c1[1] = c1[0][1]
        c1[0][0] = e
        c1[0][1] = a
    elif a > c1[1]:
        c1[1] = a

    last =lst2[0]
    a = 0
    for e in lst2:
        if last == e:
            a +=1
        else:
            if a >= c2[0][1]:
                c2[1] = c2[0][1]
                c2[0][0] = e
                c2[0][1] = a
            elif a > c2[1]:
                c2[1] = a
            last = e
            a = 1
    if a >= c2[0][1]:
        c2[1] = c2[0][1]
        c2[0][0] = e
        c2[0][1] = a
    elif a > c2[1]:
        c2[1] = a

    if c1[0][0] == c2[0][0]:
        if c1[0][1] > c2[0][1] or(c1[0][1] == c2[0][1] and c2[1] > c1[1]):
            return n - c1[0][1] - c2[1]
        else:
            return n - c2[0][1] - c1[1]
    else:
        return n - c1[0][1] - c2[0][1]

n = int(input())
lst = input().split())
print(main(n,lst))


from collections import Counter
def main(n:int,lst:list)->int:
    lst1 = [::2]
    lst2 = [1::2]
    c1 = Counter(lst1).most_common(2)
    c2 = Counter(lst2).most_common(2)
    if c1[0][0] == c2[0][0]:
        if len(c1)!=1 and len(c2)!=1:
            return n - max(c1[0][1] + c2[1],c2[0][1] + c1[1])
        else:
            return n//2
    else:
        return n - c1[0][1] - c2[0][1]

n = int(input())
lst = input().split()
print(main(n,lst))


def main(n:int,lst:list)->int:
    b = True
    lst1 = []
    lst2 = []
    for e in lst:
        if b:
            lst1.append(e)
        else:
            lst2.append(e)
        b = not b
    lst1.sort()
    lst2.sort()
    c1 = [["",0],["",0]]
    c2 = [["",0],["",0]]
    last =lst1[0]
    a = 1
    for e in lst1[1:]:
        if last == e:
            a +=1
        else:
            if a > c1[0][1]:
                c1[0][0] = e
                c1[0][1] = a

            elif a > c1[1][1]:
                c1[1][0] = e
                c1[1][1] = a
            last = e
            a = 1
    if a > c1[0][1]:
        c1[0][0] = e
        c1[0][1] = a

    elif a > c1[1][1]:
        c1[1][0] = e
        c1[1][1] = a

    last =lst2[0]
    a = 1
    for e in lst2[1:]:
        if last == e:
            a +=1
        else:
            if a > c2[0][1]:
                c2[0][0] = e
                c2[0][1] = a

            elif a > c2[1][1]:
                c2[1][0] = e
                c2[1][1] = a
            last = e
            a = 1
    if a > c2[0][1]:
        c2[0][0] = e
        c2[0][1] = a

    elif a > c2[1][1]:
        c2[1][0] = e
        c2[1][1] = a
    if c1[0][0] == c2[0][0]:
        if c1[0][1] > c2[0][1]:
            print(c1,c1)
            return n - c1[0][1] - c2[1][1]
        else:
            print(c1,c1)
            return n - c2[0][1] - c1[1][1]
    else:
        print(c1,c1)
        return n - c1[0][1] - c2[0][1]

n = int(input())
lst = list(map(int,input().split()))
print(main(n,lst))


def count2(lst):
    ans = [["",0],0]
    def check():
        nonlocal ans
        if a > ans[0][1]:
            ans[1] = ans[0][1]
            ans[0][0] = last
            ans[0][1] = a
        elif a > ans[1]:
            ans[1] = a
    lst.sort()
    last =lst[0]
    a = 0
    for e in lst:
        if last == e:
            a +=1
        else:
            check()
            last = e
            a = 1
    check()
    return ans

def main(n:int,lst:list)->int:
    ans1 =count2(lst[::2])
    ans2 =count2(lst[1::2])
    if ans1[0][0] == ans2[0][0]:
        return n - max(ans1[0][1] + ans2[1],ans2[0][1] + ans1[1])
    else:
        return n - ans1[0][1] - ans2[0][1]
n = int(input())
lst = input().split()
print(main(n,lst))

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Accepted