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 functools
import copy

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

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

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

r = [0] * N 
for j in range(N):
    if j == 0:
        continue
    else:
        r[N-j-1] = gcd(r[N-j], A[N-j])

m = [0] * N
for i in range(N):
    m[i] = gcd(l[i], r[i])

print(max(m))

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


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

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

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

R.reverse()

M = [0] * n
for i in range(n):
    if i == 0:
        M[i] = R[0]
    elif i == n-1:
        M[i] = L[-1]
    else:
        M[i] = gcd(L[i-1],R[i])


print(max(M))

Solution (Python):


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

def gcd(i, j):
    if i <= 0:
        return j
    elif j <= 0:
        return i

    while i != j:
        if i < j:
            j %= i
            if j == 0: return i
        else:
            i %= j
            if i == 0: return j
    return i


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



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

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

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

N=input()
A=map(int, sys.stdin.readline().split())

L=deque()

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

R=deque()

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

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

print ans

Solution (Python):
import fractions
 
n = int(input())
li = list(map(int,input().split()))
 
leftgcd = [0 for i in range(n)]
rightgcd = [0 for i in range(n)]
 
leftgcd[0] = li[0]
rightgcd[n - 1] = li[n - 1]
 
if n == 2:
  print(max(li[0], li[1]))
else:
  for j in range(1, n):
    leftgcd[j] = fractions.gcd(li[j], leftgcd[j - 1])
    rightgcd[n - 1 - j] = fractions.gcd(li[n - 1 - j], rightgcd[n - j])
 
  ans = max(leftgcd[n - 2], rightgcd[1])
  for k in range(1, n - 1):
    g = fractions.gcd(leftgcd[k - 1], rightgcd[k + 1])
    if g > ans:
      ans = g
  print(ans)

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

import fractions
from functools import reduce

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

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

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

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

print(M)

Solution (Python):

import functools


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


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

gcd_left = [a[0]]
gcd_right = [a[-1]]
for i in range(1,n):
    if gcd_left[i-1] != 1:
        res = gcd(gcd_left[i-1], a[i])
    else:
        res = 1
    gcd_left.append(res)

for i in range(1,n):
    if gcd_right[i-1] != 1:
        res = gcd(gcd_right[i-1], a[-(i+1)])
    else:
        res = 1
    gcd_right.append(res)
gcd_right = gcd_right[::-1]


ans = 0
for i in range(n):
    if i == 0:
        res = gcd_right[1]
    elif i == n-1:
        res = gcd_left[-2]
    else:
        res = gcd(gcd_right[i+1], gcd_left[i-1])
    ans = max(res, ans)
print(ans)

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

N=int(input())
A=list(map(int,input().split()))
x=A[0]
y=A[1]
D=sorted(list(set(make_divisors(x))|set(make_divisors(y))))[::-1]
for d in D:
    if sum([a%d==0 for a in A])>=N-1:
        break
print(d)

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


def gcd_on_blackboard(N: int, A: list)->int:
    L, R = [0] * N, [0] * N

    L[0] = A[0]
    for i, a in enumerate(A[1:]):
        L[i+1] = gcd(L[i], a)

    R[-1] = A[-1]
    for i, a in enumerate(reversed(A[:-1])):
        R[N-i-2] = gcd(R[N-i-1], a)

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

    return max_diff


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]

    ans = gcd_on_blackboard(N, A)
    print(ans)

Solution (Python):
import sys

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

input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
l_list = [0]
r_list = [0]
l_gcd, r_gcd = 0, 0
for i in range(n-1):
    l_gcd = gcd(l_gcd, a_list[i])
    l_list.append(l_gcd)
    r_gcd = gcd(r_gcd, a_list[-i-1])
    r_list.append(r_gcd)
ans = 1
for i in range(n):
    ans = max(ans, gcd(l_list[i], r_list[-i-1]))
print(ans)

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

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

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

p = make_divisors(l[0])
p.extend(make_divisors(l[1]))

p.sort(reverse=True)
for i in p:
    ans = 1
    flag = 0
    for j in l:
        if j%i != 0:
            if flag == 1:
                ans = 0
                break
            flag = 1
    if ans:
        print(i)
        exit()

Solution (Python):
from math import ceil, sqrt
from fractions import gcd

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

D = set()
for x in A[:2]:
  for i in range(1, ceil(sqrt(x))+2):
    if x % i == 0:
      D.add(i)
      D.add(x//i)

D = list(D)
D.sort(reverse=True)
for d in D:
  count = 0
  for a in A:
    if a % d != 0:
      count += 1
    if count >= 2:
      break
  else:
    print(d)
    break

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

gcd_l = [0]
gcd_r = [0]

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

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




g = 1
for i in range(1, N + 1):
    
    g_l = gcd_l[i - 1]
    g_r = gcd_r[i + 1]
    g = max(g, gcd(g_l, g_r))
print(g)

Solution (Python):
import fractions

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


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

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

Solution (Python):
from fractions import*
n,*a=map(int,open(0).read().split())
*b,=a=a+[0]
r=range(n)
for i in r:a[i]=gcd(a[i],a[i-1]);b[n-i-1]=gcd(b[n-i-1],b[n-i])
print(max(gcd(a[i-1],b[i+1])for i in r))

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

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

n = int(input())
a = list(map(int, input().split()))
fromLeft = [0] * n
fromLeft[0] = a[0]
fromRight = [0] * n
fromRight[0] = a[n - 1]
for i in range(1, n):
    fromLeft[i] = gcd(fromLeft[i - 1], a[i])
    fromRight[i] = gcd(fromRight[i - 1], a[n - i - 1])
ret = fromLeft[n - 1]
for i in range(n):
    if i == 0:
        curGcd = fromRight[n - 2]
    elif i == n - 1:
        curGcd = fromLeft[n - 2]
    else:
        curGcd = gcd(fromLeft[i - 1], fromRight[n - i - 2])
    ret = max(ret, curGcd)
print(ret)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
def gcd(x, y):
    if x<y:x,y=y,x
    while x % y != 0:
        x, y = y, x % y
    return y
left=[]
right=[]
for i in range(n):
    if i==0:
        left.append(a[i])
        right.append(a[-i-1])
    else:
        left.append(gcd(a[i],left[i-1]))
        right.append(gcd(a[-i-1],right[i-1]))
ans=max(left[n-2],right[n-2])
for i in range(n-2):
    ans=max(ans,gcd(left[i],right[n-i-3]))
print(ans)

Solution (Python):
import fractions


'''

最大公約数を求める
最大公約数を最大にするには何を変えれば良いか

最大値との最大公約数を求めて、最大になれば良い?

'''

n = list(map(int, input().split()))
A = list(map(int, input().split()))
right = []
left = []
for i,a in enumerate(A):
    if i ==0 :
        right.append(a)
    else:
        right.append(fractions.gcd(right[i-1],a))

for i,b in enumerate(reversed(A)):
    if i ==0 :
        left.append(b)
    else:
        left.append(fractions.gcd(left[i-1],b))

result = []

for i,r in enumerate(A):
    if i ==0:
        result.append(right[len(right)- 2])
    if i == len(A) -1:
        result.append(left[len(left) - 2])

    result.append(fractions.gcd(left[i -1], right[len(right) - i -2]))

print(max(result))

Solution (Python):
def gcd(x, y):
  if y == 0:
    return x
  else:
    return gcd(y, x % y) 
n=int(input())
a=list(map(int,input().split()))
l=[0]*n
r=[0]*n
for i in range(1,n):
  if i==1:
    l[1]=a[0]
    r[-2]=a[-1]
  else:
    l[i]=gcd(l[i-1],a[i-1])
    r[-i-1]=gcd(r[-i],a[-i])
g=[0]*n
for i in range(n):
  if l[i]==0 or r[i]==0:
    g[i]=l[i]+r[i]
  else:
    g[i]=gcd(l[i],r[i])
print(max(g))

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

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

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

Solution (Python):
import fractions

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

if n==2:
  print(max(a[0],a[1]))
  exit()
elif n==3:
  print(max(fractions.gcd(a[0],a[1]),fractions.gcd(a[0],a[2]),fractions.gcd(a[1],a[2])))
  exit()

for i in range(n):
  temp = fractions.gcd(ans,a[i])
  if temp != ans:
    ans = max(temp,x)
    x= fractions.gcd(temp,x)

print(ans)

Solution (Python):
import fractions

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(A[i], left[i])

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

max_gcd = 1

for i in range(N):

    l = left[i]
    r = right[i + 1]

    gcd_num = fractions.gcd(l, r)

    max_gcd = max(max_gcd, gcd_num)

print(max_gcd)

Solution (Python):
import fractions
 
n = int(input())
a = [int(value) for value in input().split(" ")]
 
from_right_gcd = []
from_left_gcd = []
 

for index in range(a.__len__()):
    if index == 0:
        from_left_gcd.append(a[index])
        continue
    from_left_gcd.append(fractions.gcd(from_left_gcd[index - 1], a[index]))
 

for index in range(a.__len__() - 1, -1, -1):
    if index == a.__len__() - 1:
        from_right_gcd.append(a[index])
        continue
    from_right_gcd.append(fractions.gcd(from_right_gcd[a.__len__() - index - 1 - 1], a[index]))
 
maybe_answer_list = []
 
for index in range(a.__len__()):
    if index == 0:
        maybe_answer_list.append(from_right_gcd[a.__len__() - 1 - 1])
        continue
    if index == a.__len__() - 1:
        maybe_answer_list.append(from_left_gcd[index - 1])
        continue
    maybe_answer_list.append(fractions.gcd(from_left_gcd[index - 1], from_right_gcd[a.__len__() - index - 1 - 1]))
 
print(max(maybe_answer_list))

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):
    t = fractions.gcd(L[i], R[N - i - 1])
    ans = max(t, ans)

print(ans)

Solution (Python):
from fractions import*
n,*a=map(int,open(0).read().split())
b=a[:]
for i in range(1,n):
    a[i]=gcd(a[i],a[i-1])
    b[n-i-1]=gcd(b[n-i-1],b[n-i])
a=[0]+a
b+=0,
print(max(gcd(a[i],b[i+1])for i in range(n)))

Solution (Python):
def gcd(a,b):
	while b:
		a,b=b,a%b
	return a
n=int(input())
l = list(map(int, input().split()))
ans=1;
m=[0]*(n+1)
o=[0]*(n+1)
for i in range(n):
	m[i+1]=gcd(m[i],l[i])
	o[i+1]=gcd(o[i],l[n-i-1])
for i in range(n):
	ans=max(gcd(m[i],o[n-i-1]),ans)
print(ans)

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


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

devs = make_divisors(As[0])
devs.extend(make_divisors(As[1]))
devs = list(set(devs))
cnt = 1
for dev in sorted(devs)[::-1]:
    a = sum([a%dev==0 for a in As])
    if N - a == 0 or N -a == 1:
        print(dev)
        break

Solution (Python):
import fractions

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

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

print(ans)

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

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

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

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

from_right_gcd = []
from_left_gcd = []


for index in range(len(a)):
    if index == 0:
        from_left_gcd.append(a[index])
        continue
    from_left_gcd.append(fractions.gcd(from_left_gcd[index - 1], a[index]))


for index in range(len(a) - 1, -1, -1):
    if index == len(a) - 1:
        from_right_gcd.append(a[index])
        continue
    from_right_gcd.append(fractions.gcd(from_right_gcd[len(a) - index - 1 - 1], a[index]))

maybe_answer_list = []

for index in range(len(a)):
    if index == 0:
        maybe_answer_list.append(from_right_gcd[len(a) - 1 - 1])
        continue
    if index == len(a) - 1:
        maybe_answer_list.append(from_left_gcd[index - 1])
        continue
    maybe_answer_list.append(fractions.gcd(from_left_gcd[index - 1], from_right_gcd[len(a) - index - 1 - 1]))

print(max(maybe_answer_list))

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

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

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

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

def factor(n):
	factors = [1, n]
	i=2
	while i*i <= n:
		if n%i==0:
			factors.append(i)
			factors.append(n//i)
		i+=1

	return factors

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

if n==2:
	print(max(A))
	quit()

gcd1 = 0
fac1 = sorted(factor(A[0]),reverse=True)
for i in range(len(fac1)):
	count = 0
	for j in range(1, n):
		if A[j]%fac1[i]!=0:
			count += 1
			if count >= 2:
				break
	if count <= 1:
		gcd1 = fac1[i]
		break

fac2 = sorted(factor(A[1]),reverse=True)
gcd2 = 0
for i in range(len(fac2)):
	count = 0
	for j in range(2, n):
		if A[j]%fac2[i]!=0:
			count += 1
			break
	if count <= 0:
		gcd2 = fac2[i]
		break

print(max(gcd1, gcd2))

Solution (Python):
import fractions

from functools import reduce
 
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
 
n = int(input())
a = [int(value) for value in input().split(" ")]

from_right_gcd = []
from_left_gcd = []


for index in range(len(a)):
    if index == 0:
        from_left_gcd.append(a[index])
        continue
    from_left_gcd.append(fractions.gcd(from_left_gcd[index - 1], a[index]))


for index in range(len(a) - 1, -1, -1):
    if index == len(a) - 1:
        from_right_gcd.append(a[index])
        continue
    from_right_gcd.append(fractions.gcd(from_right_gcd[len(a) - index - 1 - 1], a[index]))

maybe_answer_list = []

for index in range(len(a)):
    if index == 0:
        maybe_answer_list.append(from_right_gcd[len(a) - 1 - 1])
        continue
    if index == len(a) - 1:
        maybe_answer_list.append(from_left_gcd[index - 1])
        continue
    maybe_answer_list.append(fractions.gcd(from_left_gcd[index - 1], from_right_gcd[len(a) - index - 1 - 1]))

print(max(maybe_answer_list))

Solution (Python):
N = int(input())

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


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

list1=[0]
L=0
for a in A:
    L=gcd(L,a)
    list1.append(L)

list2=[0]
R=0
for a in reversed(A):
    R=gcd(R,a)
    list2.append(R)

list2.reverse()

ans=[]

for i in range(N):
    ans.append(gcd(list1[i],list2[i+1]))

print(max(ans))

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

def factors(n):
    Ret = []
    for i in range(2, int( n**(1/2))+1):
        if n%i == 0:
            Ret.append([])
            t = i
            while n%i == 0:
                Ret[-1].append(t)
                n //= i
                t *= i
    if n != 0:
        Ret.append([n])
    return Ret
    
N = int( input())
A = list( map( int, input().split()))
ans = A[1]
for i in range(1,N):
    ans = gcd(ans, A[i])
Fact = factors(A[0])
C = [1]
for F in Fact:
    D = deepcopy(C)
    for f in F:
        for d in D:
            C.append(d*f)
for g in C:
    check = 0
    for i in range(1,N):
        if A[i]%g != 0:
            check += 1
    if check <= 1:
        if ans < g:
            ans = g
print(ans)

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


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

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

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

M = [gcd(L[i],R[i]) for i in range(n)]

print(max(M))

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

N=int(input().strip())
A=[int(x) for x in input().strip().split()]
def gcd(a,b):
    if b==0:
        return a
    
    return gcd(b,a%b)

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

R.reverse()
for i in range(len(L)-1):
    ANS.append(gcd(L[i],R[i+1]))

print(max(ANS))

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

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



    


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

R.reverse()
for i in range(len(L)-1):
    ANS.append(gcd(L[i],R[i+1]))

print(max(ANS))

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

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


def listup_gcm(xlist):
    list_gcm = []
    prev = None
    for a in xlist:
        if prev is None:
            list_gcm.append(a)
            prev = a
            continue
        gcm = calc_gcm(prev, a)
        list_gcm.append(gcm)
        prev = gcm
    return list_gcm


left_gcm = listup_gcm(alist)
alist.reverse()
right_gcm = listup_gcm(alist)
right_gcm.reverse()


maxgcm = 0
for i in range(n):
    if i == 0:
        maxgcm = max(maxgcm, right_gcm[1])
    elif i == n - 1:
        maxgcm = max(maxgcm, left_gcm[n-2])
    else:
        maxgcm = max(maxgcm, calc_gcm(left_gcm[i-1], right_gcm[i+1]))

print(maxgcm)

Solution (Python):


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

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








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



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

Solution (Python):
from fractions import*
def update(k,x):
    k+=N0-1
    data[k]=x
    while k+1:
        k=~-k//2
        data[k]=gcd(data[2*k+1],data[2*k+2])
def query(l,r):
    L,R=l+N0,r+N0
    s=0
    while L<R:
        if R&1:
            R-=1
            s=gcd(s,data[R-1])
        if L&1:
            s=gcd(s,data[L-1])
            L+=1
        L>>=1
        R>>=1
    return s
n,*a=map(int,open(0).read().split())
N0=2**(n-1).bit_length()
data=[0]*2*N0
for i in range(n):
    update(i,a[i])
print(max(gcd(query(0,i),query(i+1,n))for i in range(n)))

Solution (Python):
def gcd(x, y):
    if x >= y:
        while y != 0:
            x, y = y, x % y
        return x
    else:
        while x != 0:
            y, x = x, y % x
        return y

N = int(input())
A = list(map(int, input().split()))
L = [0, 0]
for ai in A:
    L.append(gcd(L[-1], ai))
 
R = [0, 0]
for ai in A[::-1]:
    R.append(gcd(R[-1], ai))
 
print(max(gcd(l, r) for l, r in zip(L, reversed(R))))

Solution (Python):

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

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

ans = 0
for i in range(N):
    if i == 0:
        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):


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

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








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

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

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

max_gcd = 1

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

def gcd(a, b):
        if a == 0:
                return b
        elif b == 0:
                return a
        else:
                return fractions.gcd(a,b)

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

print(max(M))

Solution (Python):
from fractions import gcd

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

left = [0]*N

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

right = [0]*N

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

ans_list = [gcd(l,r) for l, r in zip(left,right)]

ans = max(ans_list)

print(ans)

Solution (Python):
n = int(raw_input())
a = raw_input().split()

cnt = 0
for i in a:
    a[cnt] = int(i)
    cnt += 1

chk = {}
chk2 = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1
    return divisors

for i in range(2):
    make_divisors(a[i])
cnt = 1
for i in range(2,len(a)):
    k = a[i]
    if k not in chk2:
        chk2[k] = 1
    else:
        if chk2[k] == 1:
            chk2[k] += 1
        elif chk2[k] > 1:
            continue
    del_list = []
    for j in chk:
        if k % j == 0:
            chk[j] += 1
        if chk[j] < cnt+1:
            del_list.append(j)
    for j in del_list:
        del chk[j]
    cnt += 1

answer = 0
for i in chk:
    if int(chk[i]) >= chk[1] - 1 and i > answer:
        answer = i
print answer

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

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

Li = [0]*(N+2)
Ri = [0]*(N+2)
A.append(0)
A.insert(0, 0)

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

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




M = []
for i in range(1, N+1):
	M.append(gcd(Li[i],Ri[i+1]))

print(max(M))

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

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

ans = 0
for i in range(N):
    if 0 < i < N-1:
        ans = max(ans, gcd(gcd_left[i-1], gcd_right[i+1]))
    elif i == 0:
        ans = max(ans, gcd_right[i+1])
    else:
        ans = max(ans, gcd_left[i-1])
print(ans)

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

P_MAX = 32000
P = []
for i in range(2,P_MAX):
    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)

def factorize(n):
    primes = []
    powers = []
    for p in P:
        if p * p > n:
            break
        elif n % p == 0:
            k = 0
            while n % p == 0:
                n //= p
                k += 1
            primes.append(p)
            powers.append(k)
    if n > 1:
        primes.append(n)
        powers.append(1)
    return primes, powers

N = int(input())
A = inpl()
infty = 100
lowest_powers = defaultdict(lambda: [infty,infty])
bottle_necks = defaultdict(lambda: -1)
all_factors = set()
for n in range(N):
    primes, powers = factorize(A[n])
    prev_primes = list(bottle_necks.keys())
    done = defaultdict(int)
    for i in range(len(primes)):
        done[primes[i]] = 1
        if not primes[i] in all_factors:
            all_factors.add(primes[i])
            if n == 1:
                lowest_powers[primes[i]] = [0,powers[i]]
                bottle_necks[primes[i]] = 0
            elif n == 0:
                lowest_powers[primes[i]] = [powers[i],infty]
                bottle_necks[primes[i]] = 0
        lowest = lowest_powers[primes[i]]
        if powers[i] == lowest[0]:
            lowest[1] = lowest[0]
            bottle_necks[primes[i]] = -1
        elif powers[i] < lowest[0]:
            lowest[1] = lowest[0]
            lowest[0] = powers[i]
            bottle_necks[primes[i]] = n
        elif powers[i] < lowest[1]:
            lowest[1] = powers[i]
    for p in all_factors:
        if done[p] == 0:
            lowest = lowest_powers[p]
            if lowest[0] == 0:
                lowest[1] = 0
                bottle_necks[p] = -1
            else:
                lowest[1] = lowest[0]
                lowest[0] = 0
                bottle_necks[p] = n

bnl = defaultdict(lambda: [])
for p,n in bottle_necks.items():
    if n >= 0:
        lowest = lowest_powers[p]
        bnl[n].append([p,lowest[1]-lowest[0]])
best_n = -1
largest_f = 1
for n in range(N):
    f = 1
    for pp in bnl[n]:
        f *= pp[0]**pp[1]
    if f > largest_f:
        largest_f = f
        best_n = n

ans = 1
bn = bnl[best_n]
pn = [ s[0] for s in bn]
for p in all_factors:
    if p in pn:
        ans *= p**lowest_powers[p][1]
    else:
        ans *= p**lowest_powers[p][0]
print(ans)

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

from fractions import gcd

L = [0, 0]
for ai in A:
    L.append(gcd(L[-1], ai))

R = [0, 0]
for ai in A[::-1]:
    R.append(gcd(R[-1], ai))

print(max(gcd(l, r) for l, r in zip(L, reversed(R))))

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

for i in range(end + 1, 0, -1):
    x = 0
    for j in a:
        if j % i != 0:
            x += 1
        if x >= 2:
            break
    if x <= 1:
        print(i)
        break

Solution (Python):
import functools
import fractions


N = int(input())
a = list(map(int, input().split()))
A = list(set(a))
ans = [functools.reduce(fractions.gcd, A)]
if len(A) == 1:
    print(A[0])
    exit()
else:
    for i in A:
        b = sorted(A)
        b.remove(i)
        if a.count(i) == 1:
            ans.append(functools.reduce(fractions.gcd, b))
print(max(ans))

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

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

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


gcdL = []
gcdR = []

N = int(input())
A = [int(x) for x in input().split()]
gcdL.append(0)
length_A = len(A)
for i in range(1,length_A):
    gcdL.append(gcd(A[i-1],gcdL[i-1]))


gcdR.append(0)
for i in range(1,length_A):
    gcdR.append(gcd(A[length_A-i],gcdR[i-1]))



list_gcd = []
for i in range(length_A):
        list_gcd.append(gcd(gcdL[i],gcdR[length_A-i-1]))

print(max(list_gcd))