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):
import sys
import fractions
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n = ni()
a = nl()
minn = 10 ** 9 + 1
idx = -1
for i in range(n - 1):
    if minn >= fractions.gcd(a[i], a[i + 1]):
        minn = fractions.gcd(a[i], a[i + 1])
        idx = i
if idx == 0:
    if n != 2:
        print(max(gcd_list(a[1:]), gcd_list(a[:1] + a[2:])))
    else:
        print(max(a[0], a[1]))
elif idx == n - 2:
    print(max(gcd_list(a[:n - 1]), gcd_list(a[:n - 2] + a[n - 1:])))
else:
    print(max(gcd_list(a[:idx] + a[idx + 1:]), gcd_list(a[:idx + 1] + a[idx + 2:])))


import sys
import fractions
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n = ni()
a = nl()
maxx = gcd_list(a)
for i in range(n - 1):
    if fractions.gcd(a[i], a[i + 1]) == maxx:
        ans = max(gcd_list(a[:i] + a[i + 1:]), gcd_list(a[:i + 1] + a[i + 2:]))
        print(ans)
        exit()


import sys
import fractions
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n = ni()
a = nl()
minn = 10 ** 9
idx = -1
for i in range(n - 1):
    if minn > fractions.gcd(a[i], a[i + 1]):
        minn = fractions.gcd(a[i], a[i + 1])
        idx = i
if idx == -1:
    print(minn)
else:
    print(max(gcd_list(a[:idx] + a[idx + 1:]), gcd_list(a[:idx + 1] + a[idx + 2:])))


import sys
import fractions
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n = ni()
a = nl()
minn = 10 ** 9 + 1
idx = -1
for i in range(n - 1):
    if minn >= fractions.gcd(a[i], a[i + 1]):
        minn = fractions.gcd(a[i], a[i + 1])
        idx = i
print(max(gcd_list(a[:idx] + a[idx + 1:]), gcd_list(a[:idx + 1] + a[idx + 2:])))


import sys
import fractions
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n = ni()
a = nl()
maxx = gcd_list(a)
for i in range(n):
    maxx = max(maxx, gcd_list(a[:i] + a[i + 1:]))
print(maxx)


import sys
import fractions
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
n = ni()
a = nl()
minn = 10 ** 9 + 1
idx = -1
for i in range(n - 1):
    if minn > fractions.gcd(a[i], a[i + 1]):
        minn = fractions.gcd(a[i], a[i + 1])
        idx = i
print(max(gcd_list(a[:idx] + a[idx + 1:]), gcd_list(a[:idx + 1] + a[idx + 2:])))


import sys
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def gcd_list(numbers):
    return reduce(gcd, numbers)
n = ni()
a = nl()
if len(a) == 2:
    print(max(a))
    exit()
minn = 10 ** 9 + 1
idx = -1
for i in range(n - 2):
    if minn >= gcd_list([a[i], a[i + 1], a[i + 2]]):
        minn = gcd_list([a[i], a[i + 1], a[i + 2]])
        idx = i
gcd1 = 0
gcd2 = 0
gcd3 = 0
for i in range(n):
    if i != idx:
        gcd1 = gcd(gcd1, a[i])
    if i != idx + 1:
        gcd2 = gcd(gcd2, a[i])
    if i != idx + 2:
        gcd3 = gcd(gcd3, a[i])
print(max(gcd1, gcd2, gcd3))


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Time Limit Exceeded

Wrong Answer

Accepted