Problem p02975 (python)

Fixed submission:

N = int(input())
a = list(map(int, input().split(' ')))

ans = True
if N % 3 != 0:
    for i in range(N):
        if a[i] != 0:   
            ans = False
else:
    first = a[0]
    i_second = -1
    for i in range(1, N):
        if a[i] != first:
            second = a[i]
            i_second = i
            break
        
    if i_second == -1:
        for i in range(N):
            if a[i] != 0:
                ans = False
    else:
        i_third = -1
        for i in range(i_second+1, N):
            if (a[i] != first) and (a[i] != second):
                third = a[i]
                i_third = i
                break
        if i_third == -1:
            ans = False
            cnt_first = 0
            cnt_second = 0
            for i in range(N):
                if a[i] == first:
                    cnt_first += 1
                elif a[i] == second:
                    cnt_second += 1
            if first ^ second == second:
                if (cnt_first == N/3) and (cnt_second == N*2/3):
                    ans = True
            if first ^ second == first:
                if (cnt_first == N*2/3) and (cnt_second == N/3):
                    ans = True
        else:
            if (first ^ second != third) or (second ^ third != first) or (third ^ first != second):
                ans = False
            else:
                cnt_first = 0
                cnt_second = 0
                cnt_third = 0
                for i in range(N):
                    if a[i] == first:
                        cnt_first += 1
                    elif a[i] == second:
                        cnt_second += 1
                    elif a[i] == third:
                        cnt_third += 1
                if (cnt_first != N/3) or (cnt_second != N/3) or (cnt_third != N/3):
                    ans = False

if ans is True:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int, input().split(' ')))

ans = True
if N % 3 != 0:
    for i in range(N):
        if a[i] != 0:   
            ans = False
else:
    first = a[0]
    i_second = -1
    for i in range(1, N):
        if a[i] != first:
            second = a[i]
            i_second = i
            break
        
    if i_second == -1:
        for i in range(N):
            if a[i] != 0:
                ans = False
    else:
        for i in range(i_second+1, N):
            if (a[i] != first) and (a[i] != second):
                third = a[i]
                break
        if (first ^ second != third) or (second ^ third != first) or (third ^ first != second):
            ans = False
        else:
            cnt_first = 0
            cnt_second = 0
            cnt_third = 0
            for i in range(N):
                if a[i] == first:
                    cnt_first += 1
                elif a[i] == second:
                    cnt_second += 1
                elif a[i] == third:
                    cnt_third += 1
            if (cnt_first != N/3) or (cnt_second != N/3) or (cnt_third != N/3):
                ans = False

if ans is True:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int, input().split(' ')))

ans = True
if N % 3 != 0:
    for i in range(N):
        if a[i] != 0:   
            ans = False
else:
    first = a[0]
    i_second = -1
    for i in range(1, N):
        if a[i] != first:
            second = a[i]
            i_second = i
            break
        
    if i_second == -1:
        ans = False
    else:
        for i in range(i_second+1, N):
            if (a[i] != first) and (a[i] != second):
                third = a[i]
                break
        if (first ^ second != third) or (second ^ third != first) or (third ^ first != second):
            ans = False
        else:
            cnt_first = 0
            cnt_second = 0
            cnt_third = 0
            for i in range(N):
                if a[i] == first:
                    cnt_first += 1
                elif a[i] == second:
                    cnt_second += 1
                else:
                    cnt_third += 1
            if (cnt_first != N/3) or (cnt_second != N/3) or (cnt_third != N/3):
                ans = False

if ans is True:
    print('Yes')
else:
    print('No')