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 = list(map(int, input().split()))

L = len(A)
from functools import reduce
from fractions import gcd

def lcm(a,b):
  return (a*b)//gcd(a,b)

g = reduce(gcd, A)
l = reduce(lcm, A)

def _gcd(a,b):
  if a is None and b is None:
    return None
  if a is None:
    return b
  if b is None:
    return a
  else:
    return gcd(a,b)

class SegTree():
  import math
  def __init__(self, func, A, identity=1):
    self._func = func
    self._N = 1
    while self._N < len(A):
      self._N *= 2
       
    self._identity = identity 

    self._tree = [self._identity] * (2*self._N -1) 
    self._tree[self._N-1:self._N-1+ len(A)] = A 
    for i in range(self._N-2, -1, -1):
      self._update_single_node(i)
    
    
    
    
    
    
    self._node_range = [(i-self._N+1,i-self._N+2) for i in range(2*self._N-1)] 
    for i in range(self._N-2, -1, -1):
      self._node_range[i] = (self._node_range[2*i+1][0], self._node_range[2*i+2][1])

  def _update_single_node(self,i):
    self._tree[i] = self._func(self._tree[2*i+1], self._tree[2*i+2])

  def point_update(self,i,x):
    
    j = self._N-1+i
    self._tree[j] = x
    while j>0:
      j = (j-1)//2
      self._update_single_node(j)

  def range_query(self, l, r, on=0):
    
    
    onl, onr = self._node_range[on]
    
    
    if l<=onl and onr<=r:
      return self._tree[on]
    
    
    elif onr<=l or r<=onl:
      return self._identity
    
    else:
      return self._func(self.range_query(l, r, on=2*on+1),
                        self.range_query(l, r, on=2*on+2))

st = SegTree(_gcd, A, identity=None)

answer = 0
for i in range(len(A)):
  st.point_update(i, A[i-1])
  answer = max(answer, st._tree[0])
  st.point_update(i, A[i])

print(answer)

Solution (Python):
from fractions import gcd
N = int(input())
Alist = [0]+list(map(int, input().split()))+[0]
Rlist = [0]*(N+2)
Llist = [0]*(N+1)
for i in range(N):
    Llist[i+1] = gcd(Llist[i], Alist[i])
    Rlist[N-i] = gcd(Rlist[N-i+1], Alist[N-i+1])
print(max([gcd(Rlist[i], Llist[i]) for i in range(1,N+1)]))

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

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

l = [0 for i in range(n+1)]
r = [0 for i in range(n+1)]

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

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

m = [0 for i in range(n)]

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

print(max(m))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

def Lgcd(i):
    if i == 0:
        return 0
    if i == 1:
        return a_s[0]
    if i in Lmemo:
        return Lmemo[i]
    re = gcd(Lgcd(i - 1), a_s[i - 1])
    Lmemo[i] = re
    return re


def Rgcd(i):
    if i == n - 1:
        return 0
    if i == n - 2:
        return a_s[-1]
    if i in Rmemo:
        return Rmemo[i]
    re = gcd(Rgcd(i + 1), a_s[i + 1])
    Rmemo[i] = re
    return re


def gcd(a, b):
    if a * b == 0:
        return a + b
    while 1:
        a, b = b, a % b
        if b == 0:
            return a


Lmemo = {}
Rmemo = {}
n = int(input())
a_s = list(map(int, input().split()))
mx = 0
for i in range(n):
    g = gcd(Lgcd(i),Rgcd(i))
    if g > mx:
        mx = g
print(mx)

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


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

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

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

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

print(max(m))

Solution (Python):
N = int(input())
A = [0] + list(map(int, input().split())) + [0]


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


B = [0 for i in range(N+2)]
C = [0 for i in range(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])
print(max(gcd(B[i-1], C[i+1]) for i in range(1, N+1)))

Solution (Python):
import fractions 

from functools import reduce

N=int(input())

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



dp=[0 for _ in range(N+1)]
dpRev=[0 for _ in range(N+1)]

def gcd(a,b):
	if a == 0: 
		return b
	elif b == 0: 
		return a
	
	return reduce(fractions.gcd, (a,b))

dp[0]=0
for n in range(0,N):
	dp[n+1]=gcd(dp[n],A[n])

dpRev[N]=0
for n in range(N-1,-1,-1):
	dpRev[n]=gcd(A[n],dpRev[n+1])





ans=0
for n in range(N):
	ans = max( ans, gcd(dp[n],dpRev[n+1]) )


print(ans)

Solution (Python):

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

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



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

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

print(max(G))

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 = []

for i in range(N-1):
  if i == 0:
    L.append(A[0])
  else:
    L.append(gcd(L[i-1], A[i]))

for i in range(N-1):
  if i == 0:
    R.append(A[N-1])
  else:
    R.append(gcd(R[i-1], A[N-1-i]))

M = []
for i in range(N):
  if i == 0:
    M.append(R[N-2])
  elif i == N-1:
    M.append(L[N-2])
  else:
    M.append(gcd(L[i-1], R[N-2-i]))

print(max(M))

Solution (Python):
from functools import reduce

result = 0

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

n = int(input())
A = list(map(int, input().split()))
X = [0 for i in range(n)]
Y = [0 for i in range(n)]
for i in range(1, n):
    X[i] = gcd(X[i-1], A[i-1])
for i in range(n-1)[::-1]:
    Y[i] = gcd(Y[i+1], A[i+1])
print(max(gcd(X[i], Y[i]) for i in range(n)))

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

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

init=1


B1=[]
for i in A:
    if init==1:
        a=i
        init=0
        B1.append(a)
    else:
        a=gcd(a,i)
        B1.append(a)

init=1
B2=[]
for i in A[::-1]:
    if init==1:
        a=i
        init=0
        B2.append(a)
    else:
        a=gcd(a,i)
        B2.append(a)



result=0
for i in range(n):
    if i==0:
        if result<B2[n-2]:
            result=B2[n-2]
    elif i==n-1:
        if result<B1[n-2]:
            result=B1[n-2]
    else:
        if result<gcd(B1[i-1],B2[n-i-2]):
            result=gcd(B1[i-1],B2[n-i-2])

print(result)

Solution (Python):
import sys
from fractions import gcd

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

if N == 2:
    print(max(A))
    sys.exit(0)

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(1, N):
    L[i] = gcd(L[i-1], A[i])
    R[-i-1] = gcd(R[-i], A[-i-1])




max_gcd = max(L[-2], R[1])
for i in range(1, N-1):
    
    max_gcd = max(max_gcd, gcd(L[i-1], R[i+1]))

print(max_gcd)

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] * (N + 1)
R = [0] * (N + 1)
for i in range(N):
    L[i+1] = gcd(L[i], A[i])
 
for i in range(N-1, -1, -1):
    R[i] = gcd(R[i+1], A[i])
 
ans = 1
for i in range(N):
    ans = max(ans, gcd(L[i], R[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()))

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

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

print(max(M))

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] * (N + 1)
R = [0] * (N + 1)
for i in range(N):
    L[i+1] = gcd(L[i], A[i])

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

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

print(ans)

Solution (Python):
import fractions

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

ans = 0

l = [0]
r = [0]

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

ans = 0

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

print(ans)

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


def gcd(x,y):
    a = max(x,y)
    b = min(x,y)
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

L = [A[0]]
R = [A[N-1]]*N
for i in range(N-1):
    L.append(gcd(L[i], A[i+1]))
    R[N-i-2]=gcd(R[N-i-1],A[N-i-2])
can = []
for i in range(N):
    if i == 0:
        can.append(R[1])
    elif i == N-1:
        can.append(L[N-2])
    else:
        can.append(gcd(L[i-1], R[i+1]))

print(max(can))

Solution (Python):


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

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








L = [0] * N
R = [0] * N

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

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

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


N = int(input())
A = list(map(int, input().split()))
R = [0 for i in range(N - 1)]
L = [0 for i in range(N - 1)]
R[0] = A[0]
L[0] = A[-1]
for i in range(1, N - 1):
    R[i] = gcd(R[i - 1], A[i])
for j in range(1, N - 1):
    L[j] = gcd(L[j - 1], A[-1 - j])
    
t = 0
for i in range(N - 2):
    
    t = max(t, gcd(R[i], L[N - 3 - i]))
print(max(t, R[-1], L[-1]))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort()
def divisors(n):
    div = set()
    A = 1
    while A*A <= n:
        if n%A == 0:
            if A*A == n:
                div.add(A)
            else:
                B = n//A
                div.add(A)
                div.add(B)
        A += 1
    return div
div = list(divisors(A[0]) | divisors(A[1]))
div.sort(reverse=True)
for x in div:
    count = 0
    for a in A:
        if a%x != 0:
            count += 1
        if count > 1:
            break
    if count <= 1:
        print(x)
        break

Solution (Python):
n = int(input())
a = list(map(int ,input().split()))
l = [a[0]]
r = [a[-1]]
def gcd(a, b):
    while a % b:
        a, b = b, a % b
    return b

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

print(max(ans))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort()
def divisors(n):
    div = set()
    A = 1
    while A*A <= n:
        if n%A == 0:
            if A*A == n:
                div.add(A)
            else:
                B = n//A
                div.add(A)
                div.add(B)
        A += 1
    return div
div = list(divisors(A[0]) | divisors(A[1]))
m = 0
for x in div:
    count = 0
    for a in A:
        if a%x != 0:
            count += 1
        if count > 1:
            break
    if count <= 1:
        m = max(m, x)
print(m)

Solution (Python):
from fractions import gcd

N = int(input())
nums = [int(i) for i in input().split()]

fort = [nums[0], gcd(nums[0], nums[1])]
back = [nums[N-1], gcd(nums[N-1], nums[N-2])]
ans = []

for i in range(N-2):
    fort.append(gcd(fort[i+1], nums[i+2]))
                
for i in range(N-2):
    back.append(gcd(back[i+1], nums[N-(i+3)]))
    
for i in range(1, N-1):
    ans.append(gcd(fort[i-1], back[N-2-i]))

ans.append(fort[N-2])
ans.append(back[N-2])

print(max(ans))

Solution (Python):
import numpy as np
from fractions import gcd
from functools import reduce

n = int(input())

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

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

right = [a[-1]] * n
for i in range(0, n - 1)[::-1]:
    right[i] = gcd(right[i + 1], a[i])
left.insert(0, right[1])
right.append(left[-2])

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

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
A.sort()
def find(inte):
    re = []
    for i in range(1,int(inte**0.5)+1):
        if inte%i==0:
            re.append(i)
            if i != inte/i:
                re.append(int(inte/i))
    return set(re)
if n==2:
    print(A[1])
else:
    a1 = find(A[0])
    a2 = find(A[1])
    a3 = find(A[2])
    t1 = a1 & a2
    t2 = a2 & a3
    t3 = a1 & a3
    cond = t1 | t2 |t3
    maxi = 0
    for con in cond:
        count = 0
        flag = True
        for a in A:
            if a%con != 0:
                count +=1
            if count >= 2:
                
                flag = False
                break
        if flag and maxi < con:
            maxi = con
    print(maxi)

Solution (Python):
from fractions import gcd

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

def divs(p):
	div=set()
	for n in range(1, int(p**0.5)+1):
		if p % n == 0:
			div.add(n)
			div.add(p // n)
	return div


for d in sorted(divs(A[0]) | divs(A[1]), reverse=True):
	if sum([0 if a % d == 0 else 1 for a in A]) < 2:
		break

print(d)

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()))

def gcd(x_,y_):
	x = max(x_, y_)
	y = min(x_, y_)
	if y==0:
		return x
	if x%y == 0:
		return y
	return gcd(y, x%y)

L = [0]*n
R = [0]*n
L[0] = a[0]
R[-1] = a[-1]

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

L = [0]+L
R = R + [0]
M = [0]*n
for i in range(n):
	M[i] = gcd(L[i],R[i+1])
print(max(M))

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

N=int(input())
A=[int(i) for i in input().split()]
X=[0 for i in range(N)]
Y=[0 for i in range(N)]
for i in range(1,N):
    X[i]=gcd(X[i-1],A[i-1])
for i in range(N-1)[::-1]:
    Y[i]=gcd(Y[i+1],A[i+1])
Z=[gcd(X[i],Y[i]) for i in range(N)]
print(max(Z))

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

def gcd_l(A):
    if len(A) == 1:
        return A[0]
    x = gcd(A[0], A[1])
    if len(A) == 2:
        return x
    for i in range(2, len(A)):
        x = gcd(x, A[i])
    return x

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

B = []
C = []
x = A[0]
y = A[-1]
for i in range(N):
    x = gcd(x, A[i])
    B.append(x)
    y = gcd(y, A[N-1-i])
    C.append(y)
    
ans = 1

for i in range(1, N-1):
    ans = max(ans, gcd(B[i-1], C[N-2-i]))

ans = max(ans, B[N-2], C[N-2])

print(ans)

Solution (Python):
from fractions import gcd

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


X=[0 for i in range(N)]
Y=[0 for i in range(N)]

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

Z=[gcd(X[i],Y[i]) for i in range(N)]
print(max(Z))

Solution (Python):
N = int(input())
Alist = list(map(int, input().split()))
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b <= 0:
        return a
    else:
        return gcd(b, a%b)
left = [0, Alist[0]]
right = [Alist[-1]]
for i in range(N-1):
    left += [gcd(Alist[i+1], left[-1])]
    right += [gcd(Alist[N-i-2], right[-1])]
right = right[::-1] + [0]
print(max([gcd(left[i], right[i+1]) for i in range(N)]))

Solution (Python):
from sys import stdin

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

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

left = []
right = []

for i in range(n):
  if(i==0):
    before = an[i]
  else:
    before = gcd(before, an[i])
  left.append(before)

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

max = 0
for i in range(n):
  if(i==0):
    before = right[n-2];
  elif(i==(n-1)):
    before = left[n-2];
  else:
    before = gcd(left[i-1],right[n-2-i])
  if(max < before ):
    max = before

print(max)

Solution (Python):
import fractions
from functools import reduce
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

n=int(input())
a=[int(x) for x in input().split()]
l=[0]
r=[0]
for i in range(n):
    l.append(fractions.gcd(l[i],a[i]))
    r.append(fractions.gcd(r[i],a[(i+1)*-1]))
r = r[::-1]
m = []
for i in range(n):
    m.append(fractions.gcd(l[i],r[i+1]))
print(max(m))

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

from fractions import gcd

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap



@mt
def slv(N, A):
    L = [A[0]]
  
    for a in A[1:]:
        L.append(gcd(L[-1], a))
        
    R = [-1] * N
    R[-1] = A[-1]
    for i in range(N-2, -1, -1):
        R[i] = gcd(R[i+1], A[i])
    
    ans = 0
    for i in range(N):
        if i == 0:
            ans = max(ans, R[i+1])
        elif i == N-1:
            ans = max(ans, L[i-1])
        else:
            ans = max(ans, gcd(L[i-1], R[i+1]))
    return ans
    


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

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

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


forwards = [A[0]]
backwards = [A[-1]]
for i in range(1, N):
    forwards.append(gcd(forwards[-1], A[i]))
    backwards.append(gcd(backwards[-1], A[-(i+1)]))
backwards = list(reversed(backwards))

ans = 0
for i in range(1, N-1):
    a = gcd(forwards[i-1], backwards[i+1])
    if a > ans:
        ans = a

if N > 2:
    ans = max([ans, forwards[-2], backwards[1]])
elif N == 2:
    ans = max(A)
else:
    ans = A[0]

print(ans)

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

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

noskip = gcd(a[0], a[1])
skipped = max(gcd(a[0], a[2]), gcd(a[1], a[2]))

for i in range(2, n):
    skipped = max(gcd(skipped, a[i]), noskip)
    noskip = gcd(noskip, a[i])

print(max(noskip, skipped))

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

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

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

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

M = list()
for i in xrange(N):
    M.append(gcd(L[i], R[i+1]))

print max(M)

Solution (Python):
from fractions import gcd

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

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

B=list(reversed(B))

print(max([A[N-2], B[1]] + [gcd(A[i-1], B[i+1]) for i in range(1,N-1)]))

Solution (Python):
from fractions import gcd

N = int(input())
A = [int(x) for x in 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 range(N - 1, 0, -1):
    R[i] = gcd(R[i + 1], A[i])

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

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

import fractions

head = {}
head[0] = aa[0]
for i in range(1, n):
    head[i] = fractions.gcd(head[i - 1], aa[i])

tail = {}
tail[n - 1] = aa[-1]
for i in range(n - 2, -1, -1):
    tail[i] = fractions.gcd(tail[i + 1], aa[i])

candidate = 1
for i in range(0, n):
    if i == 0:
        gcd2 = tail[i + 1]
        candidate = max(gcd2, candidate)
    elif i == n - 1:
        gcd1 = head[i - 1]
        candidate = max(gcd1, candidate)
    else:
        gcd1 = head[i - 1]
        gcd2 = tail[i + 1]
        candidate = max(fractions.gcd(gcd1, gcd2), candidate)

print(candidate)

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
L = [0] * N
R = [0] * N
for i in range(N - 1):
    L[i + 1] = gcd(L[i], A[i])
for i in range(N - 1, 0, -1):
    R[i - 1] = gcd(R[i], A[i])
ans = 0
for i in range(N):
    ans = max(gcd(L[i], R[i]), ans)
print(ans)

Solution (Python):
def main():
	N = int(input())
	A = list(map(int, input().split()))
	dp_l = [0]*(N+2)
	dp_r = [0]*(N+2)

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

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

	ans = 1
	for i in range(1, N+1):
		this_ans=gcd(dp_l[i-1], dp_r[i])
		ans = max(ans, this_ans)

	print(ans)



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

if __name__ == '__main__':
    main()

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

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

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




resmax = 1
for i in range(n):
	if i == 0:
		res = l[n-1-1]
	elif i == n-1:
		res = r[n-1-1]
	else:
		res = gcd(r[i-1], l[n-1-i-1])
	if res > resmax:
		resmax = res

print(int(resmax))

Solution (Python):
from functools import reduce

def gcd(m, n):
    
    tmp = max(m,n)
    n = min(m,n)
    m = tmp
    
    if n == 0:
        return m
    
    return gcd(n, m % n)

def gcda(arr, left, right):
    return reduce((lambda a,b:gcd(a,b)), arr[left:right+1], gcd(arr[left], arr[right]))

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

if n == 2:
    print(max(nums))
    exit()

left_n = [0 for _ in range(n)]
right_n = [0 for _ in range(n)]

left_n[0] = nums[0]
right_n[n-1] = nums[n-1]

for i in range(n-1):
    left_n[i+1] = gcd(left_n[i], nums[i+1])
    right_n[n-2-i] = gcd(right_n[n-1-i], nums[n-2-i])




maxgcd = 0
for i in range(n):
    if i == 0:
        maxgcd = max(maxgcd, right_n[1])
    if i == n-1:
        maxgcd = max(maxgcd, left_n[n-2])
    else:
        maxgcd = max(maxgcd, gcd(right_n[i+1], left_n[i-1]))

print(maxgcd)

Solution (Python):
import sys
input=sys.stdin.readline

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

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=0
for i in range(n):
    ans=max(ans,gcd(L[i],R[i+1]))
print(ans)

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()]

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
input = sys.stdin.readline
 
n = int(input())
a = list(map(int,input().split()))
a.sort()

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

def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a

def divisor(n):
    ass = []
    for i in range(1,int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            if i**2 == n:
                continue
            ass.append(n//i)
    return ass 
 

res = gcdlist(a[1::])
 

p = sorted(divisor(a[0]))
for i in range(len(p)-1,-1,-1):
    if p[i]<=res:
        break
    cnt = 1
    for j in range(1,n):
        if a[j]%p[i]==0:
            cnt += 1
    if cnt >= n-1:
        res = p[i]
        break
print(res)

Solution (Python):
def gcd(x,y):
    if x<y:
        x,y=y,x
    
    if y==0:
        return x
    if x%y==0:
        return y
    else:
        return gcd(y,x%y)
 
N=int(input())
A=[int(i) for i in input().split()]
X=[0 for i in range(N)]
Y=[0 for i in range(N)]
for i in range(1,N):
    X[i]=gcd(X[i-1],A[i-1])
for i in range(N-1)[::-1]:
    Y[i]=gcd(Y[i+1],A[i+1])
Z=[gcd(X[i],Y[i]) for i in range(N)]
print(max(Z))

Solution (Python):

import fractions
N = int(input())
A = list(map(int, input().split()))
x = [0] * N
y = [0] * N
tmp = A[0]
for i in range(1, N):
    t = fractions.gcd(tmp, A[i])
    x[i] = int(tmp / t)
    tmp = t
tmp = A[N - 1]
for i in range(N - 2, -1, -1):
    t = fractions.gcd(tmp, A[i])
    y[i] =  int(tmp / t)
    tmp = t
m = 0
for i in range(1, N - 1):
    a = fractions.gcd(x[i], y[i])
    if m < a:
        m = a
print(tmp * max(m, y[0], x[N - 1]))

Solution (Python):
import itertools
N = int(input())
A = [int(_) for _ in input().split()]
gcd = lambda x, y: gcd(y, x % y) if x % y else y
L = list(itertools.accumulate(A, func=gcd))
R = list(itertools.accumulate(A[::-1], func=gcd))[::-1]
ans = max(R[1], L[-2])
for i in range(1, N - 1):
    ans = max(ans, gcd(L[i - 1], R[i + 1]))
print(ans)

Solution (Python):
import fractions
import functools
import sys

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

if len(a) == 2:
  print(max(a))
  sys.exit()
  
gcd_a = functools.reduce(fractions.gcd, a)
a=[i//gcd_a for i in a]

so = 0
cnt = 0
gcd_0 = functools.reduce(fractions.gcd, a[1:])
for i in range(1,len(a)):
  if fractions.gcd(a[0],a[i]) == 1:
    so = i
    cnt+=1
    if cnt == 2:
      print(max(gcd_a,gcd_0*gcd_a))
      sys.exit()

a_i = [i for i in a if i != a[so]]
gcd_i = functools.reduce(fractions.gcd, a_i)

print(max(gcd_0,gcd_i)*gcd_a)

Solution (Python):
def m(a, b):
    if a > b:
        a, b = b, a
    while b % a:
        a, b = b % a, a
    return a


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

if N == 2:
    print(max(A))
else:
    AF = [1] * (N - 1)
    AB = [1] * (N - 1)

    AF[0] = A[0]
    AB[0] = A[-1]

    for i in range(1, N - 1):
        AF[i] = m(AF[i - 1], A[i])
        AB[i] = m(AB[i - 1], A[- (i + 1)])
    L = max(AF[-1], AB[-1], max([m(AF[i], AB[N - 3 - i]) for i in range(N - 2)]))

    print(L)

Solution (Python):
def m(a, b):
    if a > b:
        a, b = b, a
    while b % a:
        a, b = b % a, a
    return a


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

if N == 2:
    print(max(A))
else:
    AF = [1] * (N - 1)
    AB = [1] * (N - 1)

    AF[0] = A[0]
    AB[0] = A[-1]

    for i in range(1, N - 1):
        AF[i] = m(AF[i - 1], A[i])
        AB[i] = m(AB[i - 1], A[- (i + 1)])
    L = max(AF[-1], AB[-1], max([m(AF[i], AB[N - 3 - i]) for i in range(N - 2)]))

    print(L)

Solution (Python):
from functools import reduce


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


N = int(input())
a_list = list(map(int, input().split()))
gcd_list = [a_list[-1]]
for idx in range(N - 2, 0, -1):
    gcd_list.append(gcd(gcd_list[N - 2 - idx], a_list[idx]))
max_gcd_a = gcd_list[-1]
gcd_list.append(1)
gcd_list.reverse()

for idx in range(1, N):
    if idx != 1:
        left_gcd = gcd(left_gcd, a_list[idx - 1])
    else:
        left_gcd = a_list[0]
    if idx < N - 1:
        right_gcd = gcd_list[idx + 1]
    else:
        right_gcd = left_gcd

    gcd_a = gcd(left_gcd, right_gcd)
    if max_gcd_a < gcd_a:
        max_gcd_a = gcd_a

print(max_gcd_a)

Solution (Python):
from functools import reduce
import numpy as np


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


def gcd(*n):
    return reduce(_gcd, n)


def my_bisect(x):

    def find_pos(left, right):
        return left + (right - left - 1) // 2

    def func(left, right, prev):
        if right - left == 1:
            return left
        pos = find_pos(left, right)
        g1 = gcd(*x[left:pos+1])
        g2 = gcd(*x[pos+1:right])
        if prev is not None:
            g1, g2 = gcd(g1, prev), gcd(g2, prev)
        if g1 < g2:
            return func(left, pos + 1, max(g1, g2))
        else:
            return func(pos + 1, right, max(g1, g2))
    return func(0, len(x), None)


N = int(input())
A = list(map(int, input().split()))
print(gcd(*np.delete(A, my_bisect(A))))

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


N = int(input())
A = input().split()
for i in range(N):
    A[i] = int(A[i])
    
if N == 2:
    print(max(A[0],A[1]))
if N >= 3:
    gcd_all = gcd(A[0], A[1])
    gcd_without_1 = A[0]
    gcd_without_2 = A[1]
    gcd_without_1 = max(gcd_all, max( gcd(gcd_without_1, A[i]),gcd(gcd_without_2, A[i])))
    gcd_all = gcd(gcd_all,A[2])
    for i in range(3,N):
        gcd_without_1 = max(gcd_all, gcd(gcd_without_1, A[i]))
        gcd_all = gcd(gcd_all,A[i])
    print(max(gcd_all, gcd_without_1))