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 sys
sys.setrecursionlimit(pow(10, 6))

n = int(input())
alist = list(sorted(map(int, input().split())))

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

l1 = [gcd(0, alist[0])]
l2 = [gcd(alist[-1], 0)]
if n == 2:
  print(max(alist))
else:
  for a in alist[1:]:
    l1.append(gcd(a, l1[-1]))
  for a in alist[::-1][1:]:
    if l2[-1] > a:
      l2.append(gcd(l2[-1], a))
    else:
      l2.append(gcd(a, l2[-1]))
  l2 = l2[::-1]
  maxl = l2[1]
  for i in range(n - 2):
    gcda = gcd(max(l1[i], l2[i+2]), min(l1[i], l2[i+2]))
    if gcda > maxl:
      maxl = gcda
  print(max(maxl, l1[-2]))

Solution (Python):
N = int(input())
rawA = list(map(int, input().split()))
A = [rawA[0],] + rawA + [rawA[N-1],]


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



cfl = []
cfl.append(A[1]) 
for i in range(2, N): 


  cfl.append(gcd(cfl[i-2], A[i]))
  


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


  cfr.append(gcd(A[i], cfr[abs(i-N+1)]))



ans = max(cfl[-1], cfr[-1]) 
for nowcfl, nowcfr in zip(cfl[0:-1],cfr[::-1][1:]):


  ans = max(ans, gcd(nowcfl, nowcfr))

print(ans)

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

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

l = [0]*N
r = [0]*N

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

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

l.insert(0,0)
l.append(0)
r.insert(0,0)
r.append(0)

o = 1

for x in range(N):
    o = max(o,gcd(l[x],r[x+2]))

print(o)

Solution (Python):
import sys
from fractions import gcd

N = int(input())

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


if len(A) < 3:
    print(max(A))
    sys.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(right_side[-1* i], A[ -1 *(i + 1)])


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
def main():
    n = int(input())
    a = list(map(int, input().split()))
    left = [0]
    right = [0]
    for l in a[:-1]:
        g = gcd(left[-1], l)
        left.append(g)
    for r in a[n:0:-1]:
        g = gcd(right[-1], r)
        right.append(g)
    right.reverse()
    m = 0
    for i in range(n):
        m = max(m, gcd(left[i], right[i]))
    print(m)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
X = [0] * N
Y = [0] * N
sum_gcd = []
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

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

sum_gcd.append(max(Y[1], X[N-2]))
for i in range(N-2):    
    sum_gcd.append(gcd(X[i], Y[i+2]))

print(max(sum_gcd))

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


del_list = [0]


g = a[0]
for i,x in enumerate(a):
    p = gcd(g,x)
    if p<g:
        g = p
        del_list.append(i)

res = -1
for ind in del_list:
    kouho = -1
    for i,x in enumerate(a):
        if i==ind:
            continue
        if kouho < 0:
            kouho = x
            continue
        kouho = gcd(kouho,x)
    res = max(res,kouho)
print(res)

Solution (Python):
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
 
 
N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]
R = [0 for i in range(N)]
T = [0 for i in range(N)]
 
for i in range(N):
    if i == 0:
        L[i] = 0
    else:
        L[i] = gcd(L[i-1], A[i-1])
for i in range(N-1, -1, -1):
    if i == N-1:
        R[i] = 0
    else:
        R[i] = gcd(R[i+1], A[i+1])
for i in range(N):
    T[i] = gcd(L[i], R[i])
 
print(max(T))

Solution (Python):
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
curr1, curr2 = [0], [0]
for i in range(N):
  curr1 += [gcd(A[i],curr1[-1])]
  curr2 += [gcd(A[N-1-i],curr2[-1])]

if N ==2:
  print(max(A[0],A[1]))
else:
  ans = []
  for i in range(N):
    ans += [gcd(curr1[i],curr2[N-1-i])]
    
  print(max(ans))

Solution (Python):
import fractions

N = int(input())
A = list(map(int, input().split()))
R = []
L = []
right = A[-1]
left = A[0]
R.append(right)
L.append(left)

for x in range(1,N):
    right = fractions.gcd(right, A[-x-1])
    left = fractions.gcd(left, A[x])
    R.append(right)
    L.append(left)

answer = max(R[N-2], L[N-2])

for x in range(N-2):
    test = fractions.gcd(R[x], L[-x-3])
    answer = max(answer, test)

print(answer)

Solution (Python):
from fractions import gcd

def main():
    n = int(input())
    a = list(map(int, input().split()))
    left = [0]
    right = [0]
    for i in range(1, n):
        g = gcd(left[-1], a[i - 1])
        left.append(g)
    for i in range(n - 2, -1, -1):
        g = gcd(right[-1], a[i + 1])
        right.append(g)
    right.reverse()
    m = 0
    for i in range(0, n):
        m = max(m, gcd(left[i], right[i]))
    print(m)
    
if __name__ == '__main__':
    main()

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

w_min = A[1]

for i in range(w_min,0,-1):
    count = 0
    for j in A:
        if j%i != 0:
            count = count + 1
            if count > 1:
                break
    if count < 2:
        print(i)
        break

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 = [0]*n
c = [0]*n
for i in range(n):
    if i == 0:
        b[0] = a[0]
        c[-1] = a[-1]
        continue
    b[i]=gcd(b[i-1],a[i])
    c[n-1-i]=gcd(c[n-i],a[n-1-i])

res = max(b[n-2],c[1])
for i in range(1,n-1):
    res = max(res,gcd(b[i-1],c[i+1]))
print(res)

Solution (Python):

import fractions

def main():
    N=int(input())
    num=list(map(int,input().split()))
    gcd=0
    gcd_list_front=[]
    gcd_list_back=[]
    gcd_list_front.append(num[0])
    gcd_list_back.append(num[-1])

    for  i in range(1,N-1):
        gcd_list_front.append(fractions.gcd(gcd_list_front[i-1],num[i]))
        gcd_list_back.append(fractions.gcd(gcd_list_back[i-1],num[-i-1]))
    gcd_list_back.reverse()

    gcd=max(gcd_list_back[0],gcd_list_front[-1])
    for i in range(0,N-2):
       temp_gcd=fractions.gcd(gcd_list_front[i],gcd_list_back[i+1])
       if temp_gcd>gcd:
           gcd=temp_gcd
    print(gcd)


if  __name__=="__main__":
    main()

Solution (Python):
import numpy as np
import fractions

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

maxA = np.max(A)


if(maxA == np.min(A)):
    print(maxA)
elif(N==2):
    print(maxA)
else:
    R=np.zeros((N))
    L=np.zeros((N))
    M=np.zeros((N))

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

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

    print("{}".format(int(np.max(M))))

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)

    return divisors


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

divisors = sorted(set(make_divisors(A[0]) + make_divisors(A[1])), reverse=True)
ans = 1

for divisor in divisors:
    indivisible_count = 0
    for i in range(N):
        if A[i] % divisor != 0:
            indivisible_count += 1

    if indivisible_count <= 1:
        ans = divisor
        break

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)

    return divisors


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

ans = 1
divisors = make_divisors(A[0]) + make_divisors(A[1])
divisors = sorted(set(divisors), reverse=True)

for divisor in divisors:
    indivisible_count = 0
    for i in range(N):
        if A[i] % divisor != 0:
            indivisible_count += 1

    if indivisible_count <= 1:
        ans = divisor
        break

print(ans)

Solution (Python):

try:
    from math import gcd
except:
    from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
AL = [0] * N  
AR = [0] * N  
for i in range(1, N):
    AR[i] = gcd(AR[i-1], A[i-1])
for i in range(N-1)[::-1]:
    AL[i] = gcd(AL[i+1], A[i+1])
A_list = [gcd(AL[i], AR[i]) for i in range(N)]
print(max(A_list))

Solution (Python):

"""
Created on Sun Apr 28 15:46:33 2019

@author: Yamazaki Kenichi
"""

N = int(input())
A = list(map(int,input().split()))
 
def eg(x,y):
    if y == 0:
        return x
    x, y = y, x%y
    return eg(x,y)

L, R = [0 for i in range(N)],[0 for i in range(N)]
L[0], R[N-1] = A[0], A[N-1]
for i in range(1,N):
    L[i] = eg(L[i-1], A[i])
    R[N-1-i] = eg(R[N-i], A[N-1-i])
ans = [0 for i in range(N)]
for i in range(N):
    if i == 0:
        ans[i] = R[i+1]
    elif i == N-1:
        ans[i] = L[i-1]
    else:
        ans[i] = eg(L[i-1],R[i+1])

print(max(ans))

Solution (Python):

import sys
import math


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

def solve(N: int, A: "List[int]"):
    l = [0] * (N+2)
    r = [0] * (N+2)
    l[1] = A[0]
    r[N] = A[N-1]
    for i in range(2, N+1):
        l[i] = gcd(l[i-1], A[i-1])
    for i in reversed(range(1, N)):
        r[i] = gcd(r[i+1], A[i-1])
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(l[i], r[i+2]))
    print(ans)

    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 sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    非再帰
    segment tree
    '''

    def __init__(self, _n, func, inf=float('inf')):
        '''
        _n->配列の長さ
        func:func(a,b)->val,　func=minだとRMQになる
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(_n-1).bit_length()
        self.inf = inf
        self.data = [inf]*(2*self.n)
        self.func = func

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        '''
        setの後に一斉更新
        '''
        for k in reversed(range(self.n-1)):
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のminを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.inf
        while L < R:
            if R & 1:
                R -= 1
                ret = self.func(ret, self.data[R-1])
            if L & 1:
                ret = self.func(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


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


N = int(input())
L = list(map(int, input().split()))
T = SegmentTree(N, gcd, 0)
for i, l in enumerate(L):
    T.set(i, l)
T.build()

ans = 0
for i in range(N):
    l = i
    r = i+1
    ans = max(ans, gcd(T.query(0, l), T.query(r, N)))
print(ans)

Solution (Python):
def gcd(a,b):
  a,b=max(a,b),min(a,b)
  if b==0:
    return a
  else:
    return gcd(b,a%b)
  
n=int(input())
arr=list(map(int,input().split()))
lgcd=[arr[0]]
rgcd=[arr[-1]]

if n==2:
  print(max(arr[0],arr[1]))
elif n==3:
  print(max(gcd(arr[0],arr[1]),gcd(arr[1],arr[2]),gcd(arr[2],arr[0])))
else:
  tmp=arr[0]
  for i in range(1,n-1):
    lgcd.append(gcd(tmp,arr[i]))
    tmp=gcd(tmp,arr[i])
  tmp=arr[-1]
  for i in range(n-2,0,-1):
    rgcd.append(gcd(tmp,arr[i]))
    tmp=gcd(tmp,arr[i])
  rgcd=rgcd[::-1]
  ans=0
  for i in range(n):
    if i==0:
      tmp=rgcd[0]
    elif i==n-1:
      tmp=lgcd[-1]
    else:
      tmp=gcd(rgcd[i],lgcd[i-1])
    if tmp>ans:
      ans=tmp
  print(ans)

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


n = int(input())
a = list(map(int, input().split()))
a = [0]+a[:]+[0]

l = [0]*len(a)
r = [0]*len(a)

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

ans = [0]*n

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

print(max(ans))

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

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

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(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])

k = 0

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

    if g > k:
        k = g

print(k)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    RMQ　の実装
    '''

    def __init__(self, n, inf=float('inf')):
        '''
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(n-1).bit_length()
        self.inf = inf
        self.data = [inf]*(2*self.n)

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        for k in reversed(range(self.n-1)):
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のminを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.inf
        while L < R:
            if R & 1:
                R -= 1
                ret = gcd(ret, self.data[R-1])
            if L & 1:
                ret = gcd(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


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


N = int(input())
L = list(map(int, input().split()))
T = SegmentTree(N, 0)



for i, l in enumerate(L):
    T.update(i, l)

ans = 0
for i in range(N):
    l = i
    r = i+1
    ans = max(ans, gcd(T.query(0, l), T.query(r, N)))
print(ans)

Solution (Python):


import sys
import copy

sys.setrecursionlimit(1000000)






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


N = int(input())

A = [int(x) for x in input().split()]
B = list(reversed(A))

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

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

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

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

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    RMQ　の実装
    '''

    def __init__(self, n, inf=float('inf')):
        '''
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(n-1).bit_length()
        self.inf = inf
        self.data = [inf]*(2*self.n)

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        for k in reversed(range(self.n-1)):
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のminを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.inf
        while L < R:
            if R & 1:
                R -= 1
                ret = gcd(ret, self.data[R-1])
            if L & 1:
                ret = gcd(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


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


N = int(input())
L = list(map(int, input().split()))
T = SegmentTree(N, 0)
for i, l in enumerate(L):
    T.set(i, l)
T.build()

ans = 0
for i in range(N):
    l = i
    r = i+1
    ans = max(ans, gcd(T.query(0, l), T.query(r, N)))
print(ans)

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
Asort = sorted(A)[:: -1]
L = [0] * N  
R = [0] * N  
L[0] = A[0]
R[0] = A[N - 1]
for i in range(1, N):
	L[i] = gcd(L[i - 1], A[i])
	R[i] = gcd(R[i - 1], A[N - 1 - i])
ans = max(L[N - 2], R[N - 2])
for j in range(1, N - 1):
	ans = max(ans, gcd(L[j - 1], R[N - j - 2]))
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    RMQ　の実装
    '''

    def __init__(self, n, inf=float('inf')):
        '''
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(n-1).bit_length()
        self.inf = inf
        self.data = [inf]*(2*self.n)

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        for k in reversed(range(self.n-1)):
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のminを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.inf
        while L < R:
            if R & 1:
                R -= 1
                ret = gcd(ret, self.data[R-1])
            if L & 1:
                ret = gcd(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


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


N = int(input())
L = list(map(int, input().split()))
T = SegmentTree(N, 0)
for i, l in enumerate(L):
    T.set(i, l)
T.build()

ans = 0
for i in range(N):
    l = i
    r = i+1
    ans = max(ans, gcd(T.query(0, l), T.query(r, N)))
print(ans)

Solution (Python):
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
 
 
N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]
R = [0 for i in range(N)]
T = [0 for i in range(N)]
 
for i in range(N):
    if i == 0:
        L[i] = 0
    else:
        L[i] = gcd(L[i-1], A[i-1])
for i in range(N-1, -1, -1):
    if i == N-1:
        R[i] = 0
    else:
        R[i] = gcd(R[i+1], A[i+1])
for i in range(N):
    T[i] = gcd(L[i], R[i])
 
print(max(T))

Solution (Python):
from fractions import gcd


N = int(input())
lstA = list(map(int, input().split()))
lstL = [0] * N
lstR = [0] * N


for i in range(1, N):
    lstL[i] = gcd(lstL[i-1], lstA[i-1])
for i in range(N-1)[::-1]:
    lstR[i] = gcd(lstR[i+1], lstA[i+1])
lstGcd = [gcd(lstL[i], lstR[i]) for i in range(N)]
ans = max(lstGcd)


print(ans)

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


N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]
R = [0 for i in range(N)]
T = [0 for i in range(N)]

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

print(max(T))

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


N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]
R = [0 for i in range(N)]
T = [0 for i in range(N)]

for i in range(N):
    if i == 0:
        L[i] = 0
    else:
        L[i] = gcd(L[i-1], A[i-1])
for i in reversed(range(0, N)):
    if i == N-1:
        R[i] = 0
    else:
        R[i] = gcd(R[i+1], A[i+1])
for i in range(N):
    T[i] = gcd(L[i], R[i])

print(max(T))

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

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

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




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




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

max_gcd = 0
for i in range(N):
    if gcd(L[i], R[i+1]) > max_gcd:
        max_gcd = gcd(L[i], R[i+1])

print '{}'.format(max_gcd)

Solution (Python):


import sys
import math
 
 
def gcd_ab(a,b):
  a,b = sorted([a,b])
  if a == 0:
    return b
  return gcd_ab(b%a, a)
 
if __name__ == "__main__": 
  N = int(input())
  An = list(map(int, sys.stdin.readline().rstrip().split()))
  cum = [[0,0] for i in range(N+2)]
 
  for i in range(N):
    cum[i+1][0] = gcd_ab(cum[i][0], An[i])
    cum[N-i][1] = gcd_ab(cum[N-i+1][1], An[N-i-1])
 
  ans = [0 for i in range(N)]
  for i in range(1,N+1):
    ans[i-1] = gcd_ab(cum[i-1][0], cum[i+1][1])
  print(max(ans))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
def gcd(a,b):
  if a<=b:
    a,b=b,a
  if a%b==0:
    return b
  else:
    return gcd(b,a%b)
L,R=[],[]
ld,rd=A[0],A[n-1]
for i in range(n):
  ld=gcd(ld,A[i])
  rd=gcd(rd,A[n-1-i])
  L.append(ld)
  R.append(rd)
ans=max(L[-2],R[-2])
for i in range(1,n-1):
  ans=max(ans,gcd(L[i-1],R[n-i-2]))
print(ans if n!=2 else max(A))

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(reverse=True)
    return divisors


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

ans = 1
for i in range(2):
    div_list = make_divisors(A[i])

    for div_i in div_list:
        wrong_count = 0

        for A_i in A:
            if A_i % div_i != 0:
                wrong_count += 1

        if wrong_count <= 1 and div_i > ans:
            ans = div_i

print(ans)

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





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

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




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


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

print(result)

Solution (Python):

import fractions


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

    gcd_max = 0
    gcd_list_front = []
    gcd_list_back = []

    gcd_list_front.append(num[0])
    gcd_list_back.append(num[-1])

    for i in range(1, N-1):
        gcd_list_front.append(fractions.gcd(gcd_list_front[i-1], num[i]))
        gcd_list_back.append(fractions.gcd(gcd_list_back[i-1], num[-i-1]))

    gcd_list_back.reverse()
    gcd_max = max(gcd_list_back.pop(0), gcd_list_front.pop(-1))

    for i in range(len(gcd_list_front)):
        temp_gcd = fractions.gcd(gcd_list_front[i], gcd_list_back[i])
        if temp_gcd > gcd_max:
            gcd_max = temp_gcd

    print(gcd_max)
    return


if __name__ == "__main__":
    main()

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 = []
for i in range(len(a)):
    if i==0:
        L.append(None)
    elif i ==1:
        L.append(a[i-1])
    else:
        L.append(gcd(L[-1], a[i-1]))
        
R = []
for i in range(len(a)):
    if i==0:
        R.append(None)
    elif i ==1:
        R.append(a[-i])
    else:
        R.append(gcd(R[-1], a[-i]))
R = R[::-1]


max_val = 0
for r, l in zip(L,R):
    if r==None:
        val = l
    elif l==None:
        val = r
    else:
        val = gcd(r, l)
        
    if val > max_val:
        max_val = val
print(max_val)

Solution (Python):

import fractions


def main():
    N = int(input())
    
    num = list(map(int, input().split()))
    
    
    gcd = 0
    gcd_list_front = []
    gcd_list_back = []
    gcd_list_front.append(num[0])
    gcd_list_back.append(num[-1])

    for i in range(1, N-1):
        gcd_list_front.append(fractions.gcd(gcd_list_front[i-1], num[i]))
        gcd_list_back.append(fractions.gcd(gcd_list_back[i-1], num[-i-1]))

    gcd_list_back.reverse()
    
    

    gcd = max(gcd_list_back[0], gcd_list_front[-1])

    for i in range(0, N-2):
        
        temp_gcd = fractions.gcd(gcd_list_front[i], gcd_list_back[i+1])
        if temp_gcd > gcd:
            gcd = temp_gcd

    print(gcd)
    
    


if __name__ == "__main__":
    main()

Solution (Python):
from fractions import *
n = int(input())
a = [0] + list(map(int, input().split())) + [0]
b = [0] * (n+2)
c = [0] * (n+2)

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

ans = 1
for i in range(1, n+1):
    if ans < gcd(b[i-1], c[i+1]):
        ans = gcd(b[i-1], c[i+1])
print(ans)

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)

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

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

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

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

Solution (Python):
from fractions import gcd
n = int(input())
A = list(map(int,input().split()))
X = [0 for i in range(n)]
Y = [0 for i in range(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 = [gcd(X[i],Y[i]) for i in range(n)]
print(max(Z))

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()))
    tmp = A[0]
    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()

Solution (Python):


 

import fractions, functools, itertools
 

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

x = A[0]
y = A[1]
def divisors(n):
     for i in range(1, n+1):
        if i*i > n:
            break
        if n%i == 0:
            yield i
            yield n//i
ds = set(divisors(x)) | set(divisors(y))
def condition(d):
    not_div = (a%d != 0 for a in A)
    not_div = (1 if b else 0 for b in not_div)
    return sum(not_div) <= 1
ans = max(filter(condition, ds))

print(ans)

Solution (Python):
def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
N=int(input())
A=list(map(int,input().split()))

L=[0,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))
    

max=0
for l,r in zip(L,reversed(R)):
    if(gcd(l,r)>max):
        max=gcd(l,r)

print(max)

Solution (Python):
from fractions import gcd
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[i], A[i]))
    gcd_r.append(gcd(gcd_r[i], A[-(i+1)]))

gcds = []
for i in range(N):
    gcds.append(gcd(gcd_l[i], gcd_r[-(i+2)]))
ans = max(gcds)
print(ans)

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

from itertools import accumulate

gcds = []
left_all_gcd = list(accumulate(A, gcd))
right_all_gcd = list(accumulate(A[::-1], gcd))
for i in range(len(A)):
    if i == 0:
        gcds.append(right_all_gcd[-2])
    if i == len(A)-1:
        gcds.append(left_all_gcd[-2])
    else:
        hoge = [left_all_gcd[i-1]] + [right_all_gcd[-i-1]]
        gcds.append(gcd(left_all_gcd[i-1], right_all_gcd[-i-2]))

print(max(gcds))

Solution (Python):


import sys
import math


def gcd_ab(a,b):
  a,b = sorted([a,b])
  if a == 0:
    return b
  return gcd_ab(b%a, a)

if __name__ == "__main__": 
  N = int(input())
  An = list(map(int, sys.stdin.readline().rstrip().split()))
  cum = [[0,0] for i in range(N+2)]

  for i in range(N):
    cum[i+1][0] = gcd_ab(cum[i][0], An[i])
    cum[N-i][1] = gcd_ab(cum[N-i+1][1], An[N-i-1])

  ans = [0 for i in range(N)]
  for i in range(1,N+1):
    ans[i-1] = gcd_ab(cum[i-1][0], cum[i+1][1])
  print(max(ans))

Solution (Python):
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())
A=[int(i) for i in input().split()]
X=[0 for i in range(N)]
Y=[0 for i in range(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=[gcd(X[i],Y[i]) for i in range(N)]
print(max(Z))