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):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


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




class SegmentTree:
    def __init__(self, init_val, ide_ele, op):
        n = len(init_val)
        self.num = 2 ** (n - 1).bit_length()
        self.seg = [ide_ele] * 2 * self.num
        self.ide_ele = ide_ele
        self.op = op
        for i in range(n):
            self.seg[i + self.num - 1] = init_val[i]
            
        for i in range(self.num - 2, -1, -1):
            self.seg[i] = self.op(self.seg[2 * i + 1], self.seg[2 * i + 2])


    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k + 1:
            k = (k - 1) // 2
            self.seg[k] = self.op(self.seg[k * 2 + 1], self.seg[k * 2 + 2])


    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num - 1
        q += self.num - 2
        res = self.ide_ele
        while q - p > 1:
            if p & 1 == 0:
                res = self.op(res, self.seg[p])
            if q & 1 == 1:
                res = self.op(res, self.seg[q])
            p = p // 2
            q = (q - 2) // 2
        if p == q:
            res = self.op(res, self.seg[p])
        else:
            res = self.op(self.seg[p], self.op(res, self.seg[q]))
        return res



n = I()
L = LI()
ST = SegmentTree(L, 0, gcd)
print(max([gcd(ST.query(0, i), ST.query(i + 1, n)) for i in range(n)]))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


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




class SegmentTree:
    def __init__(self, init_val, ide_ele, op):
        n = len(init_val)
        self.num = 2 ** (n - 1).bit_length()
        self.seg = [ide_ele] * 2 * self.num
        self.ide_ele = ide_ele
        self.op = op
        for i in range(n):
            self.seg[i + self.num - 1] = init_val[i]
            
        for i in range(self.num - 2, -1, -1):
            self.seg[i] = self.op(self.seg[2 * i + 1], self.seg[2 * i + 2])


    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k + 1:
            k = (k - 1) // 2
            self.seg[k] = self.op(self.seg[k * 2 + 1], self.seg[k * 2 + 2])


    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num - 1
        q += self.num - 2
        res = self.ide_ele
        while q - p > 1:
            if p & 1 == 0:
                res = self.op(res, self.seg[p])
            if q & 1 == 1:
                res = self.op(res, self.seg[q])
                q -= 1
            p = p // 2
            q = (q - 1) // 2
        if p == q:
            res = self.op(res, self.seg[p])
        else:
            res = self.op(self.seg[p], self.op(res, self.seg[q]))
        return res



n = I()
L = LI()
ST = SegmentTree(L, 0, gcd)
print(max([gcd(ST.query(0, i), ST.query(i + 1, n)) for i in range(n)]))

Solution (Python):
import fractions

n = int(input())
A_list = list(map(int, input().split()))
Left = [0]*(n+1)
Right = [0]*(n+1)
for i in range(n):
    Left[i+1] = fractions.gcd(Left[i], A_list[i])
    Right[n-i-1] = fractions.gcd(Right[n-i], A_list[n-i-1])
gcd_max = 0
for del_pos in range(n):
    gcd_ = fractions.gcd(Left[del_pos], Right[del_pos+1])
    if gcd_max < gcd_:
        gcd_max = gcd_
print(gcd_max)

Solution (Python):

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

import fractions

L = []

R = []


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

R.append(0)
for j in range(N-1, 0, -1):
  r = fractions.gcd(R[-1], A[j])
  R.append(r)
R.reverse()

ans = 0
for n in range(N):
  lr = fractions.gcd(L[n], R[n])
  ans = max(ans, lr)

print(ans)

Solution (Python):

from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
lcg = A[:]
rcg = A[:]
for i in range(1, N):
  lcg[i] = gcd(lcg[i], lcg[i - 1])
for i in range(N - 2, -1, -1):
  rcg[i] = gcd(rcg[i], rcg[i + 1])
t = [0] * N
t[0] = rcg[1]
for i in range(1, N - 1):
  t[i] = gcd(lcg[i - 1], rcg[i + 1])
t[N - 1] = lcg[N - 2]
print(max(t))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()


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


l = list(accumulate(A, lambda x, y: gcd(x, y)))
r = list(reversed(list(accumulate(reversed(A), lambda x, y: gcd(x, y)))))
ret = max(l[n-2], r[1])
for i in range(n-2):
    ret = max(ret, gcd(l[i], r[i+2]))


print(ret)

Solution (Python):
import fractions
n = int(input())
a = [0] + list(map(int, input().split())) + [0]


left = [0] * (n + 2)
right = [0] * (n + 2)

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

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

for i in range(1, n):
    right[-i - 1] = fractions.gcd(right[-i], a[-i - 1])

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

Solution (Python):
import fractions
N = int(input())
A = list(map(int,input().split()))
L = [A[0]]
R = [A[-1]]

for i in range(1,N):
  L.append(fractions.gcd(L[-1],A[i]))
for j in range(N-2,-1,-1):
  R.append(fractions.gcd(R[-1],A[j]))

ans = 0
for m in range(N):
  if m == 0:
    ans = R[-2]
  elif m == N - 1:
    ans = max(ans,L[N-2])
  else:
    M = fractions.gcd(L[m-1],R[N-m-2])
    ans = max(ans,M)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans1 = a[1]
for i in range(1,n):
  big,sml = a[i],ans1
  while sml != 0:
    big,sml = sml,big%sml
  ans1 = big
ans2 = a[-2]
for i in range(n-1):
  big,sml = a[i],ans2
  while sml != 0:
    big,sml = sml,big%sml
  ans2 = big
l1 = [-1]
now = a[0]
for i in range(1,n):
  big,sml = now,a[i]
  while sml != 0:
    big,sml = sml,big%sml
  l1.append(now // big)
  now = big
l2 = [-1]
now = a[-1]
for i in range(n-2,-1,-1):
  big,sml = now,a[i]
  while sml != 0:
    big,sml = sml,big%sml
  l2.append(now // big)
  now = big
l2.reverse()
l3 = [-1]
for i in range(1,n-1):
  big,sml = l1[i],l2[i]
  while sml != 0:
    big,sml = sml,big%sml
  l3.append(big)
l3.append(-1)
now = a[0]
for i in range(n):
  big,sml = now,a[i]
  while sml != 0:
    big,sml = sml,big%sml
  now = big
ans3 = now * max(l3)





print(max(ans1,ans2,ans3))

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

A.sort()

def divisor(n):
    L = [] 
    i = 1
    while i <= n**0.5:
        if i == n**0.5:
            L.append(i)
        if n%i == 0:
            L.append(i)
            L.append(n/i)
        i += 1
    L.sort(reverse=True)
    return L

List = divisor(A[0]) + divisor(A[1])
List.sort(reverse=True)

for i in range(len(List)):
    c = 0
    for j in range(N):
        if A[j]%List[i] == 0:
            c += 1
    if c >= N-1:
        ans = List[i]
        break

print(int(ans))

Solution (Python):
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
L = [0]
for i in range(1, N):
    L.append(gcd(A[i-1], L[i-1]))
R = [0]
for i in range(1, N):
    R.append(gcd(R[i-1], A[N-i]))
R.reverse()
ans = []
for i in range(0, N):
    ans.append(gcd(L[i], R[i]))
print(max(ans))

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

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

L=[]
R=[]
for i in range(N):
    L.append(0)
    R.append(0)

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

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

M=[]
for i in range(N):
    M.append(myGCD(R[i],L[i]))



print(max(M))

Solution (Python):
import fractions

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

X=[0]*N
X[1]=A[0]
for i in range(2, N):
  X[i]=fractions.gcd(X[i-1], A[i-1])

Y=[0]*N
Y[-2]=A[-1]
for i in range(N-3, -1, -1):
  Y[i]=fractions.gcd(Y[i+1], A[i+1])
  
X[0]=Y[0]
Y[-1]=X[-1]

Z=[fractions.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()))
l=[a[0]]
r=[a[-1]]
for i in range(n-1):
  l.append(fractions.gcd(l[i],a[i+1])) 
for i in range(n-1):
  r.append(fractions.gcd(r[i],a[-2-i])) 
m=[l[n-2],r[n-2]]
for i in range(n-2):
  m.append(fractions.gcd(l[i],r[n-i-3]))
print(max(m))

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

    n, *a = map(int, open(0).read().split())

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

if __name__ == "__main__":
    f_c()

Solution (Python):
import math
import fractions

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

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

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

GCD = [0 for _ in range(N)]

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

print(max(GCD))

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

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

pre=[0,a[0]]
suf=[0]*(n+1)
suf[n-1]=a[-1]
for i in range(1,len(a)):
    pre.append(gcd(pre[-1],a[i]))
for i in range(len(a)-2,-1,-1):
    suf[i] = gcd(suf[i+1],a[i])
ans=1
for i in range(len(a)):
    ans = max(ans,gcd(pre[i],suf[i+1]))
print(ans)

Solution (Python):
def main():

    N = int(input())
    A = list(map(int, input().split()))
    prefix = [A[0]]
    for i in range(1, N):
        prefix.append(gcd(prefix[-1], A[i]))
    suffix = [A[N-1]]
    for i in range(N-2, -1, -1):
        suffix.append(gcd(suffix[-1], A[i]))
    suffix = suffix[::-1]
    
    

    ans = 1
    for i in range(N):
        if i == 0:
            ans = max(ans, suffix[1])
        elif i == N-1:
            ans = max(ans, prefix[-2])
        else:
            ans = max(ans, gcd(prefix[i-1], suffix[i+1]))
    return ans

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

if __name__ == '__main__':
    print(main())

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


N = int(input())
a = list(map(int, input().split()))
max_gcd = 0
L = [0] * (N + 2)
R = [0] * (N + 2)

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

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

Solution (Python):
import fractions

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

left = [0] * N
right = [0] * N

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

right[N-1] = A[N-1]
for j in range(N-2,0,-1):
    right[j] = fractions.gcd(A[j],right[j+1])

can = [0] * N
can[0] = right[1]
for k in range(1,N-1):
    can[k] = fractions.gcd(left[k-1],right[k+1])
can[N-1] = left[N-2]

print(max(can))

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

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

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

Solution (Python):

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


from fractions import gcd

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


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

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


left = [A[N-1]] + left
right.append(A[0])




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

print(ans)

Solution (Python):
def gcd(x, y):
  x, y=max(x, y), min(x, y)
  while y:
    x, y=y, x%y
  return x

N=int(input())
A=list(map(int, input().split()))
left_gcd=[1 for i in range(N)]
left_gcd[0]=A[0]
right_gcd=[1 for i in range(N)]
right_gcd[-1]=A[-1]
for i in range(1, N):
  left_gcd[i]=gcd(left_gcd[i-1], A[i])
for i in range(N-2, -1, -1):
  right_gcd[i]=gcd(right_gcd[i+1], A[i])
maxv=max(left_gcd[-2], right_gcd[1])
for i in range(1, N-1):
  maxv=max(maxv, gcd(left_gcd[i-1], right_gcd[i+1]))
print(maxv)

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


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


l = [0 for _ in range(n)]
l[0]=0
for i in range(1,n):
    l[i]=gcd(l[i-1],nums[i-1])

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

ans=0
for i in range(n):
    saidaikouyakusu_except_for_ni=gcd(l[i],r[i])
    if ans < saidaikouyakusu_except_for_ni:
        ans = saidaikouyakusu_except_for_ni
    else:pass

print(ans)

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

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

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

print(max_gcd)

Solution (Python):

from fractions import gcd
import sys
sys.setrecursionlimit(10 ** 7)


def solve(N: int, A: "List[int]"):
    L, R = [0], [0]
    for i in range(N - 1):
        L.append(gcd(L[i], A[i]))
        R.append(gcd(R[i], A[-i - 1]))
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(L[i], R[N - i - 1]))
    print(ans)
    return




def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)


if __name__ == '__main__':
    main()

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

Solution (Python):
from fractions import gcd

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

l = []
g = 0
for i in range(n):
  tmp = gcd(g, a[i])
  if tmp < g or g == 0:
    l.append(i)
  g = tmp

ans = 0
for i in l:
  g = 0
  for j in range(n):
    if i != j:
      g = gcd(g, a[j])
  ans = max(ans, g)
print(ans)

Solution (Python):
import fractions as m

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

gstackl = [0]*n
gstackr = [0]*n

ans = 0

for i in range(1, n):
	gstackl[i] = m.gcd(gstackl[i-1], al[i-1])
	gstackr[n-i-1] = m.gcd(gstackr[n-i], al[n-i])




for i in range(n):
	l = gstackl[i]
	r = gstackr[i]
	
	a = m.gcd(l, r)
	
	if a > ans:
		ans = a

print(ans)

Solution (Python):
class Segtree(object):
    def __init__(self,A,f,u):
        n=2**(len(A).bit_length())
        N=2*n-1
        self.__num=n
        self.__len=N
        self.__seg=[u]*N
        self.__f=f
        self.__u=u
        
        for i in range(len(A)): 
            self.__seg[n+i-1]=A[i]
        for i in range(n-2,-1,-1): 
            self.__seg[i]=f(self.__seg[2*i+1],self.__seg[2*i+2])

    def __repr__(self):
        return str(self.__seg)

    def get_tree(self):
        return self.__seg

    def update(k,v):
        "A[k]=v"
        k+=self.__num-1
        self.__seg[k]=v
        while k+1: 
            k=(k-1)//2 
            self.__seg[k]=self.__f(self.__seg[k*2+1],self.__seg[k*2+2])

    def query(self,p,q): 
        if q<=p: return self.__u
        p+=self.__num-1
        q+=self.__num-2
        res=self.__u
        while q-p>1:
            if p&1==0:
                res=self.__f(res,self.__seg[p])
            if q&1==1:
                res=self.__f(res,self.__seg[q])
                q-=1
            p=p//2
            q=(q-1)//2
        if p==q: res=self.__f(res,self.__seg[p])
        else: res=self.__f(self.__f(res,self.__seg[p]),self.__seg[q])
        return res


def resolve():
    from fractions import gcd
    n=int(input())
    A=list(map(int,input().split()))
    if n==2:
        print(max(A))
        return
    seg=Segtree(A,gcd,0)
    ans=0
    for i in range(n):
        ans=max(ans,gcd(seg.query(0,i),seg.query(i+1,n)))
    print(ans)

resolve()

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

res = []
for j in range(N):
    res.append(gcd(L[j], R[j]))

res = max(res)
print(res)

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

Ls = [0]
Rs = [0]
for i in range(N):
    la, lb = max([As[i], Ls[i]]), min([As[i], Ls[i]])
    Ls.append(gcd(la, lb))
    ra, rb = max([As[N - i - 1], Rs[i]]), min([As[N - i - 1], Rs[i]])
    Rs.append(gcd(ra, rb))
Ms = []
for i in range(N):
    a, b = max([Rs[N - i - 1], Ls[i]]), min([Rs[N - i - 1], Ls[i]])
    Ms.append(gcd(a, b))

print(max(Ms))

Solution (Python):
def slove():
    import sys
    import fractions
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    l = [0] * (n + 1)
    r = [0] * (n + 1)
    for i in range(n):
        l[i+1] = fractions.gcd(l[i], a[i])
        r[-i-2] = fractions.gcd(r[-i-1], a[-i-1])
    md = 0
    for i in range(n):
        md = max(md, fractions.gcd(l[i], r[i+1]))
    print(md)


if __name__ == '__main__':
    slove()

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

n=int(input())
a=[int(i) for i in input().split()]
l=[None for i in range(n)]
l[0]=a[0]
for i in range(1,n):
    l[i]=gcd(l[i-1],a[i])
r=[None for i in range(n)]
r[-1]=a[-1]
for i in range(n-2,-1,-1):
    r[i]=gcd(r[i+1],a[i])
res=max(r[1],l[-2])
for i in range(1,n-1):
    res=max(res,gcd(l[i-1],r[i+1]))
print(res)

Solution (Python):
from fractions import gcd

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

N = int(input())
A = getlist()

X = [0] * N
Y = [0] * N

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

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

ans = []

ans.append(X[-2])
ans.append(Y[1])

for i in range(1, N - 1):
	ans.append(gcd(X[i - 1], Y[i + 1]))


print(max(ans))

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


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

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

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

print(ans)

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

n=int(input())
a=[0] + list(map(int,input().split())) + [0]

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

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

print(max(z))

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

r=[-1]*N
l=[-1]*N
l[0]=a[0]
r[-1]=a[-1]

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

else:
    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=r[1]
    for k in range(1,N):
        if k==N-1:
            ans=max(ans,l[-2])
        else:
            ans=max(ans,gcd(l[k-1],r[k+1]))
    print(ans)

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

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

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

r = r[::-1]

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

print(ans)

Solution (Python):
from fractions import gcd


def resolve():
    n = int(input())
    aaa = [0] + list(map(int, input().split())) + [0]
    lll = [0 for i in range(n+2)]
    rrr = [0 for i in range(n+2)]
    for i in range(1, n+1):
        lll[i] = gcd(lll[i-1], aaa[i])
    for i in range(n,0,-1):
        rrr[i] = gcd(rrr[i+1], aaa[i])
    z = [gcd(lll[i-1],rrr[i+1]) for i in range(1,n+1)]
    print(max(z))
resolve()

Solution (Python):
import fractions;
n = int(input());
a = input().split(" ");
for i in range(n) :
	a[i] = int(a[i]);

a.insert(0,0);
l = [0]*(n+1);	
r = [0]*(n+2);

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


mxgcd = 0;
for i in range(1,n+1) :
	gcd_now = fractions.gcd(l[i-1],r[i+1]);
	if gcd_now > mxgcd :
		mxgcd = gcd_now;
	
print(mxgcd);

Solution (Python):
import fractions
N=int(input())
l1=list(map(int,input().split()))
l2=[0]*(N+1)
l3=[0]*(N+1)
for i in range(N):
  l2[i+1]=fractions.gcd(l2[i],l1[i])
for j in range(N-1,0,-1):
  l3[j-1]=fractions.gcd(l3[j],l1[j])
ans=1
for k in range(N):
  ans=max(ans,fractions.gcd(l2[k],l3[k]))
print(ans)

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

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

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

print(max(M))

Solution (Python):
import fractions
def gcd(x,y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x

    return fractions.gcd(x%y, y)

n = int(input())
a = list(map(int, input().split()))
L = [0]
R = [0]
now1 = 0
now2 = 0
ans = 0

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

    now2 = gcd(now2,a[-i-1])
    R.append(now2)

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

print(ans)

Solution (Python):
import fractions
def gcd(x,y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x

    return fractions.gcd(x%y, y)

n = int(input())
a = list(map(int, input().split()))
L = [0]
R = [0]
now1 = 0
now2 = 0
ans = 0

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

    now2 = gcd(now2,a[-i-1])
    R.append(now2)

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

print(ans)

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

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

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

print(max(M))

Solution (Python):
import fractions
N=int(input())
l1=list(map(int,input().split()))
l2=[0]*(N+1)
l3=[0]*(N+1)
for i in range(N):
  l2[i+1]=fractions.gcd(l2[i],l1[i])
for j in range(N-1,0,-1):
  l3[j-1]=fractions.gcd(l3[j],l1[j])
ans=1
for k in range(N):
  ans=max(ans,fractions.gcd(l2[k],l3[k]))
print(ans)

Solution (Python):
import fractions
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(n):
    l[i+1] = fractions.gcd(l[i],a[i])

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



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

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

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

if __name__ == '__main__':
    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] = euc(L[i], A[i])
    
    for i in range(N-1, -1, -1):
        R[i] = euc(R[i+1], A[i])
        
    max_gcd = 0
    for i in range(N):
        max_gcd = max(max_gcd, euc(L[i], R[i+1]))
    print(max_gcd)

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

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

l = []
r = []

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

m = []
for i in range(n):
    if i == 0:
        m.append(r[1])
    elif i == n-1:
        m.append(l[-2])
    else:
        m.append(gcd(l[i-1], r[i+1]))

print(max(m))

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from fractions import *

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


left = [A[0]]
right = [A[-1]]
for i in range(N-1):
    left.append(gcd(left[i], A[i+1]))
    right.append(gcd(right[i], A[-i-2]))


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

print(ans)

Solution (Python):
import fractions

N = int(input())
a = list(map(int,input().split()))
a1 = [0] * N
a2 = [0] * N
a1[0] = 0
a2[-1] = 0
a1[1] = a[0]
a2[-2] = a[-1]
ans = 0
for i in range(2,N):
    a1[i] = fractions.gcd(a1[i-1], a[i-1])
    a2[-(i+1)] = fractions.gcd(a2[-i], a[-i])
for a,b in zip(a1, a2):
    if a == 0:
        ans = max(ans, b)
    elif b == 0:
        ans = max(ans, a)
    else:
        ans = max(ans, fractions.gcd(a, b))
print(ans)

Solution (Python):
def slove():
    import sys
    import fractions
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    f = [0] * (n + 1)
    b = [0] * (n + 1)
    for i in range(n):
        f[i+1] = fractions.gcd(f[i], a[i])
        b[-i-2] = fractions.gcd(b[-i-1], a[-i-1])
    m_gcd = 0
    for i in range(n):
        m_gcd = max(m_gcd, fractions.gcd(f[i], b[i+1]))
    print(m_gcd)


if __name__ == '__main__':
    slove()

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors

A = sorted(A)
from collections import Counter
check = Counter(A)
for c,k in check.items():
    if k>=N-1:
        A=[c]



def del_dicts(dicts,i):
    del_list=[]
    for k,v in dicts.items():
        if v<i:
            del_list.append(k)
    for d in del_list:
        del dicts[d]
    return dicts

dicts = {}

for i in range(len(A)):
    if i<=2:
        for num in make_divisors(A[i]):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    
    else:
        del_list=[]
        
        for k in dicts.keys():
            if A[i]%k == 0:
                dicts[k]+=1
    dicts = del_dicts(dicts,i)
print(max(dicts.keys()))

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


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

if n == 2:
    print(max(nums))
else:
    l_gcds = [nums[0]]
    r_gcds = [nums[-1]]
    for i in range(1, n - 1):
        l = gcd(l_gcds[-1], nums[i])
        r = gcd(r_gcds[-1], nums[n - i - 1])
        l_gcds.append(l)
        r_gcds.append(r)
    r_gcds = r_gcds[::-1]
    gcds = [l_gcds[-1], r_gcds[0]]
    for i in range(1, n - 1):
        gcds.append(gcd(l_gcds[i - 1], r_gcds[i]))
    print(max(gcds))