Problem: There are N integers, A_1, A_2, ..., A_N, written on the blackboard.  You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.  Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Submission attempts (in order):
def gcd(x,y):
  if y == 0:
    return x
  else:
    return gcd(y,x%y)

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)
accum_gcd_i = accum_gcd_i[::-1]

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j]:
        print(accum_gcd[j])
        exit()

def gcd(x,y):
  if y == 0:
    return x
  else:
    return gcd(y,x%y)

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j] or (accum_gcd[j] == 1 and accum_gcd_i[j] == 1):
        A_pop = [k for k in A[0:j] + A[j+1:n]]
        A_i_pop = [k for k in A_i[0:j] + A_i[j+1
        :n]]
        
        gcd_A_pop = A_pop[0]
        for k in range(n-1):
            gcd_A_pop = gcd(gcd_A_pop,A_pop[k])
            
        gcd_A_i_pop = A_i_pop[0]
        for k in range(n-1):
            gcd_A_i_pop = gcd(gcd_A_i_pop,A_i_pop[k])
            
        print(max(gcd_A_pop,gcd_A_i_pop))
        exit()

def gcd(x,y):
  if y == 0:
    return x
  else:
    return gcd(y,x%y)

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))

accum_gcd_pop = []

for j in range(n):
    if j == 0:
        accum_gcd_pop.append(accum_gcd_i[n-1])
    elif j == n-1:
        accum_gcd_pop.append(accum_gcd[n-1])
    else:
        accum_gcd_pop.append(gcd(accum_gcd[j-1],accum_gcd_i[n-j-2]))
print(max(accum_gcd_pop))

def gcd(x,y):
  if y == 0:
    return x
  else:
    return gcd(y,x%y)

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)

print(accum_gcd)
print(accum_gcd_i)

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j]:
        A_pop = [k for k in A[0:j+1] + A[j+2:n]]
        print(A_pop)
        A_i_pop = [k for k in A_i[0:j+1] + A_i[j+2:n]]
        print(A_i_pop)
        gcd_A_pop = A_pop[0]
        for k in range(n-1):
            gcd_A_pop = gcd(gcd_A_pop,A_pop[k])
        gcd_A_i_pop = A_i_pop[0]
        for k in range(n-1):
            gcd_A_i_pop = gcd(gcd_A_i_pop,A_i_pop[k])
        print(max(gcd_A_pop,gcd_A_i_pop))
        exit()

def gcd(x,y):
  if y == 0:
    return x
  else:
    return gcd(y,x%y)
gcd_A_all = []
for i in range(n):
    A_pop = [a for a in A[0:i] + A[i+1:n]]
    gcd_A = A_pop[0]
    for j in range(n-1):
        gcd_A = gcd(gcd_A, A_pop[j])
    gcd_A_all.append(gcd_A)
print(max(gcd_A_all))

def gcd(x,y):
  if y == 0:
    return x
  else:
    return gcd(y,x%y)

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))

accum_gcd_pop = []

for j in range(n):
    if j == 0:
        accum_gcd_pop.append(accum_gcd_i[n-2])
    elif j == n-1:
        accum_gcd_pop.append(accum_gcd[n-2])
    else:
        accum_gcd_pop.append(gcd(accum_gcd[j-1],accum_gcd_i[n-j-2]))
print(max(accum_gcd_pop))


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted