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

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        
        while self.n < n:
            self.n *= 2

        self.N0 = 2**(n-1).bit_length()

        
        
        self.dat = [0] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

        while(i > 0):
            i = (i-1) // 2
            self.dat[i] = fractions.gcd(self.dat[i * 2 + 1], self.dat[i * 2 + 2])

    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0
        ret = 0
        
        while L < R:
            if R & 1:
                R -= 1
                ret = fractions.gcd(ret, self.dat[R-1])
                
            if L & 1:
                ret = fractions.gcd(ret, self.dat[L-1])
                L += 1
                
            L >>= 1; R >>= 1
        return ret


if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(N):
        if i == 0:
            leftv = 0
        else:
            leftv = st.query(0, i)
        
        if i == N-1:
            rightv = 0
        else:
            rightv = st.query(i+1, N)

        
        
        ans = max(ans, fractions.gcd(leftv, rightv)) 

    
    

    print(ans)

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

n = int(input())
a = list(map(int, 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

ans = 1
for i in make_divisors(a[0]):
    cnt = 0
    for j in a[1:]:
        if j%i:
            cnt += 1
            if cnt >1:
                break
    if cnt < 2:
        ans = max(ans, i)
ans = max(ans, reduce(gcd, a[1:]))
print(ans)

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

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

data_ans=[]
data1=[A[0]]
data2=[A[N-1]]
l=A[0]
m=A[N-1]
for i in range(1,N-2):
  l=gcd(l,A[i])
  m=gcd(m,A[N-i-1])
  data1.append(l)
  data2.append(m)
l=gcd(l,A[N-2])
m=gcd(m,A[1])
data_ans.append(l)
data_ans.append(m)
for i in range(0,N-2):
  data_ans.append(gcd(data1[i],data2[N-3-i]))
print(max(data_ans))

Solution (Python):
from itertools import*;from fractions import*;u=accumulate;g=gcd;n,*a=map(int,open(0).read().split());print(max(g(s,t)for s,t in zip(u([0]+a,g),list(u([0]+a[::-1],g))[::-1][1:])))

Solution (Python):
from itertools import*;from fractions import*;u=accumulate;g=gcd;n,*a=map(int,open(0).read().split());print(max(g(s,t)for s,t in zip(list(u([0]+a,g)),list(u([0]+a[::-1],g))[::-1][1:])))

Solution (Python):
import fractions
import itertools

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


def gcds(list_):
    return list(itertools.accumulate(list_, fractions.gcd))


def reversed_gcds(list_):
    return list(itertools.accumulate(reversed(list_), fractions.gcd))


as_accum_gcd, as_rev_accum_gcd = gcds(as_), reversed_gcds(as_)

dropped_gcds = []
for i in range(len(as_)):
    if i == 0:
        dropped_gcd = as_rev_accum_gcd[-2]
    elif i == len(as_) - 1:
        dropped_gcd = as_accum_gcd[-2]
    else:
        dropped_gcd = fractions.gcd(as_accum_gcd[i - 1], as_rev_accum_gcd[-2 - i])
    dropped_gcds.append(dropped_gcd)

print(max(dropped_gcds))

Solution (Python):
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
  
n = int(input())
A = list(int(i) for i in (input().split()))

front = []
end = []
for i in range(n):
  if(i == 0):
    front.append(gcd(*[A[0]]))
    end.append(gcd(*[A[n-1]]))
  else:
    front.append(gcd(*[front[i - 1],A[i]]))
    end.append(gcd(*[end[i - 1],A[n-1-i]]))

ans = 0
for i in range(n):
  if(i == 0):
    tmp = end[n-2]
  elif(i == n-1):
    tmp = front[n-2]
  else:
    tmp = gcd(*[front[i-1],end[n-2-i]])
  if(ans < tmp):
    ans = tmp
print(ans)

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

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

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

print(counter)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
answer = sorted(a)[1]
if n == 2:
    pass
else:
    for b in reversed(range(answer)):
        cnt = 2
        flag = True
        for i in range(n):
            if a[i] % answer != 0:
                cnt -= 1
            if cnt == 0:
                flag = False
                break
        if flag:
            break
        answer -= 1
print(answer)

Solution (Python):

from fractions import gcd

N = int(input())
As = [int(elm) for elm in input().split(" ")] 

if N >= 3:
    L = []
    R = []

    gl = As[0]
    gr = As[N-1]
    L.append(gl)
    R.append(gr)

    for i in range(1, N-1):
        gl = gcd(gl, As[i]) 
        L.append(gl)

    for i in range(1, N-1):
        gr = gcd(gr, As[N-i-1])
        R.append(gr)

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

    out = max_g
else:
    out = max(As)

print(out)

Solution (Python):

from fractions import gcd

N = int(input())
As = [int(elm) for elm in input().split(" ")] 


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

if N >= 3:
    gs = []
    for i in range(0, (N//2)*2 , 2):
        g = gcd(As[i], As[i+1])
        gs.append(g)
    if N % 2 == 1:
        gs.append(As[N-1])

    while len(gs) != 2:
        
        new_gs = []
        N1 = len(gs)
        
        for i in range(0, (N1//2)*2, 2):
            g = gcd(gs[i], gs[i+1])
            new_gs.append(g)
        if N1 % 2 == 1:
            new_gs.append(gs[-1])
        gs = new_gs

    g1, g2 = gs
    d1 = divisor(g1) 
    d2 = divisor(g2) 
    ds = sorted(d1 + d2, reverse=True)

    
    
    for d in ds:
        count = sum([int(A % d == 0) for A in As])
        if count >= N-1:
            out = d
            break

else:
    out = max(As)

print(out)

Solution (Python):
def gcd(n,m):
  a=max(n,m)
  b=min(n,m)
  while b!=0:
    a,b=b,a%b
  return a
n=int(input())
l=list(map(int,input().split()))
l1=[]
l1.append(l[0])
for i in range(1,n):
  l1.append(gcd(l1[-1],l[i]))
l2=[]
l2.append(l[-1])
for i in range(n-2,-1,-1):
  l2.append(gcd(l2[-1],l[i]))
l3=[]
for i in range(2,n):
  l3.append(gcd(l1[i-2],l2[n-i-1]))
l3.append(l2[n-2])
l3.append(l1[n-2])
print(max(l3))

Solution (Python):
import math
def get_div_list(num):
    if num == 1:
        return [1]
    else:
        div_list = []
        div_list.append(1)
        div_list.append(num)
        for i in range(2, math.floor(math.sqrt(num))+ 1):
            if num % i == 0:
                div_list.append(i)
                div_list.append(num // i)
        list(set(div_list))
        div_list.sort(reverse = True)
        return div_list
n = int(input())
a = [int(_) for _ in input().split()]
d = sorted(list(set(get_div_list(a[0])+get_div_list(a[1]))))[::-1]
for i in range(len(d)):
    cnt = 0
    for j in range(n):
        if a[j]%d[i]==0:cnt+=1
    if cnt >= n-1:
        print(d[i])
        break

Solution (Python):

from sys import stdin




s_in = lambda: stdin.readline()[:-1]  
d_in = lambda: int(stdin.readline())  
ds_in = lambda: list(map(int, stdin.readline().split()))  


def gcd(a, b):
    """ Greatest Common Divisor(最大公約数) """
    while b:
        a, b = b, a % b
    return a


N = d_in()
A_list = ds_in()

cum_gcd = [A_list[0]]
for a in A_list[1:]:
    pre = cum_gcd[-1]
    cum_gcd.append(gcd(a, pre))

reverse = [A_list[-1]]
for a in A_list[-2::-1]:
    pre = reverse[-1]
    reverse.append(gcd(a, pre))

ans = max(reverse[-2], cum_gcd[-2])
for i in range(1, N-1):
    
    ans = max(ans, gcd(cum_gcd[i-1], reverse[N-2-i]))

print(ans)

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


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])
for i in reversed(range(n)):
	r[i] = gcd(r[i+1],a[i])

ext = [0] * n
for i in range(n):
	ext[i] = gcd(l[i],r[i+1])

print (max(ext))

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a  
lgcds = [A[0]]
rgcds = [A[N-1]]
for n in range(N-2):
    lgcds.append(gcd(lgcds[-1],A[n+1]))
    rgcds.append(gcd(rgcds[-1],A[-2-n]))
gcds = [lgcds[-1],rgcds[-1]]
for i in range(N-2):
    gcds.append(gcd(lgcds[i],rgcds[-2-i]))
print(max(gcds))

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

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

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
g=[]
l=[0]
r=[0]
for i in range(n):
    l.append(gcd(l[i], a[i]))
    r.append(gcd(r[i], a[-i-1]))
r.reverse()
for i in range(n):
    g.append(gcd(l[i], r[i+1]))
print(max(g))

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

import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


def update(i, x, seg):
    i = len(a) + i - 1
    seg[i] = x
    while i > 0:
        i = math.floor((i-1) / 2)  
        
        seg[i] = fractions.gcd(seg[2*i + 1], seg[2*i + 2])
    
    
    return seg[0]


n, a = II(), LI()

segtree = [0] * (2*n -1)
maxi = 0


for i in range(n):
    update(i, a[i], segtree)

for i in range(n):
    maxi = max(maxi, update(i, 0, segtree))
    
    update(i, a[i], segtree)

print(maxi)

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

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

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

for i in range(N-1,-1,-1):
    if i + 1 >= N:
        B[i] = A[i]
    else:
        B[i] = gcd(B[i + 1], A[i])


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

ans = max(ans, F[N - 2])
ans = max(ans, B[1])

print(ans)

Solution (Python):
import fractions

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

Solution (Python):
import fractions

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

Solution (Python):
class SegmentTree():
    def __init__(self,param,default,func):
        if isinstance(param,list):
            n=len(param)
        else:
            n=param
        self.d=default
        self.leaf=1<<(n-1).bit_length()
        if isinstance(param,list):
            self.tree=[default]*(self.leaf-1)+param+[default]*(self.leaf-n)
            for i in range(self.leaf-2,-1,-1):
                self.tree[i]=func(self.tree[i*2+1],self.tree[i*2+2])
        else:
            self.tree=[default]*(self.leaf*2-1)
        self.f=func
    def find(self,i):
        return self.tree[i+self.leaf-1]
    def update(self,idx,a):
        idx+=self.leaf-1
        self.tree[idx]=a
        while idx:
            idx=(idx-1)>>1
            self.tree[idx]=self.merge(self.tree[idx*2+1],self.tree[idx*2+2])
    def merge(self,a,b):
        return self.f(a,b)
    def get_top(self):
        return self.tree[0]
    def query(self,a,b): 
        T=self.tree
        l,r=a+self.leaf-1,b+self.leaf-1
        res=self.d
        while l<r:
            if not l%2:
                res=self.merge(res,T[l])
                l+=1
            if not r%2:
                r-=1
                res=self.merge(res,T[r])
            l>>=1
            r>>=1
        return res

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

n=int(input())
A=list(map(int,input().split()))
seg=SegmentTree(A,0,gcd)
ans=0
for i in range(n):
    seg.update(i,0)
    ans=max(ans,seg.get_top())
    seg.update(i,A[i])
print(ans)

Solution (Python):
import fractions

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

Solution (Python):
from itertools import*
from fractions import*
n,*a=map(int,open(0).read().split())
b=list(accumulate([0]+a,gcd))
c=list(accumulate([0]+a[::-1],gcd))
print(max(gcd(s,t)for s,t in zip(b,c[::-1][1:])))

Solution (Python):
import itertools
import fractions

n = int(input())
a = list(map(int, input().split()))
cumgcd = list(itertools.accumulate(a, func=fractions.gcd))
cumgcd_rv = list(itertools.accumulate(a[::-1], func=fractions.gcd))
ans = max(cumgcd[-2],cumgcd_rv[-2])
if n == 2:
  ans = max(a)
else:
  for key,value in enumerate(cumgcd[:n-2]):
    ans = max(ans, fractions.gcd(value,cumgcd_rv[n-3-key]))
print(ans)

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

def getNum(a, b):
    while(True):
        if a == 0:
            return b
        a, b = max(a, b), min(a, b)
        a %= b

leftGBC, rightGBC= [AList[0]], [AList[-1]]
for i in range(1, N):
    leftGBC.append(getNum(leftGBC[i - 1], AList[i]))
for i in range(1, N):
    rightGBC.append(getNum(rightGBC[i - 1], AList[N - i - 1]))

rightGBC.reverse()

ans = max(leftGBC[-2], rightGBC[1])
for i in range(0, N-2):
    ans = max(getNum(leftGBC[i], rightGBC[i + 2]), ans)

print(ans)

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


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


L = [0] + list(accumulate(A, gcd))

R = [0] + list(accumulate(reversed(A), gcd))


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


print(ans)

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


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


L = list(accumulate([0] + A, gcd))

R = list(accumulate([0] + list(reversed(A)), gcd))


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


print(ans)

Solution (Python):
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m


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

left = [0]
right = [0]



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

right.reverse()

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

Solution (Python):
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m


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

left = [0]
right = [0]

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


right.reverse()  


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

print(max(gcd_list))

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


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

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

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


if __name__ == "__main__":
    main()

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

import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


n, a = II(), LI()
left = [0] * (n+1)
right = [0] * (n+1)

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

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




maxi = -INF

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

print(maxi)

Solution (Python):

from fractions import gcd

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

L = [0]
R = [0]

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

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

print(ans)

Solution (Python):


from fractions import gcd

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

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
gcd_list=[]
reverse_gcd_list = []


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)

gcd_list.append(A[0])
reverse_gcd_list.append(A[-1])

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

reverse_gcd_list.reverse()
num = [gcd_list[-2], reverse_gcd_list[1]]
for i in range(N-2):
    num.append(gcd(gcd_list[i], reverse_gcd_list[i+2]))

print(max(num))

Solution (Python):
n = int(input())
v = list(map(int, input().split()))
m = 1
gf = []
gp = []


def gcd(x, y):
    if x < y:
        x += y
        y = x - y
        x -= y

    while y > 0:
        r = x % y
        x = y
        y = r

    return x


gf.append(v[0])
for i in range(n-1):
    gf.append(gcd(gf[i], v[i+1]))

gp.append(v[n-1])
for i in range(n-1):
    gp.append(gcd(gp[i], v[n-2-i]))

m = gp[n-2]

for i in range(n-1):
    b = gcd(gf[i], gp[n-3-i])
    if m < b:
        m = b
if m < gf[n-2]:
    m = gf[n-2]

print(m)

Solution (Python):
import fractions


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

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

ans = []
for i in range(N):
    ans.append(fractions.gcd(gcd_l[i], gcd_r[i+1]))

print(max(ans))

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

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

best = 0
for i in range(0, n):
    if i == 0:
        best = max(best, r[1])
    elif i == n-1:
        best = max(best, l[n-2])
    else:
        best = max(best, fractions.gcd(l[i-1], r[i+1]))
print(best)

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


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


def check(A, d):
  count = 0
  for a in A:
    if a % d == 0:
      continue
    count += 1
    if count > 1:
      return False
  return True

for i in sorted(list(set(divisor(A[0])+divisor(A[1]))))[::-1]:
  if check(A, i):
    print(i)
    exit()

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)

left = [0] * (N+1)
right = [0] * (N+1)

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

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

Solution (Python):
def gcd(a,b):
  if b>a:
    a,b=b,a
  while b:
    a,b=b,a%b
  return a
n = int(input())
a = list(map(int,input().split()))
left = list(range(n-1))
right = list(range(n-1))
left[0]=a[0]
right[0]=a[n-1]
for i in range(1,n-1):
  left[i]=gcd(left[i-1],a[i])
  right[i]=gcd(right[i-1],a[n-i-1])
m=0
for i in range(n):
  if i == 0:
    m=right[n-2]
  elif i == n-1:
    m=max(m,left[n-2])
  else:
    m=max(m,gcd(left[i-1],right[n-i-2]))
print(m)

Solution (Python):
from fractions import gcd

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

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

results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

Solution (Python):
from fractions import gcd

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

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



results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

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

from fractions import gcd

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

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



results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

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

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

l_gcd_list = [0]*(N+1)
r_gcd_list = [0]*(N+1)
for i in range(N):
    l_gcd_list[i+1] = (gcd(l_gcd_list[i], A[i]))
    r_gcd_list[-(i+2)] = (gcd(r_gcd_list[-(i+1)], A[-(i+1)]))



results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

Solution (Python):

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

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

fl=[]
bl=[]

a=l[0]
for x in l:
  a=gcd(a,x)
  fl.append(a)
fl=[0]+fl

l.reverse()
a=l[0]
for x in l:
  a=gcd(a,x)
  bl.append(a)
bl.reverse()
bl=bl+[0]

l2=[]
for i in range(n):
  if fl[i]==0:
    l2.append(bl[i+1])
  elif bl[i+1]==0:
    l2.append(fl[i])
  else:
    l2.append(gcd(fl[i],bl[i+1]))

print(max(l2))

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

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

l_gcd_list = [0]*(N+1)
r_gcd_list = [0]*(N+1)
for i in range(N):
    l_gcd_list[i+1] = (gcd(l_gcd_list[i], A[i]))
    r_gcd_list[-(i+2)] = (gcd(r_gcd_list[-(i+1)], A[-(i+1)]))



results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

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

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

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

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

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

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



results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
from fractions import gcd
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)]))
gcd_r=gcd_r[::-1]
ans=[]
for i in range(N):
    ans.append(gcd(gcd_l[i],gcd_r[i+1]))
print(max(ans))

Solution (Python):
from fractions import gcd
N = int(input())
A = [int(s) for s in input().split()]
ans = 0
L = [0] * (N)
R = [0] * (N)
for n in range(0, N):
    if n != N-1:
        L[n+1] = gcd(A[n], L[n])
        R[-(n+2)] = gcd(A[-(n+1)], R[-(n+1)])
for n in range(0, N):
    temp = gcd(L[n], R[n])
    ans = max(temp, ans)
print(ans)

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



def check_yakusuu(n):
    list_yakusuu = []
    for i in range(1,int(n**0.5)+1):
        if n % i == 0:
            if i != n//i:
                list_yakusuu.append(i)
                list_yakusuu.append(n//i)
            else:
                list_yakusuu.append(i)
    list_yakusuu.sort(reverse = True)
    return list_yakusuu
ans = []
for i in range(2):
    list_yakusuu = check_yakusuu(A[i])
    for j in list_yakusuu:
        tmp = 0
        for k in range(N):
            if A[k] % j == 0:
                tmp += 1
        if tmp >= N - 1:
            ans.append(j)
            break
print(max(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()))

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

  L = [0 for j in range(N+1)]
  R = [0 for j in range(N+1)]
  
  for j in range(N):
    L[j+1] = gcd(L[j], A[j])
  for j in reversed(range(N)):
    R[j] = gcd(R[j+1], A[j])

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

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

a = [0] + a + [0]


lef = [0] * (N + 2)
rig = [0] * (N + 2)
def gcd(a, b):
    if a * b == 0:
        return a + b
    elif a % b == 0:
        return b
    else:
        return gcd(b, a % b)

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

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

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

print(ans)

Solution (Python):
import sys

from fractions import gcd





from itertools import accumulate











input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



N = ii()
A = lmi()
Leftgcd = list(accumulate(A, gcd))
Rightgcd = list(accumulate(reversed(A), gcd))
Leftgcd.insert(0, 0)
Rightgcd.insert(0, 0)
maxgcd = [1]
for i in range(N):
    maxgcd.append(gcd(Leftgcd[i], Rightgcd[N - 1 - i]))
print(max(maxgcd))

Solution (Python):
import sys

from fractions import gcd





from itertools import accumulate











input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



N = ii()
A = lmi()
Leftgcd = list(accumulate(A, gcd))
Rightgcd = list(accumulate(reversed(A), gcd))
Leftgcd.insert(0, 0)
Rightgcd.insert(0, 0)
maxgcd = 1
for i in range(N):
    maxgcd = max(maxgcd, gcd(Leftgcd[i], Rightgcd[N - 1 - i]))
print(maxgcd)

Solution (Python):
from fractions import gcd
n = int(input())
A = list(map(int,input().split()))
left = [0]*(n+1)
right = [0]*(n+2)
for a,aa,i in zip(A,A[::-1],range(1,n+1)):
    left[i] = gcd(a,left[i-1])
    right[n+1-i] = gcd(aa,right[n-i+2])
ans = 1
for i in range(1,n+1):
    if i ==1:
        if ans < right[i+1]:
            ans = right[i+1]
    elif i == n:
        if ans < left[i-1]:
            ans = left[i-1]
    else:
        if ans < gcd(left[i-1],right[i+1]):
            ans = gcd(left[i-1],right[i+1])

print(ans)

Solution (Python):
def gcd(x, y):
    if x * y == 0:
        return (x+y)
    x, y = max(x, y), min(x, y)
    while y > 0:
        r = x % y
        x, y = y, r
    return x

n = int(input())
a = [int(i) for i in input().split()]
l_list = [0]
r_list = [0]
for i in range(n):
    l = gcd(l_list[i], a[i])
    l_list.append(l)
    r = gcd(r_list[-1], a[-i-1])
    r_list.append(r)
r_list = list(reversed(r_list))
r_list = r_list[1:]
l_list = l_list[:n]
c_list = []
for i in range(n):
    c_list.append(gcd(l_list[i], r_list[i]))
print(max(c_list))

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

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

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



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

Solution (Python):
from fractions import gcd

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