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 fractions import gcd
N=int(input())
L=list(map(int,input().split()))
l=[0]*(N+1)
r=[0]*(N+1)
ans=0
for i in range(N):
  l[i+1]=gcd(L[i],l[i])
  r[N-i-1]=gcd(L[N-i-1],r[N-i])
for i in range(N):
  ans=max(ans,gcd(l[i],r[i+1]))
print(ans)

Solution (Python):
import fractions

N = int(input())
A = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
M = 1

for i in range(N):
  L[i+1] = fractions.gcd(L[i], A[i])
  R[N-i-1] = fractions.gcd(R[N-i], A[N-i-1])
  
if N == 2:
   print(max(A))
else:
  for i in range(N):
    M = max(M, fractions.gcd(L[i], R[i+1]))
  print(M)

Solution (Python):
import fractions

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

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


arr=[]
for i in range(N):
    a=fractions.gcd(L[i],R[N-i-1])
    arr.append(a)
    
print(max(arr))

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] * (N + 1)
L[0] = 0
R = [0] * (N + 1)
R[0] = 0

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

Solution (Python):
N = int(input())
li = [ int(it) for it in input().split() ]


def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
  
lg = [0]*N
lr = [0]*N

lg[0] = li[0]
for i in range(N-1):
  lg[i+1] = gcd(lg[i],li[i+1])
lr[-1] = li[-1]
for i in range(N-1):
  lr[-2-i] = gcd(lr[-1-i],li[-i-2])


s = 0
for i in range(N):
  if (0<i and i<N-1):
    s = max(s,gcd(lg[i-1],lr[i+1]))
  if (i==0):
    s = max(s,lr[i+1])
  if (i==N-1):
    s = max(s,lg[i-1])
print (s)

Solution (Python):
n = int(input())
dat_a = map(int, input().split())
dat_a = list(dat_a)
l = [0] * n
r = [0] * n
res = [0] * n
import fractions

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

for i in range(n-2, -1, -1):
    r[i] = fractions.gcd(r[i+1], dat_a[i+1])
for i in range(n):
    res[i] = fractions.gcd(l[i], r[i])

print(max(res))

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

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

if N <= 2:
    print(max(A))
    exit()

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

ans = 1
left_gcd = [0]
left_gcd[0] = A[0]
right_gcd = [0]
right_gcd[0] = A[N-1]

for i in range(1,N):
    if left_gcd[i-1] > A[i]:
        left_gcd.append(gcd(left_gcd[i-1], A[i]))
    else:
        left_gcd.append(gcd(A[i], left_gcd[i-1]))
    if right_gcd[i-1] > A[N-1-i]:
        right_gcd.append(gcd(right_gcd[i-1], A[N-1-i]))
    else:
        right_gcd.append(gcd(A[N-1-i], right_gcd[i-1]))
right_gcd.reverse()

g = [left_gcd[N-2], right_gcd[1]]

for i in range(1,N-1):
    l = left_gcd[i-1]
    r = right_gcd[i+1]
    if l > r:
        g.append(gcd(l,r))
    else:
        g.append(gcd(r,l))

print(max(g))

Solution (Python):
import sys
import fractions

def input(): return sys.stdin.readline()[:-1]
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 MAT(h): return [list(map(int, input().split())) for _ in range(h)]
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

def main():
    N = INT()
    A = LIST()
    L = [0] * N
    R = [0] * N
    M = [0] * N
    for i in range(N - 1):
        L[i + 1] = fractions.gcd(L[i], A[i])
        R[N - i - 2] = fractions.gcd(R[N - i - 1], A[N - i - 1])
    maxv = 0
    for i in range(N):
        M[i] = fractions.gcd(L[i], R[i])
        if maxv < M[i]:
            maxv = M[i]
    print(maxv)

if __name__ == '__main__':
    main()

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

def input(): return sys.stdin.readline()[:-1]
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 MAT(h): return [list(map(int, input().split())) for _ in range(h)]
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

def main():
    N = INT()
    A = LIST()
    L = [0] * N
    R = [0] * N
    M = [0] * 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])
    maxv = 0
    for i in range(N):
        M[i] = gcd(L[i], R[i])
        if maxv < M[i]:
            maxv = M[i]
    print(maxv)

if __name__ == '__main__':
    main()

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

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

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


rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

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

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()


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

def main():
  n=I()
  l=LI()

  l1=l[:]
  l2=l[:]

  for i in range(1,n):
    l1[i]=gcd(l1[i-1],l1[i])

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

  n+=1
  l1=[0]+l1
  l2=l2+[0]

  
  

  mx=0
  for i in range(n-1):
    mx=max(mx,gcd(l1[i],l2[i+1]))

  return mx

print(main())

Solution (Python):
from fractions import gcd

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

left = [0]*N
right = [0]*N
count = 0
left[0] = A[0]
right[-1] = A[-1]
Ans = []

for i in A[1:]:
    left[count+1] = gcd(left[count],i)
    count += 1

for i in reversed(A[:-1]):
    right[count-1] = gcd(right[count],i)
    count -= 1

Ans.append(right[1])
Ans.append(left[-2])
for i in range(N-2):
    Ans.append(gcd(left[i],right[i+2]))
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

class SegTree:
    """
    以下のクエリを処理する
    1.update:  i番目の値をxに更新する
    2.get_val: 区間[l, r)の値を得る
    """
 
    def __init__(self, n, func, init):
        """
        :param n: 要素数
        :param func: 値の操作に使う関数(min, max, addなど)
        :param init: 要素の初期値
        """
        n += 1
        self.n = n
        self.func = func
        self.init = init
        
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.init] * (n2 << 1)
 
    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.n2
        self.tree[i] = x
        while i > 1:
            self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1])
            i >>= 1
 
    def get_val(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        return self._get_val(a, b, 1, 0, self.n2)
 
    def _get_val(self, a, b, k, l, r):
        """
        [a, b)の値を得る内部関数
        :param k: 現在調べている区間のtree内index
        :param l, r: kが表す区間の左右端index [l, r)
        :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値
        """
        
        if r <= a or b <= l:
            return self.init
        
        if a <= l and r <= b:
            return self.tree[k]
        
        m = (l + r) // 2
        return self.func(
            self._get_val(a, b, k << 1, l, m),
            self._get_val(a, b, (k << 1) + 1, m, r)
        )

N=INT()
A=LIST()

st=SegTree(N+2, gcd, 0)
for i in range(N):
    st.update(i+1, A[i])

ans=0
for i in range(1, N+1):
    ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2)))
print(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

class SegTree:
    """
    以下のクエリを処理する
    1.update:  i番目の値をxに更新する
    2.get_val: 区間[l, r)の値を得る
    """
 
    def __init__(self, n, func, init):
        """
        :param n: 要素数
        :param func: 値の操作に使う関数(min, max, addなど)
        :param init: 要素の初期値
        """
        n += 1
        self.n = n
        self.func = func
        self.init = init
        
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.init] * (n2 << 1)
 
    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.n2
        self.tree[i] = x
        while i > 1:
            self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1])
            i >>= 1
 
    def get_val(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        return self._get_val(a, b, 1, 0, self.n2)
 
    def _get_val(self, a, b, k, l, r):
        """
        [a, b)の値を得る内部関数
        :param k: 現在調べている区間のtree内index
        :param l, r: kが表す区間の左右端index [l, r)
        :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値
        """
        
        if r <= a or b <= l:
            return self.init
        
        if a <= l and r <= b:
            return self.tree[k]
        
        m = (l + r) // 2
        return self.func(
            self._get_val(a, b, k << 1, l, m),
            self._get_val(a, b, (k << 1) + 1, m, r)
        )

N=INT()
A=[0]+LIST()+[0]

st=SegTree(N+2, gcd, 0)
for i in range(N+2):
    st.update(i, A[i])

ans=0
for i in range(1, N+1):
    ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2)))
print(ans)

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

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

ans = [0]*n
lileft = [0]*(n+1)
liright = [0]*(n+1)
for i in range(n):
  lileft[i+1] = gcd(lileft[i],li[i])
for i in range(n):
  liright[i+1] = gcd(liright[i],li[n-i-1])
for i in range(n):
  ans[i] = gcd(lileft[i],liright[n-i-1])
print(max(ans))

Solution (Python):
import copy

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

max_a = max(a)


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





l = [0] * n
r = [0] * n

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

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

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


tmp = 0
ans = 0

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

print(ans)

Solution (Python):
import fractions as fr

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

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

ans = 0
for i in range(N):
    ans = max(ans, fr.gcd(l[i],r[-i-1]))
print(ans)

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

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

ans = 0
for i in range(N):
    ans = max(ans, fr.gcd(l[i],r[-i-1]))
print(ans)

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

class SegAccumCalc:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def set_input(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new
    
    def get_input(self, n):
        return self.node[(self.N-1)+n]

    def get_output(self,
                   a=0, b=-1,
                   k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left = self.get_output(a,b,2*k+1,l,(l+r)//2)
            right = self.get_output(a,b,2*k+2,(l+r)//2,r)
            return self.calc(left,right)

N = int(input())
A = inpl()
sac = SegAccumCalc(A,calc=gcd)
ans = 0
for i in range(N):
    sac.set_input(i,0)
    g = sac.get_output()
    if g > ans:
        ans = g
    sac.set_input(i,A[i])
print(ans)

Solution (Python):
import fractions
n = int(input())
a = list(map(int,input().split()))
l = []
m = []
l.append(0)
m.append(0)
for i in range(n):
    l.append(fractions.gcd(l[i],a[i]))
    m.append(fractions.gcd(m[i],a[-(i+1)]))
b = [fractions.gcd(l[i],m[-(i+2)]) for i in range(n)]
print(max(b))

Solution (Python):
import fractions

n = int(input())
a = list(map(int, 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)]))
    
m = [fractions.gcd(l[i], r[-(i+2)]) for i in range(n)]
print(max(m))

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

class SegAccumCalc:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def set_value(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new

    def get(self,
            a=0, b=-1,
            k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left = self.get(a,b,2*k+1,l,(l+r)//2)
            right = self.get(a,b,2*k+2,(l+r)//2,r)
            return self.calc(left,right)

N = int(input())
A = inpl()
sac = SegAccumCalc(A,calc=gcd)
ans = 0
for i in range(N):
    if i == 0:
        c = sac.get(1,N)
    elif i == N-1:
        c = sac.get(0,N-1)
    else:
        c = gcd(sac.get(0,i),sac.get(i+1,N))
    if c > ans:
        ans = c
print(ans)

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

class SegCalcTree:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def set_value(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new

    def get(self,
            a=0, b=-1,
            k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left = self.get(a,b,2*k+1,l,(l+r)//2)
            right = self.get(a,b,2*k+2,(l+r)//2,r)
            return self.calc(left,right)

N = int(input())
A = inpl()
sct = SegCalcTree(A,calc=gcd)
ans = 0
for i in range(N):
    a = A[i]
    sct.set_value(i,None)
    c = sct.get()
    if c > ans:
        ans = c
    sct.set_value(i,a)
print(ans)

Solution (Python):
import fractions
from functools import reduce

n = int(input())
a = list(map(int,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)]))

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

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

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

gcd_from_left = [0]*N 
gcd_from_right = [0]*N

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

ans = 0

for a,b in zip(gcd_from_left, gcd_from_right):
  x = gcd(a,b)
  if ans < x:
    ans = x
    
print(ans)

Solution (Python):
from fractions import gcd

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

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

Solution (Python):
from fractions import gcd

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

L = []
R = []
L.append(a[0])
R.append(a[-1])

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

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

print(ans)

Solution (Python):
INF = float('inf')
MOD = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ass = set()
def div(x):
    for i in range(1,int(x**0.5)+1):
        if x%i == 0:
            ass.add(i)
            ass.add(x//i)
div(A[0])
div(A[1])
ans = 1
for i in ass:
    cnt = 0
    for a in A:
        if a%i == 0:
            cnt += 1
    if cnt >= N-1:
        ans = max(ans,i)
print(ans)

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))
Gleft = [A[0]]
Gright = [A[-1]]
for i in range(1,N):
    Gleft.append(gcd(Gleft[-1],A[i]))
    Gright.append(gcd(Gright[-1],A[-1-i]))
Gright = Gright[::-1]
res = max(Gright[1],Gleft[-2])
for i in range(1,N-1):
    res = max(res,gcd(Gleft[i-1],Gright[i+1]))
print(res)

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

s1=a[0]
s2=a[-1]
su=[s1]
sd=[s2]
for i in range(1,n-1):
    s1=gcd(s1,a[i])
    s2=gcd(s2,a[(i+1)*(-1)])
    su+=[s1]
    sd+=[s2]
sd.reverse()

ans=[su[-1],sd[0]]
for i in range(n-2):
    ans+=[gcd(su[i],sd[i+1])]
print(max(ans))

Solution (Python):
def larger(x, y):
	if x > y: return x
	return y

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

N = int(input())
A = list(map(int, input().split()))
left = [A[0]]
for i in range(1, N):
	left.append(euc(left[-1], A[i]))
right = [A[-1]]
for i in range(1, N):
	right.append(euc(right[-1], A[-1-i]))
right.reverse()

max = larger(left[N-2], right[1])
for i in range(1, N-1):
	max = larger(max, euc(left[i-1], right[i+1]))
print(max)

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

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

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

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

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


Left = [0] * (N + 1)
Right = [0] * (N + 1)

for i in range(N):
    Left[i + 1] = gcd(Left[i], A[i])
    Right[N - i - 1] = gcd(Right[N - i], A[N - i - 1])
ans = 0
for i in range(N):
    ans = max(ans, gcd(Left[i], Right[i + 1]))
    
print(ans)

Solution (Python):
import fractions
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
l_gcd = [0] * (len(data) + 1)
r_gcd = [0] * (len(data) + 1)
for ind,(i,j) in enumerate(zip(data,reversed(data))):
    l_gcd[ind + 1] = fractions.gcd(l_gcd[ind],i)
    r_gcd[ind + 1] = fractions.gcd(r_gcd[ind],j)
r_gcd = list(reversed(r_gcd))
ans = 0
for k in range(N):
    ans = max(fractions.gcd(l_gcd[k],r_gcd[k+1]),ans)
print(ans)

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

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

left = np.zeros(n + 1, dtype=np.int)
right = np.zeros(n + 1, dtype=np.int)

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

last = np.zeros(n, dtype=np.int)

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


print(max(last))

Solution (Python):
from fractions import gcd




n=int(input())

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

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

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



last=[0]*n

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


print(max(last))

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()))
output=0
KA=[0 for i in range(N)]
LA=[0 for i in range(N)]
GCD = []

for k in range(1,N):
        LA[k]=gcd(A[k-1],LA[k-1])

for k in range(1,N):
        KA[k]=gcd(A[N-k],KA[k-1])


for k in range(N):
        GCD.append(gcd(LA[k],KA[N-k-1]))
output = max(GCD)
print(output)

Solution (Python):
N=int(input())
A=sorted(map(int,input().split()))
ans=1
yakusu=[[],[]]

for i in range(2):
    for j in range(2,int(A[i]**0.5)+1):
        if A[i]%j==0:
            yakusu[i].append(j)
            yakusu[i].append(A[i]//j)
    yakusu[i].append(A[i])

yakusum=yakusu[0]
yakusum.extend(yakusu[1])
yakusum=sorted(set(yakusum))

for i in yakusum:
    counter=0
    for j in range(N):
        if A[j]%i!=0:
            counter+=1
            if counter==2:
                break
    if counter<=1:
        ans=i
print(ans)

Solution (Python):
N=int(input())
A=sorted(map(int,input().split()))
ans=1
yakusu=[[],[]]

for i in range(2):
    for j in range(2,int(A[i]**0.5)+1):
        if A[i]%j==0:
            yakusu[i].append(j)
            yakusu[i].append(A[i]//j)
    yakusu[i].append(A[i])

yakusum=yakusu[0]
yakusum.extend(yakusu[1])
yakusum=sorted(set(yakusum))

for i in yakusum:
    counter=0
    for j in range(N):
        if A[j]%i!=0:
            counter+=1
            if counter==2:
                break
    if counter<=1:
        ans=i
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=1
Amax=max(A)
yakusu=[[],[]]

for i in range(2):
    for j in range(2,int(A[i]**0.5)+1):
        if A[i]%j==0:
            yakusu[i].append(j)
            yakusu[i].append(A[i]//j)
    yakusu[i].append(A[i])

yakusum=yakusu[0]
yakusum.extend(yakusu[1])
yakusum=sorted(set(yakusum))

for i in yakusum:
    counter=0
    for j in range(N):
        if A[j]%i!=0:
            counter+=1
            if counter==2:
                break
    if counter<=1:
        ans=i
print(ans)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=1
Amax=max(A)
yakusu=[[],[]]

for i in range(2):
    for j in range(2,int(A[i]**0.5)+1):
        if A[i]%j==0:
            yakusu[i].append(j)
            yakusu[i].append(A[i]//j)
    yakusu[i].append(A[i])

yakusum=yakusu[0]
yakusum.extend(yakusu[1])
yakusum=sorted(set(yakusum))

for i in yakusum:
    counter=0
    for j in range(N):
        if A[j]%i!=0:
            counter+=1
            if counter==2:
                break
    if counter<=1:
        ans=i
print(ans)

Solution (Python):
from fractions import gcd

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

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

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

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

print(max(M))

Solution (Python):
n1 = int(input())
nl1 = [int(i1) for i1 in input().split()]
 
r1 = 0
nl1.sort()

for i5 in range(2):
    c1 = 0
    for i2 in range(n1):
        if nl1[i2] % nl1[i5] != 0:
            c1 += 1
        if c1 >= 2:
             break
        if i2 == (n1 - 1):
            r1 = nl1[i5] 
 
i3 = nl1[1] // 2
 
while r1 == 0:
    c1 = 0
    for i4 in range(n1):
        if nl1[i4] % i3 != 0:
            c1 += 1
        if c1 >= 2:
            break
        if i4 == (n1 - 1):
            r1 = i3
    i3 -= 1
 
print(str(r1))

Solution (Python):
import fractions
n = int(input())
a = list(map(int, input().split()))
forward = [0]
back =[0]
for i in range(0,n-1):
    forward.append(fractions.gcd(forward[i], a[i]))
    back.append(fractions.gcd(back[i], a[n-i-1]))
ans = 0
for i in range(n):
    ans = max(ans, fractions.gcd(forward[i], back[n-1-i]))
print(ans)

Solution (Python):
import fractions
import sys

n1 = int(input())
nl1 = [int(i1) for i1 in input().split()]

if n1 == 2:
    print(max(nl1))
    sys.exit()

r1 = 0

nl1.sort()

if n1 >= 3:
    n2 = fractions.gcd(nl1[0], nl1[1]) 
    n2 = max(n2, fractions.gcd(nl1[1], nl1[2]))
    n2 = max(n2, fractions.gcd(nl1[0], nl1[2]))

c1 = 0
for i2 in range(n1):
    if nl1[i2] % n2 != 0:
        c1 += 1
    if c1 >= 2:
        break
    if i2 == (n1 - 1):
        r1 = n2

i3 = n2

while r1 == 0:
    c1 = 0
    for i4 in range(n1):
        if nl1[i4] % i3 != 0:
            c1 += 1
        if c1 >= 2:
            break
        if i4 == (n1 - 1):
            r1 = i3
    i3 -= 1

print(str(r1))

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

def gcd(x, y):
    if x < 0 and y < 0:
        return -1
    if x < 0:
        return y
    if y < 0:
        return x


    x, y = max(x, y), min(x, y)
    r = x % y
    if r == 0:
        return y
    else:
        return gcd(r, y)




L = [-1] * N
R = [-1] * N

L[1] = A[0]
R[N-2] = A[N-1]

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

G = [gcd(L[i], R[i]) for i in range(N)]
print(max(G))

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

def gcd(x, y):
    if x < 0 and y < 0:
        return -1
    if x < 0:
        return y
    if y < 0:
        return x


    x, y = max(x, y), min(x, y)
    r = x % y
    if r == 0:
        return y
    else:
        return gcd(r, y)




L = [-1] * N
R = [-1] * N

L[1] = A[0]
R[N-2] = A[N-1]

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

G = [gcd(L[i], R[i]) for i in range(N)]
print(max(G))

Solution (Python):
import fractions as f

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

left = []
right = []

left.append(A[0])
for i in range(1,n):
    left.append(f.gcd(left[i-1],A[i]))

right.append(A[n-1])
for i in range(1,n):
    right.append(f.gcd(right[i-1],A[n-1-i]))

ans = 0
for i in range(n):
    if i == 0:
        ans = right[n-2]
    if i >= 1 and i <= n-2:
        ans = max(ans,f.gcd(left[i-1],right[n-2-i]))
    if i == n-1:
        ans = max(ans,left[n-2])
print(ans)

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

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

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

maxGCD = 1
for j in range(N):
    maxGCD = max(maxGCD, gcd(L[j], R[N-j-1]))
print(maxGCD)