Problem:
 Score : 300 points    Problem Statement 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.      Constraints  All values in input are integers.  2 \leq N \leq 10^5  1 \leq A_i \leq 10^9       Output Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.       Sample Input 1 3
7 6 8      Sample Output 1 2  If we replace 7 with 4 , the greatest common divisor of the three integers on the blackboard will be 2 , which is the maximum possible value.       Sample Input 2 3
12 15 18      Sample Output 2 6       Sample Input 3 2
1000000000 1000000000      Sample Output 3 1000000000  We can replace an integer with itself.

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]


class SegmentTree():
    def __init__(self, array, m, e, size):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        m : func
            binary operation of the monoid
        e : 
            identity element of the monoid
        size : int
            limit for array size
        """
        self.m = m
        self.e = e
        self.size = size
        self.n = n = len(array)
        self.dat = dat = [e] * n + array + [e] * (2 * size - 2 * n)
        for i in range(n - 1, 0, -1):  
            dat[i] = m(dat[i << 1], dat[i << 1 | 1])

    def modify(self, p, v):  
        m, n, dat = self.m, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            dat[p >> 1] = m(dat[p], dat[p ^ 1])
            p >>= 1

    def query(self, l, r):  
        m, e, n, dat = self.m, self.e, self.n, self.dat
        res = e
        l += n
        r += n
        while l < r:
            if l & 1:
                res = m(res, dat[l])
                l += 1
            if r & 1:
                r -= 1
                res = m(res, dat[r])
            l >>= 1
            r >>= 1
        return res


gcd = lambda x, y: gcd(y, x) if x < y else gcd(y, x % y) if y else x
e = 0
size = N
ST = SegmentTree(A, m=gcd, e=e, size=size)
ans = 1
for i, a in enumerate(A):
    ST.modify(i, 0)
    ans = max(ans, ST.query(0, N))
    ST.modify(i, a)
print(ans)

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]


class SegmentTree():
    def __init__(self, array, m, e, size):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        m : func
            binary operation of the monoid
        e : 
            identity element of the monoid
        size : int
            limit for array size
        """
        self.m = m
        self.e = e
        self.size = size
        self.n = n = len(array)
        self.dat = dat = [e] * n + array + [e] * (2 * size - 2 * n)
        for i in range(n - 1, 0, -1):  
            dat[i] = m(dat[i << 1], dat[i << 1 | 1])

    def modify(self, p, v):  
        m, n, dat = self.m, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            dat[p >> 1] = m(dat[p], dat[p ^ 1])
            p >>= 1

    def query(self, l, r):  
        m, e, n, dat = self.m, self.e, self.n, self.dat
        res = e
        l += n
        r += n
        while l < r:
            if l & 1:
                res = m(res, dat[l])
                l += 1
            if r & 1:
                r -= 1
                res = m(res, dat[r])
            l >>= 1
            r >>= 1
        return res


gcd = lambda x, y: gcd(y, x) if x < y else x if y == 0 else gcd(y, x % y)
e = 0
size = N
ST = SegmentTree(A, m=gcd, e=e, size=size)
ans = 1
for i, a in enumerate(A):
    ST.modify(i, 0)
    ans = max(ans, ST.query(0, N))
    ST.modify(i, a)
print(ans)

Solution (Python):
def gcd(a, b):
    if b > a:
        a, b = b, a
    if a % b == 0:
        return b
    return gcd(a%b, b)


N = int(input())
A = [int(s) for s in input().split(" ")]

left = A.copy()
right = A.copy()

for i in range(1, N):
    left[i] = gcd(left[i-1], left[i])

for i in reversed(range(0, N-1)):
    right[i] = gcd(right[i+1], right[i])

ans = max(right[1], left[-2])

for i in range(1, N-1):
    ans = max(ans, gcd(left[i-1], right[i+1]))

print(ans)

Solution (Python):




import fractions, functools, itertools


n = int(input())
A = [int(s) for s in input().split()]

gcd = fractions.gcd
L = list(itertools.accumulate(A, gcd))
R = list(itertools.accumulate(A[::-1], gcd))
R = R[::-1]
L.append(0)
R.append(0)
gs = [gcd(L[i-1], R[i+1]) for i in range(n)]
ans = max(gs)

print(ans)

Solution (Python):
from fractions import gcd

n = int(input())
a = list(map(int,input().split()))
l = [0]
r = [0]*n

first = a[0]
for i in a:
    first = gcd(first,i)
    l.append(first)
    
last = a[-1]
for i in range(len(r))[::-1]:
    last = gcd(last,a[i])
    r[i] = last
    
r.append(0)
ans = 0
    
for i in range(len(a)):
    a = l[i]
    b = r[i+1]
    temp = gcd(a,b)
    ans = max(ans,temp)
    
print(ans)

Solution (Python):
def gcd(a, b):
    a, b = min(a, b), max(a, b)
    while b % a != 0:
        a, b = b % a, a
    return a


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



s = [-1] * n
t = [-1] * n
for x in range(0, n - 1):
    s[x + 1] = a[x]
for x in range(1, n):
    t[x - 1] = a[x]

for x in range(1, n - 1):
    s[x + 1] = gcd(s[x + 1], s[x])
for x in range(n - 2, 0, -1):
    t[x - 1] = gcd(t[x - 1], t[x])

ans = -float('inf')
for x, y in zip(s, t):
    if x == -1:
        ans = max(ans, y)
    elif y == -1:
        ans = max(ans, x)
    else:
        ans = max(ans, gcd(x, y))
    
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
from fractions import gcd
import functools
l=[(t.append(gcd(t[i-1],a[i])),t)[1] for t in [[a[0]]] for i in range(1, n)][0]
r=[(t.append(gcd(t[i-1],a[n-1-i])),t)[1] for t in [[a[n-1]]] for i in range(1, n)][0][::-1]
print(functools.reduce(lambda x,y:max(x,y),[gcd(l[i-1]if 0<i else r[i+1],r[i+1]if i<n-1 else l[i-1])for i in range(n)]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
from fractions import gcd
import functools
l=[(hoge.append(gcd(hoge[i-1],a[i])),hoge)[1] for hoge in [[a[0]]] for i in range(1, n)][0]
r=[(hoge.append(gcd(hoge[i-1],a[n-1-i])),hoge)[1] for hoge in [[a[n-1]]] for i in range(1, n)][0][::-1]
print(functools.reduce(lambda x,y:max(x,y),[gcd(l[i-1]if 0<i else r[i+1],r[i+1]if i<n-1 else l[i-1])for i in range(n)]))

Solution (Python):
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

def ngcd(li):
    temp=gcd(li[0], li[1])
    if len(li)-3 >=0:
        for i in range(2, len(li)):
            temp=gcd(temp, li[i])
    return temp


n=int(input())
a=[int(i) for i in input().split()]

l=[]
l.append(0)
for i in range(len(a)):
  l.append(gcd(l[i], a[i]))
r=[]
r.append(0)
for i in range(len(a)):
  r.append(gcd(r[i], a[n-i-1]))



ans=[]
for i in range(len(a)):
  ans.append(gcd(l[i], r[n-i-1]))

print(max(ans))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

from fractions import gcd

l = [(hoge.append(gcd(hoge[i - 1], a[i])),hoge)[1] for hoge in [[a[0]]]for i in range(1, n)][0]





r = [a[n - 1]]
for i in range(1, n):
    r.append(gcd(r[i - 1], a[n - 1 - i]))
r.reverse()
from functools import reduce

print (reduce(lambda x,y:max(x,y),[gcd(l[i-1]if 0<i else r[i+1],r[i+1] if i<n-1 else l[i-1])for i in range(n)]))

Solution (Python):
import fractions

gcd = fractions.gcd
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
ryuho = 0

if n == 2:
  print(max(a[0],a[1]))
  exit()

ans = gcd(a[0],a[1])
ryuho = a[2]

if ans < gcd(a[1],a[2]):
  ans = gcd(a[1],a[2])
  ryuho = a[0]

if ans < gcd(a[0],a[2]):
  ans = gcd(a[0],a[2])
  ryuho = a[1]

if n == 3:
  print(ans)
  exit()

for i in range(3,n):
  temp1 = gcd(ans,a[i])
  temp2 = gcd(ans,ryuho)
  if temp1 > temp2:
    ans = temp1
  else:
    ans = temp2
    ryuho = a[i]

print(ans)

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
left = []
l = arr[0]
right = []
r = arr[-1]
ans = 0
left = [0 for _ in range(n)]
right =  [0 for _ in range(n)]

def gcd(x, y):
    while (y):
        x, y = y, x % y
    return x
for i in range(n):
    l = gcd(l,arr[i])
    left[i]= l
    r = gcd(r,arr[-(i+1)])
    right[-(i+1)] = r
for i in range(n):
    if i-1 <0:
        a = arr[-1]
    else:
        a = left[i-1]
    if i+1>n-1:
        b = arr[0]
    else:
        b = right[i+1]
    
    ans = max(gcd(a,b),ans)


if n == 2:
    print(max(arr))
else:
    print(ans)

Solution (Python):
def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
N=int(input())
A=list(map(int,input().split()))

L=[0,0]
for ai in A:
    L.append(gcd(L[-1],ai))
    

R=[0,0]
for ai in A[::-1]:
    R.append(gcd(R[-1],ai))
    

print(max(gcd(l,r) for l,r in zip(L,reversed(R))))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

from fractions import gcd

l = [a[0]]
for i in range(1, n):
    l.append(gcd(l[i - 1], a[i]))
r = [a[n - 1]]
for i in range(1, n):
    r.append(gcd(r[i - 1], a[n - 1 - i]))
r.reverse()
from functools import reduce

print (reduce(lambda x,y:max(x,y),[gcd(l[i-1]if 0<i else r[i+1],r[i+1] if i<n-1 else l[i-1])for i in range(n)]))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

from fractions import gcd

tl = [a[0]]
for i in range(1, n):
    tl.append(gcd(tl[i - 1], a[i]))

tr = [a[n - 1]]
for i in range(1, n):
    tr.append(gcd(tr[i - 1], a[n - 1 - i]))
tr.reverse()

ans = 1
for i in range(n):
    tmp = 1
    l = tl[i - 1] if 0 < i else tr[i + 1]
    r = tr[i + 1] if i < n - 1 else tl[i - 1]
    ans = max(ans, gcd(l, r))
print(ans)

Solution (Python):
import fractions 

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

gcd_l = [0]
gcd_r = [0]
for i in range(N):
    gcd_l.append(fractions.gcd(gcd_l[i], A[i]))
    gcd_r.append(fractions.gcd(gcd_r[i], A[-(i+1)]))
    
    
gcd_r = gcd_r[::-1]

ans = []
for i in range(N):
    ans.append(fractions.gcd(gcd_l[i], gcd_r[i+1]))

print(max(ans))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

from fractions import gcd

tl = [a[0]]
for i in range(1, n):
    tl.append(gcd(tl[i - 1], a[i]))

tr = [a[n - 1]]
for i in range(1, n):
    tr.append(gcd(tr[i - 1], a[n - 1 - i]))
tr.reverse()

ans = 1
for i in range(n):
    tmp = 1
    if i == 0: tmp = tr[i + 1]
    elif i == n - 1: tmp = tl[i - 1]
    else: tmp = gcd(tl[i - 1], tr[i + 1])
    ans = max(ans, tmp)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
A = [0] + list(map(int, input().split())) + [0]
gcdl, gcdr = [0] + [None] * N + [0], [0] + [None] * N + [0]
gcd = lambda x, y : x if y == 0 else gcd(y, x % y)
for i in range(1, N + 1):
    gcdl[i] = gcd(gcdl[i - 1], A[i])
    gcdr[N + 1 - i] = gcd(gcdr[N + 2 - i], A[N + 1 - i])
ans = 1
for i in range(1, N + 1):
    tmp = gcd(gcdl[i - 1], gcdr[i + 1])
    if tmp > ans:
        ans = tmp
print(ans)

Solution (Python):


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

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

L = [0]
R = [0]

for i in range(n):
    L.append(gcd(L[i], A[i]))
    R.append(gcd(R[i], A[n-1-i]))
R.reverse()

result = []

for i in range(n):
    result.append(gcd(L[i], R[i+1])) 

print(max(result))

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import fractions
from functools import reduce
N = int(input())
A = [int(i) for i in input().split()]



L = [0 for i in range(N)]
R = [0 for i in range(N)]
L[0] = A[0]
R[-1] = A[-1]
for i in range(N-1):
    L[i+1] = fractions.gcd(L[i],A[i+1])

for j in reversed(range(N-1)):
    R[j] = fractions.gcd(A[j],R[j+1])

L.append(0)
L.insert(0,0)
R.append(0)
R.insert(0,0)

ans = 1
for i in range(N):
    
    ans = max(ans,fractions.gcd(L[i],R[i+2]))

print(ans)







"""
嘘解法
def gcd(nums):
    return reduce(fractions.gcd,nums)

ans = 1
A.sort()
gcd1 = gcd(A)
for i in range(N):
    A[i] //= gcd1

ans *= gcd1

gcd2 = A[0]
for i in range(1,N):
    gcd2 = fractions.gcd(gcd2,A[i])
    if gcd2 == 1:
        key = i
        break

a = copy.deepcopy(A)
a.remove(A[0])
b = copy.deepcopy(A)
b.remove(A[key])

ans *= max(gcd(a),gcd(b))
print(ans)
"""

Solution (Python):
import sys,fractions
input=sys.stdin.readline
N=int(input())
num = list(map(int,input().split()))
g = fractions.gcd
left=[0]*(N+1)
right=[0]*(N+1)
for i in range(N):
  j = N-i-1
  left[i+1] = g(left[i],num[i])
  right[j] = g(right[j+1],num[j])
ans=0
for k in range(N):
  ans = max(ans,g(left[k],right[k+1]))
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
if n==2:
    print(max(a[0],a[1]))
    exit()
def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)
gl = [0]*n
gl[0] = a[0]
gr = [0]*n
gr[-1] = a[-1]

for i in range(1,n):
    gl[i] = gcd(max(gl[i-1],a[i]),min(gl[i-1],a[i]))
for i in range(n-2,-1,-1):
    gr[i] = gcd(max(gr[i+1],a[i]),min(gr[i+1],a[i]))
ans = max(gr[1],gl[-2])
for i in range(1,n-1):
    g = gcd(max(gl[i-1],gr[i+1]),min(gl[i-1],gr[i+1]))
    ans = max(ans,g)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

L, R = [0]*(N+1), [0]*(N+1)
for i in range(1,N+1):
    L[i] = gcd(L[i-1], A[i-1])
    R[N-i] = gcd(R[N+1-i], A[N-i])

ans = 0
for i in range(N):
    ans = max(ans, gcd(L[i], R[i+1]))

print(ans)

Solution (Python):
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)
 
N = int(input())
A = list(map(int, input().split()))

L = [0, 0]
for ai in A:
    L.append(gcd(L[-1], ai))

R = [0, 0]
for ai in A[::-1]:
    R.append(gcd(R[-1], ai))
 
print(max(gcd(l, r) for l, r in zip(L, reversed(R))))

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
A = read_ints()

from fractions import gcd




left = []
right = []
tmp_fo = 0
tmp_re = 0
for fo, re in zip(A, A[::-1]):
    tmp_fo = gcd(tmp_fo, fo)
    tmp_re = gcd(tmp_re, re)
    left.append(tmp_fo)
    right.append(tmp_re)

right = right[::-1]

ans = 0
for i in range(1, N-1):
    ans = max(ans, gcd(left[i - 1], right[i + 1]))

ans = max(ans, right[1], left[-2])

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


N = int(input())
A = list(map(int, input().split()))
L = [0]*(N+1)
R = [0]*(N+1)

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

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

ans = max([gcd(x, y) for x, y in zip(L, R[1:])])
print(ans)

Solution (Python):
N = int(input())
A = map(int, input().split())

def factorize(x):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    d = 2
    while x > 1:
        c = 0
        while x % d == 0:
            c += 1
            x //= d
        if c > 0:
            f[d] = c
        d += 1 if d == 2 else 2
        if d * d > x > d:
            f[x] = 1
            break
    return f

def factorizeWithSet(x, D):
    f = {}
    for d in D:
        c = 0
        while x % d == 0:
            c += 1
            x //= d
        if c > 0:
            f[d] = c
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    
    l = [float('inf')] * N
    for i in range(1, N):
        l[i] = min(l[i - 1], ls[i - 1])
    
    r = [float('inf')] * N
    for i in range(N - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p


A = list(A)
F = [factorize(a) for a in A[:2]]


D =  set(F[0].keys())
D.update(F[1].keys())

E = set()
for d in D:
    c = 0
    for a in A:
        if a % d != 0:
            c += 1
            if c >= 2:
                E.add(d)
                break
D -= E

F = [factorizeWithSet(a, D) for a in A]


X = {d: [f[d] if d in f else 0 for f in F] for d in D}


X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D) for i in range(N)]
print(max(S))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 1

def gcd(m, n):
  x = max(m, n)
  y = min(m, n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x % y
      x = y
      y = z
    else:
      return z

L, R = {}, {}
for i in range(N):
  if i == 0:
    L[0] = A[0]
    R[N-(i+1)] = A[N-(i+1)]
  else:
    L[i] = gcd(L[i-1], A[i])
    R[N-(i+1)] = gcd(R[N-i], A[N-(i+1)])

for i in range(N):
  if i == 0:
    tmp = R[1]
  elif i == N-1:
    tmp = L[N-2]
  else:
    tmp = gcd(L[i-1], R[i+1])
  ans = max(tmp, ans)

print(ans)

Solution (Python):
from fractions import gcd

n = int(input())
a_l =[int(i) for i in input().split()]

left=[0]
right=[0]

for i in range(1,n+1):
    left.append(gcd(left[i-1],a_l[i-1]))
    right.append(gcd(right[i-1],a_l[n-i]))
right=right[::-1]

ans=1
for i in range(n):
    ans=max(ans,gcd(left[i],right[i+1]))
print(ans)

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

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

left_gcd = [A[0]]
a = A[0]
for i in range(1,N-1):
  a = gcd(a, A[i])
  left_gcd.append(a)

right_gcd = [A[N-1]]
b = A[N-1]
for k in range(N-2, 0, -1):
  b = gcd(b, A[k])
  right_gcd.append(b)

ans_gcd = [right_gcd[N-2], left_gcd[N-2]]

for p in range(N-2):
  ans_gcd.append(gcd(left_gcd[p], right_gcd[N-p-3]))

print(max(ans_gcd))

Solution (Python):
from fractions import gcd

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

L = [0]
R = [0]
for i in range(n):
  
  L.append(gcd(L[i],a[i]))
  
  R.append(gcd(R[i],a[-(i+1)]))

R = R[::-1]
ans = []
for i in range(n):
  ans.append(gcd(L[i],R[i+1]))
print(max(ans))

Solution (Python):


import sys
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd

def input(): return sys.stdin.readline().strip()
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=LIST()


L=[0]*(N+2)
for i in range(N):
    L[i+1]=gcd(L[i], A[i])
R=[0]*(N+2)
for i in range(N-1, -1, -1):
    R[i+1]=gcd(R[i+2], A[i])


ans=0
for i in range(N):
    ans=max(ans, gcd(L[i], R[i+2]))
print(ans)

Solution (Python):
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
 
N = (int(input()))
A = [int(i) for i in input().split()]

A1 = [0] * (N)
A2 = [0] * (N)
A3 = [0] * (N)
A1[0] = A[0]
A2[N-1] = A[N-1]

for i in range(N):
  if i==0 : continue
  A1[i] = gcd(A1[i-1],A[i])
  A2[N-1-i] = gcd(A2[N-i],A[N-1-i])
  
for i in range(N):
  if i==0 : A3[i] = A2[i+1] ; continue
  if i==N-1 : A3[i] = A1[i-1] ; continue
  A3[i] = gcd(A1[i-1],A2[i+1])

print(max(A3))

Solution (Python):


import itertools
try:
    from math import gcd
except ImportError:
    from fractions import gcd


class Cumulative(object):

    def __init__(self, seq, op):
        self.cumul_array = [0]
        self.cumul_array.extend(itertools.accumulate(seq, op))


def solve(n, xs):
    left_cumul = Cumulative(xs, gcd)
    right_cumul = Cumulative(reversed(xs), gcd)
    res = 1
    for i in range(n):
        left_gcd = left_cumul.cumul_array[i]
        right_gcd = right_cumul.cumul_array[n - i - 1]
        cand = gcd(left_gcd, right_gcd)
        if cand > res:
            res = cand
    return res


def main():
    n = int(input())
    xs = [int(x) for x in input().split()]
    res = solve(n, xs)
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
def gcd(x, y):
	if y == 0:
		return x
	r = x % y
	while r != 0:
		x = y
		y = r
		r = x % y
	return y

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

l = [0] * (n + 2)
r = [0] * (n + 2)

for i in range(1, n + 1):
	l[i] = gcd(l[i - 1], a[i - 1])

for i in range(n, 0, -1):
	r[i] = gcd(r[i + 1], a[i - 1])

ans = 1
for i in range(1, n + 1):
	ans = max(ans, gcd(l[i - 1], r[i + 1]))

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d1 = []
d2 = []
d1.append(a[0])
d2.append(a[-1])

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


g = a[0]
for i in range(1, n - 1):
    g = gcd(g, a[i])
    d1.append(g)

g = a[-1]
for i in range(n - 2, 0, -1):
    g = gcd(g, a[i])
    d2.append(g)

d2 = d2[::-1]
d1 = [0] + d1
d2.append(0)
num = 0
for i in range(n):
    num = max(num, gcd(d1[i], d2[i]))
print(num)

Solution (Python):
from fractions import *
n = int(input())
a = [0]+list(map(int, input().split()))+[0]
b = [0]*(n+2)
c = [0]*(n+2)
for i in range(1, n+1):
	b[i] = gcd(b[i-1], a[i])
for i in range(n, 0, -1):
	c[i] = gcd(c[i+1], a[i])

ans=1
for i in range(1, n+1):
    x1 = b[i-1]
    x2 = c[i+1]
    ans=max(ans, gcd(x1, x2))

print(ans)

Solution (Python):

"""
Created on Sat Apr 27 20:57:32 2019

@author: Yamazaki Kenichi
"""

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

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

T = [[1,1,1] for i in range(N)]
T[0][0],T[0][1],T[0][2] = A[0], 0, 0
for i in range(1,N):
    T[i][0] = eg(T[i-1][0],A[i])
    T[i][1] = T[i-1][0]
    T[i][2] = max(eg(T[i-1][2],A[i]),eg(T[i-1][1],A[i]))
print(max(T[N-1]))

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def gcd(a,b):
    if a%b == 0:
        return b
    else:
        return gcd(b, a%b)
    
n = ni()
a = list(li())
    
gcd_left = [0, a[0]]
gcd_right = [0, a[-1]]

for ai in a[1:]:
    gcd_left.append(gcd(gcd_left[-1], ai))
    
for ai in a[-2::-1]:
    gcd_right.append(gcd(gcd_right[-1], ai))
    
ans = 1

for le, ri in zip(gcd_left[:-1], gcd_right[::-1][1:]):
    if le == 0 or ri == 0:
        ans = max(ans, le, ri)
        
    else:
        ans = max(ans, gcd(le, ri))
        
print(ans)

Solution (Python):
def gcd(x, y):
    while y!=0:
        x, y = y, x % y 
    return x

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

if n == 2:
    print(max(a))

elif n>=3:
    l = [-1] * n
    r = [-1] * n
    l[0] = a[0]  
    r[-1] = a[-1] 



    for i in range(1, n): 
        l[i] = gcd(l[i-1], a[i]) 
        
    for i in range(n-2, -1, -1): 
        r[i] = gcd(r[i+1], a[i]) 

        
        

    ans = max(r[1], l[-2]) 
        

    for i in range(1, n-1): 
        g = gcd(l[i-1], r[i+1]) 
        ans = max(ans, g)
        
    print(ans)

Solution (Python):
import sys

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

N = int(sys.stdin.readline().strip())
As = list(map(int, sys.stdin.readline().strip().split()))

left = [0] * N
right = [0] * (N+1)

for i in range(1, N):
    left[i] = gcd(left[i-1], As[i-1])
for i in range(N-1, -1, -1):
    right[i] = gcd(right[i+1], As[i])

ans = 0
for i in range(N):
    ans = max(ans, gcd(left[i], right[i+1]))
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]


def gcd(a: int, b: int) -> int:
    while a % b != 0:
        r = a % b
        a = b
        b = r
    return b


L = [0] * N
R = [0] * N
for i in range(1, N):
    L[i] = gcd(L[i-1], A[i-1])
    R[N-i-1] = gcd(R[N-i], A[N-i])

for i in range(N):
    if i == 0:
        ans = R[i]
    elif i == N-1:
        ans = max(ans, L[i])
    else:
        ans = max(ans, gcd(L[i], R[i]))

print(ans)

Solution (Python):
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def solve(n, a):
    l = [0] * n
    r = [0] * (n + 1)

    for i in range(1, n):
        l[i] = gcd(l[i - 1], a[i - 1])
    for i in range(n - 1, 0, -1):
        r[i] = gcd(r[i + 1], a[i])

    m = 0
    for i in range(n):
        m = max(m, gcd(l[i], r[i + 1]))
    return m


def input_from_console():
    n = int(input())
    a = list(map(int, input().split()))
    return n, a


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    main()

Solution (Python):
def gcd(x, y):
    if x < y:
        return gcd(y, x)
    if y == 0:
        return x
    return gcd(y, x % y)
N = int(input())
A = list(map(int, input().split()))
B = [A[0]] * N
C = [A[N - 1]] * N
for i in range(N - 1):
    B[i + 1] = gcd(B[i], A[i + 1])
    C[N - i - 2] = gcd(C[N - i - 1], A[N - i - 2])
ans = max(B[N - 2], C[1])
for i in range(N - 2):
    ans = max(ans, gcd(B[i], C[i + 2]))
print(ans)

Solution (Python):
def gcd(x, y):
    if x < y:
        return gcd(y, x)
    if y == 0:
        return x
    return gcd(y, x % y)
N = int(input())
A = list(map(int, input().split()))
B = [A[0]] * N
C = [A[N - 1]] * N
for i in range(N - 1):
    B[i + 1] = gcd(B[i], A[i + 1])
    C[N - i - 2] = gcd(C[N - i - 1], A[N - i - 2])
ans = max(B[N - 2], C[1])
for i in range(N - 2):
    ans = max(ans, gcd(B[i], C[i + 2]))
print(ans)

Solution (Python):

def gcd(a, b):
    if b == 0:
        return a
    elif a == 0:
        return b
    else:
        return gcd(b, a % b)


def solve(n, a):
    l, r = [], []
    l = [0 for i in range(n + 1)]
    r = [0 for i in range(n + 1)]
    r[n] = 0

    for i in range(0, n):
        if i == 0:
            l[0] = 0
        else:
            l[i] = gcd(l[i - 1], a[i - 1])

    for i in range(n, 0, -1):

        if i == n:
            r[i] = 0
        else:
            r[i] = gcd(r[i+1], a[i])
    m = 0
    for i in range(n):
        
        m = max(m, gcd(l[i], r[i + 1]))
    return m


def input_from_console():
    n = int(input())
    a = list(map(int, input().split()))
    return n, a


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    main()

Solution (Python):
from fractions import gcd
N = int(input())
aa = list(map(int, input().split()))
front = [0]*N
back = [0]*N
front[0] = aa[0]
back[-1] = aa[-1]
for i in range(1, N):
    front[i] = gcd(front[i-1], aa[i])
    back[-1-i] = gcd(back[-i], aa[-1-i])
ans = back[1]
for i in range(2, N):
    ans = max(gcd(front[i-2], back[i]), ans)
ans = max(front[-2], ans,)
print(ans)

Solution (Python):
def gcd(x,y):
    if x<y:
        x,y=y,x
    while x%y!=0:
        x,y=y,x%y
    return(y)

n=int(input())

v=[]
line=input().split()

for i in range(n):
    v.append(int(line[i]))

gcd_l=[]
gcd_r=[]

for i in range(n-1):
    if i==0:
        gcd_l.append(v[i])
    else:
        gcd_l.append(gcd(gcd_l[i-1],v[i]))

for i in range(n-1):
    if i==0:
        gcd_r.append(v[n-1])
    else:
        gcd_r.append(gcd(gcd_r[i-1],v[n-i-1]))

gcd_list=[]

for i in range(n):
    if i==0:
        gcd_list.append(gcd_r[n-2])
    elif i==n-1:
        gcd_list.append(gcd_l[n-2])
    else:

        gcd_list.append(gcd(gcd_l[i-1],gcd_r[n-i-2]))



print(max(gcd_list))

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))

param = [[0, 0, 0]]
while True:
    if len(param) <= 0:
        break
    addr, Li, Lv = param.pop()
    if addr == 0:
        if Li == N-1:
            Rv = 0
            M = Lv
            continue            
        param += [[addr+1, Li, Lv], [0, Li+1, gcd(Lv, A[Li])]]
        continue 
    elif addr == 1:
        Rv = gcd(A[Li+1], Rv)
        M = max(M, gcd(Lv, Rv))
        continue

print(M)

Solution (Python):
from functools import reduce
import copy

def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

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

result = 0
l = [0]*n
r = [0]*n
for i in range(n-1):
    l[i+1] = gcd(l[i],a[i])

for i in reversed(range(1, n)):
    r[i-1] = gcd(r[i], a[i])

ans = 0
for i in range(n):
    lr = gcd(l[i], r[i])
    ans = max(ans, lr)

print(ans)

Solution (Python):
from fractions import*
n,*a=map(int,open(0).read().split())
b=[0]
for i in a:b+=gcd(b[-1],i),
w=m=0
for i in range(n):m=max(m,gcd(w,b[-i-2]));w=gcd(w,a[~i])
print(m)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

res = [0] * N
l = [0] * (N+1)
r = [0] * (N+1)

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

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

for i in range(N):
    res[i] = gcd(l[i],r[i+1])

print(max(res))

Solution (Python):
from fractions import*
n,*a=map(int,open(0).read().split())
*b,=a=a+[0]
r=range(n)
for i in r:a[i]=gcd(a[i],a[i-1]);b[-i-2]=gcd(b[-i-2],b[~i])
print(max(gcd(a[i-1],b[i+1])for i in r))

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

def gcd(x, y):
    while y > 0:
        x, y = y, x % y
    return x

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

C = A.copy()
C.reverse()
for i in range(N - 1):
    C[i + 1] = gcd(C[i], C[i + 1])
C.reverse()

ans = 0
for i in range(N):
    if i == 0:
        tmp = C[i + 1]
    elif i == N - 1:
        tmp = B[i - 1]
    else:
        tmp = gcd(B[i - 1], C[i + 1])
    ans = max(ans, tmp)
    
print(ans)

Solution (Python):
import itertools
import fractions
from functools import reduce

n = int(input())
a = [int(s) for s in input().split(' ')]

L = [0] * n
R = [0] * n

for i in range(len(a)):
    if i == 0:
        L[i] = a[i]
        R[n - 1] = a[n - 1]
    else:
        L[i] = fractions.gcd(L[i - 1], a[i])
        R[n - 1 - i] = fractions.gcd(R[n - i], a[n - 1 - i])

val = 1

for i in range(len(a)):
    ret = 0
    if i == 0:
        ret = R[i + 1]
    elif i == n - 1:
        ret = L[i - 1]
    else:
        ret = fractions.gcd(L[i - 1], R[i + 1])

    if ret > val:
       val = ret
        
print(val)