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.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

    # KeyInsight
    # 一つを書き換える→消すと同じ意味
    # 最大公約数の交換則
    # 左右から最大公約数を前計算
    # See:
    # https://www.youtube.com/watch?v=8lm8o8L9Bmw
    # http://drken1215.hatenablog.com/entry/2019/04/27/224100_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()

Submission status: Accepted

Solution code:
import fractions
from functools import reduce

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

#累積GCD
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)

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
#'init(a)': 配列aで初期化。O(N)
#'update(k,x)': a[k]をxに変更 O(logN)
#'query(p,q)': [p,q)について "segfunc" したものを返す O(logN)
#'ide_ele' : 単位元。単位元(ide_ele)は、区間外の値などに設定する値です。

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

#####segfunc######
def segfunc(x,y):
    return gcd(x,y)

def init(init_val):
    #set_val
    for i in range(n):
        seg[i+num-1]=init_val[i]
    #built
    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)は、区間外の値などに設定する値です。
#            ex) 最小値のセグ木 → +inf
#             　　和のセグ木 → 0
#　　             積のセグ木 → 1
#　　             gcdのセグ木 → 0
ide_ele = 0

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

#num:n以上の最小の2のべき乗
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)

Submission status: Accepted

Solution code:
import sys
import fractions

def solve(N: int, A: "List[int]"):
    A.sort()
    gcd_left = [A[0]] ##iまでのgcd
    for i in range(1,N):
        gcd_left.append(fractions.gcd(A[i],gcd_left[i-1]))
        
    gcd_right = [A[-1]] ##右からiまでのgcd
    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))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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))
#print(lgcd)
#print(rgcd)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def gcd(a, b):  # 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])

# print(toRight)
# print(toLeft)
    
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)

Submission status: Accepted

Solution code:
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])
#print(k)
#print(l)
ans=1
for i in range(n):
    ans=max(ans,gcd(l[i],r[n-i-1]))
    #iを除いたgcd
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#最大公約数を求める関数を再帰的に定義
def gcd(x,y):
	if x < y:
		x,y = y,x
	if y == 0:
		return x
	#一方が0の時、他方を最大公約数として定義
	return gcd(y,x % y)
	
N = int(input())
A = list(map(int,input().split()))

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

#リストの前と後ろから値を取得し
#A[i]を除いてA[i]以前と以降の最大公約数を算出
for i in range(N):
	L.append(gcd(L[i],A[i]))
	R.append(gcd(R[i],A[N-i-1]))


#i毎にA[i]を除いた最大公約数のリストを作成
for i in range(N):
	gcdlist.append(gcd(L[i],R[N-i-1]))

print(max(gcdlist))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
def gcd(a, b):
    if not a:
        # a が ゼロ ならば b
        return b
    if not b:
        # b が ゼロ ならば a
        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()

Submission status: Accepted

Solution code:
def gcd(a, b):
    if not a:
        # a が ゼロ ならば b
        return b
    if not b:
        # b が ゼロ ならば a
        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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]))  # 0, gcd(0,A[0]), gcd(0,A[0],A[1]) , ...
    R.append(gcd(R[i], A[N - i -1])) # 0 , gcd(0, A[N-1]), gcd(0, A[N-1], A[N-2]))..

R.reverse() # R is gcd(0,A[N-1]...A[0]), gcd(0,A[N-1]...A[1]),....
# pairs are 
#  L     :R
#  0    : (0,A[N-1],A[n-2], ... A[1])
#  A[0] : (0,A[N-1],A[n-2], ... A[2])
#  ...
#  A[0,1,..N-3] :(0,A[N-1]
#  A[0,1,..N-2] :(0)
print(max( (gcd(L[i], R[i+1]) for i in range(N))))

Submission status: Accepted

Solution code:
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)]))

Submission status: Accepted

Solution code:
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)]))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

# print('H', H)
# print('T', T)

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

Submission status: Accepted

Solution code:
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])
#	print(ansb)
	listb.append(ansb)

#print(int(max(list_ans)))
ansc= lista[n-1]

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


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

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

    # divisors.sort()
    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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)
#print(right)
#print(left)
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)
'''

Submission status: Accepted

Solution code:
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-1-i]=gcd(a[n-i],right[n-i])

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

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

for i in range(1,n+1):
    L[i] = gcd(L[i-1],a[i-1])
for i in range(0,n)[::-1]:
    R[i] = gcd(R[i+1],a[i])
    
gcd_ans=[]
for i in range(n):
    gcd_ans.append(gcd(L[i],R[i+1]))
        
print(max(gcd_ans))

Submission status: Accepted

Solution code:
import copy

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

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

# 1つの数値を順に取り除いて、残りの最大公約数を求める
l_gcd_list = [A[0]]
r_gcd_list = [A[-1]]
gcd_list = []

# 左右端を抜く場合は後で計算
for i in range(1,N-1):
    l_gcd_list.append(gcd(l_gcd_list[-1], A[i]))
    r_gcd_list.append(gcd(r_gcd_list[-1], A[-i-1]))

# 左右端以外のi番目の数値を抜いた場合
for i in range(1,N-1):
    gcd_list.append(gcd(l_gcd_list[i-1],r_gcd_list[-i-1]))

# 左右どちらかの端の数値を抜いたとき
gcd_list.append(l_gcd_list[-1])
gcd_list.append(r_gcd_list[-1])
    
print(max(gcd_list))

Submission status: Accepted

Solution code:
import math
n = int(input())
a = []
a.append(list(map(int, input().split())))
def gcd(x,y):
  if y == 0:
    return x
  return gcd(y, x % y)

pow_2 = 1
exp = 0
while pow_2 < n:
  pow_2 *= 2
  exp += 1

a[0].extend([0]*(pow_2 - n))
for i in range(1, exp):
  a.append([])
  for j in range(int(pow_2 / math.pow(2, i))):
    a[i].append(gcd(a[i-1][2*j], a[i-1][2*j+1]))

ans = [0]*n
for i in range(n):
  g = 0
  for j in range(0,exp):
    if (i >> j) % 2 == 0:
      bit = 1
    else:
      bit = -1
    g = gcd(a[j][(i>>j)+bit], g)
  ans[i] = g
print(sorted(ans)[n-1])

Submission status: Accepted

Solution code:
import fractions as math

n = int(input()) # 入力データの個数
a = list(map(int, input().split())) # 入力データ
 
l = [a[0] for _ in range(n+2)] # データの左端を初期値として追加
r = [a[-1] for _ in range(n+2)] # データの右端を初期値として追加
 
for i in range(n): # データのどこを置き換えるか
    l[i+1] = math.gcd(l[i], a[i]) # 置きかえる部分より左の最大公約数と置きかえる部分の最大公約数を更新する
    r[-(i+2)] = math.gcd(r[-(i+1)], a[-(i+1)]) # 置きかえる部分より右の最大公約数と置きかえる+1の最大公約数を更新する 
l[0] = r[0] = l[-1] = r[-1] = 0 # 初期値形に0を入れていく
 
ans = [math.gcd(l[i], r[i+2]) for i in range(n)] # 最大公約数のセットを改めて配列へ
print(max(ans)) # 最大公約数を出力

Submission status: Accepted

Solution code:
def gcd(a, b):
    if b==0:
        return a
    while b>0:
        a, b = b, a%b
    return a
N = int(input())
A = list(map(int, input().split()))
S_left = [0 for _ in range(N+1)]
S_right = [0 for _ in range(N+1)]
for i in range(N):
    S_left[i+1] = gcd(S_left[i], A[i])
    S_right[i+1] = gcd(S_right[i], A[-i-1])
S_right = S_right[::-1]
S_left = S_left + [0]
S_right = [0] + S_right
ans = 0
for i in range(1, N+1):
    ans = max(ans, gcd(S_left[i-1], S_right[i+1]))
print(ans)

Submission status: Accepted

Solution code:
# ABC125C - GCD on Blackboard
from fractions import gcd


class SegmentTree:
    def __init__(
        self,
        init_val: "initial value: Array or str",
        segfunc: "operation unique in case",
        ide_ele: "identity element corresponding init_val" = 0,
    ):
        self.segfunc = segfunc
        self.size = 1 << (len(init_val) - 1).bit_length()
        self.ide_ele = ide_ele
        self.tree = self._build(init_val)

    def _build(self, init_val: "Array or str"):
        tree = [self.ide_ele] * (2 * self.size)

        for idx, val in enumerate(init_val):  # set
            # modify val if needed (e.g. str -> ord())
            tree[idx + self.size - 1] = val

        for idx in range(self.size - 2, -1, -1):  # build
            tree[idx] = self.segfunc(tree[2 * idx + 1], tree[2 * idx + 2])
        return tree

    def update(self, idx: int, val):
        idx += self.size - 1
        # modify val if needed as same as in _build()
        self.tree[idx] = val
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = self.segfunc(
                self.tree[2 * idx + 1], self.tree[2 * idx + 2]
            )

    def query(self, left: int, right: int):
        if left >= right:
            return self.ide_ele

        left += self.size
        right += self.size
        ret = self.ide_ele
        while left < right:
            if left & 1:
                ret = self.segfunc(ret, self.tree[left - 1])
                left += 1
            if right & 1:
                right -= 1
                ret = self.segfunc(ret, self.tree[right - 1])
            left >>= 1
            right >>= 1
        return ret


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from fractions import gcd
N = int(input())
array = list(map(int,input().split()))
all_gcd = [0]*N
#calc from start to end
gcd_L = [array[0]]
for i in range(N-1):
  gcd_L.append(gcd(gcd_L[-1],array[i+1]))
  
#calc from end to start
gcd_R = [array[-1]]
for j in range(N-1,0,-1):
  gcd_R.append(gcd(gcd_R[-1],array[j-1]))
  
gcd_arr = []
  
for idx in range(N):
  if idx==0:
    gcd_arr.append(gcd_R[-2])
  elif idx==N-1:
    gcd_arr.append(gcd_L[-2])
  else:
    gcd_arr.append(gcd(gcd_L[idx-1],gcd_R[N-1-idx-1]))
print(max(gcd_arr))

Submission status: Accepted

Solution code:
class SegmentTree:
    def __init__(self, seq, func="min"):
        if func=="min":
            self.f = min
            self.e = float('inf')
        if func=="max":
            self.f = max
            self.e = -float('inf')
        if func=="gcd":
            self.f = gcd
            self.e = 0
        self._n = len(seq)
        self.n_seg = 2**(self._n-1).bit_length()
        self.seg = [self.e]*2*self.n_seg
        
        for i in range(self._n):
            self.seg[self.n_seg+i-1] = seq[i]
            
        for i in range(self.n_seg-2, -1, -1):
            self.seg[i] = self.f(self.seg[2*i+1], self.seg[2*i+2])
        
    def update(k, val):
        k += self.n_seg -1
        self.seg[k] = val
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.f(self.seg[k*2+1], self.seg[k*2+2])
        
    def query(self, a, b):
        if b<=a:
            return self.e
        a += self.n_seg-1
        b += self.n_seg-2
        res = self.e
        while b-a>1:
            if a&1==0:
                res = self.f(res, self.seg[a])
            if b&1==1:
                res = self.f(res, self.seg[b])
                b -= 1
            a = a//2
            b = (b-1)//2
        if a==b:
            res = self.f(res, self.seg[a])
        else:
            res = self.f(self.f(res, self.seg[a]), self.seg[b])
        return res
    
    def __repr__(self):
        ret = str(self.seg[self._n-1:])
        return ret

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

if __name__=="__main__":
    N = int(input())
    A = list(map(int, input().split()))
    A_seg = SegmentTree(A, func="gcd")
    ans = -1
    for i in range(N):
        ans = max(ans, gcd(A_seg.query(0, i), A_seg.query(i+1, N)))
    print(ans)