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

argN = int(input())
argA = list(map(int,input().split()))

aryL = [0] * argN
aryR = [0] * argN
aryL[0] = argA[0]
aryR[argN-1] = argA[argN-1]

for i in range(1,argN):
  aryL[i] = fractions.gcd(aryL[i-1],argA[i])
  aryR[argN-1-i] = fractions.gcd(aryR[argN-i],argA[argN-1-i])

maxGcd = 0
for i in range(argN):
  if i == 0:
    wkGcd = aryR[1]
  elif i == argN - 1:
    wkGcd = aryL[argN - 2]
  else:
    wkGcd = fractions.gcd(aryL[i-1],aryR[i+1])
  if maxGcd < wkGcd:
    maxGcd = wkGcd

print(maxGcd)

Solution (Python):
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

N = int(input())
A = [int(x) for x in input().split()]
L = [0]*N
R = [0]*N
ans = 0
for i in range(N):
    if(i==0):
        L[i] = A[0]
        R[i] = A[-1]
    else:
        L[i] = gcd(A[i],L[i-1])
        R[i] = gcd(A[-i-1],R[i-1])

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

Solution (Python):
from functools import reduce
import collections 
import fractions
import math
import copy

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    N = int(input().rstrip())
    A = list(int(_) for _ in input().split())
    ans = 0
    R = [0]*N
    R[0] = A[0]
    L = [0]*N
    L[N-1] = A[N-1]
    for i in range(1,N):
        R[i] = fractions.gcd(R[i-1],A[i])
    for i in range(N-1,0,-1):
        L[i-1] = fractions.gcd(L[i],A[i-1])        
    X = []
    for i in range(N):
        if i == 0:
            X.append(L[1])
        elif i == N-1:
            X.append(R[N-2])
        else:
            X.append(fractions.gcd(R[i-1],L[i+1]))
    print(max(X))
    
if __name__ == '__main__':
    main()

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

from fractions import gcd
gcds_ascendant = [-1, a[0]]
for i in range(1, N-1):
    gcds_ascendant.append(gcd(gcds_ascendant[-1], a[i]))

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

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

print(ans)

Solution (Python):

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

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



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


list_gcd = [0] * N
list_gcd_rev = [0] * N
































for i in range(1,N):
    list_gcd[i]=gcd(list_gcd[i-1],list_A[i-1])
for i in range(N-1)[::-1]:
    list_gcd_rev[i]=gcd(list_gcd_rev[i+1],list_A[i+1])
Z = [gcd(list_gcd[i], list_gcd_rev[i]) for i in range(N)]
print (max(Z))

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

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

l = []
r = []

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

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

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

print(ans)

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

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

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

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

print(max(merge))

Solution (Python):
import fractions


def solute(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(fractions.gcd(gcd_left[i], a[i]))
        gcd_right.append((fractions.gcd(gcd_right[i], a[-(i+1)])))
    gcd_right = gcd_right[::-1]

    ans = []
    for i in range(n):
        ans.append(fractions.gcd(gcd_left[i], gcd_right[i+1]))
    return max(ans)


n = int(input())
a = [int(x) for x in input().split()]
print(solute(n, a))

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


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





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

print(ans)

Solution (Python):
from fractions import gcd


def main():
    N = int(input())
    A = list(map(int, input().split()))
    left = [0] * N
    right = [0] * N
    for i in range(1, N):
        left[i] = gcd(left[i-1], A[i-1])
        right[-1-i] = gcd(right[-i], A[-i])
    gcds = []
    for i in range(N):
        gcds.append(gcd(left[i], right[i]))
    print(max(gcds))


if __name__ == '__main__':
    main()

Solution (Python):

import sys

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


def solve(N: int, A: "List[int]"):
    tmp = A[0]
    l = []
    for a in A:
        tmp = gcd(tmp, a)
        l.append(tmp)
    tmp = A[-1]
    r = []
    for a in A[::-1]:
        tmp = gcd(tmp, a)
        r.append(tmp)
    r.reverse()
    ret = 0
    for i in range(N):
        if i < 1:
            tmp = r[i + 1]
        elif i >= N - 1:
            tmp = l[i - 1]
        else:
            tmp = gcd(l[i - 1], r[i + 1])
        ret = max(ret, tmp)
    print(ret)
    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):

import fractions

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


gcd = fractions.gcd


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

    left[i] = gcd(left[i-1], A[i])
    right[N-i-1] = gcd(right[N-i], A[N-i-1])

max_gcd = right[1]
for i in range(N):
    if i == 0:
        continue
    elif i == N - 1:
        if max_gcd < left[-2]:
            max_gcd = left[-2]
    else:
        target = gcd(left[i-1], right[i+1])
        if max_gcd < target:
            max_gcd = target

print(max_gcd)

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


def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=10
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0
for i in range(1,math.ceil(M**0.5)+1 ):
    if M%i==0:
        cnt=0
        for j in range(p,N):
            if a[j]%i!=0:
                cnt+=1
                if cnt==2: break
            elif j==N-1 and i>ans: ans=i
        
        x=M//i
        cnt=0
        for j in range(p,N):
            if a[j]%x!=0:
                cnt+=1
                if cnt==2: break
            elif j==N-1 and x>ans: ans=x

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

if R>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),R)
        if s>ans: ans=s

print(ans)

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

def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
p=40
N=int(input())
if p>N: p=N
a=list(map(int,input().split()))

ans=0
for i in range(p):
    s=cul(i,a[:p])
    if s>ans: ans=s

print(ans)

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

def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
p=50
N=int(input())
if p>N: p=N
a=list(map(int,input().split()))

ans=0
for i in range(p):
    s=cul(i,a[:p])
    if s>ans: ans=s

print(ans)

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

def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
p=50
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()

ans=0
for i in range(p):
    s=cul(i,a[:p])
    if s>ans: ans=s

print(ans)

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

def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
p=100
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()

ans=0
for i in range(p):
    s=cul(i,a[:p])
    if s>ans: ans=s

print(ans)

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


def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=100
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0
for i in range(p):
    s=gcd(cul(i,a[:p]),a[p])
    if s>ans: ans=s

print(ans)

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


def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=100
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0


R=a[p]
for i in range(p,N):
    r=gcd(R,a[i])

if r>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),R)
        if s>ans: ans=s

print(ans)

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

d = set()
for aa in a[0], a[1]:
    for i in range(1, int(aa ** 0.5) + 1):
        if aa % i == 0:
            d.add(i)
            d.add(aa // i)  




g = 1
for dd in d:
    cnt = 0
    for aa in a:
        if aa % dd == 0:
            cnt += 1
    if cnt >= N - 1:
        
        g = max(g, dd)
print(g)

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

d = set()
for aa in a[0], a[1]:
    for i in range(1, int(aa ** 0.5) + 1):
        if aa % i == 0:
            d.add(i)
            d.add(aa // i)  




g = 1
for dd in d:
    cnt = 0
    for aa in a:
        if aa % dd == 0:
            cnt += 1
    if cnt >= N - 1:
        
        g = max(g, dd)
print(g)

Solution (Python):
import unittest
import fractions


def solution(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(fractions.gcd(gcd_left[i], a[i]))
        
        
        gcd_right.append(fractions.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  

    ans = []
    for i in range(n):
        ans.append(fractions.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)


class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertEqual(solution(3, [7, 6, 8]), 2)

    def test2(self):
        self.assertEqual(solution(3, [12, 15, 18]), 6)

    def test3(self):
        self.assertEqual(solution(2, [1000000000, 1000000000]), 1000000000)




if __name__ == '__main__':

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

    unittest.main()

Solution (Python):
import fractions
from functools import reduce

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] = (fractions.gcd(left[i],a[i]))
    
    right[n - i - 1] = (fractions.gcd(right[n - i],a[n - 1 - i]))



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

Solution (Python):
from sys import exit
from fractions import gcd


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


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


left_side = [0 for _ in range(N)]
left_side[0] = A[0]


right_side = [0 for _ in range(N)]
right_side[-1] = A[-1]

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

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

max_gcd = 1


max_gcd = max(max_gcd, right_side[1])

max_gcd = max(max_gcd, left_side[-2])

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

    max_gcd = max(max_gcd, gcd(left, right))

print(max_gcd)

Solution (Python):
from sys import stdin, exit
from fractions import gcd


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


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


left_side = [0 for _ in range(N)]
left_side[0] = A[0]


right_side = [0 for _ in range(N)]
right_side[-1] = A[-1]

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

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

max_gcd = 1


max_gcd = max(max_gcd, right_side[1])

max_gcd = max(max_gcd, left_side[-2])

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

    max_gcd = max(max_gcd, gcd(left, right))

print(max_gcd)

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
B, C = [A[0]], [A[N - 1]]

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

AA = list(reversed(A))
for i in range(1, N):
    C.append(gcd(C[i - 1], AA[i]))
C.reverse()
ans = 0
for i in range(1, N - 1):
    ans = max(ans, gcd(B[i - 1], C[i + 1]))
ans = max(ans, B[N - 2], C[1])
print(ans)

Solution (Python):
import copy

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

N = int(input())
A = list(map(int, input().split()))
F = copy.copy(A)
B = copy.copy(A)
for i in range(N - 1):
  F[i+1] = gcd(F[i+1], F[i])
for i in range(N - 2, -1, -1):
  B[i] = gcd(B[i], B[i+1])
ans = max(F[N-2], B[1])
for i in range(1, N - 1):
  ans = max(ans, gcd(F[i-1], B[i+1]))
print(ans)

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


def gcd_custom(a, b):
    if a == 0 or b == 0:
        return max(a, b)
    return gcd(a, b)


def get(n, change_dict):
    if n < 0 or N <= n:
        return 0
    if n in change_dict:
        return
    else:
        return A[n]


gcd_left = [0]  
gcd_tmp_val = 0
for i in range(N):
    gcd_tmp_val = gcd_custom(gcd_tmp_val, A[i])
    gcd_left.append(gcd_tmp_val)

gcd_right_reversed = [0]  
gcd_tmp_val = 0
for i in range(N - 1, -1, -1):
    gcd_tmp_val = gcd_custom(gcd_tmp_val, A[i])
    gcd_right_reversed.append(gcd_tmp_val)

ans = 1
for i in range(N):
    ans = max(ans,
              gcd_custom(gcd_left[i], gcd_right_reversed[N - 1 - i])
              )
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
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)
    return ass
a0 = A[0]
a1 = A[1]
div(a0)
div(a1)
ans = []
for i in ass:
    cnt = 0
    for a in A:
        if a%i == 0:
            cnt += 1
    if cnt >= n-1:
        ans.append(i)
print(max(ans))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
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)
    return ass
a0 = A[0]
a1 = A[1]
div(a0)
div(a1)
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):
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)


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

N_MAX = 1 << N.bit_length()

v = [0 for _ in range(2 * N_MAX - 1)]


for idx, aa in enumerate(a):
    idx += N_MAX - 1  
    v[idx] = aa
    while idx > 0:
        idx = (idx - 1) // 2  
        v[idx] = gcd(v[idx * 2 + 1], v[idx * 2 + 2])  



def query(a, b, k=0, l=0, r=N_MAX):
    
    
    if r <= a or b <= l: return 0  
    if a <= l and r <= b: return v[k]  
    gcd_l = query(a, b, 2 * k + 1, l, (l + r) // 2)
    gcd_r = query(a, b, 2 * k + 2, (l + r) // 2, r)
    return gcd(gcd_l, gcd_r)


g = 0
for i in range(N):
    g = max(g, gcd(query(0, i), query(i + 1, N)))
print(g)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    def gcd(a,b):
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    
    def query(p,q):
        if p>n-1 or q<1:
            return 0
        if abs(q-p)<=1:
            return a[p]
        p += num-1
        q += num-2
        res=0
        while q>p:
            if p&1 == 0:
                res = gcd(res,seg[p])
            if q&1 == 1:
                res = gcd(res,seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        res = gcd(res,seg[p])
        return res

    n = int(input())
    a = tuple(map(int,input().split()))
    
    num =2**(n-1).bit_length()
    seg=[0]*2*num

    for i in range(n):
        seg[i+num-1]=a[i]    
    for i in range(num-2,-1,-1) :
        seg[i]=gcd(seg[2*i+1],seg[2*i+2]) 

    ans = -1
    for i in range(n):
        ans = max(ans,gcd(query(0,i),query(i+1,n)))
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    def gcd(a,b):
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    
    def query(p,q):
        if p>n-1 or q<1:
            return 0
        if abs(q-p)<=1:
            return a[p]
        p += num-1
        q += num-2
        res=0
        while q>p:
            if p&1 == 0:
                res = gcd(res,seg[p])
            if q&1 == 1:
                res = gcd(res,seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        res = gcd(res,seg[p])
        return res

    n = int(input())
    a = tuple(map(int,input().split()))
    
    num =2**(n-1).bit_length()
    seg=[0]*2*num

    for i in range(n):
        seg[i+num-1]=a[i]    
    for i in range(num-2,-1,-1) :
        seg[i]=gcd(seg[2*i+1],seg[2*i+2]) 

    ans = -1
    for i in range(n):
        ans = max(ans,gcd(query(0,i),query(i+1,n)))
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):
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)
    return ass

a0 = A[0]
a1 = A[1]
div(a0)
div(a1)
ans = []
for i in ass:
    cnt = 0
    for a in A:
        if a%i == 0:
            cnt += 1
    if cnt >= n-1:
        ans.append(i)
print(max(ans))

Solution (Python):
from fractions import gcd
from itertools import accumulate


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


L = list(accumulate(A, gcd))

R = list(accumulate(reversed(A), gcd))


ans = max(
    L[-2],
    R[-2],
    max(
        (
            gcd(L[k], R[-(k + 3)])
            for k in range(N - 2)
        ),
        default=L[-1]
    )
)


print(ans)

Solution (Python):
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))+[0]
left=[0]*(n+1)
right=[0]*(n+1)
for i in range(1,n):
    left[i]=gcd(left[i-1],a[i-1])
for i in range(n,0,-1):
    right[i-1]=gcd(right[i],a[i])
ans=[gcd(right[i],left[i]) for i in range(n)]
print(max(ans))

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

N = int(input())
A = [i for i in 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]))
R.reverse()
g = []
for i in range(N):
    g.append(gcd(L[i],R[i+1]))
print(max(g))

Solution (Python):
import copy

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

N = int(input())
A = list(map(int, input().split()))
F = copy.copy(A)
B = copy.copy(A)
for i in range(N - 1):
  F[i + 1] = gcd(F[i + 1], F[i])
for i in range(N - 2, -1, -1):
  B[i] = gcd(B[i], B[i + 1])
ans = max(F[N-2], B[1])
for i in range(1, N - 1):
  ans = max(ans, gcd(F[i-1], B[i+1]))
print(ans)

Solution (Python):
import sys
def p(*a):
  s=" ".join(map(str,a))
  
  sys.stderr.write(s+"\n")

import fractions
from functools import reduce

def gcd(*numbers):
	return reduce(fractions.gcd, numbers)
	

def gcd_list(numbers):
	return reduce(fractions.gcd, numbers)
	



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

p("N", N)
p("A", A)


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


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



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



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



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

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

p("M", M)

print( max(M) )

Solution (Python):
import numpy as np
import fractions

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


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

gcd_right = gcd_right[::-1]

value = []
for i in range(N):
    value.append(fractions.gcd(gcd_left[i], gcd_right[i+1]))

print(max(value))

Solution (Python):


import sys
input = sys.stdin.readline


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


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

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

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

print(ans)

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

def gcd(n, m):
    if n == 0 or m == 0:
        return max(n, m)
    while n % m != 0:
        n, m = m, n % m
    return m

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

Solution (Python):

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


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

    index = list(range(N))
    forward = index.copy()
    forward[0] = 0
    for i in index[:-1]:
        forward[i+1] = gcd(A[i], forward[i])

    backward = index.copy()
    backward.append(0)
    for i in reversed(index):
        backward[i] = gcd(A[i], backward[i+1])
    
    
    M = [gcd(f, b) for f, b in zip(forward, backward[1:])]
    
    print(max(M))


if __name__ == "__main__":
    main()

Solution (Python):
from fractions import *
from math import *

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    gcd_left = [a[0]]
    gcd_right = [a[-1]]
    for i in range(n-1):
        gcd_left += [gcd(gcd_left[-1], a[i+1])]
        gcd_right += [gcd(gcd_right[-1], a[-i-2])]
    gcd_right = gcd_right[::-1]
    ans = 1
    for i in range(n):
        if i == 0:
            ans = max(ans, gcd_right[i+1])
        elif i == n-1:
            ans = max(ans, gcd_left[i-1])
        else:
            ans = max(ans, gcd(gcd_left[i-1], gcd_right[i+1]))

    print(ans)

main()

Solution (Python):
from collections import defaultdict
from math import sqrt
inpl = lambda: list(map(int,input().split()))
 
def primes(N):
    P = []
    for i in range(2,N):
        sieve = 1
        for p in P:
            if p*p > i:
                break
            elif i % p == 0:
                sieve = 0
                break
        if sieve == 0:
            continue
        else:
            P.append(i)
    return P
 
def factorize(n, P=None):
    if P is None:
        P = primes(int(sqrt(n))+1)
    factor = []
    power = []
    for p in P:
        if p * p > n:
            break
        elif n % p == 0:
            k = 0
            while n % p == 0:
                n //= p
                k += 1
            factor.append(p)
            power.append(k)
    if n > 1:
        factor.append(n)
        power.append(1)
    return factor, power
 
class Counter:
    def __init__(self, start=0):
        self.index = start-1
 
    def __call__(self):
        self.index += 1
        return self.index
 
N = int(input())
A = inpl()
 
P_MAX = 32000
infty = 100
P = primes(P_MAX)
 
np = defaultdict(Counter())
Np = 0
 
lowest_powers = []
bottle_necks = []
primelist = []
all_factors = set()
for n in range(N):
    primes, powers = factorize(A[n],P)
    pp = [0]*Np
    for i in range(len(primes)):
        k = np[primes[i]]
        if k >= Np:
            Np += 1
            pp.append(powers[i])
            primelist.append(primes[i])
            if n > 1:
                lowest_powers.append([0,0])
                bottle_necks.append(-1)
            elif n == 1:
                lowest_powers.append([0,infty])
                bottle_necks.append(0)
            else:
                lowest_powers.append([infty,infty])
                bottle_necks.append(-1)
        else:
            pp[k] = powers[i]
    for k in range(Np):
        lowest = lowest_powers[k]
        if pp[k] == lowest[0]:
            lowest[1] = lowest[0]
            bottle_necks[k] = -1
        elif pp[k] < lowest[0]:
            lowest[1] = lowest[0]
            lowest[0] = pp[k]
            bottle_necks[k] = n
        elif pp[k] < lowest[1]:
            lowest[1] = pp[k]
 
bottleneck_factors = [ [] for _ in range(N) ]
for k in range(Np):
    n = bottle_necks[k]
    if n >= 0:
        lowest = lowest_powers[k]
        bottleneck_factors[n].append([primelist[k],lowest[1]-lowest[0]])
best_n = -1
largest_f = 1
for n in range(N):
    f = 1
    for pp in bottleneck_factors[n]:
        f *= pp[0]**pp[1]
    if f > largest_f:
        largest_f = f
        best_n = n
 
ans = 1
pn = [ s[0] for s in bottleneck_factors[best_n] ]
for k in range(Np):
    p = primelist[k]
    if bottle_necks[k] == best_n:
        ans *= p**lowest_powers[k][1]
    else:
        ans *= p**lowest_powers[k][0]
print(ans)

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

N = int(input())
A = [i for i in 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]))
R.reverse()
g = []
for i in range(N):
    g.append(gcd(L[i],R[i+1]))
print(max(g))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    def gcd(a,b):
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    def query(p,q):
        p += num
        q += num
        res=0
        while p<q:
            if q&1:
                q-=1
                res=gcd(res,data[q-1])
            if p&1:
                res=gcd(res,data[p-1])
                p+=1
            p = p//2
            q = q//2
        return res

    n = int(input())
    a = tuple(map(int,input().split()))
    num =2**(n-1).bit_length()
    data=[0]*2*num

    for i in range(n):
        data[i+num-1]=a[i]
    
    for i in range(num-2,-1,-1) :
        data[i]=gcd(data[2*i+1],data[2*i+2]) 

    ans = -1
    for i in range(n):
        ans = max(ans,gcd(query(0,i),query(i+1,n)))
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):
import fractions as f,itertools as i;n,*a=map(int,open(0).read().split());b,c=[list(i.accumulate([0]+t,f.gcd))for t in(a,a[::-1])];print(max(f.gcd(b[i],c[-i-2])for i in range(n)))

Solution (Python):
import sys
import math 
ni = lambda: int(ns())
na = lambda: list(map(int, input().split()))
ns = lambda: input()

N = ni()
A = na()

def gcd(x,y):
    r = x%y
    while(r!=0):
        x,y = y,r
        r = x%y
    return y
L = []
R = []
l = A[0]
r = A[-1]
for i in range(N-1):
    l = gcd(l,A[i])
    L.append(l)
for i in range(N-1,0,-1):
    r = gcd(r,A[i])
    R.append(r)
M = 0
M = max(R[-1],L[-1])
for i in range(N-2):
    M = max(M,gcd(L[i],R[-(i+2)]))
print(M)

Solution (Python):



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


def main():
    n = int(input())
    A = list(map(int, input().split()))
    gcdA = [0]*n
    gcdB = [0]*n
    gcdA[0] = A[0]
    gcdB[n-1] = A[n-1]
    for i in range(1, n):
        gcdA[i] = gcd(A[i], gcdA[i-1])
        gcdB[n-i-1] = gcd(A[n-i-1], gcdB[n-i])
    ans = max(gcdA[n-2], gcdB[1])
    for i in range(1, n-1):
        ans = max(ans, gcd(gcdA[i-1], gcdB[i+1]))
    print(ans)


if __name__ == "__main__":
    main()