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 fractions import gcd


class SegmentTree:
    __slots__ = ["_default_value", "_seg_func", "_size", "_tree"]

    def __init__(self, initial_values, default_value, seg_func) -> None:
        self._default_value = default_value
        self._seg_func = seg_func
        self._size = 1 << (len(initial_values) - 1).bit_length()
        self._tree = self._build(initial_values)

    def _build(self, initial_values) -> list:
        """Build a segment tree with initial values."""
        tree = [self._default_value] * (2 * self._size)

        for idx, val in enumerate(initial_values):  
            
            tree[idx + self._size - 1] = val

        for idx in range(self._size - 2, -1, -1):  
            tree[idx] = self._seg_func(tree[2 * idx + 1], tree[2 * idx + 2])

        return tree

    def update(self, index: int, value: int) -> None:
        """Update index to value."""
        index += self._size - 1
        
        self._tree[index] = value
        while index:
            index = (index - 1) // 2
            self._tree[index] = self._seg_func(self._tree[2 * index + 1], self._tree[2 * index + 2])

    def query(self, left: int, right: int):
        """Respond to query in [left, right]."""
        left += self._size
        right += self._size
        result = self._default_value
        while left < right:
            if left & 1:
                result = self._seg_func(result, self._tree[left - 1])
                left += 1
            if right & 1:
                right -= 1
                result = self._seg_func(result, self._tree[right - 1])
            left >>= 1
            right >>= 1
        return result


def main():
    N, *A = map(int, open(0).read().split())
    seg = SegmentTree(A, 0, gcd)
    ans = max(gcd(seg.query(0, i), seg.query(i + 1, N)) for i in range(N))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
class SegmentTree():
    def __init__(self,arr,func=min,ie=2**63):
        self.n = 2**(len(arr)-1).bit_length()
        self.ie = ie
        self.func = func
        self.tree = [ie for _ in range(2*self.n)]
        for i in range(len(arr)):
            self.tree[self.n+i-1] = arr[i]
        for i in range(self.n-1)[::-1]:
            self.tree[i] = func(self.tree[2*i+1],self.tree[2*i+2])

    def set(self,index,x):
        index += self.n-1
        self.tree[index] = x
        while index:
            index = (index-1)//2
            self.tree[index] = self.func(self.tree[2*index+1],self.tree[2*index+2])

    def query(self,left,right):
        if right <= left:
            return self.ie
        left += self.n-1
        right += self.n-2
        tmp_l = self.ie
        tmp_r = self.ie
        while right-left>1:
            if left & 1 == 0:
                tmp_l = self.func(tmp_l,self.tree[left])
            if right & 1 == 1:
                tmp_r = self.func(self.tree[right],tmp_r)
                right -= 1
            left = left//2
            right = (right-1)//2
        if left == right:
            tmp_l = self.func(tmp_l,self.tree[left])
        else:
            tmp_l = self.func(self.func(tmp_l,self.tree[left]),self.tree[right])
        return self.func(tmp_l,tmp_r)

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

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

st = SegmentTree(A,gcd,-1)

maxgcd = 1

for i in range(N):
    maxgcd = max(maxgcd,gcd(st.query(0,i),st.query(i+1,N+1)))

print(maxgcd)

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

import fractions

def segfunc(x, y):
    return fractions.gcd(x, y)

def init(init_val):
    
    for i in range(n):
        seg[i+num-1] = init_val[i]
    
    for i in range(num-2, -1, -1):
        seg[i] = segfunc(seg[2*i+1], seg[2*i+2])

def update(k, x):
    k += num - 1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[2*k+1], seg[2*k+2])

def query(p, q):
    if q <= p:
        return ide_ele
    p += num - 1
    q += num - 2
    res = ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res, seg[p])
        if q&1 == 1:
            res = segfunc(res, seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res, seg[p])
    else:
        res = segfunc(segfunc(res, seg[p]), seg[q])
    return res


ide_ele = 0


num = 2**(n-1).bit_length()
seg = [ide_ele]*2*num

init(A)

ans = -1
for i in range(n):
    ans =max(ans, fractions.gcd(query(0, i), query(i+1, n)))
print(ans)

Solution (Python):



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

def main():
  n = int(input())
  As = list(map(int, input().split()))
  rgcds, lgcds = [0] * n, [0] * n
  lgcds[0], rgcds[-1] = As[0], As[-1]
  
  for i in range(1, n):
    lgcds[i] = gcd(lgcds[i-1], As[i])
    rgcds[n-(i+1)] = gcd(rgcds[n-(i)], As[n-(i+1)])
  
  max = -1
  for i in range(n):
    if i==0:
      tmp = rgcds[i+1]
    if i==n-1:
      tmp = lgcds[i-1]
    if 0 < i < n-1:
      tmp = gcd(lgcds[i-1], rgcds[i+1])
    if max < tmp:
      max = tmp
  print(max)
  
if __name__=='__main__':
  main()

Solution (Python):
import functools
import fractions
import copy

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

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

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

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




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

print(ans)

Solution (Python):
import fractions
import functools

def main():
    n = int(input())
    a = [int(t)for t in input().split()]

    a_gcd = functools.reduce(fractions.gcd,a)
    a = [x//a_gcd for x in a]
    
    a_rev = a[::-1]

    lgcd = [0]
    rgcd = [0]
    for i in range(n):
        l = fractions.gcd(a[i],lgcd[i])
        r = fractions.gcd(a_rev[i],rgcd[i])
        lgcd.append(l)
        rgcd.append(r)

    rgcd = rgcd[::-1]
    g = [fractions.gcd(lgcd[i],rgcd[i+1]) for i in range(n)]
    
    print(max(g)*a_gcd)
    
    
if __name__ == "__main__":
    main()

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

l = [0] * (n + 1)
r = [0] * (n + 1)
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])

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

print(ans)

Solution (Python):
from functools import reduce


def gcd(v1, v2):
    v3 = v1 % v2
    if v3 == 0:
        return v2
    return gcd(v2, v3)


def main():
    N = int(input())
    A = [int(v) for v in input().split()]

    max_gcd = max(reduce(gcd, A[1:]), reduce(gcd, A[:N-1]))

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

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

    
    

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

    print(max_gcd)


if __name__ == "__main__":
    main()

Solution (Python):
import fractions
N = int(input())
A = [int(i) for i in input().split()]
gcd_left = [0] * N
gcd_right = [0] * N
chk = 1
for i in range(N):
	if i == 0:
		chk = A[0]
	else:
		chk = fractions.gcd(chk,A[i])
	gcd_left[i] = chk
temp = 0
for i in range(N):
	num = N-1-i
	if i == 0 :
		temp = A[num]
	else:
		temp = fractions.gcd(temp,A[num])
	gcd_right[num] = temp
ans = 0
for i in range(N):
	if i == 0:
		ans = max(ans,gcd_right[1])
	elif i == N-1:
		ans = max(ans,gcd_left[N-2])
	else:
		ans = max(ans,fractions.gcd(gcd_left[i-1],gcd_right[i+1]))
print(ans)

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

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

Solution (Python):
import fractions
n=int(input())
a=list(map(int,input().split()))
ans=1
l=[0]
r=[0]
for i in range(1,n+1):
    l.append(fractions.gcd(l[i-1],a[i-1]))
    r.append(fractions.gcd(r[i-1],a[n-i]))
r.reverse()
for i in range(n):
    buf=fractions.gcd(l[i],r[i+1])
    ans=max(ans,buf)
print(ans)

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

gcd_from_left = [a[0]]
gcd_from_right = [a[-1]]

for i in range(1, n):
    gcd_from_left.append(fractions.gcd(gcd_from_left[i - 1], a[i]))
    gcd_from_right.append(fractions.gcd(gcd_from_right[i - 1], a[n - 1 - i]))

gcd_from_right.reverse()

ans = 0
for i in range(n):
    if i == 0:
        ans = max(ans, gcd_from_right[1])
    elif i == n - 1:
        ans = max(ans, gcd_from_left[n - 2])
    else:
        ans = max(ans, fractions.gcd(gcd_from_left[i - 1], gcd_from_right[i + 1]))
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(reverse=True)
    return divisors
import numpy as np
N = int(input())
B = list(map(int,input().split()))
B.sort()
memory = 0
C = np.array(B)
min_number = B[0]
if len(B) < 3:
    print(B[-1])
    exit()
for i in make_divisors(min_number):
    if min_number % i == 0:
        if len(C[C % i != 0]) < 2:
                memory = i
                break
min_number = B[1]
for i in make_divisors(min_number):
    if min_number % i == 0:
        if len(C[C % i != 0]) < 2:
            if memory < i:    
                memory = i
            break
print(memory)

Solution (Python):

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

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

print(ans)

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

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

l = [0]*(n+1)
r = [0]*(n+1)
for i in range(n):
    l[i+1] = gcd(l[i],L1[i])
    r[n-(i+1)] = gcd(r[n-i],L1[n-(i+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
n = int(input())
a = list(map(int,input().split()))
b = a[::-1]
ai = [a[0]]
bi = [b[0]]
for i in range(1, n):
    ai.append(gcd(ai[i-1], a[i]))
    bi.append(gcd(bi[i-1], b[i]))
ans = max(ai[n-2], bi[n-2])
for i in range(1, n-1):
    ans = max(gcd(ai[i-1], bi[n-i-2]), ans)
print(ans)

Solution (Python):
from fractions import gcd
N=int(input())
a_list = list(map(int, input().split()))
gcd_l = [0] * (N+1) 
gcd_r = [0] * (N+1) 
for i, a in enumerate(a_list):
    gcd_l[i+1] = gcd(gcd_l[i], a)
for i in range(N, 0, -1):
    a = a_list[i-1]
    gcd_r[i-1] = gcd(gcd_r[i], a)
ans = 1

for i in range(N):
    tmp = gcd(gcd_l[i], gcd_r[i+1])
    ans = max(tmp, ans)
print(ans)

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



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

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

ans=1
for i in range(len(A)):
  if i==0:
    if ans<R[i+1]:
      ans=R[i+1]
  elif i==len(A)-1:
    if ans<L[i-1]:
      ans=L[i-1]
  else:
    p=gcd(L[i-1],R[i+1])
    if ans<p:
      ans=p
print(ans)

Solution (Python):
from functools import reduce


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


def gcd_list(nums):
    return reduce(gcd, nums)


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


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



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



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


print(ans)

Solution (Python):
from fractions import gcd
import sys

n = int(input())
nums = list(map(int, sys.stdin.readline().split()))

l_gcds = [nums[0]]
r_gcds = [nums[n-1]]

for i in range(n - 1):
  l_gcds.append(gcd(l_gcds[i], nums[i+1]))
for j in range(n - 1, 0, -1):
  r_gcds.append(gcd(r_gcds[n - 1 - j], nums[j-1]))

max_num = 0
for k in range(n):
  t = 0
  if k == 0:
    t = l_gcds[n-2]
  elif k == n-1:
    t = r_gcds[n-2]
  else:
    t = gcd(l_gcds[k-1], r_gcds[n-2-k])
  if t > max_num:
    max_num = t

print(max_num)

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

class SegTree:
    def __init__(self,arr,func,unit):
        self.func = func
        self.unit = unit
        n = 1
        while n < len(arr):
            n *= 2
        self.n = n
        nodes = [unit]*(2*n-1)
        nodes[n-1:n-1+len(arr)] = arr
        for i in range(n-2,-1,-1):
            nodes[i] = func(nodes[2*i+1],nodes[2*i+2])
        self.nodes = nodes

    def update(self,i,val):
        i += self.n-1
        self.nodes[i] = val
        while i >= 0:
            i = (i - 1) // 2
            self.nodes[i] = self.func(self.nodes[2*i+1], self.nodes[2*i+2])

    def query(self,l,r):
        l += self.n
        r += self.n
        s = self.unit
        while l < r:
            if r & 1:
                r -= 1
                s = self.func(s, self.nodes[r-1])
            if l & 1:
                s = self.func(s, self.nodes[l-1])
                l += 1
            l >>= 1
            r >>= 1
        return s

segt = SegTree(A, lambda x,y:gcd(x,y), 0)
ans = 0
for i in range(N):
    l = segt.query(0,i)
    r = segt.query(i+1,N)
    g = gcd(l,r)
    ans = max(ans, g)
print(ans)

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

def yakusuu(a):
    P = [a]
    for k in range(2,math.floor(math.sqrt(a))+1):
        if a % k == 0:
            P.append(k)
            P.append(a//k)
    return P

P = set(yakusuu(A[-1])+yakusuu(A[-2]))
ans = 1
for e in P:
    f = 0
    for a in A:
        if a%e != 0:
            f += 1
        if f == 2:
            break
    if f <= 1:
        ans = max(ans,e)
print(ans)

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

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



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

print(max(s))

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

n, *a = map(int, open(0).read().split())

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

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

Solution (Python):
import sys

def gcd(a, b):
    a, b = max(a, b), min(a, b)
    if b == -1: return a
    while a % b > 0:
        a, b = b, a % b
    return b         

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    lGCD = [-1 for _ in range(N + 1)]
    rGCD = [-1 for _ in range(N + 1)]

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

    maxGCD = 1
    for i in range(N):
        gcdI = gcd(lGCD[i], rGCD[i + 1])
        maxGCD = max(maxGCD, gcdI)
    print(maxGCD)
    return 0

if __name__ == "__main__":
    solve()

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

Solution (Python):
import sys
from fractions import gcd
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
class SegTree:
  def segfunc(self, x, y):
    return gcd(x, y) 
  def __init__(self, n, ide_ele, init_val):
    
    self.ide_ele = ide_ele
    
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    
    for i in range(n):
      self.seg[i + self.num - 1] = init_val[i]    
    
    for i in range(self.num - 2, -1, -1) :
      self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) 
  def update(self, k, x):
    k += self.num - 1
    self.seg[k] = x
    while k + 1:
      k = (k - 1) // 2
      self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) 
  def query(self, p, q):
    if q <= p:
      return self.ide_ele
    p += self.num - 1
    q += self.num - 2
    res = self.ide_ele
    while q - p > 1:
      if p & 1 == 0:
        res = self.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res
seg = SegTree(N, 0, a)
res = 0
for i in range(N):
  res = max(res, gcd(seg.query(0, i), seg.query(i + 1, N)))
  
print(res)

Solution (Python):


def main():
    import sys
    from fractions import gcd
    input = sys.stdin.readline

    def init(init_val):
        
        for i in range(n):
            seg[i+num-1]=init_val[i]    
        
        for i in range(num-2,-1,-1) :
            seg[i]=gcd(seg[2*i+1],seg[2*i+2]) 
        
    def update(k,x):
        k += num-1
        seg[k] = x
        while k:
            k = (k-1)//2
            seg[k] = gcd(seg[k*2+1],seg[k*2+2])
        
    def query(p,q):
        if q<=p:
            return ide_ele
        p += num-1
        q += num-2
        res=ide_ele
        while q-p>1:
            if p&1 == 0:
                res = gcd(res,seg[p])
            if q&1 == 1:
                res = gcd(res,seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = gcd(res,seg[p])
        else:
            res = gcd(gcd(res,seg[p]),seg[q])
        return res



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

    
    ide_ele = 0

    
    num =2**(n-1).bit_length()
    seg=[ide_ele]*(2*num)
    init(a)

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

if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd
N, = map(int, input().split())
X = list(map(int, input().split()))
rs = [0] * N
for i in range(N-1, 0, -1):
    rs[i-1] = gcd(rs[i], X[i])
ls = [0] * N
for i in range(N-1):
    ls[i+1] = gcd(ls[i], X[i])

s = 0
for i in range(N):
    s = max(s, gcd(ls[i], rs[i]))
print(s)

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

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

rg = [A[-1]]
for a in reversed(A[:-1]):
    rg.append(gcd(rg[-1], a))
rg.reverse()

ans = max(lg[-2], rg[1])
for l,r in zip(lg,rg[2:]):
    g = gcd(l,r)
    ans = max(ans, g)
print(ans)

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')

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


def main():
    N=I()
    A=LI()
    candidate = set(find_divisor(A[0]) + find_divisor(A[1]))
    ans = 0
    for c in candidate:
        ng_cnt = 0
        for a in A:
            if a%c:
                ng_cnt+=1
        if ng_cnt<=1:
            ans = max(ans, c)
    print(ans)
        
    
    
    

if __name__ == '__main__':
    main()

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


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


fromLeft = [0]
fromRight = [0]
for i, a in enumerate(A):
    if i == 0:
        fromLeft.append(a)
        continue
    fromLeft.append(gcd(a, fromLeft[-1]))

for i, a in enumerate(A[::-1]):
    if i == 0:
        fromRight.append(a)
        continue
    fromRight.append(gcd(a, fromRight[-1]))
fromRight = fromRight[::-1]

fromLeft[0] = fromRight[1]
fromRight[-1] = fromLeft[-2]

ans = 1
for i in range(N):
    gl = fromLeft[i]
    gr = fromRight[i + 1]
    ans = max(ans, gcd(gl, gr))

print(ans)

Solution (Python):
import sys

def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import math
import heapq
import fractions
from collections import defaultdict, Counter, deque
MOD = 10**9 + 7
INF = 10**15

def getyaku(n):
    res = []
    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            res.append(i)
            res.append(n // i)

    return(sorted(list(set(res))))

def judge(cand, nums):
    w = 0
    for num in nums:
        if num % cand !=0:
            if w == 1:
                return False
            else:
                w += 1

    return True

def main():
    n = getN()
    nums = getlist()

    ans = 0
    cands = getyaku(nums[0])

    for cand in cands:
        if judge(cand, nums):
            if ans < cand:
                ans = cand
    cands = getyaku(nums[1])

    for cand in cands:
        if judge(cand, nums):
            if ans < cand:
                ans = cand

    print(ans)
    return

if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
lgcd = [0]*N
rgcd = [0]*N
from fractions import gcd




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

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

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

Solution (Python):
from fractions import gcd

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

gcd_left = [0] * n
gcd_right = [0] * n
gcd_left[1] = a[0]
gcd_right[n-2] = a[n-1]

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

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

gcd_ex = [0] * n
gcd_ex[0] = gcd_right[0]
gcd_ex[n-1] = gcd_left[n-1]

for i in range(1,n-1):
    gcd_ex[i] = gcd(gcd_left[i], gcd_right[i])

print(max(gcd_ex))

Solution (Python):
def gcd(n,m):
    if n==0:
        return m
    elif m==0:
        return n
    elif n>m:
        n=n%m
        return gcd(n,m)
    else:
        m=m%n
        return gcd(n,m)

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

list1=[1 for i in range(0,N)]
list2=[1 for i in range(0,N)]
list1[0]=A[0]
list2[0]=A[N-1]

for i in range(1,N):
    list1[i]=gcd(list1[i-1],A[i])
    list2[i]=gcd(list2[i-1],A[N-1-i])
m=list2[N-2]
for i in range(0,N-2):
    test=gcd(list1[i],list2[N-3-i])
    if test>m:
        m=test
test=list1[N-2]
if test>m:
    m=test
print(m)

Solution (Python):
from fractions import gcd

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

    lgcd = [0 for _ in range(n)]
    rgcd = [0 for _ in range(n)]
    for i in range(n):
        if i == 0:
            lgcd[i] = a[i]
            rgcd[-i-1] = a[-i-1]
        else:
            lgcd[i] = gcd(lgcd[i-1],a[i])
            rgcd[-i-1] = gcd(rgcd[-i],a[-i-1]) 
    
    ans = 0
    for i in range(n):
        if i == 0:
            ans = max(ans,rgcd[i+1])
        elif i == n-1:
            ans = max(ans,lgcd[i-1])
        else:
            ans = max(ans,gcd(lgcd[i-1],rgcd[i+1]))

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np

Mod = 1000000007
import sys
sys.setrecursionlimit(1000000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): 
    n = int(input())
    a = list(map(int,input().split()))
    l = [0]*n
    r = [0]*n
    for i in range(n):
        if i == 0:
            l[0] = a[0]
            r[n-1] =a[n-1]
        else:
            l[i] = gcd(a[i],l[i-1])
            r[n-i-1] = gcd(a[n-i-1],r[n-i])
    if n > 2:
        ans = max(r[1],l[n-2])
    else:
        ans = max(a)
    for i in range(1,n-1):
        ans = max(ans,gcd(l[i-1],r[i+1]))

    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
a.sort()
d=defaultdict(int)

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

for i in make_divisors(a[0]):
    d[i]+=1
for g in make_divisors(a[1]):
    d[g]+=1

for g in range(2,n):
    for i,m in d.items():
        if a[g]%i==0:
            d[i]+=1

ans=1
for i,m in d.items():
    if m==n-1 or m==n:
        ans=max(ans,i)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
d={}

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
for i in make_divisors(a[0]):
    d[i]=1


for g in range(1,n):
    if g==1:
        new={}
        for i,m in d.items():
            if a[g]%i==0:
                d[i]+=1
                if not a[g]//i in d.keys():
                    new[a[g]//i]=1
        d.update(new)
    else:
        for i,m in d.items():
            if a[g]%i==0:
                d[i]+=1

ans=1
for i,m in d.items():
    if m==n-1 or m==n:
        ans=max(ans,i)
print(ans)

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

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

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

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

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

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

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

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

l_gcd = [0]*(N+2)
r_gcd = [0]*(N+2)

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

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

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

print(ans)

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

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

l_gcd = [0]*(N+2)
r_gcd = [0]*(N+2)

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

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

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

print(ans)