Problem:
 Score : 300 points    Problem Statement There are N integers, A_1, A_2, ..., A_N , written on the blackboard.  You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.  Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.      Constraints  All values in input are integers.  2 \leq N \leq 10^5  1 \leq A_i \leq 10^9       Output Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.       Sample Input 1 3
7 6 8      Sample Output 1 2  If we replace 7 with 4 , the greatest common divisor of the three integers on the blackboard will be 2 , which is the maximum possible value.       Sample Input 2 3
12 15 18      Sample Output 2 6       Sample Input 3 2
1000000000 1000000000      Sample Output 3 1000000000  We can replace an integer with itself.

Solution (Python):


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

def list_euclid(int_list):
    ans = int_list[0]
    if len(int_list) == 1:
        return ans
    kako_hash = {}
    kako_hash[ans] = ans
    for i in range(1,len(int_list)):
        if int_list[i] in kako_hash:
            continue
        else:
            kako_hash[int_list[i]] = int_list[i]
            ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans

n = int(input())
a = list(map(int, input().split()))
max_gcd = 0
b_int = {}
min = min(a)
if n == 2:
    max_gcd = max(a)
elif n < 5:
    for i in range(0, n):
        if a[0] in b_int:
            p = a.pop(0)
            
        else:
            p = a.pop(0)
            b_int[p] = p
            x = list_euclid(a)
            if max_gcd < x:
                max_gcd = x
            a.append(p)
else:
    for i in range(0, n):
        if a[0] in b_int:
            p = a.pop(0)
            
        else:
            if max_gcd == 0:
                p = a.pop(0)
                b_int[p] = p
                k = len(a) // 2
                x = 0
                y = list_euclid(a[0:k])
                if y == 1:
                    x = 1
                else:
                    z = list_euclid(a[k:len(a)])
                    x = euclid(y, z)
                if max_gcd < x:
                    max_gcd = x
                a.append(p)
            elif min <= max_gcd:
                break
            else:
                p = a.pop(0)
                b_int[p] = p
                k = len(a) // 2
                x = 0
                if k < 1:
                    x = list_euclid(a)
                else:
                    y = list_euclid(a[0:k])
                    if y == 1:
                        x = 1
                    else:
                        z = list_euclid(a[k:len(a)])
                        x = euclid(y, z)
                if max_gcd < x:
                    max_gcd = x
                a.append(p)
print(max_gcd)

Solution (Python):


import sys
def gcd(a, b): 
  while b:
    a, b = b, a % b
  return int(a)
    
n = int(input())
a = list(map(int, input().split()))
if n==2:
  print(max(a))
  sys.exit(0)
elif n==3:
  lis = [gcd(a[0], a[1]), gcd(a[1], a[2]), gcd(a[2], a[0])]
  print(max(lis))
  sys.exit(0)
tmp_unused = gcd(a[0], a[1])
tmp_used = max(a[0], a[1])

for i in range(2,n):
  
  tmp_used = max(tmp_unused, gcd(tmp_used, a[i]))
  tmp_unused = gcd(tmp_unused, a[i])
  
  
print(tmp_used)

Solution (Python):

import sys

def gcd(a, b): 
  while b:
    a, b = b, a % b
  return int(a)
    
n = int(input())
a = list(map(int, input().split()))


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


tmp_unused = gcd(a[0], a[1])
tmp_used = max(a[0], a[1])


for i in range(2,n):
  
  tmp_used = max(tmp_unused, gcd(tmp_used, a[i]))
  tmp_unused = gcd(tmp_unused, a[i])
  
  
print(tmp_used)

Solution (Python):
import sys
import fractions


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    ls = [[0] * (n + 1) for _ in range(2)]
    for i in range(n):
        if i == 0:
            ls[0][i+1] = a[i]
            ls[1][-i-2] = a[-i-1]
        else:
            ls[0][i+1] = fractions.gcd(ls[0][i], a[i])
            ls[1][-i-2] = fractions.gcd(ls[1][-i-1], a[-i-1])
    mgcd = 0
    for i in range(n):
        mgcd = max(mgcd, fractions.gcd(ls[0][i], ls[1][i + 1]))
    print(mgcd)


if __name__ == '__main__':
    solve()

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 = -1
    for i in range(n):
        ans = max(ans,gcd(query(0,i),query(i+1,n)))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
dp = [[0,0] for i in range(n)]

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


d1 = [0 for i in range(n)]
d2 = [0 for i in range(n)]
d1[0] = a[0]
d2[-1] = a[-1]

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

otv = 1

for i in range(n):
    if i == 0:
        otv = max(otv,d2[i + 1])
    elif i == n - 1:
        otv = max(otv,d1[i - 1])
    else:
        otv = max(otv, gcd(d2[i + 1],d1[i - 1]))
print(otv)

Solution (Python):
from fractions import gcd


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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd


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

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

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


if __name__ == '__main__':
    main()

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


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

ans = A[1]
for i in range(2,N):
    ans = gcd(ans,A[i])

d = []
for n in range(1,int(A[0]**0.5)+1):
    if A[0]%n == 0:
        d.append(n)
        d.append(A[0]//n)

d.sort(reverse=True)

for n in d:
    cnt = 0
    for a in A:
        if a%n==0:
            cnt += 1
    if cnt >= N-1:
        break
ans = max(ans,n)

print(ans)

Solution (Python):
from collections import Counter
from functools import lru_cache

N = int(input())


@lru_cache(maxsize=1000)
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

A_list = [make_divisors(int(e)) for e in input().split()]

all_divisors_list=[]

for i in A_list:
  all_divisors_list += i

collected_adl = Counter(all_divisors_list)

ans = 1
for a in collected_adl.most_common():
  if a[1] <= N-2:
    break
  elif a[0]>ans:
    ans=a[0]
    
print(ans)

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


def gcd_list(lst):
    a = lst[0]
    for i in lst[1:]:
        a = gcd(a, i)
    return a


n = int(input())
a = list(map(int, input().split()))
l = [0 for _ in range(n)]
r = [0 for _ in range(n)]
for i in range(n-1):
    l_i = 0 if 0 == i else l[i]
    l[i+1] = gcd(l_i, a[i])
for i in range(n)[::-1]:
    r_i1 = 0 if i == n-1 else r[i+1]
    r[i] = gcd(r_i1, a[i])
m = []
for i in range(n):
    r_i1 = 0 if i == n-1 else r[i+1]
    m.append(gcd(l[i], r_i1))
print(max(m))

Solution (Python):
import numpy as np

def gcd(v1, v2):
    if v1 == 0 or v2 == 0:
        return max(v1, v2)

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

N = int(input())

A = list(map(int, input().split()))
L = np.zeros(N+1)
R = np.zeros(N+1)
for i in range(N):
    L[i+1] = gcd(L[i], A[i])

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

print(int(max(M)))

Solution (Python):
def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
def mgcd(l):
  if len(l) == 1:
    return l[0]
  else:
    new_l = []
    for i in range(-(-len(l)//2)):
      temp = gcd(l[i], l[-i-1])
      if temp == 1:
        return 1
      else:
        new_l.append(temp)
    return mgcd(new_l)

n = int(input())
a = list(map(int, input().split()))
a.sort()
ans1 = mgcd(a[1:])
min_gcd = 1001001001
min_idx = 0
for i in range(1,n):
  temp = gcd(a[0], a[i])
  if temp < min_gcd:
    min_gcd = temp
    min_idx = i
a.pop(min_idx)
ans2 = mgcd(a)
print(max(ans1, ans2))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
N=int(input())
A=list(map(int,input().split()))

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

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

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

print(max(M))

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

def break_primes(n):
    pn = n
    cnt = 0
    d = set()
    while pn % 2 == 0:
        pn //= 2
        d.add(2)
    p = 3
    while(p*p <= n):
        if pn % p == 0:
            d.add(p)
        while(pn % p == 0):
            pn //= p
        p += 2
    if pn != 1:
        d.add(pn)
    return d

def vectorize(n,primes,a):
    pn = n
    for p in range(len(primes)):
        a[p] = 0
        while pn % primes[p] == 0:
            a[p] += 1
            pn //= primes[p]


N = int(input())
A = list(map(int,input().split()))
if N == 2:
    print(max(A[0],A[1]))
if N > 2:
    primes = list(break_primes(gcd(A[0],A[1]))|break_primes(gcd(A[1],A[2]))|break_primes(gcd(A[2],A[0])))
    P = len(primes)
    vec_0 = [0 for _ in range(P)]
    vec_1 = [0 for _ in range(P)]
    vectorize(A[0],primes,vec_0)
    vectorize(A[1],primes,vec_1)
    perfect = [0 for _ in range(P)]
    perfect_idx = [ -1 for _ in range(P)]
    onemiss = [0 for _ in range(P)]
    for p in range(P):
        perfect_idx[p] = 0 if vec_0[p] < vec_1[p] else 1
        perfect[p] = min(vec_0[p],vec_1[p])
        onemiss[p] = max(vec_0[p],vec_1[p])
    vec_n = [0 for _ in range(P)]
    for n in range(2,N):
        vectorize(A[n],primes,vec_n)
        for p in range(P):
            if vec_n[p] < perfect[p]:
                onemiss[p] = perfect[p]
                perfect[p] = vec_n[p]
                perfect_idx[p] = n
            elif vec_n[p] < onemiss[p]:
                onemiss[p] = vec_n[p]
    import collections
    d = collections.defaultdict(lambda :1)
    ans = 1
    for p in range(P):
        ans *= primes[p] ** perfect[p]
        d[perfect_idx[p]] *= primes[p] ** (onemiss[p]-perfect[p])
    if len(d.items()) == 0:
        print(ans)
    else:
        print(ans*max(d.items(),key=lambda x:x[1])[1])

Solution (Python):



def main():
    from fractions import gcd

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

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

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

def lgcd(l):
    if len(l)==1:
        return l[0]
    res = l[0]
    for i in range(1,len(l)):
        res = fractions.gcd(res,l[i])
    return res

def main():
    n = int(input())
    a= list(map(int,input().split()))
    b = a[:]
    b.reverse()
    l = [0]
    r = [0]
    for i in range(n):
        l.append(fractions.gcd(l[i],a[i]))
        r.append(fractions.gcd(r[i],b[i]))
    m=[]
    for i in range(n):
        m.append(fractions.gcd(l[i],r[n-1-i]))
    return max(m)


print(main())

Solution (Python):





def gcd(x,y):
    if x==0:
        return y
    if y==0:
        return x
    if x==y:
        return x
    elif x>y:
        r=-1
        while r!=0:
            r=x%y
            x=y
            y=r
        return x
    else:
        r=-1
        while r!=0:
            r=y%x
            y=x
            x=r
        return y


def segfunc(x,y):
    return 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+1:
        k = (k-1)//2
        seg[k] = segfunc(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 = 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

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


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)

Solution (Python):
import sys
import fractions

def solve(N: int, A: "List[int]"):
    A.sort()
    gcd_left = [A[0]] 
    for i in range(1,N):
        gcd_left.append(fractions.gcd(A[i],gcd_left[i-1]))
        
    gcd_right = [A[-1]] 
    for i in range(N-2,-1,-1):
        gcd_right.append(fractions.gcd(A[i],gcd_right[N-2-i]))
    answer = 1

    for i in range(N):
        if i == 0:
            answer = max(answer,gcd_right[N-2])
        elif i == N-1:
            answer = max(answer,gcd_left[N-2])
        else:
            answer = max(answer,fractions.gcd(gcd_left[i-1],gcd_right[N-2-i]))
    print(answer)
    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):
from fractions import gcd
n = int(input())
a = [int(i) for i in input().split()]
lgcd = [a[0]]*n
rgcd = [a[n-1]]*n
for i in range(n-1):
    lgcd[i+1] = gcd(lgcd[i],a[i+1])
    rgcd[n-i-2] = gcd(rgcd[n-i-1],a[n-i-2])
ans = [0]*n
for i in range(1,n-1):
    ans[i] = gcd(lgcd[i-1],rgcd[i+1])
ans[0] = rgcd[1]
ans[n-1] = lgcd[n-2]
print(max(ans))

Solution (Python):
from collections import deque
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
list1 = deque(list1)
L = deque([0])
R = deque([0])
M = []

for i in range(len(list1)-1):
	L.append(math.gcd(L[i],list1[i]))
	R.append(math.gcd(R[i],list1[n-i-1]))

for i in range(len(L)):
	M.append(math.gcd(L[i],R[len(R)-i-1]))
print(max(M))

Solution (Python):
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))
gcda = [A[0]]
gcdb = [A[-1]]
for i in range(N-2):
    gcda.append(gcd(gcda[-1],A[i+1]))
    gcdb.append(gcd(gcdb[-1],A[-(i+2)]))
gcdb.reverse()
GC = [gcdb[0]]
for i in range(N-2):
    GC.append(gcd(gcda[i],gcdb[i+1]))
else:
    GC.append(gcda[-1])
print(max(GC))

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

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

leftGcd = [-1] * (N + 1)
leftGcd[0] = A[0]

for i, a in enumerate(A, start=1):
    leftGcd[i] = gcd(leftGcd[i - 1], a)

rightGcd = [-1] * (N + 1)
rightGcd[-1] = A[-1]

for i, a in enumerate(reversed(A), start=1):
    rightGcd[-(i + 1)] = gcd(rightGcd[-i], a)

ans = max(rightGcd[1], leftGcd[-2])
for mid in range(1, N):
    ans = max(ans, gcd(leftGcd[mid], rightGcd[mid + 1]))

print(ans)

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

def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

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

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

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



    
ans = max(toRight[-2], toLeft[2])
for i in range(2, N-1 + 1):
    now = gcd(toRight[i-1], toLeft[i+1])
    ans = max(ans, now)

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


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

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

Solution (Python):
n = int(input())
a_list = [ int(v) for v in input().split() ]

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

gcd_l = [ a_list[0] for i in range(n) ]
gcd_r = [ a_list[-1] for i in range(n) ]
for i in range(n-1):
    gcd_l[i+1] = gcd(gcd_l[i],a_list[i+1])
a_list.reverse()
for i in range(n-1):
    gcd_r[i+1] = gcd(gcd_r[i],a_list[i+1])
gcd_r.reverse()

ans_list = [ gcd_r[1] ] + [ gcd(gcd_l[i], gcd_r[i+2])  for i in range(n-2) ] + [ gcd_l[-2] ]
print(max(ans_list))

Solution (Python):
def slove():
    import sys
    import fractions
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    ls = [[0] * (n + 1) for _ in range(2)]
    for i in range(n):
        ls[0][i+1] = fractions.gcd(ls[0][i], a[i])
        ls[1][-i-2] = fractions.gcd(ls[1][-i-1], a[-i-1])
    m_gcd = 0
    for i in range(n):
        gcd = fractions.gcd(ls[0][i], ls[1][i+1])
        m_gcd = max(m_gcd, gcd)
    print(m_gcd)


if __name__ == '__main__':
    slove()

Solution (Python):
from fractions import gcd

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

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

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

print(m)

Solution (Python):

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

L = [0]
R = [0]
gcdlist = []



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



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

print(max(gcdlist))

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

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

for i in range(n):
	if i == 0:
		ma = right[n-2]
	elif i == n-1:
		if ma < left[n-2]:
			ma = left[n-2]
	else:
		gc = fractions.gcd(left[i-1], right[n-2-i])
		if ma < gc:
			ma = gc
		left.append(fractions.gcd(left[i-1], a[i]))

print(ma)

Solution (Python):
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
l, r = [0] * n, [0] * n

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

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

print(ans)

Solution (Python):
class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        res = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                res = f(res, dat[l])
                l += 1
            if r & 1:
                r -= 1
                res = f(dat[r], res)
            l >>= 1
            r >>= 1
        return res


def gcd(x, y):
    if x * y == 0:
        return x + y
    elif x > y:
        return gcd(y, x)
    else:
        return gcd(y % x, x)


N = int(input())
A = [int(_) for _ in input().split()]
ST = SegmentTree(array=A, f=gcd, ti=0)
ans = 0
for i in range(N):
    ans = max(ans, gcd(ST.query(0, i), ST.query(i + 1, N)))
print(ans)

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

num = int(input())
nums = [int(s) for s in input().split()]

l = nums[0]
r = nums[-1]
left = []
right = []
for i in range(num-1):
  l,r = gcd(l,nums[i]), gcd(r,nums[-i-1])
  left.append(l)
  right.append(r)
answer = [left[-1],right[-1]]
for i in range(len(left)-1):
  answer.append(gcd(left[i],right[-i-2]))
print(max(answer))

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


def main():
    N, *A = map(int, open(0).read().split())
    L = [0] * N
    R = L[:]
    R.append(0)
    for i in range(N - 1):
        L[i + 1] = gcd(L[i], A[i])
    for i in range(N - 1, -1, -1):
        R[i] = gcd(R[i + 1], A[i])
    print(max(gcd(L[i], R[i + 1]) for i in range(N)))
    return


main()

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


def main():
    N, *A = map(int, open(0).read().split())
    A.insert(0, 0)
    A.append(0)
    L = [0] * (N + 2)
    R = L[:]
    for i in range(1, N):
        L[i + 1] = gcd(L[i], A[i])
    for i in range(N, 0, -1):
        R[i] = gcd(R[i + 1], A[i])
    print(max(gcd(L[i], R[i + 1]) for i in range(1, N + 1)))
    return


main()

Solution (Python):
import fractions

ans=1
n=int(input())
a=list(map(int,input().split()))
l=[0]
r=[0]
for i in range(n-1):
    l.append(fractions.gcd(l[i],a[i]))
    r.append(fractions.gcd(r[i],a[-(i+1)]))
for i in range(n):
    c=fractions.gcd(l[i],r[-(i+1)])
    if ans<c:
        ans=c
print(ans)

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

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

ans=0
for i,j in zip(left,right):
    ans=max(ans,gcd(i,j))
print(ans)

Solution (Python):
from fractions import gcd

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

L = [0]
R = [0]

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

R.reverse() 







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

Solution (Python):
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()))
L = [0]
R = [0]
for i in range(N):
    L.append(gcd(L[i],A[i]))
    R.append(gcd(R[i],A[N-i-1]))
R.reverse()
print(max([gcd(L[i],R[i+1])for i in range(N)]))

Solution (Python):
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()))
L = [0]
R = [0]
M = []
for i in range(N):
    L.append(gcd(L[i],A[i]))
    R.append(gcd(R[i],A[N-i-1]))
R.reverse()
print(max([gcd(L[i],R[i+1])for i in range(N)]))

Solution (Python):
import fractions
from functools import reduce
def gcd(*i):
    return reduce(fractions.gcd, i)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    L=[0]
    R=[0]
    for i in range(N):
        L.append(gcd(L[i],A[i]))
        R.append(gcd(R[i],A[-(i+1)]))
    R=R[::-1]
    ans=[]
    for i in range(N):
        ans.append(gcd(L[i],R[i+1]))
    print(max(ans))
if __name__ == "__main__":
    main()

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

from fractions import gcd

H = [A[0]]
x = A[0]
for i in range(1, N):
    x = gcd(x, A[i])
    H.append(x)


T = [A[-1]]
x = A[-1]
for i in range(2, N+1):
    x = gcd(x, A[-1 * i])
    T.append(x)




r = []
for i in range(N):
    if i == 0:
        ans = T[-2]
        r.append(ans)
    elif i == N - 1:
        ans = H[-2]
        r.append(ans)
    else:
        ans = gcd(H[i-1], T[N-i-2])
        r.append(ans)
print(max(r))

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

import fractions
import copy

listb=[]
listc=[]
ansb= lista[0]

for i in range(1,n-1):
	ansb=fractions.gcd(ansb,lista[i])

	listb.append(ansb)


ansc= lista[n-1]

for i in range(n-2,0,-1):
	ansc=fractions.gcd(ansc,lista[i])

	listc.append(ansc)


list_b=[lista[0]]
listb=[1]+list_b+listb
list_c=[lista[n-1]]
listc=list(reversed(listc))+list_c+[1]



ans=0
list_ans=[listc[0],listb[n-1]]
for i in range(1,n-1):
	ans=fractions.gcd(listb[i],listc[i])
	list_ans.append(ans)
	
ansans=max(list_ans)

if n==2:
	ansans=max(lista[0],lista[1])
	
print(ansans)

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

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

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

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

Solution (Python):
n=int(input())
A=[]*n
A[:]=map(int,input().split())

def gcd(a,b):
    while b:
        a,b=b,a%b
    return a
      
left=[0]*n
right=[0]*n
for i in range(1,n):
  left[i]=gcd(left[i-1],A[i-1])
  right[n-1-i]=gcd(right[n-i],A[n-i])

ans=0
for i,j in zip(left,right):
  ans=max(ans,gcd(i,j))
print(ans)

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

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

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

    A_sorted = sorted(A)
    div0 = make_divisors(A_sorted[0])
    ans = 1
    for d in div0[1:]:
        count = 0
        isAns = True
        for i in A_sorted[1:]:
            if i % d != 0:
                count += 1
                if count == 2:
                    isAns = False
                    break
        if isAns:
            ans = max(ans, d)
    div1 = make_divisors(A_sorted[1])
    for d in div1[1:]:
        count = 0
        isAns = True
        for i in A_sorted[2:]:
            if i % d != 0:
                isAns = False
                break
        if isAns:
            ans = max(ans, d)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd


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

G_FW    = [None] * N
G_FW[0] = A[0]

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

G_RV     = [None] * N
G_RV[-1] = A[-1]

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

G     = [None] * N
G[ 0] = G_RV[1]
G[-1] = G_FW[-2]

for i in range(1, N-1):
    G[i] = gcd(G_FW[i-1], G_RV[i+1])

print(max(G))

Solution (Python):
import sys
import fractions
import functools
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
a = LIST()

L = [0]*n
R = [0]*n
L[0] = INF
L[1] = a[0] 
R[n-1] = INF
R[n-2] = a[n-1]

GCDs = [0]*n

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

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

GCDs[0] = R[0]
GCDs[n-1] = L[n-1]

for i in range(1,n-1):
    GCDs[i] = fractions.gcd(L[i], R[i])

res = max(GCDs)

print(res)

Solution (Python):
import sys
sys.setrecursionlimit(500000)
def gcd(x , y):
    while y != 0:
        x , y = y , x % y
    return x
N = int(input())
A = list(map(int ,input().split()))
max_val = 0
if N == 2:
    print(max(A[1], A[0]))
    exit()
right = []
left = []
work_right = gcd(A[N - 2] , A[N - 1])
right.append(A[N - 1])
work_left = gcd(A[0] , A[1])
left.append(A[0])
for i in range(1 , N - 1):
    work_left = gcd(work_left , A[i])
    left.append(work_left)
    work_right = gcd(work_right , A[N - i - 1])
    right.append(work_right)


right.reverse()
max_val = 0
for i in range(N):
    if i == 0:
        max_val = right[0]
    elif i == N - 1:
        if max_val < left[-1]:
            max_val = left[-1]
    else:
        tmp = gcd(right[i] , left[i - 1])
        if tmp > max_val:
            max_val = tmp
print(max_val)
'''
for i in range(N):
    work = A[:i] + A[i + 1:]
    for j in range(1 , len(work)):
        if j == 1:
            tmp = gcd(work[0],work[1])
        else:
            tmp = gcd(tmp , work[j])
    if max_val < tmp:
        max_val = tmp
print(max_val)
'''