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, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

class SegmentTree:
    
    def __init__(self, N, operator, unit, a=None):
        self.op = operator
        self.UNIT = unit
        self.N0 = 2**(N-1).bit_length()
        self.tree = [self.UNIT]*(2*self.N0)
        if a != None:
            self.tree[self.N0:self.N0+N] = a[:]
            for k in range(self.N0-1, 0, -1):
                self.tree[k] = self.op(self.tree[2*k], self.tree[2*k+1])
    
    def update(self, k, x):
        k += self.N0
        self.tree[k] = x
        k //= 2
        while k:
            self.tree[k] = self.op(self.tree[2*k], self.tree[2*k+1])
            k //= 2
    
    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0 + 1
        s = self.UNIT
        while L < R:
            if R & 1:
                R -= 1
                s = self.op(self.tree[R], s)
            if L & 1:
                s = self.op(s, self.tree[L])
                L += 1
            L >>= 1
            R >>= 1
        return s
    def get(self, k):  
        return self.tree[k+self.N0]

N = INT()
A = LIST()
lcm_base = lambda x, y: (x * y) // gcd(x, y)
many_lcm = lambda numbers: reduce(lcm_base, numbers)
unit = many_lcm(A)
seg = SegmentTree(N+1, gcd, unit)

for i, x in enumerate(A):
	seg.update(i+1, x)

ans = 0
for i, x in enumerate(A):
	seg.update(i+1, unit)
	ans = max(ans, seg.query(1, N))
	seg.update(i+1, A[i])
print(ans)


import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

def gcd(a, b):
	# if a == 0:
	# 	return b
	# elif b == 0:
	# 	return a
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)

Li = [0]*(N+2)
Ri = [0]*(N+2)
A.append(0)
A.insert(0, 0)

for i in range(N+1):
	Li[i+1] = gcd(Li[i], A[i])

for i in range(N, -1, -1):
	Ri[i] = gcd(Ri[i+1], A[i])

# print(Li)
# print(Ri)

M = []
for i in range(1, N+1):
	M.append(gcd(Li[i],Ri[i+1]))
# print(M)
print(max(M))


Verdicts: Time Limit Exceeded

Accepted