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:
n = int(input())
a = list(map(int,input().split()))
ans1 = a[1]
for i in range(1,n):
  big,sml = a[i],ans1
  while sml != 0:
    big,sml = sml,big%sml
  ans1 = big
ans2 = a[-2]
for i in range(n-1):
  big,sml = a[i],ans2
  while sml != 0:
    big,sml = sml,big%sml
  ans2 = big
l1 = [-1]
now = a[0]
for i in range(1,n):
  big,sml = now,a[i]
  while sml != 0:
    big,sml = sml,big%sml
  l1.append(now // big)
  now = big
l2 = [-1]
now = a[-1]
for i in range(n-2,-1,-1):
  big,sml = now,a[i]
  while sml != 0:
    big,sml = sml,big%sml
  l2.append(now // big)
  now = big
l2.reverse()
l3 = [-1]
for i in range(1,n-1):
  big,sml = l1[i],l2[i]
  while sml != 0:
    big,sml = sml,big%sml
  l3.append(big)
l3.append(-1)
now = a[0]
for i in range(n):
  big,sml = now,a[i]
  while sml != 0:
    big,sml = sml,big%sml
  now = big
ans3 = now * max(l3)
#print(now)
#print(l1)
#print(l2)
#print(l3)
#print(ans1,ans2,ans3)
print(max(ans1,ans2,ans3))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]

A.sort()

def divisor(n):
    L = [] 
    i = 1
    while i <= n**0.5:
        if i == n**0.5:
            L.append(i)
        if n%i == 0:
            L.append(i)
            L.append(n/i)
        i += 1
    L.sort(reverse=True)
    return L

List = divisor(A[0]) + divisor(A[1])
List.sort(reverse=True)

for i in range(len(List)):
    c = 0
    for j in range(N):
        if A[j]%List[i] == 0:
            c += 1
    if c >= N-1:
        ans = List[i]
        break

print(int(ans))

Submission status: Accepted

Solution code:
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
L = [0]
for i in range(1, N):
    L.append(gcd(A[i-1], L[i-1]))
R = [0]
for i in range(1, N):
    R.append(gcd(R[i-1], A[N-i]))
R.reverse()
ans = []
for i in range(0, N):
    ans.append(gcd(L[i], R[i]))
print(max(ans))

Submission status: Accepted

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

def myGCD(a,b):
    if a == 0:
        return b
    while b!=0:
        a,b = b,a%b
    return a

L=[]
R=[]
for i in range(N):
    L.append(0)
    R.append(0)

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

for i in range(N-2,-1,-1):
    #print(i)
    R[i] = myGCD(R[i+1],A[i+1])

M=[]
for i in range(N):
    M.append(myGCD(R[i],L[i]))
#print(L)
#print(R)

print(max(M))

Submission status: Accepted

Solution code:
import fractions

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

X=[0]*N
X[1]=A[0]
for i in range(2, N):
  X[i]=fractions.gcd(X[i-1], A[i-1])

Y=[0]*N
Y[-2]=A[-1]
for i in range(N-3, -1, -1):
  Y[i]=fractions.gcd(Y[i+1], A[i+1])
  
X[0]=Y[0]
Y[-1]=X[-1]

Z=[fractions.gcd(X[i], Y[i]) for i in range(N)]
    
print(max(Z))

Submission status: Accepted

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
l=[a[0]]
r=[a[-1]]
for i in range(n-1):
  l.append(fractions.gcd(l[i],a[i+1])) #l[i] は最初i個
for i in range(n-1):
  r.append(fractions.gcd(r[i],a[-2-i])) #r[i]は右i r[n-1]=全部 l[0]and r[n-3]
m=[l[n-2],r[n-2]]
for i in range(n-2):
  m.append(fractions.gcd(l[i],r[n-i-3]))
print(max(m))

Submission status: Accepted

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

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

    l, r = [0], [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[-1-i]))
    print(m)

if __name__ == "__main__":
    f_c()

Submission status: Accepted

Solution code:
import math
import fractions

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

L = [0 for _ in range(N)]
L[0] = A[0]
for i in range(1,N):
    L[i] = fractions.gcd(L[i-1], A[i])

R = [0 for _ in range(N)]
R[N-1] = A[N-1]
for i in range(N-2, -1, -1):
    R[i] = fractions.gcd(R[i+1], A[i])

GCD = [0 for _ in range(N)]

GCD[0] = R[1]
GCD[N-1] = L[N-2]
for i in range(1,N-1):
    GCD[i] = fractions.gcd(L[i-1],R[i+1])

print(max(GCD))

Submission status: Accepted

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

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

pre=[0,a[0]]
suf=[0]*(n+1)
suf[n-1]=a[-1]
for i in range(1,len(a)):
    pre.append(gcd(pre[-1],a[i]))
for i in range(len(a)-2,-1,-1):
    suf[i] = gcd(suf[i+1],a[i])
ans=1
for i in range(len(a)):
    ans = max(ans,gcd(pre[i],suf[i+1]))
print(ans)

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    A = list(map(int, input().split()))
    prefix = [A[0]]
    for i in range(1, N):
        prefix.append(gcd(prefix[-1], A[i]))
    suffix = [A[N-1]]
    for i in range(N-2, -1, -1):
        suffix.append(gcd(suffix[-1], A[i]))
    suffix = suffix[::-1]
    # print(prefix)
    # print(suffix)

    ans = 1
    for i in range(N):
        if i == 0:
            ans = max(ans, suffix[1])
        elif i == N-1:
            ans = max(ans, prefix[-2])
        else:
            ans = max(ans, gcd(prefix[i-1], suffix[i+1]))
    return ans

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

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

Submission status: Accepted

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


N = int(input())
a = list(map(int, input().split()))
max_gcd = 0
L = [0] * (N + 2)
R = [0] * (N + 2)

for i in range(N):
    L[i+2] = gcd(L[i+1], a[i])
for i in range(N):
    R[-3-i] = gcd(R[-2-i], a[-1-i])

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

Submission status: Accepted

Solution code:
import fractions

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

left = [0] * N
right = [0] * N

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

right[N-1] = A[N-1]
for j in range(N-2,0,-1):
    right[j] = fractions.gcd(A[j],right[j+1])

can = [0] * N
can[0] = right[1]
for k in range(1,N-1):
    can[k] = fractions.gcd(left[k-1],right[k+1])
can[N-1] = left[N-2]

print(max(can))

Submission status: Accepted

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

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

ans=0
for i in range(n):
  ans=max(ans,gcd(L[i],R[i+1]))
print(ans)

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

from fractions import gcd

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

# 累積gcdを左からと右から
left = [0] * N
right = [0] * N
left[0] = A[0]
for i in range(1, N):
    left[i] = gcd(left[i-1], A[i])

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

# 番兵
left = [A[N-1]] + left
right.append(A[0])

#print(left)
#print(right)

ans = 0
for i in range(N):
    x = gcd(left[i], right[i+1])
    ans = max(ans, x)

print(ans)

Submission status: Accepted

Solution code:
def gcd(x, y):
  x, y=max(x, y), min(x, y)
  while y:
    x, y=y, x%y
  return x

N=int(input())
A=list(map(int, input().split()))
left_gcd=[1 for i in range(N)]
left_gcd[0]=A[0]
right_gcd=[1 for i in range(N)]
right_gcd[-1]=A[-1]
for i in range(1, N):
  left_gcd[i]=gcd(left_gcd[i-1], A[i])
for i in range(N-2, -1, -1):
  right_gcd[i]=gcd(right_gcd[i+1], A[i])
maxv=max(left_gcd[-2], right_gcd[1])
for i in range(1, N-1):
  maxv=max(maxv, gcd(left_gcd[i-1], right_gcd[i+1]))
print(maxv)

Submission status: Accepted

Solution code:
def gcd(a,b):
    a,b=max(a,b),min(a,b)
    if b==0:
        return a
    c = a//b
    d = a%b
    if d==0:
        return b
    else:
        return gcd(b,d)


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


l = [0 for _ in range(n)]
l[0]=0
for i in range(1,n):#nums[i]左側の最大公約数を求める
    l[i]=gcd(l[i-1],nums[i-1])

r=[0 for _ in range(n)]
r[-1]=0
for i in reversed(range(0,n-1)):#nums[i]の右側の最大公約数を求める
    r[i]=(gcd(nums[i+1],r[i+1]))

ans=0
for i in range(n):
    saidaikouyakusu_except_for_ni=gcd(l[i],r[i])
    if ans < saidaikouyakusu_except_for_ni:
        ans = saidaikouyakusu_except_for_ni
    else:pass

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-1):
  L.append(gcd(L[i],A[i]))
  R.append(gcd(R[i],A[-i-1]))

max_gcd = 0
for i in range(N):
  max_gcd = max(max_gcd, gcd(L[i],R[-i-1]))

print(max_gcd)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from fractions import gcd
import sys
sys.setrecursionlimit(10 ** 7)


def solve(N: int, A: "List[int]"):
    L, R = [0], [0]
    for i in range(N - 1):
        L.append(gcd(L[i], A[i]))
        R.append(gcd(R[i], A[-i - 1]))
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(L[i], R[N - i - 1]))
    print(ans)
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools
# (tips: You use the default template now. You can remove this line by using your custom template)
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 = list(map(int, 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[-i-1]))
MAX = 0
for i in range(N):
    MAX = max(MAX, gcd(L[i], R[-i-1]))
print(MAX)

Submission status: Accepted

Solution code:
from fractions import gcd

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

l = []
g = 0
for i in range(n):
  tmp = gcd(g, a[i])
  if tmp < g or g == 0:
    l.append(i)
  g = tmp

ans = 0
for i in l:
  g = 0
  for j in range(n):
    if i != j:
      g = gcd(g, a[j])
  ans = max(ans, g)
print(ans)

Submission status: Accepted

Solution code:
import fractions as m

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

gstackl = [0]*n
gstackr = [0]*n

ans = 0

for i in range(1, n):
	gstackl[i] = m.gcd(gstackl[i-1], al[i-1])
	gstackr[n-i-1] = m.gcd(gstackr[n-i], al[n-i])

#print(gstackl)
#print(gstackr)

for i in range(n):
	l = gstackl[i]
	r = gstackr[i]
	#print("{} {}".format(l, r))
	a = m.gcd(l, r)
	#print(a)
	if a > ans:
		ans = a

print(ans)

Submission status: Accepted

Solution code:
class Segtree(object):
    def __init__(self,A,f,u):
        n=2**(len(A).bit_length())
        N=2*n-1
        self.__num=n
        self.__len=N
        self.__seg=[u]*N
        self.__f=f
        self.__u=u
        # init
        for i in range(len(A)): # set
            self.__seg[n+i-1]=A[i]
        for i in range(n-2,-1,-1): # built, iの子のnodeは2i+1,2i+2
            self.__seg[i]=f(self.__seg[2*i+1],self.__seg[2*i+2])

    def __repr__(self):
        return str(self.__seg)

    def get_tree(self):
        return self.__seg

    def update(k,v):
        "A[k]=v"
        k+=self.__num-1
        self.__seg[k]=v
        while k+1: # k=0の処理が終わったら-1になってる
            k=(k-1)//2 # kの親のnodeは(k-1)//2
            self.__seg[k]=self.__f(self.__seg[k*2+1],self.__seg[k*2+2])

    def query(self,p,q): # reduce(f,A[p:q])
        if q<=p: return self.__u
        p+=self.__num-1
        q+=self.__num-2
        res=self.__u
        while q-p>1:
            if p&1==0:
                res=self.__f(res,self.__seg[p])
            if q&1==1:
                res=self.__f(res,self.__seg[q])
                q-=1
            p=p//2
            q=(q-1)//2
        if p==q: res=self.__f(res,self.__seg[p])
        else: res=self.__f(self.__f(res,self.__seg[p]),self.__seg[q])
        return res


def resolve():
    from fractions import gcd
    n=int(input())
    A=list(map(int,input().split()))
    if n==2:
        print(max(A))
        return
    seg=Segtree(A,gcd,0)
    ans=0
    for i in range(n):
        ans=max(ans,gcd(seg.query(0,i),seg.query(i+1,n)))
    print(ans)

resolve()

Submission status: Accepted

Solution code:
from fractions import gcd

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

res = []
for j in range(N):
    res.append(gcd(L[j], R[j]))

res = max(res)
print(res)

Submission status: Accepted

Solution code:
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
      
N = int(input())
As = list(map(int, input().split()))

Ls = [0]
Rs = [0]
for i in range(N):
    la, lb = max([As[i], Ls[i]]), min([As[i], Ls[i]])
    Ls.append(gcd(la, lb))
    ra, rb = max([As[N - i - 1], Rs[i]]), min([As[N - i - 1], Rs[i]])
    Rs.append(gcd(ra, rb))
Ms = []
for i in range(N):
    a, b = max([Rs[N - i - 1], Ls[i]]), min([Rs[N - i - 1], Ls[i]])
    Ms.append(gcd(a, b))

print(max(Ms))

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


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

n=int(input())
a=[int(i) for i in input().split()]
l=[None for i in range(n)]
l[0]=a[0]
for i in range(1,n):
    l[i]=gcd(l[i-1],a[i])
r=[None for i in range(n)]
r[-1]=a[-1]
for i in range(n-2,-1,-1):
    r[i]=gcd(r[i+1],a[i])
res=max(r[1],l[-2])
for i in range(1,n-1):
    res=max(res,gcd(l[i-1],r[i+1]))
print(res)

Submission status: Accepted

Solution code:
from fractions import gcd

def getlist():
	return list(map(int, input().split()))

N = int(input())
A = getlist()

X = [0] * N
Y = [0] * N

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

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

ans = []

ans.append(X[-2])
ans.append(Y[1])

for i in range(1, N - 1):
	ans.append(gcd(X[i - 1], Y[i + 1]))


print(max(ans))

Submission status: Accepted

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

# gcd(最大公約数)の定義(Euclidの互除法)
def gcd(a, b):
    if b == 0:
        return a
    else:
        return(gcd(b, a % b))

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

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):
    while b != 0:
        a,b=b,a%b
    return a

n=int(input())
a=[0] + list(map(int,input().split())) + [0]#a[n+1]=0とするため

l=[0]*(n+2)
r=[0]*(n+2)#r[n+1]=0としたいから
for i in range(1,n+1):
    l[i] = gcd(l[i-1],a[i])
for i in range(n,0,-1):
    r[i] = gcd(r[i+1],a[i])

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

print(max(z))

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

r=[-1]*N
l=[-1]*N
l[0]=a[0]
r[-1]=a[-1]

if N==2:
    print(max(a))

else:
    for i in range(1,N):
        l[i]=gcd(l[i-1],a[i])
    for i in range(N-2,-1,-1):
        r[i]=gcd(r[i+1],a[i])
    ans=r[1]
    for k in range(1,N):
        if k==N-1:
            ans=max(ans,l[-2])
        else:
            ans=max(ans,gcd(l[k-1],r[k+1]))
    print(ans)

Submission status: Accepted

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

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

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 = r[::-1]

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

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd


def resolve():
    n = int(input())
    aaa = [0] + list(map(int, input().split())) + [0]
    lll = [0 for i in range(n+2)]
    rrr = [0 for i in range(n+2)]
    for i in range(1, n+1):
        lll[i] = gcd(lll[i-1], aaa[i])
    for i in range(n,0,-1):
        rrr[i] = gcd(rrr[i+1], aaa[i])
    z = [gcd(lll[i-1],rrr[i+1]) for i in range(1,n+1)]
    print(max(z))
resolve()

Submission status: Accepted

Solution code:
import fractions;
n = int(input());
a = input().split(" ");
for i in range(n) :
	a[i] = int(a[i]);

a.insert(0,0);
l = [0]*(n+1);	
r = [0]*(n+2);

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


mxgcd = 0;
for i in range(1,n+1) :
	gcd_now = fractions.gcd(l[i-1],r[i+1]);
	if gcd_now > mxgcd :
		mxgcd = gcd_now;
	
print(mxgcd);

Submission status: Accepted

Solution code:
import fractions
N=int(input())
l1=list(map(int,input().split()))
l2=[0]*(N+1)
l3=[0]*(N+1)
for i in range(N):
  l2[i+1]=fractions.gcd(l2[i],l1[i])
for j in range(N-1,0,-1):
  l3[j-1]=fractions.gcd(l3[j],l1[j])
ans=1
for k in range(N):
  ans=max(ans,fractions.gcd(l2[k],l3[k]))
print(ans)

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)

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

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

print(max(M))

Submission status: Accepted

Solution code:
import fractions
def gcd(x,y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x

    return fractions.gcd(x%y, y)

n = int(input())
a = list(map(int, input().split()))
L = [0]
R = [0]
now1 = 0
now2 = 0
ans = 0

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

    now2 = gcd(now2,a[-i-1])
    R.append(now2)

for i in range(n):
    ans = max(ans, gcd(L[i], R[n-i-1]))

print(ans)

Submission status: Accepted

Solution code:
import fractions
n = int(input())
a = list(map(int,input().split(" ")))
l = [0 for i in range(n+1)]
r = [0 for i in range(n+1)]
for i in range(n):
    l[i+1] = fractions.gcd(l[i],a[i])

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



m = 1
for i in range(n):
    m = max(m,fractions.gcd(l[i],r[i+1]))
print(m)

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)
 
L[0] = 0
R[-1] = 0
 
for i in range(len(A)):
    L[i] = gcd(L[i-1], A[i])
    
for i in reversed(range(len(A))):
    R[i] = gcd(R[i+1], A[i])
    
ans = 0    
 
for i in range(N):
    ans = max(ans, gcd(L[i-1], R[i+1]))
    
print(ans)

Submission status: Accepted

Solution code:
def euc(a, b):
    if a > b:
        a, b = b, a
    if a == 0:
        return b
    return euc(a, b%a)

if __name__ == '__main__':
    N = int(input())
    
    A = list(map(int, input().split()))
    
    L = [0]*(N+1)
    R = [0]*(N+1)
    
    for i in range(N):
        L[i+1] = euc(L[i], A[i])
    
    for i in range(N-1, -1, -1):
        R[i] = euc(R[i+1], A[i])
        
    max_gcd = 0
    for i in range(N):
        max_gcd = max(max_gcd, euc(L[i], R[i+1]))
    print(max_gcd)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))

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

l = []
r = []

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

m = []
for i in range(n):
    if i == 0:
        m.append(r[1])
    elif i == n-1:
        m.append(l[-2])
    else:
        m.append(gcd(l[i-1], r[i+1]))

print(max(m))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from fractions import *

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


left = [A[0]]
right = [A[-1]]
for i in range(N-1):
    left.append(gcd(left[i], A[i+1]))
    right.append(gcd(right[i], A[-i-2]))


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

print(ans)

Submission status: Accepted

Solution code:
import fractions

N = int(input())
a = list(map(int,input().split()))
a1 = [0] * N
a2 = [0] * N
a1[0] = 0
a2[-1] = 0
a1[1] = a[0]
a2[-2] = a[-1]
ans = 0
for i in range(2,N):
    a1[i] = fractions.gcd(a1[i-1], a[i-1])
    a2[-(i+1)] = fractions.gcd(a2[-i], a[-i])
for a,b in zip(a1, a2):
    if a == 0:
        ans = max(ans, b)
    elif b == 0:
        ans = max(ans, a)
    else:
        ans = max(ans, fractions.gcd(a, b))
print(ans)

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()))
    f = [0] * (n + 1)
    b = [0] * (n + 1)
    for i in range(n):
        f[i+1] = fractions.gcd(f[i], a[i])
        b[-i-2] = fractions.gcd(b[-i-1], a[-i-1])
    m_gcd = 0
    for i in range(n):
        m_gcd = max(m_gcd, fractions.gcd(f[i], b[i+1]))
    print(m_gcd)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
N =int(input())
A = 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)

    # divisors.sort()
    return divisors

A = sorted(A)
from collections import Counter
check = Counter(A)
for c,k in check.items():
    if k>=N-1:
        A=[c]



def del_dicts(dicts,i):
    del_list=[]
    for k,v in dicts.items():
        if v<i:
            del_list.append(k)
    for d in del_list:
        del dicts[d]
    return dicts

dicts = {}

for i in range(len(A)):
    if i<=2:
        for num in make_divisors(A[i]):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    
    else:
        del_list=[]
        
        for k in dicts.keys():
            if A[i]%k == 0:
                dicts[k]+=1
    dicts = del_dicts(dicts,i)
print(max(dicts.keys()))

Submission status: Accepted

Solution code:
def gcd(a, b):
    while True:
        a, b = b, a % b
        if b == 0:
            break
    return a


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

if n == 2:
    print(max(nums))
else:
    l_gcds = [nums[0]]
    r_gcds = [nums[-1]]
    for i in range(1, n - 1):
        l = gcd(l_gcds[-1], nums[i])
        r = gcd(r_gcds[-1], nums[n - i - 1])
        l_gcds.append(l)
        r_gcds.append(r)
    r_gcds = r_gcds[::-1]
    gcds = [l_gcds[-1], r_gcds[0]]
    for i in range(1, n - 1):
        gcds.append(gcd(l_gcds[i - 1], r_gcds[i]))
    print(max(gcds))

Submission status: Accepted

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

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

GCD = [[0] * n for i in range(2)]

GCD[0][0] = float("inf")
GCD[1][-1] = float("inf")
GCD[0][1] = A[0]
GCD[1][-2] = A[-1]

for i in range(2, n):
    GCD[0][i] = gcd(GCD[0][i-1], A[i-1])
    #print(GCD[0][i-1], A[i-1])
    GCD[1][-i-1] = gcd(GCD[1][-i], A[-i])

#print(GCD)

ans = 1
for i in range(1, n-1):
    ans = max(ans, gcd(GCD[0][i], GCD[1][i]))

print(max(ans, GCD[0][-1], GCD[1][0]))

Submission status: Accepted

Solution code:
import fractions


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

    gcd_ans = []
 
    l = [0] * N
    r =[0] * N
    for i in range(N-1):
        l[i+1] = fractions.gcd(l[i],A[i])
        r[i+1] = fractions.gcd(r[i],A[N-1-i])
    r.reverse()
 
    for i in range(N):
        gcd_ans.append(fractions.gcd(l[i],r[i]))
 
    print(max(gcd_ans))


main()

Submission status: Accepted

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

l = [0] * (n + 1)
r = [0] * (n + 1)

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

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-1-i])
r.reverse()

for i in range(n):
  gcd_ans.append(gcd(l[i],r[i]))

print(max(gcd_ans))

Submission status: Accepted

Solution code:
import fractions
from functools import reduce

n=int(input())
a1=[int(i) for i in input().split()]
a2=[]
a3=[]
nul2 = a1[0]
nul3 = a1[-1]
a2.append(nul2)
a3.append(nul3)
#nが2以上?
for i in range(1,n-1):
    nul2=fractions.gcd(nul2,a1[i])
    nul3=fractions.gcd(nul3,a1[-i-1])
    a2.append(nul2)
    a3.append(nul3)
#print(a2)
#print(a3)
c=max([a2[-1],a3[-1]])
for i in range(n-2):
    d=fractions.gcd(a2[i],a3[-i-2])
    if d>c:
        c=d
print(c)

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])
    R[-i-1] = gcd(R[-i], A[-i])
M = [gcd(L[i], R[i+1]) for i in range(N)] 

print(max(M))

Submission status: Accepted

Solution code:
N = int(input())

A_list = list(map(int, input().split())) # case1 : [7, 6, 8]

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

left_gcd = [0] * (N+1)
right_gcd = [0] * (N+1)
for i in range(N):
    left_gcd[i+1] = gcd(A_list[i], left_gcd[i])
for i in range(N-1, 0, -1):
    right_gcd[i] = gcd(A_list[i], right_gcd[i+1])

ans = 0

for i in range(N):
    i_gcd = gcd(left_gcd[i], right_gcd[i+1])
    if ans < i_gcd:
        ans = i_gcd

print(ans)

Submission status: Accepted

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

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

from collections import defaultdict

L = defaultdict(lambda :1)
R = defaultdict(lambda :1)
L[1] = A[0]
R[1] = A[N-1]

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

ans = max(L[N-1],R[N-1])
for i in range(1,N-1):
  ans = max(ans,gcd(L[i],R[N-1-i]))

print(ans)

Submission status: Accepted

Solution code:
import fractions
from functools import reduce
 
N = int(input())
lst = list(map(int,input().split()))
 
L = [0] * (N+1)
R = [0] * (N+1)
for i in range(N):
    L[i+1] = fractions.gcd(L[i], lst[i])
    R[N-i-1] = fractions.gcd(R[N-i], lst[N-i-1])
ans = 0
for i in range(N):
    ans = max(ans, fractions.gcd(L[i], R[i+1]))
print(ans)

Submission status: Accepted

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

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))

accum_gcd_pop = []

for j in range(n):
    if j == 0:
        accum_gcd_pop.append(accum_gcd_i[n-2])
    elif j == n-1:
        accum_gcd_pop.append(accum_gcd[n-2])
    else:
        accum_gcd_pop.append(gcd(accum_gcd[j-1],accum_gcd_i[n-j-2]))
print(max(accum_gcd_pop))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
import fractions

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

    L = [0]*(N+1)
    R = [0]*(N+1)
    for i in range(1,N):
        L[i+1] = fractions.gcd(L[i],A[i-1])
    for i in range(N-1,-1,-1):
        R[i] = fractions.gcd(R[i+1],A[i])
    ma = 1
    for i in range(0,N+1):
        ma = max(ma, fractions.gcd(L[i], R[i]))
    print(ma)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

#input = open(sys.argv[1], "r").readline

import fractions

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

    if N <= 2:
        print(max(A))
        return
    
    L = [0]*(N+1)
    for i in range(N):
        L[i+1] = fractions.gcd(L[i],A[i])
#    print(L)
    R = [0]*(N+1)
    for i in range(N-1,-1,-1):
        R[i] = fractions.gcd(R[i+1],A[i])
#    print(R)
    ma = 1
    for i in range(1,N+1):
        ma = max(ma, fractions.gcd(L[i-1], R[i]))
    print(ma)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import fractions
from functools import reduce
 
N = int(input())
lst = list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
from fractions import gcd
n = int(input())
lis = list(map(int, input().split()))
x = [lis[0]]
y = [lis[-1]]
for i in range(n - 1):
    x.append(gcd(x[-1], lis[i + 1]))
    y.append(gcd(y[-1], lis[n - i - 2]))
ans = max(x[-2], y[-2])
for i in range(1, n - 1):
    ans = max(ans, gcd(x[i - 1], y[-i - 2]))
print(ans)