Problem:
 Score : 300 points    Problem Statement There are N integers, A_1, A_2, ..., A_N , written on the blackboard.  You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.  Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.      Constraints  All values in input are integers.  2 \leq N \leq 10^5  1 \leq A_i \leq 10^9       Output Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.       Sample Input 1 3
7 6 8      Sample Output 1 2  If we replace 7 with 4 , the greatest common divisor of the three integers on the blackboard will be 2 , which is the maximum possible value.       Sample Input 2 3
12 15 18      Sample Output 2 6       Sample Input 3 2
1000000000 1000000000      Sample Output 3 1000000000  We can replace an integer with itself.

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

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

left=[0]*n
right=[0]*n

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

ans=0
for i,j in zip(left,right):
    ans=max(ans,gcd(i,j))
print(ans)

Solution (Python):
from fractions import gcd

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

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

Solution (Python):
import copy

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

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


l_gcd_list = [A[0]]
r_gcd_list = [A[-1]]
gcd_list = []


for i in range(1,N-1):
    l_gcd_list.append(gcd(l_gcd_list[-1], A[i]))
    r_gcd_list.append(gcd(r_gcd_list[-1], A[-i-1]))


for i in range(1,N-1):
    gcd_list.append(gcd(l_gcd_list[i-1],r_gcd_list[-i-1]))


gcd_list.append(l_gcd_list[-1])
gcd_list.append(r_gcd_list[-1])
    
print(max(gcd_list))

Solution (Python):
import math
n = int(input())
a = []
a.append(list(map(int, input().split())))
def gcd(x,y):
  if y == 0:
    return x
  return gcd(y, x % y)

pow_2 = 1
exp = 0
while pow_2 < n:
  pow_2 *= 2
  exp += 1

a[0].extend([0]*(pow_2 - n))
for i in range(1, exp):
  a.append([])
  for j in range(int(pow_2 / math.pow(2, i))):
    a[i].append(gcd(a[i-1][2*j], a[i-1][2*j+1]))

ans = [0]*n
for i in range(n):
  g = 0
  for j in range(0,exp):
    if (i >> j) % 2 == 0:
      bit = 1
    else:
      bit = -1
    g = gcd(a[j][(i>>j)+bit], g)
  ans[i] = g
print(sorted(ans)[n-1])

Solution (Python):
import fractions as math

n = int(input()) 
a = list(map(int, input().split())) 
 
l = [a[0] for _ in range(n+2)] 
r = [a[-1] for _ in range(n+2)] 
 
for i in range(n): 
    l[i+1] = math.gcd(l[i], a[i]) 
    r[-(i+2)] = math.gcd(r[-(i+1)], a[-(i+1)]) 
l[0] = r[0] = l[-1] = r[-1] = 0 
 
ans = [math.gcd(l[i], r[i+2]) for i in range(n)] 
print(max(ans))

Solution (Python):
def gcd(a, b):
    if b==0:
        return a
    while b>0:
        a, b = b, a%b
    return a
N = int(input())
A = list(map(int, input().split()))
S_left = [0 for _ in range(N+1)]
S_right = [0 for _ in range(N+1)]
for i in range(N):
    S_left[i+1] = gcd(S_left[i], A[i])
    S_right[i+1] = gcd(S_right[i], A[-i-1])
S_right = S_right[::-1]
S_left = S_left + [0]
S_right = [0] + S_right
ans = 0
for i in range(1, N+1):
    ans = max(ans, gcd(S_left[i-1], S_right[i+1]))
print(ans)

Solution (Python):

from fractions import gcd


class SegmentTree:
    def __init__(
        self,
        init_val: "initial value: Array or str",
        segfunc: "operation unique in case",
        ide_ele: "identity element corresponding init_val" = 0,
    ):
        self.segfunc = segfunc
        self.size = 1 << (len(init_val) - 1).bit_length()
        self.ide_ele = ide_ele
        self.tree = self._build(init_val)

    def _build(self, init_val: "Array or str"):
        tree = [self.ide_ele] * (2 * self.size)

        for idx, val in enumerate(init_val):  
            
            tree[idx + self.size - 1] = val

        for idx in range(self.size - 2, -1, -1):  
            tree[idx] = self.segfunc(tree[2 * idx + 1], tree[2 * idx + 2])
        return tree

    def update(self, idx: int, val):
        idx += self.size - 1
        
        self.tree[idx] = val
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = self.segfunc(
                self.tree[2 * idx + 1], self.tree[2 * idx + 2]
            )

    def query(self, left: int, right: int):
        if left >= right:
            return self.ide_ele

        left += self.size
        right += self.size
        ret = self.ide_ele
        while left < right:
            if left & 1:
                ret = self.segfunc(ret, self.tree[left - 1])
                left += 1
            if right & 1:
                right -= 1
                ret = self.segfunc(ret, self.tree[right - 1])
            left >>= 1
            right >>= 1
        return ret


def main():
    N, *A = map(int, open(0).read().split())
    seg = SegmentTree(A, gcd)
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(seg.query(0, i), seg.query(i + 1, N)))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from fractions import gcd
N = int(input())
array = list(map(int,input().split()))
all_gcd = [0]*N

gcd_L = [array[0]]
for i in range(N-1):
  gcd_L.append(gcd(gcd_L[-1],array[i+1]))
  

gcd_R = [array[-1]]
for j in range(N-1,0,-1):
  gcd_R.append(gcd(gcd_R[-1],array[j-1]))
  
gcd_arr = []
  
for idx in range(N):
  if idx==0:
    gcd_arr.append(gcd_R[-2])
  elif idx==N-1:
    gcd_arr.append(gcd_L[-2])
  else:
    gcd_arr.append(gcd(gcd_L[idx-1],gcd_R[N-1-idx-1]))
print(max(gcd_arr))

Solution (Python):
class SegmentTree:
    def __init__(self, seq, func="min"):
        if func=="min":
            self.f = min
            self.e = float('inf')
        if func=="max":
            self.f = max
            self.e = -float('inf')
        if func=="gcd":
            self.f = gcd
            self.e = 0
        self._n = len(seq)
        self.n_seg = 2**(self._n-1).bit_length()
        self.seg = [self.e]*2*self.n_seg
        
        for i in range(self._n):
            self.seg[self.n_seg+i-1] = seq[i]
            
        for i in range(self.n_seg-2, -1, -1):
            self.seg[i] = self.f(self.seg[2*i+1], self.seg[2*i+2])
        
    def update(k, val):
        k += self.n_seg -1
        self.seg[k] = val
        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, a, b):
        if b<=a:
            return self.e
        a += self.n_seg-1
        b += self.n_seg-2
        res = self.e
        while b-a>1:
            if a&1==0:
                res = self.f(res, self.seg[a])
            if b&1==1:
                res = self.f(res, self.seg[b])
                b -= 1
            a = a//2
            b = (b-1)//2
        if a==b:
            res = self.f(res, self.seg[a])
        else:
            res = self.f(self.f(res, self.seg[a]), self.seg[b])
        return res
    
    def __repr__(self):
        ret = str(self.seg[self._n-1:])
        return ret

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

if __name__=="__main__":
    N = int(input())
    A = list(map(int, input().split()))
    A_seg = SegmentTree(A, func="gcd")
    ans = -1
    for i in range(N):
        ans = max(ans, gcd(A_seg.query(0, i), A_seg.query(i+1, N)))
    print(ans)

Solution (Python):

from fractions import gcd


def segfunc(x, y):  
    return gcd(x, y)


class SegmentTree:
    def __init__(
        self,
        init_val: "initial value: Array or str",
        ide_ele: "identity element corresponding init_val" = 0,
    ):
        self.size = 1 << (len(init_val) - 1).bit_length()
        self.ide_ele = ide_ele
        self.tree = self._build(init_val)

    def _build(self, init_val: "Array or str"):
        tree = [self.ide_ele] * (2 * self.size)

        for idx, val in enumerate(init_val):  
            
            tree[idx + self.size - 1] = val

        for idx in range(self.size - 2, -1, -1):  
            tree[idx] = segfunc(tree[2 * idx + 1], tree[2 * idx + 2])
        return tree

    def update(self, idx: int, val):
        idx += self.size - 1
        
        self.tree[idx] = val
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = segfunc(self.tree[2 * idx + 1], self.tree[2 * idx + 2])

    def query(self, left: int, right: int):
        left += self.size
        right += self.size
        ret = self.ide_ele
        while left < right:
            if left & 1:
                ret = segfunc(ret, self.tree[left - 1])
                left += 1
            if right & 1:
                right -= 1
                ret = segfunc(ret, self.tree[right - 1])
            left >>= 1
            right >>= 1
        return ret


def main():
    N, *A = map(int, open(0).read().split())
    seg = SegmentTree(A)
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(seg.query(0, i), seg.query(i + 1, N)))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from fractions import gcd


def segfunc(x, y):  
    return gcd(x, y)


class SegmentTree:
    def __init__(
        self,
        init_val: "initial value: Array or str",
        ide_ele: "identity element corresponding init_val" = 0,
    ):
        self.size = 1 << (len(init_val) - 1).bit_length()
        self.ide_ele = ide_ele
        self.tree = self._build(init_val)

    def _build(self, init_val: "Array or str"):
        tree = [self.ide_ele] * (2 * self.size)

        for idx, val in enumerate(init_val):  
            
            tree[idx + self.size - 1] = val

        for idx in range(self.size - 2, -1, -1):  
            tree[idx] = segfunc(tree[2 * idx + 1], tree[2 * idx + 2])
        return tree

    def update(self, idx: int, val):
        idx += self.size - 1
        
        self.tree[idx] = val
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = segfunc(self.tree[2 * idx + 1], self.tree[2 * idx + 2])

    def query(self, left: int, right: int):
        if left >= right:
            return self.ide_ele

        left += self.size
        right += self.size
        ret = self.ide_ele
        while left < right:
            if left & 1:
                ret = segfunc(ret, self.tree[left - 1])
                left += 1
            if right & 1:
                right -= 1
                ret = segfunc(ret, self.tree[right - 1])
            left >>= 1
            right >>= 1
        return ret


def main():
    N, *A = map(int, open(0).read().split())
    seg = SegmentTree(A)
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(seg.query(0, i), seg.query(i + 1, N)))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import fractions as math

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

l = [a[0] for _ in range(n+2)]
r = [a[-1] for _ in range(n+2)]

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

ans = [math.gcd(l[i], r[i+2]) for i in range(n)]
print(max(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(1,N+1):
    L[i] = gcd(L[i-1],A[i-1])
for i in range(0,N)[::-1]:
    R[i] = gcd(R[i+1],A[i])
ans = 1
for i in range(N):
    val = gcd(L[i],R[i+1])
    if val > ans:
        ans = val
print(ans)

Solution (Python):
def GCD(x,y):
    x,y=max(x,y),min(x,y)
    if y==0:
        return x
    elif x%y==0:
        return y
    else:
        return GCD(y,x%y)

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

L=[0 for i in range(N+2)]
R=[0 for i in range(N+2)]

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

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

print(max(G))

Solution (Python):
from fractions import gcd

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

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



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

Solution (Python):
from fractions import gcd

import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
ans = 0

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

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): 
	while y != 0:
		x , y = y , x % y 
	return x
n = int(input())
A = list(map(int,input().split()))
lgcds=[1 for _ in range(n)]
rgcds=[1 for _ in range(n)]
lgcd  = A[0]
rgcd = A[-1]
for i in range(1,n):
    lgcd =gcd(A[i-1],lgcd)
    lgcds[i] = lgcd
for i in range(n-2,-1,-1):
    rgcd =gcd(A[i+1],rgcd)
    rgcds[i] = rgcd
ans =[1 for _ in range(n)]
for i in range(n):
    if i ==0:
        ans[i]=rgcds[i]
    elif i ==n-1:
        ans[i]=lgcds[i]
    else:
        ans[i]=gcd(rgcds[i],lgcds[i])
print(max(ans))

Solution (Python):
from fractions import gcd

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


def gcd2(x, y):
    if not x:
        return y
    if not y:
        return x
    return gcd(x, y)


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

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

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(1,n):
	l=gcd(L[i-1],A[i-1])
	L.append(l)
	r=gcd(R[i-1],A[n-i])
	R.append(r)
G=[]
for i in range(n):
	g=gcd(L[i],R[n-1-i])
	G.append(g)
print(max(G))

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

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

Prefix=[0 for i in range(n + 2)] 
Suffix=[0 for i in range(n + 2)] 

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

Suffix[n] = a[n - 1] 

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

ans = max(Suffix[2], Prefix[n - 1]) 

for i in range(2,n): 
    ans = max(ans, gcd(Prefix[i - 1], Suffix[i + 1])) 

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)
        """
        l = a + self.n2
        r = b + self.n2
        s = self.init
        while l < r:
            if r & 1:
                r -= 1
                s = self.func(s, self.tree[r])
            if l & 1:
                s = self.func(s, self.tree[l])
                l += 1
            l >>= 1
            r >>= 1
        return s

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

from fractions import gcd

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))

    L = [0]
    R = [0]

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

    ans = 0

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

    print(ans)

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

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

left = [a[0]]
right = [a[-1]]

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

ans = 0
for i in range(n):
    if i == 0:
        num = right[n-i-2]
    elif i == n - 1:
        num = left[i-1]
    else:
        num = gcd(left[i-1], right[n-i-2])
    if num > ans:
        ans = num

print(ans)

Solution (Python):
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *

from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

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

def main():
    """ main """
    N = int(input())
    AS = list(map(int, input().split()))
    L = [0]*(N+2)
    R = [0]*(N+2)
    for i in range(0,N):
        L[i+1] = gcd(L[i],AS[i])
    for i in range(N-1,-1,-1):
        R[i] = gcd(R[i+1],AS[i])
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(L[i],R[i+1]))
    print(ans)


if __name__ == '__main__':
    main()

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

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

gcds = [-1] * N
gcds_rev = [-1] * N
for i in range(0,N):
  if i == 0: gcds[i] = A[0]
  else: gcds[i] = gcd(gcds[i-1], A[i])

  rev_i = N-1-i
  if rev_i == N-1: gcds_rev[rev_i] = A[-1]
  else: gcds_rev[rev_i] = gcd(gcds_rev[rev_i+1], A[rev_i])

ans = -float('inf')
for i in range(N):
  if i == 0:val = gcds_rev[1]
  elif i == N-1:val = gcds[-2]
  else: val = gcd(gcds[i-1], gcds_rev[i+1])
  ans = max(ans, val)
print(ans)

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


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

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

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

l = [0] + l[:-1]
r = r[1:] + [0]

ans = 0
for x, y in zip(l, r):
    ans = max(ans, gcd(x, y))
print(ans)

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
class SegTree:
  def segfunc(self, x, y):
    return gcd(x, y) 
  def __init__(self, n, ide_ele, init_val):
    
    self.ide_ele = ide_ele
    
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    
    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.segfunc(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.segfunc(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.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res
sg = SegTree(N, 0, A)
res = 0
for i in range(N):
  res = max(res, gcd(sg.query(0, i), sg.query(i + 1, N)))
print(res)

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


def gcd(a,b):
  x = max([a,b])
  y = min([a,b])
  while y:
    x, y = y, x % y
  return x

L = [0]
R = [0]

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

R = list(reversed(R))


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


ans = max(ans, R[1])
ans = max(ans, L[-2])

print(ans)

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


def gcd(a,b):
  x = max([a,b])
  y = min([a,b])
  while y:
    x, y = y, x % y
  return x

L = [0]
R = [0]

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

R = list(reversed(R))


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

ans = max(ans, R[1])
ans = max(ans, L[-2])

print(ans)

Solution (Python):
import sys




def gcd(a,b):
	if a < b:
		a,b=b,a
		
	while a != b:
		t=a%b
		if t == 0:
			return b
		else:
			a,b = b,t
	return a
	
def main():
	n = int(input())
	al = list(map(int, input().split()))
	
	if n == 2:
		return max(al)
	
	from_left=[0]*len(al)
	kk=al[0]
	for i, v in enumerate(al):
		kk=gcd(kk,v)
		from_left[i]=kk
	
	from_right=[0]*len(al)
	kk=al[-1]
	for i,v in enumerate(al[::-1]):
		kk=gcd(kk,v)
		from_right[i]=kk
	from_right=from_right[::-1]
	
	ans=0
	for i,_ in enumerate(al):
		if i == 0:
			vv=from_right[1]
		elif i == len(al)-1:
			vv=from_left[-2]
		else:
			a=from_right[i+1]
			b=from_left[i-1]
			
			vv=gcd(a,b)
		ans = max(ans, vv)
	print(ans)






if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
from fractions import gcd
class segtree():
    def segfunc(x,y):
        return min(x,y)
    
    ide_ele = -1
    n=1
    init_val=[0]*n
    
    num =2**(n-1).bit_length()
    seg=[ide_ele]*2*num
    
    def __init__(self,INIT_VAL,SEGFUNC,IDE_ELE):
        self.ide_ele=IDE_ELE
        self.init_val=[i for i in INIT_VAL]
        self.segfunc=SEGFUNC
        self.n=len(self.init_val)
        self.num =2**(self.n-1).bit_length()
        self.seg=[self.ide_ele]*2*self.num
 
        
        for i in range(self.n):
            self.seg[i+self.num-1]=self.init_val[i]    
        
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(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.segfunc(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.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res
N=int(input())
A=[int(i) for i in input().split()]
B=segtree(A,gcd,0)
C=[gcd(B.query(0,i),B.query(i+1,N)) for i in range(N)]
print(max(C))

Solution (Python):
from fractions import gcd
class segtree():
    def segfunc(x,y):
        return min(x,y)
    
    ide_ele = -1
    n=1
    init_val=[0]*n
    
    num =2**(n-1).bit_length()
    seg=[ide_ele]*2*num
    
    def __init__(self,INIT_VAL,SEGFUNC,IDE_ELE):
        self.ide_ele=IDE_ELE
        self.init_val=[i for i in INIT_VAL]
        self.segfunc=SEGFUNC
        self.n=len(self.init_val)
        self.num =2**(self.n-1).bit_length()
        self.seg=[self.ide_ele]*2*self.num
 
        
        for i in range(self.n):
            self.seg[i+self.num-1]=self.init_val[i]    
        
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(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.segfunc(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.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res
N=int(input())
A=[int(i) for i in input().split()]
B=segtree(A,gcd,0)
C=[gcd(B.query(0,i),B.query(i+1,N+1)) for i in range(N+1)]
print(max(C))

Solution (Python):
from fractions import gcd
class segtree():
    def segfunc(x,y):
        return min(x,y)
    
    ide_ele = -1
    n=1
    init_val=[0]*n
    
    num =2**(n-1).bit_length()
    seg=[ide_ele]*2*num
    
    def __init__(self,INIT_VAL,SEGFUNC,IDE_ELE):
        self.ide_ele=IDE_ELE
        self.init_val=[i for i in INIT_VAL]
        self.segfunc=SEGFUNC
        self.n=len(self.init_val)
        self.num =2**(self.n-1).bit_length()
        self.seg=[self.ide_ele]*2*self.num
 
        
        for i in range(self.n):
            self.seg[i+self.num-1]=self.init_val[i]    
        
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(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.segfunc(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.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res
N=int(input())
A=[int(i) for i in input().split()]
B=segtree(A,gcd,0)
C=[gcd(B.query(0,i),B.query(i+1,N+1)) for i in range(N+1)]
print(max(C))

Solution (Python):

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

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]
l2 = [0]
for i in range(n):
    l.append(gcd(l[-1], a[i]))
for i in range(n-1, -1, -1):
    l2.append(gcd(l2[-1], a[i]))

ans = 0

for i in range(n):
    ans = max(ans, gcd(l[i], l2[n-i-1]))
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()))
    g_l = [[0, 0] for _ in range(n + 1)]

    for i in range(n):
        g_l[i+1][0] = fractions.gcd(g_l[i][0], a[i])
        g_l[-i-2][1] = fractions.gcd(g_l[-i-1][1], a[-i-1])
    m_g = 0
    for i in range(n):
        m_g = max(m_g, fractions.gcd(g_l[i][0], g_l[i + 1][1]))
    print(m_g)


if __name__ == '__main__':
    slove()

Solution (Python):

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

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

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

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

Solution (Python):

import sys
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
if len(a)==2:
    print(max(a))
    sys.exit()
g=gcd(a[0],a[1])
b=[]
for i in range(2,n):
    l=gcd(g,a[i])
    if l!=g:
        b.append(i)
        g=l
s=[]
g=gcd(a[0],a[2])
for i in range(3,n):
    g=gcd(g,a[i])
s.append(g)
g=gcd(a[1],a[2])
for i in range(3,n):
    g=gcd(g,a[i])
s.append(g)
for i in range(len(b)):
    g=gcd(a[0],a[1])
    for j in range(2,n):
        if j==b[i]:
            continue
        g=gcd(g,a[j])
    s.append(g)
print(max(s))

Solution (Python):
from fractions import gcd

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    M = [0] * N
    L = [0] * (N+2)
    R = [0] * (N+2)
    for i in range(1,N+1):
        i_r = N-i
        L[i] = gcd(L[i-1], A[i-1])
        R[i_r] = gcd(R[i_r+1], A[i_r])
        
    for i in range(N):
        M[i] = gcd(L[i], R[i+1])

    print(max(M))
    
solve()

Solution (Python):

n=int(input())
listsT=list(map(int,input().split()))
from fractions import gcd
lists_left=[]
lists_right=[]
number1=0
number2=0
for i in range(n):
    if i==0:
        lists_left.append(0)
    if i==1:
        lists_left.append(listsT[0])
        number1=listsT[0]
    if i>=2:
        number1=gcd(number1,listsT[i-1])
        lists_left.append(number1)

number2=listsT[-1]
for j in range(n,0,-1):
    if j==n:
        lists_right.append(0)
    if j==n-1:
        lists_right.append(listsT[-1])
    if j<n-1:      
        number2=gcd(number2,listsT[j])  
        lists_right.append(number2)
ans=0
for x in range(n):
    if ans<= gcd(lists_right[x],lists_left[n-1-x]):
        ans=gcd(lists_right[x],lists_left[n-1-x])   
print(ans)

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

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

for i in range(N):
    max_val = max(max_val, fractions.gcd(L[i], R[i]))


print(max_val)