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):
def gcd(a, b):
    if a == 0:
        return b
    if b == 0:
        return a
    return gcd(b % a, a)


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

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

ans = 0
for i in range(n):
    ans = max(ans, gcd(b[i], c[n - i - 1]))

print(ans)

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

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

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

    R.reverse()
    M = [gcd(L[i], R[i+1]) for i in range(N)]
    print(max(M))


if __name__ == '__main__':
    main()

Solution (Python):

import fractions

N = int(input())
X = list(map(int, input().split()))
   
M = 1  
L = [0 for i in range(100004)] 
R = [0 for i in range(100004)]  

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

for i in reversed(range(N)):
  
  R[i] = (fractions.gcd(R[i+1],X[i]))
    
ans = 1



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

print(ans)

Solution (Python):
import sys

def gcd(a, b):
	if a < b:
		a, b = b, a
	if not b:
		return a
	return gcd (b, a % b)
	
n = int(input())
a = list(map(int, input().split()))
l = [0 for i in range(100004)]
r = [0 for i in range(100004)]

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

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

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

Solution (Python):
import sys

def gcd(a, b):
	if a < b:
		a, b = b, a
	if not b:
		return a
	return gcd (b, a % b)
	
n = int(input())
a = list(map(int, input().split()))
l = [0 for i in range(100004)]
r = [0 for i in range(100004)]

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


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

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

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


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

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

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

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

print(ret)

Solution (Python):
import fractions
n=int(input())
nums=list(map(int,input().split()))
def g(a,b):
  if a==0:return b
  if b==0:return a
  return fractions.gcd(a,b)
l=[0]*(n+1)
r=[0]*(n+1)
for i in range(1,n+1):
  l[i]=g(l[i-1],nums[i-1])
for i in range(n)[::-1]:
  r[i]=g(r[i+1],nums[i])
ans=0
for i in range(n):
  c=g(l[i],r[i+1])
  if c>ans:ans=c
print(ans)

Solution (Python):
def euclid(x, y):
    if x == 0:
        return y
    if y == 0:
        return x
    while x % y != 0:
        temp = x
        x = y
        y = temp % y
    return y

n = int(input())
data = list(map(int, input().split()))
array = data
L = []
L.append(0)
R = []
R.append(0)
m = []
for i in range(n):
    L.append(euclid(L[i], data[i]))
data.reverse()
for i in range(n):
    R.append(euclid(R[i], data[i]))
R.reverse()
for i in range(0,n):
    m.append(euclid(L[i] , R[i + 1]))
print(max(m))

Solution (Python):


import fractions

N = int(input())
arr = list(map(int, input().split()))
L = [0]
R = [0]
B = []

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

Solution (Python):

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
  
n = int(input())
a = [int(x) for x in input().split()]
reversed_a = list(reversed(a))

length = len(a)
left_gcd = [0] * length
right_gcd = [0] * length

for i in range(1, length):
  left_gcd[i] = gcd(a[i-1], left_gcd[i-1])
  right_gcd[i] = gcd(reversed_a[i-1], right_gcd[i-1])
  
right_gcd = right_gcd[::-1]
m = [gcd(r, l) for l, r in zip(left_gcd, right_gcd)]





print(max(m))

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

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

def main():
    N = i2n()
    A = i2nn()
    
    
    
    
    
    
    L = [0] * N
    R = [0] * N
    L[0] = A[0]
    R[-1] = A[-1]
    for i in range(1, N):
        L[i] = gcd(L[i-1], A[i])
    for i in range(N-2, -1, -1):
        R[i] = gcd(R[i+1], A[i])
    n = -1
    if n < R[1]:
        n = R[1]
    if n < L[-2]:
        n = L[-2]
    for i in range(1, N-1):
        t = gcd(L[i-1], R[i+1])
        if n < t:
            n = t
    print(n)

main()

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

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

ans = 1

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

print(ans)

Solution (Python):
import sys
from copy import deepcopy
def gcd(a, b):
    if b > a:
        a, b = b, a
    if b == 0:
        return a
    return gcd(b, a%b)

def process(arr):
    L = [0]*(n+1)
    for i in range(1, n+1):
        gcd1 = gcd(L[i-1], arr[i-1])
        L[i] = gcd1
    R = [0]*(n+1)
    for i in range(1, n+1):
        gcd2 = gcd(R[-1-i+1], arr[-1-i+1])
        R[-1-i] = gcd2
    return L, R
    
n = int(raw_input())
arr = map(int, raw_input().split())
if n <= 2:
    print max(arr)
    exit()
L, R = process(arr)
M = [-1] * (n+1)
for i in range(n):
    M[i] = gcd(L[i], R[i+1])

print max(M)

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

L = []
R = []
tl = A[0]
tr = A[-1]
for i in range(N):
  tl = gcd(tl, A[i])
  L.append(tl)
  tr = gcd(tr, A[N-1-i])
  R.append(tr)
mx = 1
R.reverse()
for i in range(N):
  tl = L[i-1] if i else R[i+1]
  tr = R[i+1] if i+1 < N else L[i-1]
  mx = max(mx, gcd(tl, tr))
print(mx)

Solution (Python):



""" convenient functions




"""

import sys
sys.setrecursionlimit(10**7)
from itertools import accumulate, combinations, permutations, product 

from math import factorial, ceil, floor
def factorize(n):
    """return the factors of the Arg and count of each factor
    
    Args:
        n (long): number to be resolved into factors
    
    Returns:
        list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
    """
    fct = []  
    b, e = 2, 0  
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct
def combinations_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    
    if n < 0 or r < 0:
        raise Exception('combinations_count(n, r) not defined when n or r is negative')
    if n - r < r: r = n - r
    if r < 0: return 0
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result
def combinations_with_replacement_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    if n < 0 or r < 0:
        raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
    elif n == 0:
        return 1
    else:
        return combinations_count(n + r - 1, r)
from bisect import bisect_left, bisect_right
from collections import deque, Counter, defaultdict 
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
from copy import deepcopy, copy 
from operator import itemgetter


from functools import reduce
def chmin(x, y):
    """change minimum
    if x > y, x = y and return (x, True).
    convenient when solving problems of dp[i]
    
    Args:
        x (long): current minimum value
        y (long): potential minimum value
    
    Returns:
        (x, bool): (x, True) when updated, else (x, False)
    """
    if x > y:
        x = y
        return (x, True)
    else:
        return (x, False)
def chmax(x, y):
    """change maximum
    if x < y, x = y and return (x, True).
    convenient when solving problems of dp[i]
    
    Args:
        x (long): current maximum value
        y (long): potential maximum value
    
    Returns:
        (x, bool): (x, True) when updated, else (x, False)
    """
    if x < y:
        x = y
        return (x, True)
    else:
        return (x, False)

from fractions import gcd 
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers):
    return reduce(lcm, numbers, 1)



INF = 10 ** 18
MOD = 10 ** 9 + 7
modpow = lambda a, n, p = MOD: pow(a, n, p) 
def modinv(a, p = MOD):
    
    
    return modpow(a, p-2, p)
def modinv_list(n, p = MOD):
    if n <= 1:
        return [0,1][:n+1]
    else:
        inv_t = [0,1]
        for i in range(2, n+1):
            inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
        return inv_t
def modfactorial_list(n, p = MOD):
    if n == 0:
        return [1]
    else:
        l = [0] * (n+1)
        tmp = 1
        for i in range(1, n+1):
            tmp = tmp * i % p
            l[i] = tmp
        return l
def modcomb(n, k, fac_list = [], p = MOD):
    
    
    from math import factorial
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    if len(fac_list) <= n:
        a = factorial(n) % p
        b = factorial(k) % p
        c = factorial(n-k) % p
    else:
        a = fac_list[n]
        b = fac_list[k]
        c = fac_list[n-k]
    return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
def modadd(a, b, p = MOD):
    return (a + b) % MOD
def modsub(a, b, p = MOD):
    return (a - b) % p
def modmul(a, b, p = MOD):
    return ((a % p) * (b % p)) % p
def moddiv(a, b, p = MOD):
    return modmul(a, modpow(b, p-2, p))

""" initialize variables and set inputs

    
    

    
    
    
    
    

    
    
    
    

    
    
"""


r = lambda: sys.stdin.readline().strip()
r_int = lambda: int(r())
R = lambda: list(map(int, r().split()))
Rfloat = lambda: list(map(float, r().split()))
Rtuple = lambda: tuple(map(int, r().split()))
Rmap = lambda: map(int, r().split())

""" how to treat input







"""





N = int(r())
A = R()
B = [0] * N
C = [0] * N
for i, a in enumerate(A):
    if i == 0:
        tmp = a
    else:
        tmp = gcd(a, tmp)
    B[i] = tmp

A.reverse()
for i, a in enumerate(A):
    if i == 0:
        tmp = a
    else:
        tmp = gcd(a, tmp)
    C[i] = tmp
C.reverse()

ans = 1
for i in range(N):
    if i == 0:
        ans = max(ans, C[i+1])
    elif i == N-1:
        ans = max(ans, B[i-1])
    else:
        ans = max(ans, gcd(B[i-1],C[i+1]))

print(ans)



"""memo: how to use defaultdict of list

Dic = defaultdict(list)

Dic[x].append(y)

for k, v in Dic.items():
"""

Solution (Python):
from fractions import gcd


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

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


main()

Solution (Python):
from fractions import gcd


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

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


main()

Solution (Python):
from fractions import gcd


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

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


main()

Solution (Python):


from fractions import gcd

def main():
    n = int(input())
    a = tuple(map(int, input().rstrip().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)


if __name__ == "__main__":
    main()

Solution (Python):
import fractions

N = int(input())

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

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

now = A[0]
for i in range(N):

    if i >= 1:

        now = fractions.gcd(now,A[i-1])
        L[i] = now

A.reverse()

now = A[0]
for i in range(N):

    if i >= 1:

        now = fractions.gcd(now,A[i-1])
        R[i] = now

A.reverse()
R.reverse()

maxi = 0
for i in range(N):

    if L[i] == 0 or R[i] == 0:
        maxi = max(maxi,L[i],R[i])

    else:
        maxi = max(maxi, fractions.gcd(L[i],R[i]))

print (maxi)

Solution (Python):
from fractions import gcd


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

g = A[0]
gcd1 = [0] * N
gcd1[0] = A[0]
for i in range(1, N):
    g = gcd1[i] = gcd(A[i], g)

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

gcd0 = [0] * N
gcd0[0] = gcd2[1]
gcd0[N-1] = gcd1[N-2]

for i in range(1, N-1):
    gcd0[i] = gcd(gcd1[i-1], gcd2[i+1])

print(max(gcd0))

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

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[i+1] = gcd(right[i],a[n-i-1])
left.append(0)
right.append(0)
right.reverse()
ans = 0    
for i in range(n):
    gcdi = gcd(left[i],right[i+2])
    ans = max(ans, gcdi)
    
print(ans)

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()))
B = [A[0]]
C = [A[N-1]]
for i in range(1,N):
  B.append(gcd(A[i],B[i-1]))
  C.append(gcd(A[N-i-1],C[i-1]))
D = [C[N-2]]
for j in range(N-2):
  D.append(gcd(B[j],C[N-j-3]))
D.append(B[N-2])
print(max(D))

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[n - i - 1]))

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

print(ans)

Solution (Python):
from fractions import gcd

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

left = [0]
right = [0]

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

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

print(ans)

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

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

Solution (Python):

"""
Created on Sat Jun 29 20:51:40 2019

@author: makigon
"""

import fractions
from functools import reduce
 
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
 
N = int(input())
a = list(map(int,input().split()))
ans = 0

t_left = [0 for i in range(N)]
t_right = [0 for i in range(N)]

t_left[0] = a[0]
t_left[1] = fractions.gcd(a[0],a[1])

for i in range(2,N):
    t_left[i] = fractions.gcd(t_left[i-1],a[i])

t_right[N-1] = a[N-1]
t_right[N-2] = fractions.gcd(a[N-1],a[N-2])

for j in range(N-3,0,-1):
    t_right[j] = fractions.gcd(t_right[j+1],a[j])
    

for i in range(0,N-2):
    tmp = fractions.gcd(t_left[i],t_right[i+2])
    if tmp > ans:
        ans = tmp
    
print(max(ans,t_right[1],t_left[N-2]))

Solution (Python):
import fractions

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

  right = []
  left  = []

  right.append(a[0])
  left.append(a[-1])

  for i in range(1, len(a)-1):
    right.append(fractions.gcd(right[i-1], a[i]))
    left.append(fractions.gcd(left[i-1], a[-i-1]))
  
  ans = max(right[-1], left[-1])
  
  for i in range(len(right)-1):
    ans = max(ans, fractions.gcd(right[i], left[-i-2]))
    
  print(ans)
  
main()

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

Solution (Python):
from fractions import gcd

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


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

max_m = -1
for i in range(N+1):
    m = gcd(dp_l[i], dp_r[N-i-1])
    max_m = max(max_m, m)
print(max_m)

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

from fractions import gcd
r = []
l = []
r.append(a[-1])

l.append(a[0])

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

l.insert(0,a[-1])
r.insert(0,a[0])


def solve(num):
    ans = gcd(l[num],r[n-1-num])
    return ans

can = []
for i in range(n):
    can.append(solve(i))

print(max(can))

Solution (Python):
try :
    from math import gcd
except ImportError :
    from fractions import gcd


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


X = [A[0]]
Y = [A[-1]]

for i in range(1,N) :
    x = gcd(X[i-1], A[i])
    y = gcd(Y[i-1], A[N-i-1])
    X.append(x)
    Y.append(y)

ans = 0

Y = Y[::-1]

for i in range(N) :
    if i == 0 :
        calc = Y[1]
    elif i == N-1 :
        calc = X[N-2]
    else :
        calc = gcd(X[i-1],Y[i+1])
    
    ans = max(ans, calc)

print(ans)

Solution (Python):
import fractions

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

gcd_l = [0]
gcd_r = [0]

for i in range(n):
  gcd_l.append(fractions.gcd(gcd_l[i], l[i]))
  gcd_r.append(fractions.gcd(gcd_r[i], l[-(i+1)]))
gcd_r = gcd_r[::-1]



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

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


L = [0]*(N - 1)
R = [0]*(N - 1)
L[0] = A[0]
R[0] = A[-1]

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




B = [R[-1]]
for i in range(N - 2):
    l = L[i]
    r = R[N - 3 - i]
    
    B.append(gcd(l, r))
B.append(L[-1])


ans = max(B)
print(ans)

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

N, = map(int, input().split())
L = [int(v) for v in input().split()]

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

l1 = [None] * N
l2 = [None] * N

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

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

def solve(n, a):
    l = [0] * n
    l[0] = a[0]
    for i in range(1, n):
        l[i] = gcd(l[i-1], a[i])
    r = [0] * n
    r[n-1] = a[n-1]
    for i in range(n-2, -1, -1):
        r[i] = gcd(r[i+1], a[i])
    b = []
    for i in range(n):
        if i == 0:
            b.append(r[i+1])
        elif i == n-1:
            b.append(l[i-1])
        else:
            b.append(gcd(l[i-1], r[i+1]))
    return max(b)

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

Solution (Python):
from fractions import gcd
from collections import deque

N = int(input())
A = [int(a) for a in input().split()]
L = deque([0])
R = deque([0])

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = []
L = []
R = []

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

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

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

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

print(max(ans))

Solution (Python):
import fractions

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

left = [0]
right = [0]
for a in a_list:
    left.append(fractions.gcd(left[-1], a))
    
a_list.reverse()
for a in a_list:
    right.append(fractions.gcd(right[-1], a))

right.reverse()

m = []
for i in range(len(a_list)):
    m.append(fractions.gcd(left[i], right[i + 1]))
    
print(max(m))

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

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

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

R.reverse()

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

print(max(M))

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

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

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

list_gcd = [l[-2], r[1]]
for n in range(N - 2):
	list_gcd += [gcd(l[n], r[n + 2])]





print(max(list_gcd))

Solution (Python):
import fractions

N = int(input())
A = [int(i) for i in input().split()]
A.insert(0,0)
A.append(0)
tmp = 0

L, R = [0]*(N+2), [0]*(N+2)
L[0] = 0
R[N+1] = 0

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

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

Solution (Python):
import math

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

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

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


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

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




ans=[0]*N
ans[0]=rgcd[1]
ans[N-1]=lgcd[N-2]
for i in range(1,N-1):
    ans[i]=gcd(lgcd[i-1],rgcd[i+1])
print(max(ans))

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

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

l_gcd = []
r_gcd = []

for i, a in enumerate(A):
    if i == 0:
        l_gcd.append(a)
    else:
        l_gcd.append(gcd(l_gcd[i-1], a))
for i, a in enumerate(reversed(A)):
    if i == 0:
        r_gcd.append(a)
    else:
        r_gcd.append(gcd(r_gcd[i-1], a))
ans = 1
for i in range(N):
    if i == 0:
        ans = max(ans, r_gcd[N-2])
    elif i == N-1:
        ans = max(ans, l_gcd[i-1])
    else:
        ans = max(ans, gcd(l_gcd[i-1], r_gcd[N-2-i]))
print(ans)

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

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

l_gcd = []
r_gcd = []

for i, a in enumerate(A):
    if i == 0:
        l_gcd.append(a)
    else:
        l_gcd.append(gcd(l_gcd[i-1], a))
for i, a in enumerate(reversed(A)):
    if i == 0:
        r_gcd.append(a)
    else:
        r_gcd.append(gcd(r_gcd[i-1], a))
ans = 1
for i in range(N):
    if i == 0:
        ans = max(ans, r_gcd[N-2])
    elif i == N-1:
        ans = max(ans, l_gcd[i-1])
    else:
        ans = max(ans, gcd(l_gcd[i-1], r_gcd[N-2-i]))
print(ans)

Solution (Python):
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
As = nl()

from fractions import gcd
gcds_left = [0] * N
gcds_right = [0] * N
gcds_left[0] = As[0]
gcds_right[N-1] = As[N-1]

for i in range(1, N):
    gcds_left[i] = gcd(As[i], gcds_left[i-1])
    gcds_right[N-1-i] = gcd(As[N-1-i], gcds_right[N-i])

max_gcd = -1
for i in range(1, N-1):
    g = gcd(gcds_left[i-1], gcds_right[i+1])
    if g > max_gcd:
        max_gcd = g
print(max(max_gcd, gcds_right[1], gcds_left[N-2]))

Solution (Python):
from fractions import gcd

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

li=[0]*(n+1)
ri=[0]*(n+1)

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

ans=0

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

Solution (Python):
from fractions import gcd


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

total=0
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-1-i])

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

print(total)

Solution (Python):

from collections import defaultdict
import math
import re

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

def main():
    
    N = int(input())
    A_list = [int(a) for a in input().split()]

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

    max_val = 0
    for i in range(N):
        if i == 0:
            g = right[N-2]
        elif i == N-1:
            g = left[N-2]
        else:
            g = gcd(left[i-1], right[N-i-2])
        
        if g > max_val:
            max_val = g

    print(max_val)

if __name__ == '__main__':
    main()

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

def gcds(t,X,V):
	if X[t] > V[t]:
		x, y = X[t], V[t]
	else:
		x, y = V[t], X[t]
	while(y>0):
		x, y = y, x%y
		V[t+1] = x

def gcde(t,X,V):
	if X[t] > V[t+1]:
		x, y = X[t], V[t+1]
	else:
		x, y = V[t+1], X[t]
	while(y>0):
		x, y = y, x%y
		V[t] = x

def main():
	N = int(input())
	A = list(map(int,input().split()))
	ans = 1
	S = [A[0]]*(N+1)
	E = [A[N-1]]*(N+1)
	for t in range(N):
		gcds(t, A, S)
		gcde(N-t-1, A, E)
	ans = max(ans, E[1])
	ans = max(ans, S[N-1])
	for t in range(1,N-1):
		ans = max(ans, gcd(S[t], E[t+1]))
	print(ans)

main()

Solution (Python):

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

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




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

L[0]=A[0]
R[N-1]=A[N-1]
for i,a in enumerate(A):
    if i==0:continue
    L[i]=gcd(L[i-1],a)
for i,a in enumerate(reversed(A)):
    if i==0:continue
    R[N-i-1]=gcd(R[N-i],a)
ans=R[1] 
ans=max(ans,L[N-2])
for i in range(N-1):
    if i==0:continue
    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,queue

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

  l2=[l[-1]]
  for i in range(1,n):
    l2.append(gcd(l2[i-1],l[n-i-1]))
  l2.reverse()

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

  return mx

print(main())

Solution (Python):


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

n=int(raw_input())
a=map(int,raw_input().split())
res=0
x=[0]*(n+1)
y=[0]*(n+1)
for i in range(n):
 x[i+1]=gcd(x[i],a[i])
 y[i+1]=gcd(y[i],a[n-i-1])
for i in range(n):
 res=max(gcd(x[i],y[n-i-1]),res)
print res

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

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

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

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

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

Solution (Python):
import fractions
N = int(input())
A = [int(i) for i in input().split()]
ans = 0

L = [0]*(N+2)
R = [0]*(N+2)
L[0] = 0
L[1] = A[0]
R[N+1] = 0
R[N] = A[N-1]

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