Problem p03061 (python)

Fixed submission:
import fractions
n = int(input())
listA = list(map(int, input().split()))
listA.sort()
if len(listA) < 3:
    print(max(listA))
else:
    minGcd1 = fractions.gcd(listA[0], listA[1])
    minGcd2 = fractions.gcd(listA[1], listA[2])
    minGcd3 = fractions.gcd(listA[0], listA[2])
    ming1 = minGcd1
    maxg1 = 1
    while ming1 > 1:
        count=0
        flag=0
        index1=0
        while index1 < len(listA):
            if listA[index1]%ming1==0:
                pass
            else:
                count+=1
            if count >1:
                flag=1
                break
            index1+=1
        if flag:
            ming1-=1
        else:
            maxg1 = ming1
            break
    ming2 = minGcd2
    maxg2 = 1
    while ming2 > 1:
        count = 0
        flag = 0
        index1 = 0
        while index1 < len(listA):
            if listA[index1] % ming2==0:
                pass
            else:
                count += 1
            if count > 1:
                flag = 1
                break
            index1 += 1
        if flag:
            ming2 -= 1
        else:
            maxg2 = ming2
            break
    ming3 = minGcd3
    maxg3= 1
    while ming3 > 1:
        count = 0
        flag = 0
        index1 = 0
        while index1 < len(listA):
            if listA[index1] % ming3==0:
                pass
            else:
                count += 1
            if count > 1:
                flag = 1
                break
            index1 += 1
        if flag:
            ming3 -= 1
        else:
            maxg3 = ming3

            break
    print(max(maxg1, maxg2, maxg3))

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
listA = list(map(int, input().split()))
listA.sort()
if len(listA) < 3:
    print(max(listA))
else:
    minGcd1 = math.gcd(listA[0], listA[1])
    minGcd2 = math.gcd(listA[1], listA[2])
    minGcd3 = math.gcd(listA[0], listA[2])
    ming1 = minGcd1
    maxg1 = 1
    while ming1 > 1:
        count=0
        flag=0
        index1=0
        while index1 < len(listA):
            if listA[index1]%ming1==0:
                pass
            else:
                count+=1
            if count >1:
                flag=1
                break
            index1+=1
        if flag:
            ming1-=1
        else:
            maxg1 = ming1
            break
    ming2 = minGcd2
    maxg2 = 1
    while ming2 > 1:
        count = 0
        flag = 0
        index1 = 0
        while index1 < len(listA):
            if listA[index1] % ming2==0:
                pass
            else:
                count += 1
            if count > 1:
                flag = 1
                break
            index1 += 1
        if flag:
            ming2 -= 1
        else:
            maxg2 = ming2
            break
    ming3 = minGcd3
    maxg3= 1
    while ming3 > 1:
        count = 0
        flag = 0
        index1 = 0
        while index1 < len(listA):
            if listA[index1] % ming3==0:
                pass
            else:
                count += 1
            if count > 1:
                flag = 1
                break
            index1 += 1
        if flag:
            ming3 -= 1
        else:
            maxg3 = ming3

            break
    print(max(maxg1, maxg2, maxg3))

Buggy submission (verdict: Runtime Error):
import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

n = int(input())
listA=list(map(int, input().split()))

index=0
max=0
while index < len(listA):
    if index == 0:
        gc = gcd_list(listA[1:])
    elif index == len(listA)-1:
        gc = gcd_list(listA[:len(listA)-1])
    else:
        listB = listA[:index]
        listB.extend(listA[index+1:len(listA)])
        gc = gcd_list(listB)
    if gc > max:
        max = gc
    index+=1
print(max)