Problem p03061 (python)

Fixed submission:
from sys import stdin, exit
from fractions import gcd


N = int(input())
A = list(map(int, input().split()))
A.sort()

# List if length of 2 or shorter
if len(A) <= 2:
    print(max(A))
    exit()

# Left Side = [A0, 0, 0, 0, 0]
left_side = [0 for _ in range(N)]
left_side[0] = A[0]

# Right Side = [0, 0, 0, 0, An]
right_side = [0 for _ in range(N)]
right_side[-1] = A[-1]

for i in range(N - 1):  # DP: L1 = gcd(L0, A1); Maximum GCD on very RIGHT of list  (Dir -->)
    left_side[i + 1] = gcd(left_side[i], A[i + 1])

for i in range(1, N):   # DP: Rn-2 = gcd(An-2, Rn-1); Maximum GCD on very LEFT of list  (Dir <--)
    right_side[-1 * (i+1)] = gcd(A[-1*(i+1)], right_side[-1*i])

max_gcd = 1

# Left
max_gcd = max(max_gcd, right_side[1])
# Max of Right and Left
max_gcd = max(max_gcd, left_side[-2])

for i in range(1, N - 1):
    left = left_side[i - 1]
    # We exclude the number at i here, and compare whether new gcd is greater than previous
    right = right_side[i + 1]

    max_gcd = max(max_gcd, gcd(left, right))

print(max_gcd)

Buggy submission (verdict: Runtime Error):
from sys import stdin, exit
from math import gcd


N = int(input())
A = list(map(int, input().split()))
A.sort()

# List if length of 2 or shorter
if len(A) <= 2:
    print(max(A))
    exit()

# Left Side = [A0, 0, 0, 0, 0]
left_side = [0 for _ in range(N)]
left_side[0] = A[0]

# Right Side = [0, 0, 0, 0, An]
right_side = [0 for _ in range(N)]
right_side[-1] = A[-1]

for i in range(N - 1):  # DP: L1 = gcd(L0, A1); Maximum GCD on very RIGHT of list  (Dir -->)
    left_side[i + 1] = gcd(left_side[i], A[i + 1])

for i in range(1, N):   # DP: Rn-2 = gcd(An-2, Rn-1); Maximum GCD on very LEFT of list  (Dir <--)
    right_side[-1 * (i+1)] = gcd(A[-1*(i+1)], right_side[-1*i])

max_gcd = 1

# Left
max_gcd = max(max_gcd, right_side[1])
# Max of Right and Left
max_gcd = max(max_gcd, left_side[-2])

for i in range(1, N - 1):
    left = left_side[i - 1]
    # We exclude the number at i here, and compare whether new gcd is greater than previous
    right = right_side[i + 1]

    max_gcd = max(max_gcd, gcd(left, right))

print(max_gcd)

Buggy submission (verdict: Runtime Error):
from sys import stdin, exit
from math import gcd


rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())


N = ri()
A = list(rim())
A.sort()

# List if length of 2 or shorter
if len(A) <= 2:
    print(max(A))
    exit()

# Left Side = [A0, 0, 0, 0, 0]
left_side = [0 for _ in range(N)]
left_side[0] = A[0]

# Right Side = [0, 0, 0, 0, An]
right_side = [0 for _ in range(N)]
right_side[-1] = A[-1]

for i in range(N - 1):  # DP: L1 = gcd(L0, A1); Maximum GCD on very RIGHT of list  (Dir -->)
    left_side[i + 1] = gcd(left_side[i], A[i + 1])
    
for i in range(1, N):   # DP: Rn-2 = gcd(An-2, Rn-1); Maximum GCD on very LEFT of list  (Dir <--)
    right_side[-1 * (i+1)] = gcd(A[-1*(i+1)], right_side[-1*i])
    
max_gcd = 1

# Left
max_gcd = max(max_gcd, right_side[1])
# Max of Right and Left
max_gcd = max(max_gcd, left_side[-2])

for i in range(1, N - 1):
    left = left_side[i - 1]
    # We exclude the number at i here, and compare whether new gcd is greater than previous
    right = right_side[i + 1]

    max_gcd = max(max_gcd, gcd(left, right))

print(max_gcd)

Buggy submission (verdict: Runtime Error):
from sys import stdin
from math import gcd



rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())


def lcm(a, b):
    return (a * b) / gcd(a, b)


def findGCD(lst):
    ret = 1
    for i in range(len(lst)-1):
        ret = max(ret, gcd(lst[i], lst[i+1]))
    return ret


def findLCM(lst):
    ret = max(lst)
    for i in range(len(lst)-1):
        ret = min(ret, lcm(lst[i], lst[i+1]))
        print(lcm(lst[i], lst[i+1]))
    return ret


N = ri()
integers = list(rim())
gc = findGCD(integers)
cnt = 0
for num in integers:
    if num % (gc*2) != 0:
        cnt += 1

if cnt > 1:
    print(gc)
else:
    print(gc*2)

Buggy submission (verdict: Runtime Error):
from sys import stdin
from math import gcd


rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())


N = ri()
integers = list(rim())

lowest = [float('-inf'), float('-inf')]
for i in range(len(integers)-1):
    for j in range(i+1, len(integers)):
        temp = gcd(integers[i], integers[j])
        if temp > min(lowest):
            lowest[lowest.index(min(lowest))] = temp

print(max(lowest))

Buggy submission (verdict: Runtime Error):
from sys import stdin
from collections import deque, defaultdict as ddic
from itertools import combinations, permutations, product
import bisect
import heapq
from math import gcd
from operator import mul
import re


rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())


def findGCD(lst):
    ret = 1
    for i in range(len(lst)-1):
        ret = max(ret, gcd(lst[i], lst[i+1]))
    return ret


def findGCD2(lst, k):
    ret = 1
    for i in range(len(lst)-1):
        if i == k:
            continue
        ret = max(ret, gcd(lst[i], lst[i+1]))
    return ret



N = ri()
integers = list(rim())

lowest = [float('-inf'), float('-inf')]
for i in range(len(integers)-1):
    for j in range(i+1, len(integers)):
        temp = gcd(integers[i], integers[j])
        if temp > min(lowest):
            lowest[lowest.index(min(lowest))] = temp

print(max(lowest))