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 collections import Counter

def getDivisor(x):
    ans = []
    for d in range(1, int(x**0.5)+1):
        if x % d == 0:
            ans.append(d)
            if x//d != d:
                ans.append(x//d)
    return sorted(ans)

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

divisors = getDivisor(As[0]) + getDivisor(As[1])
cnt = Counter(divisors)

for i, A in enumerate(As[2:], 2):
    for d, num in list(cnt.items()):
        if A%d == 0:
            cnt[d] += 1

    for d, num in list(cnt.items()):
        if num < i:
            del cnt[d]

print(max(cnt.keys()))

Solution (Python):
import fractions

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

gcd = fractions.gcd(A[0], A[1])
ag = {A[0], A[1]}

for d in A[2:]:
    gc = fractions.gcd(gcd, d)
    ag2 = set()
    for c in ag:
        ag2.add(fractions.gcd(c, d))
    ag = ag2
    ag.add(gcd)
    gcd = gc

print(max(ag))

Solution (Python):
import random
from operator import itemgetter
import sys
sys.setrecursionlimit(1000000)
ans=0

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

def gcd_all(l):
    rt=l.pop()
    for i in l:
        rt=gcd(i,rt)
    return rt
    
n=int(input())

g=[]

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

ans=0
d=a[-1]
m=a[-1]
mi=-1
k1=[]
flg=0
for i in range(n):
    gg=gcd(a[i],d)
    if gg<m:
        m=gg
        mi=i
ans=max(ans,gcd_all(a[:mi]+(a[mi+1:] if mi+1<n else [])))

d=a[0]
m=a[0]
mi=0
k1=[]
flg=0
for i in range(n):
    gg=gcd(a[i],d)
    if gg<m:
        m=gg
        mi=i
ans=max(ans,gcd_all(a[:mi]+(a[mi+1:] if mi+1<n else [])))

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

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

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

res = 0
for i in range(n):
    res = max(gcd(pre[i], post[n - 1 - i]), res)

print(res)

Solution (Python):

from fractions import gcd

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

if N == 2:
    print(max(A))
else:
    ans = gcd(A[0], A[1])
    ans2 = max(gcd(A[0], A[2]), gcd(A[1], A[2]))
    for a in A[2:]:
        ans2 = max(gcd(a, ans2), ans)
        ans = gcd(a, ans)
    
    print(ans2)

Solution (Python):
import queue

"""
N = int(input())



A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""
def newton(a,b):
	if b == a:
		return a
	if a>b:
		(a,b) = (b,a)
	while b%a != 0:
		b = b%a
		(a,b) = (b,a)
	return a
N = int(input())
A = [int(i) for i in input().split()]
l = []
r = []

l.append(A[0])
r.append(A[-1])

for i in range(1,N):
	l.append(newton(l[-1],A[i]))
	r.append(newton(r[-1],A[-(i+1)]))
m = max(l[-2],r[-2])
for i in range(1,N-1):
	m = max(m,newton(l[i-1],r[N-2-i]))
print(m)

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

B.sort()

def gcd(a,b):
    Y=max(a,b)
    X=min(a,b)
    while Y%X!=0:
        X,Y=Y%X,X
    return X

dp=[[0,0,0]for i in range(N)]
dp[0][0]=B[0]
dp[1][0]=gcd(B[0],B[1])
dp[1][1]=B[0]
dp[1][2]=B[1]

for i in range(2,N):
    dp[i][0]=gcd(dp[i-1][0],B[i])
    dp[i][1]=dp[i-1][0]
    dp[i][2]=max(gcd(dp[i-1][1],B[i]),gcd(dp[i-1][2],B[i]))
print(max(dp[N-1]))

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


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


def gcd(n, m):
    
    a = max(n,m)
    b = min(n,m)
    while b:
        a, b = b, a % b
    return a



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

Solution (Python):
n=int(input())
a = list(map(int,input().split()))
a.sort()
 
def gcd(a,b):
  if a>b:
    return gcd(b,a)
  else:
    if b%a==0:
      return a
    else:
      return gcd(b%a,a)

dp1=[[0 for i in range(n)] for j in range(2)]
dp1[0][0]=a[0]
dp1[1][0]=a[0]
dp1[0][1]=gcd(a[0],a[1])
dp1[1][1]=a[1]
for i in range(n-2):
  dp1[0][i+2]=gcd(dp1[0][i+1],a[i+2])
  dp1[1][i+2]=max(gcd(dp1[1][i+1],a[i+2]),dp1[0][i+1])

dp2=[[0 for i in range(n)] for j in range(2)]
dp2[0][0]=a[0]
dp2[1][0]=a[0]
dp2[0][1]=gcd(a[0],a[1])
dp2[1][1]=a[0]
for i in range(n-2):
  dp2[0][i+2]=gcd(dp2[0][i+1],a[i+2])
  dp2[1][i+2]=max(gcd(dp2[1][i+1],a[i+2]),dp2[0][i+1])
 
print(max(dp1[1][n-1],dp2[1][n-1]))

Solution (Python):
import fractions

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

def gcd3(nums):
    ans1 = fractions.gcd(nums[0], nums[1])
    ans2 = fractions.gcd(nums[0], nums[2])
    ans3 = fractions.gcd(nums[1], nums[2])

    if ans1 >= ans2 and ans1 >= ans3:
        return ans1, nums[2]
    if ans2 >= ans1 and ans2 >= ans3:
        return ans2, nums[1]
    if ans3 >= ans1 and ans3 >= ans2:
        return ans3, nums[0]

ans = 0
if n == 2:
    ans = max(aList)
else:
    sList = []
    sList.append(aList.pop(0))
    sList.append(aList.pop(0))
    sList.append(aList.pop(0))
    ans, v = gcd3(sList)

    for i in range(len(aList)):
        tmp = aList.pop(0)
        tmp1 = fractions.gcd(ans, v)
        tmp2 = fractions.gcd(ans, tmp)
        if tmp1 >= tmp2:
            ans = tmp1
            v = tmp
        else:
            ans = tmp2

print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import fractions
from functools import reduce



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

def gcd(nums):
    return reduce(fractions.gcd,nums)

ans = 1
A.sort()
gcd1 = gcd(A)
for i in range(N):
    A[i] //= gcd1

ans *= gcd1

gcd2 = A[0]
for i in range(1,N):
    gcd2 = fractions.gcd(gcd2,A[i])
    if gcd2 == 1:
        key = i
        break

a = copy.deepcopy(A)
a.remove(A[0])
if gcd(a)!=1:
    ans *= gcd(a)
    print(ans)
    exit()

a = copy.deepcopy(A)
a.remove(A[key])
if gcd(a)!=1:
    ans *= gcd(a)
    print(ans)
    exit()
print(ans)

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)                                    
    else:                                            
        return(gcd(b, a % b))                        
dp = [0 for i in range(N)]                           
dp[0] = A[0]                                         
for i in range(1,N):                                 
    dp[i] = gcd(dp[i-1] , A[i])                      
dpr = [0 for i in range(N)]                          
dpr[N-1] = A[N-1]                                    
for i in range(N-2,-1,-1):                           
    dpr[i] = gcd(dpr[i+1],A[i])                      
maxgcd = dpr[1]                                      
for i in range(1,N-1):                               
    maxgcd = max(maxgcd, gcd(dp[i-1], dpr[i+1]))     
maxgcd = max(maxgcd,dp[N-2])                         
print(maxgcd)

Solution (Python):
import math
N = int(input())
A = sorted(list(map(int, input().split())))
if N == 2:
    print(max(A))
    exit()
result = []
num = 0
for a in A[1:]:
    if a % A[0] != 0:
        num += 1
        if num == 2:
            break
else:
    result.append(A[0])
for n in range(2, int(math.sqrt(A[0])) + 1):
    if A[0] % n != 0:
        continue
    num = 0
    x = A[0] // n
    for a in A[1:]:
        if a % x != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(x)
    num = 0
    for a in A[1:]:
        if a % n != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(n)
num = 0
B = A[0:1] + A[2:]
for a in B:
    if a % A[1] != 0:
        num += 1
        if num == 2:
            break
else:
    result.append(A[1])
for n in range(2, int(math.sqrt(A[1])) + 1):
    if A[1] % n != 0:
        continue
    num = 0
    x = A[1] // n
    for a in B:
        if a % x != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(x)
    num = 0
    for a in B:
        if a % n != 0:
            num += 1
            if num == 2:
                break
    else:
        result.append(n)
if len(result) != 0:
    print(max(result))
else:
    print(1)

Solution (Python):
n=int(input())
a = list(map(int,input().split()))
a.sort()
 
def gcd(a,b):
  if a>b:
    return gcd(b,a)
  else:
    if b%a==0:
      return a
    else:
      return gcd(b%a,a)

dp=[[0 for i in range(n)] for j in range(2)]
dp[0][0]=a[0]
dp[1][0]=a[0]
dp[0][1]=gcd(a[0],a[1])
dp[1][1]=a[1]
for i in range(n-2):
  dp[0][i+2]=gcd(dp[0][i+1],a[i+2])
  dp[1][i+2]=max(gcd(dp[1][i+1],a[i+2]),dp[0][i+1])

bp=[[0 for i in range(n)] for j in range(2)]
bp[0][0]=a[0]
bp[1][0]=a[0]
bp[0][1]=gcd(a[0],a[1])
bp[1][1]=a[0]
for i in range(n-2):
  bp[0][i+2]=gcd(bp[0][i+1],a[i+2])
  bp[1][i+2]=max(gcd(bp[1][i+1],a[i+2]),bp[0][i+1])
 
print(max(dp[1][n-1],bp[1][n-1]))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.sort()
min_ptn = A[1]
for i in range(min_ptn,0,-1):
  count = 0
  for a in A:
    if a%i!=0:
      count+=1
    if count>1:
      break
  if count<2:
    print(i)
    break

Solution (Python):
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
A.sort()
def gcd(a, b): 
    a = abs(a)
    b = abs(b)
    while b:
        a, b = b, a % b
    return a
g0 = reduce(gcd,A[1:])

now = A[0]

id = 0
for i in range(1,N):
    ne = gcd(now,A[i])
    if ne < now:
        id = i
        now = ne
A.pop(id)

g = reduce(gcd,A)
print(max(g,g0))

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

n = int(input())
a = list(map(int, input().split()))
a.sort()
if n == 2:
    print(max(a))
else:
    a1 = gcd(a[0],a[1])
    a2 = gcd(a[1],a[2])
    a3 = gcd(a[2],a[0])
    m = max(a1,a2,a3)
    judge = 0
    for i in range(m):
        if judge == 1:
            break
        cnt = 0
        for j in range(n):
            if a[j] % (m-i) != 0:
                cnt += 1
            if cnt > 1:
                break
            if j == n-1 and cnt <= 1:
                print(m-i)
                judge = 1

Solution (Python):
import math 
N = int(input())
A= list(map(int,input().split()))
memo_gcd_foward = [A[0]]
memo_gcd_backward = [A[N-1]]

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

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

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


cand = []
cand.append(memo_gcd_foward[-1])
cand.append(memo_gcd_foward[-2])
cand.append(memo_gcd_backward[-2])

for i in range(N-2):
    cand.append(gcd(memo_gcd_foward[i],memo_gcd_backward[-i-3]))
print(max(cand))

Solution (Python):
from fractions import gcd

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

if N == 2:
    ans = max(A)

else:
    memo = {(0,0):1, (N,N):1}

    for i in range(1,N+1):

        if i == 1:
            memo[(0,1)] = A[0]
            memo[(N-1,N)] = A[-1]

        else:

            
            memo[(0,i)] = gcd(memo[(0,i-1)], A[i-1])

            
            memo[(N-i,N)] = gcd(memo[(N-i+1,N)], A[-i])

    ans = memo[(0,N)]

    for i in range(1,N+1):

        if i == 1:
            curgcd = memo[(1,N)]
        elif i == N:
            curgcd = memo[(0,N-1)]
        else:
            curgcd = gcd(memo[(0,i-1)], memo[(i,N)])

        ans = max(ans, curgcd)

print(ans)

Solution (Python):
import sys


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


def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A)

    if N == 2:
        return A[1]

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

    g = A[0]
    B = []
    for a in A:
        g = gcd(a, g)
        B.append(g)

    i = B.index(g)
    A.pop(i)

    g = A[0]
    for a in A:
        g = gcd(g, a)

    return max(g, g0)


if __name__ == '__main__':
    print(main())

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

nums = A[:]
import fractions

gcd_queue = [
    (nums[0], True),
    (nums[1], True),
    (fractions.gcd(nums[0], nums[1]), False),
]
for n in nums[2:]:
    tmp_queue = []
    for q in gcd_queue:
        if q[1] == True:
            tmp_queue.append((fractions.gcd(q[0], n), True))
        else:
            tmp_queue.append((fractions.gcd(q[0], n), False))
            tmp_queue.append((q[0], True))
    gcd_queue = set(tmp_queue[:])

ans = 0
for q in gcd_queue:
    ans = max(ans, q[0])
print(ans)

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


N = int(input())
a = list(map(int, input().split()))
a.sort()

g_left = []
g_right = []

t = a[0]
for i in range(N):
    t = gcd(t, a[i])
    g_left.append(t)

t = a[-1]
for i in range(N - 1, -1, -1):
    t = gcd(t, a[i])
    g_right.append(t)

g_right.reverse()

g = 1
for i in range(1, N - 1):
    g = max(g, gcd(g_left[i - 1], g_right[i + 1]))
g = max(g, g_left[N - 2])
g = max(g, g_right[1])

print(g)

Solution (Python):
N = int(input())
As = list(map(int,input().split()))
As.sort(reverse=True)
first_d = As[0]
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

if N == 2:
    print(max(As))
else:
    first = make_divisors(As[0])
    second = make_divisors(As[1])
    third  = make_divisors(As[2])
    div_list = list((set(first) & set(second)) | (set(first) & set(third)) | (set(third) & set(second)))
    div_list.sort(reverse=True)
    
    for d in range(len(div_list)):
        cnt = 0
        for i in range(N):
            if As[i] % (div_list[d]) != 0:
                cnt += 1
                if cnt > 1:
                    break
        if cnt <= 1 :
            ans = div_list[d]
            break
    print(ans)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


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


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v

def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False



dp = None


def main():
    N = int(input())
    A = sorted(li_input(), reverse=True)

    ans = 10**100
    ans_i = -1

    for i in range(len(A)-1):
        g = gcd(A[-1], A[i])

        if g < ans:
            ans = g
            ans_i = i

    print(max(
        gcd_list(A[:-1]),
        gcd_list(A[:ans_i] + A[ans_i+1:])
    ))


main()

Solution (Python):


def readInts():
  return [int(s) for s in input().split(" ")]

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

def gcds(A):
  ret = A[0]
  for n in A[1:]:
    ret = gcd(ret, n)
  return ret

def main():
  N = readInts()[0]
  A = readInts()

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

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

  m = 1
  for i in range(N):
    if i == 0:
      m = max(m, rgcds[i+1])
    elif i == N - 1:
      m = max(m, lgcds[i-1])
    else:
      m = max(m, gcd(lgcds[i-1], rgcds[i+1]))
  print(m)

if __name__ == "__main__":
  main()

Solution (Python):
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
 
n = int(input())
a = list(map(int,input().split()))
 
b = [a[0]]
c = [a[-1]]
 
for i in range(1,n):
     j = n-1-i
     b.append(gcd(b[-1],a[i]))
     c.append(gcd(c[-1],a[j]))
     
ans = max(b[-2],c[-2])
for i in range(n):
     ans = max(ans,gcd(b[i],c[n-3-i]))
print(ans)

Solution (Python):
from fractions import gcd

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

gcd_num = a_list[0]
for a in a_list[1:]:
    gcd_num = gcd(gcd_num, a)

b_list = []

for a in a_list:
    b_list.append(a // gcd_num)

first = False
b_list.sort(reverse=True)
gcd_b = b_list[0]
for i, b in enumerate(b_list[1:]):
    tmp = gcd(gcd_b, b)
    if tmp == 1:
        if not first:
            first = True
            if i == 0:
                gcd_b = max(gcd_b, b)
            gcd_b = gcd_b
        else:
            gcd_b = 1
            break
    else:
        gcd_b = tmp
first = False
gcd_b3 = b_list[1]
b_list.append(b_list[0])

for i, b in enumerate(b_list[1:]):
    tmp = gcd(gcd_b3, b)
    if tmp == 1:
        if not first:
            first = True
            gcd_b3 = gcd_b3
        else:
            gcd_b3 = 1
            break
    else:
        gcd_b3 = tmp

print(gcd_num * max(gcd_b, gcd_b3))

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

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


def get_custum(xlist):
    prev = None
    gcm_list = []
    for a in xlist:
        if prev is None:
            gcm_list.append(a)
            prev = a
            continue
        ret = calc_gcm(a, prev)
        gcm_list.append(ret)
        prev = ret

    for boundary, gcm in enumerate(gcm_list):
        if gcm == gcm_list[-1]:
            break

    gcm_1 = None
    for i, a in enumerate(xlist):
        if i == boundary - 1:
            continue
        if gcm_1 is None:
            gcm_1 = a
            continue
        gcm_1 = calc_gcm(a, gcm_1)

    gcm_2 = None
    for i, a in enumerate(xlist):
        if i == boundary:
            continue
        if gcm_2 is None:
            gcm_2 = a
            continue
        gcm_2 = calc_gcm(a, gcm_2)

    return max(gcm_1, gcm_2)

ret1 = get_custum(alist)
alist.reverse()
ret2 = get_custum(alist)

print(max(ret1, ret2))

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

l=[0]*N
r=[0]*N
l[0]=A[0]
r[0]=A[N-1]
for i in range(1,N-1):
   l[i]=gcd(l[i-1],A[i])
   r[i]=gcd(r[i-1],A[N-1-i])
gcdlist = [1,l[N-2],r[N-2]]
for i in range(N-2):
   gcdlist.append(gcd(l[i],r[N-3-i]))

print(max(gcdlist))

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

def gcd (a: int,b: int) -> int:
    if a < b: a,b = b,a
    return a if b == 0 else gcd(b,a%b)
def lcm (a: int,b: int) -> int:
    return a // gcd(a,b) * b

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


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

Solution (Python):
from collections import deque

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

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

left_gcd = deque()
left_gcd.append(0)
right_gcd = deque()
right_gcd.append(0)

for i in range(n):
    left_gcd.append(gcd(left_gcd[-1], a[i]))
    right_gcd.appendleft(gcd(right_gcd[0], a[(n - 1) - i]))

max_gcd = 0

for i in range(n):
    get_gcd = gcd(left_gcd[i], right_gcd[i + 1])
    if get_gcd > max_gcd:
        max_gcd = get_gcd

print(max_gcd)

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

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

l=make_divisors(a[0])
l2=make_divisors(a[1])
le=len(l)
le2=len(l2)         
an=[0]*le
an2=[0]*le2
for i in range(n):
    for j in range(le):
        if a[i]%l[j]==0:
            an[j]+=1
    for j in range(le2):
        if a[i]%l2[j]==0:
            an2[j]+=1
ans=1
ans2=1
for i in range(le):
    if an[i]>=n-1:
        ans=max(ans,l[i])
for i in range(le2):
    if an2[i]>=n-1:
        ans2=max(ans2,l2[i])

print(max(ans,ans2))

Solution (Python):
import math
import fractions
from collections import Counter as ct

def gcd_list(l):
    for n,i in enumerate(l):
        if n==0:cur=i
        cur=fractions.gcd(cur,i)
    return cur

def yakusulist(n):
    ans=[]
    for i in range(1,int(math.sqrt(n+1))+1):
        if n%i==0:
            ans.append(i)
            ans.append(n//i)
    return(set(ans))
    
def warennnokaomaera(n):
    warenai=0
    for k,i in enumerate(A):
        if i%n and warenai==0:
            warenai+=1
            continue
        if i%n and warenai:return(False)
    return(True)

N=int(input())
data=[int(i) for i in input().split()]
datagcd=gcd_list(data)
A=[i//datagcd for i in data]
if N>2:
    co=ct(A)
    alpha=sorted(co.keys())
    if len(alpha)>=3:
        min3=[alpha[0],alpha[1],alpha[2]]
        p,q,r=[yakusulist(n) for n in min3]
        pq=p&q
        qr=q&r
        rp=r&p
        overtwo=pq|qr|rp
        overtwo=sorted(list(overtwo),reverse=True)
        for i in overtwo:
            if warennnokaomaera(i):
                print(datagcd*i)
                break
    elif len(alpha)==2:
        if co[alpha[0]]==1:
            print(datagcd*alpha[1])
        elif co[alpha[1]]==1:
            print(datagcd*alpha[0])
        else:print(datagcd)
    else:print(datagcd)
else:print(max(data))

Solution (Python):
N = input()

A = map(int,raw_input().split())

B = []
C = []

for i in A:
    if i not in B:
        B.append(i)
    elif i not in C:
        C.append(i)

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

ans = 0;
for i in range(len(B)):
    res = 0
    for j in range(len(B)):
        if B[i] not in C:
            if i == j:
                continue
        res = gcd(res,B[j])
    ans = max(ans,res)
        

if ans == 0:
    if len(B) == 1:
        print B[0]
    else:
        print 1
else:
    print ans

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

ctr = Counter(A)
solos = []
for k,v in ctr.items():
    if v > 1: continue
    solos.append(k)
solos.sort()

if len(solos) == 0:
    g = A[0]
    for a in A:
        g = gcd(g,a)
    print(g)
    exit()

g1 = g2 = None
for k in ctr.keys():
    if k != solos[0]:
        if g1 is None:
            g1 = k
        else:
            g1 = gcd(g1,k)
    if len(solos) > 1 and k != solos[1]:
        if g2 is None:
            g2 = k
        else:
            g2 = gcd(g2,k)

if g2 is None: g2 = 0
print(max(g1,g2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
def gcd(a, b):
    P = max(a, b)
    Q = min(a, b)
    R = P%Q
    P = Q
    Q = R
    while R != 0:
        R = P%Q
        P = Q
        Q = R
    return P

candidate = [(gcd(A[0], A[1]), True), (A[0], False), (A[1], False)]
for i in range(2, N):
    nxt_cand = []
    for v, b in candidate:
        if(v == 1):
            continue
        if(b == True and gcd(v, A[i]) < v):
            nxt_cand.append((v, False))
            nxt_cand.append((gcd(v, A[i]), True))
        else:
            nxt_cand.append((gcd(v, A[i]), b))
    if(len(nxt_cand)==0):
        nxt_cand = [(1, False)]
    candidate = nxt_cand[::]

print(max(candidate)[0])

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


l=[0]*N
r=[0]*N
l[0]=A[0]
r[0]=A[N-1]
for i in range(1,N-1):
    l[i]=gcd(l[i-1],A[i])
    r[i]=gcd(r[i-1],A[N-1-i])    
gcdlist = [1,l[N-2],r[N-2]]
for i in range(N-2):
    gcdlist.append(gcd(l[i],r[N-3-i]))

print(max(gcdlist))

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


def g():
  from functools import reduce
  n = int(input())
  vs = list(map(int, input().split()))
  
  while True:
    n = len(vs)
    
    if n < 10:
      mrr = 0
      for i, v in enumerate(vs):
        rr = reduce(gcd, vs[:i]+vs[i+1:])
        mrr = max(rr, mrr)
      return mrr
    
    v1 = vs[:n//2]
    v2 = vs[n//2:]
    
    r1 = reduce(gcd, v1)
    r2 = reduce(gcd, v2)
    if r1 == r2:
      return r1
    if r1 > r2:
      vs = v2
    else:
      vs = v1
  
  
print(g())

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, int(math.sqrt(m)) + 1):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False
        self.ds_memo = {}
        self.ds_memo[1] = set([1])

    def is_prime(self, n):
        return self.A[n]

    def division(self, n):
        d = collections.defaultdict(int)
        for c in self.T:
            while n % c == 0:
                d[c] += 1
                n //= c
            if n < 2:
                break
        if n > 1:
            d[n] += 1
        return d.items()

    
    def divisions(self, n):
        if n in self.ds_memo:
            return self.ds_memo[n]

        for c in self.T:
            if n % c == 0:
                rs = set([c])
                for cc in self.divisions(n // c):
                    rs.add(cc)
                    rs.add(cc * c)
                self.ds_memo[n] = rs
                return rs

        rs = set([n])
        self.ds_memo[n] = rs
        return rs

    def sowa(self, n):
        r = 1
        for k,v in self.division(n):
            t = 1
            for i in range(1,v+1):
                t += math.pow(k, i)
            r *= t
        return r


def main():
    n = I()
    a = sorted(LI())
    pr = Prime(10**9)
    p = collections.defaultdict(int)

    for b in a:
        for k in pr.divisions(b):
            p[k] += 1

    r = 1
    for k,v in p.items():
        if v >= n-1 and r < k:
            r = k

    return r


print(main())

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

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


a=sorted(a)

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



l=1
r=10**9
a.sort()
ans=[a[0],a[1]]
k=-1
nexts=0
stock=0
s_2=-1


ans=gcd(a[0],a[1])
for j in range(2,len(a)):
    if(stock!=2):
        s=gcd(ans,a[j])
        if(s<ans and stock==0):
            stock+=1
            nexts=s
        elif(s<ans and stock==1):
            if(s==nexts):
                stock-=1
                ans=s
            else:
                stock+=1
                s_2=s
                s_1=nexts
                
    else:
        s_1=gcd(s_1,a[j])
        s_2=gcd(s_2,a[j])
if(s_2==-1):
    ans1=ans
else:
    ans1=max(s_1,s_2)
    
ans=a[0]
for j in range(2,len(a)):
    ans=gcd(ans,a[j])
ans2=ans
ans=a[1]
for j in range(2,len(a)):
    ans=gcd(ans,a[j])

ans3=ans

    

print(max([ans1,ans2,ans3]))

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
anses=set()
for i in [0,1]:
    for j in range(1, int(a[i]**0.5)+1):
        if a[i] % j == 0:
            anses.add(j)
            if j != a[i] // j:
                anses.add(a[i] // j)
anses=sorted(anses,reverse=True)
for ans in anses:
    solvenum=0
    for j in a:
        if j%ans==0:
            solvenum+=1
    if solvenum==n or solvenum==n-1:
        print(ans)
        break

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

def gcd_array(A):
    n = len(A)
    s = A[0]
    i = 1
    Down = [0]
    while(i < n):
        t = gcd(s,A[i])
        if t < s:
            Down.append(i)
        s = t
        i += 1
    return s,Down

if __name__ == "__main__":
    N = int(input())
    B = list(map(int,input().split()))
    max = 0
    d,Down = gcd_array(B)
    C = [i//d for i in B]
    c = C[0]
    K = 1
    for j in Down:
        A = [i for i in C]
        c = A.pop(j)
        l = gcd_array(A)[0]
        if K < l:
            K = l
    print(K * d)