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

Solution (Python):
import copy

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


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


def calc(B):
    lo, hi = 1, 10 ** 9 + 1
    while abs(hi - lo) > 1:
        X = copy.deepcopy(B)
        m = (hi + lo) // 2

        skipped = False
        while len(X) > 1:
            a, b = X.pop(), X.pop()
            GCD = gcd(a, b)
            if GCD < m:
                if skipped is False:
                    skipped = True
                    X.append(a)
                    continue
                else:
                    hi = m
                    break
            X.append(GCD)
        else:
            if X[0] >= m:
                lo = m
            else:
                hi = m
    return lo


ans1 = calc(A)
ans2 = calc(A[::-1])

print(max(ans1, ans2))

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

input()
a = list(map(int, input().split()))
b = list(accumulate(a, gcd))
c = list(accumulate(reversed(a), gcd))
print(max(b[-2], c[-2], *map(gcd, b[:-2], c[-3::-1])))

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

A.sort()
flag = 0

for i in reversed(range(int(A[1])+1)):
    for k in range(N):
        if A[k]%i != 0:
            flag+=1
            if flag >1:
                flag = 0
                break
        if k >= N-1:
            print(i)
            exit()

Solution (Python):
'''
import math

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

def gcd_list(arr):

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

    
    return divisors

N = int(input())

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

af = A[0]
ag = A[1]

F = make_divisors(af)
G = make_divisors(ag)
F.sort(reverse=True)
G.sort(reverse=True)
del F[-1]
del G[-1]

'''
print(F)
print(G)
'''

ansf = 1
ansg = 1

for i in F:
    count = 0
    for j in A:
        if j % i != 0:
            count += 1
            if count == 2:
                break

    if count == 1:
        ansf = i
        break
    if count == 0:
        ansf = i
        break

for i in G:
    count = 0
    for j in A:
        if j % i != 0:
            count += 1
            if count == 2:
                break

    if count == 1:
        ansg = i
        break

print(max(ansf,ansg))
'''
g = gcd_list(A)
print(g)

for i in range(N):
    tmp = A.pop(0)
    if g < gcd_list(A):
        g = gcd_list(A)
    A.append(tmp)

print(g)
'''

Solution (Python):

 
from fractions import gcd
 
def all_gcd(numbers):
    result = numbers[0]
    for v in numbers:
        result = gcd(result, v)
    return result
 
def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    x = []
    for i in range(n):
        prev = 0 if i == 0 else x[i - 1]
        x.append(gcd(a[i], prev))
    
    y = [0] * n
    y[n-1] = a[n-1]
    for i in range(2, n + 1):
        y[n-i] = gcd(y[n-i+1], a[n-i])

    max_gcd = 0
    for i in range(n):
        if i == 0:
            now_gcd = y[1]
        elif i == n-1:
            now_gcd = x[n-2]
        else:
            now_gcd = gcd(x[i-1], y[i+1])
        if now_gcd > max_gcd:
            max_gcd = now_gcd
    print(max_gcd)
 
if __name__ == '__main__':
    main()

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

Solution (Python):
a = input()
l = map(int, raw_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)

    divisors.sort()
    return divisors

def gcd(l):
	l = sorted(l, reverse=True)
	l1 = make_divisors(l[0])
	l2 = make_divisors(l[1])
	l_mix = list(set(l1 + l2))
	l_mix = sorted(l_mix)
	r = 0
	for i in l_mix:
		f = 0
		for j in l:
			if j % i != 0:
				f += 1
			if f > 1:
				break
		if f <= 1:
			r = i
	return r 

print gcd(l)

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

import fractions
from functools import reduce

def gcds(numbers):
  return reduce(fractions.gcd,numbers)
def gcd2(*numbers):
  return reduce(fractions.gcd,numbers)
  

mx = 0

lisgcd = [0]*n
rlisgcd = [0]*n

lisgcd[0] = lis[0]
rlisgcd[n-1] = lis[n-1]

for i in range(n-1):
  lisgcd[i+1] = gcd2(lisgcd[i],lis[i])
  
for i in range(n-2,-1,-1):
  rlisgcd[i] = gcd2(rlisgcd[i+1],lis[i])
  
for i in range(n):
  if i == 0:
    if mx < rlisgcd[1]:
      mx = rlisgcd[1]
  elif i == n-1:
    if mx < lisgcd[n-1]:
      mx = lisgcd[n-1]
  else:
    t = gcd2(lisgcd[i],rlisgcd[i+1])
    if mx < t:
      mx = t
print(mx)

Solution (Python):
N = int(input())
A = list(map(int,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(reverse=True)
    return divisors

divisors = make_divisors(A[-1])
answer = 1
for d in divisors:
  NG = 0
  for a in A:
    if a % d != 0:
      NG += 1
    if NG > 1:
      break
  if NG <= 1:
    answer = d
    break
    
divisors = make_divisors(A[-2])
for d in divisors:
  NG = 0
  for a in A:
    if a % d != 0:
      NG += 1
    if NG > 1:
      break
  if NG <= 1:
    if d > answer:
      print(d)
    else:
      print(answer)
    break

Solution (Python):
import fractions

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

if N == 2:
  print(max(A))
else:
  ans = [A[1], A[0]]
  for i in range(2,N):
    ans_0 = max(fractions.gcd(ans[0], A[i]),fractions.gcd(ans[1], A[i]))
    ans_1 = fractions.gcd(ans[0], ans[1])
    ans = [ans_0, ans_1]
  print(max(ans))

Solution (Python):
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
GA=[a[0]]
GB=[a[n-1]]
G=gcd(a[0],a[1])
GA.append(G)
for i in range(2,n-1):
  G=gcd(G,a[i])
  GA.append(G)
G=gcd(a[n-1],a[n-2])
GB.append(G)
for j in range(n-3,0,-1):
  G=gcd(G,a[j])
  GB.append(G)
GB.reverse()
ans=[]
for i in range(len(GA)-1):
  ans.append(gcd(GA[i],GB[i+1]))
ans.append(GA[len(GA)-1])
ans.append(GB[0])
if n!=2:
  print(max(ans))
else:
  print(max(a))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
s=[[0]for _ in range(n)]

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

l.sort()
ans=1

b=l[1]
bl=make_divisors(b)
for i in bl:
    check=True
    for j in range(1,n):
        if l[j]%i!=0:
            check=False
            break
    if check:
        ans=max(ans,i)
        break
a=min(l)
al=make_divisors(a)

for i in al:
    cnt=0
    for j in range(n):
        if l[j]%i!=0:
            cnt+=1
        if cnt>1:
            break
    if cnt==0 or cnt==1:
        ans=max(ans,i)
        break
print(ans)

Solution (Python):

n = int(input())
a = [int(m) for m in input().split()]
a.sort()
m0 = a[0]
m1 = a[1]
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



mm0 = make_divisors(m0)
mm1 = make_divisors(m1)
mm = mm0 + mm1
mm.sort()
si = 0
candi = 0
for g in mm:
    si = 0
    for i in range(n):
        if a[i] % g != 0:
            si += 1
            if si == 2:
                break
    else:
        candi = g
print(candi)

Solution (Python):
def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return a

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

l=[0]*N
l[0]=a[0]

for i in range(1,N):
    l[i]=egcd(l[i-1],a[i])

r=[0]*N
r[N-1]=a[N-1]
for i in range(N-2,-1,-1):
    r[i]=egcd(r[i+1],a[i])

ans=0
for i in range(N):
    if i==0: tmp=r[1]
    elif i==N-1: tmp=l[N-2]
    else: tmp=egcd(l[i-1],r[i+1])
    ans=max(tmp,ans)

print(ans)

Solution (Python):
from functools import reduce
read = input
n = int(read())
A = list(map(int, read().split()))
def gcd(a, b):

    if a > b:
        a,b = b,a
    return a if b %a ==0 else gcd(b%a, a)
def cal(ac, x):
    if len(ac) == 0:
        ac.append(x)
        return ac
    nxt = gcd(ac[-1], x)
    ac.append(nxt)
    return ac
A1 = list(reduce(cal, A, []))
A.reverse()
A2 = list(reduce(cal, A, []))
A2.reverse()
mx = 1
for i in range(n):
    if i == 0:
        mx =max(A2[ 1 ], mx)
        continue
    elif i == n-1:
        mx = max(A1[ -2 ], mx)
        continue
    else: 
        nxt = gcd(A1[i-1], A2[i+1])
        mx = max(nxt, mx)
print(mx)

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

if n == 2:
    print(max(a[0], a[1]))
    exit()

noskip = gcd(a[0], a[1])
skipped = max(gcd(a[0], a[2]), gcd(a[1], a[2]))

for i in range(2, n):
    skipped = max(gcd(skipped, a[i]), noskip)
    noskip = gcd(noskip, a[i])

print(max(noskip, skipped))

Solution (Python):
import math

words = lambda t : list(map(t, input().split()))


def gcd (a,b):
    if a > b:
        t = b
        b = a
        b = t
    v = b
    while (a % b != 0):
        t = a % b
        a = b
        b = t
        v = b
    return v


n = int(input())
a = words(int)

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

else:
    cgcdm1 = max(gcd(a[0],a[1]),gcd(a[1],a[2]),gcd(a[2],a[0]))
    allgcd = gcd(a[0],gcd(a[1],a[2]))

    for i in range(3,len(a)):
        cgcdm1 = max(gcd(cgcdm1,a[i]),allgcd)
        allgcd = gcd(allgcd, a[i])

    print(cgcdm1)


def gcd (a,b):
    if a > b:
        t = b
        b = a
        b = t
    v = b
    while (a % b != 0):
        t = a % b
        a = b
        b = t
        v = b
    return v

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):
def GCD(x,y):
    if x==0:
        return y 
    elif y==0:
        return x
    elif x>y:
        return GCD_ordered(x,y)
    else:
        return GCD_ordered(y,x)

def GCD_ordered(x,y):
    z=x%y
    if z!=0:
        return GCD_ordered(y,z)
    if z ==0:
        return y




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

tmp_forward=A[0]
tmp_backward=A[-1]
gcd_forward=[0,tmp_forward]
gcd_backward=[0,tmp_backward]

for i in range(1,N):
    tmp_forward = GCD(tmp_forward,A[i])
    gcd_forward.append(tmp_forward)
    
    tmp_backward = GCD(tmp_backward,A[-i-1])
    gcd_backward.append(tmp_backward)






ans=0

for i in range(0,N):
    tmp_gcd = GCD(gcd_forward[i],gcd_backward[(N-1)-i])

    if ans < tmp_gcd:
        ans = tmp_gcd
      
print(ans)

Solution (Python):
from fractions import gcd
N = int(input())
a = list(map(int, input().split()))
N0 = 2**(N-1).bit_length()
INF = 2**31-1
data = [INF]*(2*N0+1)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        if data[2*k+1] == INF:
            data[k] = data[2*k+2]
        elif data[2*k+2] == INF:
            data[k] = data[2*k+1]
        else:
            data[k] = gcd(data[2*k+1], data[2*k+2])

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

ans = 0
for i in range(N):
    update(i, INF)
    ans = max(ans, data[0])
    update(i, a[i])

print(ans)

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
from fractions import gcd
gcd_all=gcd(a[0],a[1])
gcd_eli=[a[0],a[1]]
for i in range(2,n):
    temp0=gcd(gcd_eli[0],a[i])
    temp1=gcd(gcd_eli[1],a[i])
    gcd_eli=sorted([temp0,temp1,gcd_all])[1:]
    gcd_all=gcd(gcd_all,a[i])
print(max(gcd_all,max(gcd_eli)))

Solution (Python):
import fractions

from functools import reduce


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


def main():
    result = sub()
    print(result)


def sub():
    N = int(input())
    As = [int(i) for i in input().split()]

    lh_gcds = [0] * N
    for i in range(N):
        if i == 0:
            lh_gcds[i] = As[i]
        else:
            lh_gcds[i] = fractions.gcd(lh_gcds[i-1], As[i])

    rh_gcds = [0] * N
    for i in range(N-1, -1, -1):
        if i == N-1:
            rh_gcds[i] = As[i]
        else:
            rh_gcds[i] = fractions.gcd(rh_gcds[i+1], As[i])

    max_gcd = 1
    for i in range(N):
        if i == 0:
            max_gcd = max(max_gcd, rh_gcds[1])
        if i == N-1:
            max_gcd = max(max_gcd, lh_gcds[-2])
        else:
            max_gcd = max(max_gcd, fractions.gcd(lh_gcds[i-1], rh_gcds[i+1]))
    return max_gcd


if __name__ == '__main__':
    main()

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

    
    return divisors


n = int(input())
a = input().split()

A = [int(element) for element in a]

A.sort()

a_min = A[0]
a_min2 = A[1]


a_min_divs = make_divisors(a_min)
a_min2_divs = make_divisors(a_min2)

a_min_divs.remove(1)
a_min2_divs.remove(1)

max_div = 1

results = [1]


for diver in a_min_divs:
    thr = 0
    for num in A:
        
        
        
        if num % diver != 0:
            thr = thr+1
            if thr > 1:

                break
            else:
                continue
    if thr < 2:
        results.append(diver)

for diver in a_min2_divs:
    thr = 0
    for num in A:
        
        
        
        if num % diver != 0:
            thr = thr+1
            if thr > 1:

                break
            else:
                continue
    if thr < 2:
        results.append(diver)



print(max(results))

Solution (Python):

def main():
    N = int(input())
    A = [int(n) for n in input().split()]
    fore = [0]*N
    back=[0]*N
    fore[0] = A[0]
    back[-1] = A[-1]
    for i in range(1, N):
        fore[i] = gcd(fore[i-1], A[i])
    for i in reversed(list(range(0, N-1))):
        back[i] = gcd(back[i+1], A[i])
    maximum = 0
    for i in range(N):
        f = fore[i-1] if i != 0 else 0
        b = back[i+1] if i != N-1 else 0
        m = gcd(f, b)
        maximum = max(m, maximum)
    print(maximum)
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
main()

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

    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 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()
    ac = collections.Counter(LI())
    if n == 2:
        return max(ac.keys())
    a = []
    aa = []
    for k,v in ac.items():
        if v > 1:
            aa.append(k)
        else:
            a.append(k)

    tr = -1
    if len(aa) > 0:
        tr = aa[0]
        for k in aa[1:]:
            tr = fractions.gcd(k,tr)

    l = len(a)
    if l < 2:
        return tr
    if l < 3:
        r = 1
        for i in a:
            kr = fractions.gcd(i,tr)
            if r < kr:
                r = kr
        return r

    m = max(a)
    lm = m // (l-1)
    pr = Prime(10**9)
    p = collections.defaultdict(int)

    for b in a:
        r = [1]
        for k, v in pr.division(b):
            for c in r[:]:
                for i in range(1,v+1):
                    t = c*(k**i)
                    if t > lm:
                        break
                    r.append(t)
        for k in r:
            p[k] += 1



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

    r = 1
    for k,v in p.items():
        kk = fractions.gcd(tr,k)
        if v >= l-1 and r < kk:
            r = kk

    return r


print(main())

Solution (Python):

"""
Created on Sat Apr 27 20:57:32 2019

@author: Yamazaki Kenichi
"""
import sys

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

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

if N == 2:
    print(max(A[0],A[1]))
    sys.exit()

tmp1 = eg(A[0],eg(A[1],A[2]))
tmp2 = max(eg(A[0],A[1]),eg(A[1],A[2]),eg(A[0],A[2]))

T = [[1,1,1] for i in range(N)]
T[1][0],T[1][1],T[1][2] = tmp1, tmp2, tmp2
for i in range(2,N):
    T[i][0] = eg(T[i-1][0],A[i])
    T[i][1] = T[i-1][0]
    T[i][2] = max(eg(T[i-1][2],A[i]),eg(T[i-1][1],A[i]))
print(max(T[N-1]))

Solution (Python):
import fractions
n = int(input())
listA = list(map(int, input().split()))
listA.sort()
if len(listA) < 3:
    print(max(listA))
else:
    minGcd1 = fractions.gcd(listA[0], listA[1])
    minGcd2 = fractions.gcd(listA[1], listA[2])
    minGcd3 = fractions.gcd(listA[0], listA[2])
    ming1 = minGcd1
    maxg1 = 1
    while ming1 > 1:
        count=0
        flag=0
        index1=0
        while index1 < len(listA):
            if listA[index1]%ming1==0:
                pass
            else:
                count+=1
            if count >1:
                flag=1
                break
            index1+=1
        if flag:
            ming1-=1
        else:
            maxg1 = ming1
            break
    ming2 = minGcd2
    maxg2 = 1
    while ming2 > 1:
        count = 0
        flag = 0
        index1 = 0
        while index1 < len(listA):
            if listA[index1] % ming2==0:
                pass
            else:
                count += 1
            if count > 1:
                flag = 1
                break
            index1 += 1
        if flag:
            ming2 -= 1
        else:
            maxg2 = ming2
            break
    ming3 = minGcd3
    maxg3= 1
    while ming3 > 1:
        count = 0
        flag = 0
        index1 = 0
        while index1 < len(listA):
            if listA[index1] % ming3==0:
                pass
            else:
                count += 1
            if count > 1:
                flag = 1
                break
            index1 += 1
        if flag:
            ming3 -= 1
        else:
            maxg3 = ming3

            break
    print(max(maxg1, maxg2, maxg3))

Solution (Python):
import math
import numpy as np

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 set_list(es):
  return len(list(set(es))) == 1
  
n = int(input())
n_list = [int(i) for i in input().split()] 

if set_list(n_list):
    print(n_list[0])
else:
  min1 = min(n_list)
  n_list.remove(min1)
  min2 = min(n_list)
  n_list.append(min1)

  div_list = make_divisors(min1) + make_divisors(min2)
  div_set = set(div_list)
  div_list = sorted(list(div_set))

  k = 0
  length = len(div_set)
  while k < length: 
    s = div_list[length - k - 1]
    count = 0
    j = 0
    for x in n_list:
      if n_list[j] % s == 0:
        count += 1
      j += 1
    if count >= n - 1:
      print(s)
      break
      
    k += 1

Solution (Python):
from math import sqrt
from sys import exit
N=int(input())
A=list(map(int,input().split()))

if len(A)==2:
    print(max(A))
    exit()

def gcd(a,b):
    if a<b: a,b=b,a
    if a%b==0: return b
    return gcd(b,a%b)
    
g=gcd(A[0],A[-1])

p=[]

for i in range(1,int(sqrt(g)+1)):
    if g%i==0:
        if i*i==g: p.append(i)
        else: p+=[i,g//i]
        
def ok(n):
    f=False
    for a in A[1:-1]:
        if a%n:
            if f: return False
            f=True
    return True
    
def GCD(L):
    if len(L)==1: return L[0]
    ans=gcd(L[0],L[1])
    if len(L)>2:
        for l in L[2:]:
            ans=gcd(ans,l)
    return ans

p.sort(reverse=True)
for num in p:
    if ok(num):
        ans=num
        break
p1=GCD(A[1:-1])
print(max([ans,gcd(p1,A[0]),gcd(p1,A[-1])]))

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

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

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

GG2=[0]*N
GG2[-1]=A[-1]

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

ans=[0]*N

ans[0]=GG2[1]
ans[-1]=GG[N-2]





temp=ans[0]
for i in range(1,N-1):
    ans[i]=gcd(GG[i-1],GG2[i+1])
    if ans[i]>temp:
        temp=ans[i]
if ans[-1]>temp:
    temp=ans[-1]


print(temp)

Solution (Python):


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

m = 1
l = 0
while m < n:
    m *= 2
    l += 1
tr = [0] * (2 * m)

tr[m:m + n] = a


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


def init_tr():
    for d in range(l - 1, -1, -1):
        for i in range(1 << d, 1 << (d + 1)):
            tr[i] = gcd(tr[i * 2], tr[i * 2 + 1])


def upd_tr(j):
    i = j // 2
    if i == 0:
        return
    tr[i] = gcd(tr[i * 2], tr[i * 2 + 1])
    upd_tr(i)


init_tr()

ans = 0

for i in range(n):
    tmp = tr[m + i]
    tr[m + i] = 0
    upd_tr(m + i)
    ans = max(ans, tr[1])
    tr[m + i] = tmp
    upd_tr(m + i)

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
def divi(a, b):
    rem = b % a
    if rem == 0:
        return a
    else:
        return divi(rem, a)
A.sort()
all_d = divi(A[0], A[1])
max_d = max(all_d, max(divi(A[1],A[2]), divi(A[0],A[2]))) if N > 2 else A[1]
for i in range(2,N):
    d = divi(all_d, A[i])
    max_d = divi(max_d, A[i])
    if max_d < all_d:
        max_d = all_d
    all_d = d
print(max_d)

Solution (Python):
from collections import*
from functools import*
from fractions import*
def dv(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 Counter(divisors)

n,*a=map(int,open(0).read().split())
if n<1000:
    ans=Counter()
    for i in range(n):
        ans+=dv(a[i])
    l=[]
    for k,v in ans.items():
        if v==n-1 or v==n:
            l.append(k)
    print(max(l))
else:
    print(max(reduce(gcd,a[n//4:]),reduce(gcd,a[:n//4]+a[n//2:]),reduce(gcd,a[:n//2]+a[3*n//4:]),reduce(gcd,a[:3*n//4])))

Solution (Python):
n = int(input())
a = list(map(int,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
divs = list(set(make_divisors(a[0])+make_divisors(a[1]))) 

divsTF = [0]*len(divs)
for i in range(len(divs)):
    j = [1 if a[_] % divs[i] else 0 for _ in range(n) ]

    if sum(j) <= 1:
        divsTF[i] = 1

ans = 1
for i in range(len(divs)):
    if divsTF[i] == 1:
        ans = max(ans,divs[i])
print(ans)

Solution (Python):
n = int(input())
arr = sorted(list(map(int,input().split())))
arr1 = arr[::-1]
dp = [[0]*2 for i in range(n)]
dp1 = [[0]*2 for i in range(n)]

dp[0][0] = arr[0]
dp[0][1] = arr[1]
dp1[0][0] = arr1[0]
dp1[0][1] = arr1[1]

def gcd(a,b):
    x = max(a,b)
    y = min(a,b)
    if x%y:
        return gcd(y,x%y)
    else:
        return y
for i in range(n-1):
    dp[i+1][0] = gcd(dp[i][0],arr[i+1])
    dp[i+1][1] = max(dp[i][0],gcd(dp[i][1],arr[i+1]))
    dp1[i+1][0] = gcd(dp1[i][0],arr1[i+1])
    dp1[i+1][1] = max(dp1[i][0],gcd(dp1[i][1],arr1[i+1]))
print(max(dp[-1][0],dp[-1][1],dp1[-1][0],dp1[-1][1]))

Solution (Python):
from functools import reduce
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
def gcd_list(numbers):
    return reduce(gcd, numbers)

n = int(input())
a = list(map(int, input().split()))
if n == 2:
  print(max(a))
else:
  p = gcd_list(a[:3])
  q = max([gcd(a[0], a[1]), gcd(a[0], a[2]), gcd(a[1], a[2])])
  for i in range(3, n):
    q = max([p, gcd(q, a[i])])
    p = gcd(p, a[i])
    
  print(q)

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

n = int(input())
vals = [int(x) for x in input().split(" ")]
vals.sort()
vals.reverse()

rewrite = False

minindex = 0

minval = vals[0]
for b in vals[1:]:
    temp = b
    a = minval
    tempmin = minval
    
    minval = gcd(a, b)
    if minval < tempmin:
        minindex = temp

if len(vals) > 2 and vals[1] == minindex:
    
    v1 = gcd(vals[0], vals[2])
    v2 = gcd(vals[1], vals[2])
    if v1 < v2:
        minindex = vals[0]

if minindex == vals[0]:
    minval = vals[1]
else:
    minval = vals[0]

jump = False

for b in vals[1:]:
    if not jump and b == minindex:
        jump = True
        continue
    a = minval
    minval = gcd(a, b)

print(minval)

Solution (Python):


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

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

divisors = list(set(make_divisors(A_list[0]) + make_divisors(A_list[1])))
divisors.sort(reverse=True)

counter = 0
for divisor in divisors:
    for A in A_list:
        if A % divisor != 0:
            counter += 1
        if counter > 1:
            break
    if counter <= 1:
        print(divisor)
        break
    else:
        counter = 0

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

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

A.sort()

l = [0]
m = A[0]
for i in range(1, N):
    tmp = gcd(m, A[i])
    if tmp < m:
        l.append(i)
    m = tmp
    

max_num = m
for l_i in l:
    b = A.copy()
    b.pop(l_i)
    x = b[0]
    for i in range(1, N-1):
        
        x = gcd(x, b[i])
    
    max_num = max(max_num, x)

print(max_num)

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

Solution (Python):
def euclidean(x, y):
    b = max(x, y)
    s = min(x, y)
    while True:
        ns = b % s
        if ns == 0:
            return s
        b = s
        s = ns

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

W = set()
for n in range(N):
    if n == 0:
        v = A[n]
    elif n == 1:
        nW = set()
        nW.add(v)
        nW.add(A[n])
        v = euclidean(v, A[n])
        W = nW
    else:
        nW = set()
        nW.add(v)
        for w in W:
            nW.add(euclidean(w, A[n]))
        v = euclidean(v, A[n])
        W = nW
print(max(W))

Solution (Python):

import sys
from functools import reduce


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

def gcd_group(A):
    return reduce(gcd, A)


def solve(N: int, A: "List[int]"):
    left = [A[0]] 
    right = [A[N-1]] 
    for i in range(1, N):
        left.append(gcd(left[-1], A[i]))
        right.append(gcd(right[-1], A[-i-1]))
    right.reverse()

    ret = 0
    for i in range(N):
        if i == 0:
            temp = right[1]
        elif i == N-1:
            temp = left[N-2]
        else:
            temp = gcd(left[i-1], right[i+1])

        if temp > ret:
            ret = temp
    print(ret)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
import fractions
from functools import reduce

n = int(input())

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




def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


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

gcd_r.reverse()

ans = max(gcd_r[1],gcd_l[-2])
for i in range(1,n-1):
    gcd = fractions.gcd(gcd_l[i-1],gcd_r[i+1])
    ans = max(ans,gcd)

print(ans)

Solution (Python):
from fractions import gcd
n = int(input())
a = list(map(int,input().split()))
mae = [a[0]]
for i in range(n-1):
    mae.append(gcd(mae[i],a[i+1]))
ushiro = [a[n-1]]
for i in range(n-1):
    ushiro.append(gcd(ushiro[i],a[n-i-2]))

ans = ushiro[n-2]

for i in range(n-1):
    if i != n-2:
        ans = max(ans,gcd(mae[i],ushiro[n-i-3]))
    else:
        ans = max(ans,mae[n-2])

print(ans)