Problem p03611 (python)

Fixed submission:
import collections
N = int(input())
a = list(map(int,input().split()))
ans = []
for i in range(N):
    ans.append(a[i]-1)
    ans.append(a[i])
    ans.append(a[i]+1)
c = collections.Counter(ans)
print(c.most_common()[0][1])

Buggy submission (verdict: Runtime Error):
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
alen = len(a)
MAX = 0
if alen == 1:
    print(a[0][1])
elif alen == 2:
    if abs(a[0][1]-a[1][1])<=2:
        print(a[0][1]+a[1][1])
    else:
        print(max(a[0][1],a[1][1]))
else:
    for i in range(alen-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) <= 2 or abs(a[i+2][0]-a[i+1][0]) <= 2:
            if abs(a[i][0]-a[i+1][0]) <= 2:
                karia = a[i][1]+a[i+1][1]
            if abs(a[i+1][0]-a[i+2][0]) <= 2:
                karib = a[i+1][1]+a[i+2][1]
            kari = max(karia,karib)
        else:
            kari = max(a[i][1],a[i+1][1],a[i+2][1])
        if MAX <= kari:
            MAX = kari
    print(MAX)

Buggy submission (verdict: Runtime Error):
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
alen = len(a)
MAX = 0
if alen == 1:
    print(a[0][1])
elif alen == 2:
    if abs(a[0][1]-a[1][1])<=2:
        print(a[0][1]+a[1][1])
    else:
        print(max(a[0][1],a[1][1]))
else:
    for i in range(alen-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) <= 2 or abs(a[i+2][0]-a[i+1][0]) <= 2:
            if abs(a[i][0]-a[i+1][0]) <= 2:
                kari1 = a[i][1]+a[i+1][1]
            if abs(a[i+1][0]-a[i+2][0]) <= 2:
                kari2 = a[i+1][1]+a[i+2][1]
            kari = max(kari1,kari2)
        else:
            kari = max(a[i][1],a[i+1][1],a[i+2][1])
        if MAX <= kari:
            MAX = kari
    print(MAX)

Buggy submission (verdict: Runtime Error):
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
alen = len(a)
MAX = 0
if alen == 1:
    print(a[0][1])
elif alen == 2:
    if abs(a[0][1]-a[1][1])<=2:
        print(a[0][1]+a[1][1])
    else:
        print(max(a[0][1],a[1][1]))
else:
    for i in range(alen-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) <= 2 or abs(a[i+2][0]-a[i+1][0]) <= 2:
            if abs(a[i][0]-a[i+1][0]) <= 2:
                kari1 = a[i][1]+a[i+1][1]
            if abs(a[i+1][0]-a[i+2][0]) <= 2:
                kari2 = a[i+1][1]+a[i+2][1]
            kari = max(kari1,kari2)
        else:
            kari = max(a[i][1],a[i+1][1],a[i+2][1])
        if MAX <= kari:
            MAX = kari
    print(MAX)

Buggy submission (verdict: Runtime Error):
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
MAX = 0
if N == 1:
    print(1)
elif N == 2:
    if abs(alist[0]-alist[1])<=2:
        print(2)
    else:
        print(1)
else:
    for i in range(N-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]
        elif abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i+1][1]+a[i+2][1]
        if MAX <= kari:
            MAX = kari
    print(MAX)