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 fractions
N = int(input())
A = [int(i) for i in input().split()]
ans = 0

L = [0]*(N+2)
R = [0]*(N+2)
L[0] = 0
L[1] = A[0]
R[N+1] = 0
R[N] = A[N-1]

for i in range(1, N):
  L[i+1] = fractions.gcd(L[i], A[i])
  R[N-i] = fractions.gcd(R[N+1-i],A[N-1-i])
for i in range(N):
  tmp = fractions.gcd(L[i], R[i+2])
  ans = max(tmp, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

N = int(input())
A = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
M = 1

for i in range(N):
  L[i+1] = fractions.gcd(L[i], A[i])
  R[N-i-1] = fractions.gcd(R[N-i], A[N-i-1])
  
if N == 2:
   print(max(A))
else:
  for i in range(N):
    M = max(M, fractions.gcd(L[i], R[i+1]))
  print(M)

Submission status: Accepted

Solution code:
import fractions

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

L=[0,A[0]]
R=[0,A[-1]]
for i in range(N-1):
    #l=fractions.gcd(L[i+1],A[i+1])
    #r=fractions.gcd(R[i+1],A[N-i-2])
    L.append(fractions.gcd(L[i+1],A[i+1]))
    R.append(fractions.gcd(R[i+1],A[N-i-2]))
    #print(L[i+1],A[i+1],R[i+1],A[N-i-2])
#print(L)
#print(R)
arr=[]
for i in range(N):
    a=fractions.gcd(L[i],R[N-i-1])
    arr.append(a)
    #print(i,L[i],R[N-i-1],a,arr)
print(max(arr))

Submission status: Accepted

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


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

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

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

Submission status: Accepted

Solution code:
N = int(input())
li = [ int(it) for it in input().split() ]


def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
  
lg = [0]*N
lr = [0]*N

lg[0] = li[0]
for i in range(N-1):
  lg[i+1] = gcd(lg[i],li[i+1])
lr[-1] = li[-1]
for i in range(N-1):
  lr[-2-i] = gcd(lr[-1-i],li[-i-2])
#print ( lg , lr ) 

s = 0
for i in range(N):
  if (0<i and i<N-1):
    s = max(s,gcd(lg[i-1],lr[i+1]))
  if (i==0):
    s = max(s,lr[i+1])
  if (i==N-1):
    s = max(s,lg[i-1])
print (s)

Submission status: Accepted

Solution code:
n = int(input())
dat_a = map(int, input().split())
dat_a = list(dat_a)
l = [0] * n
r = [0] * n
res = [0] * n
import fractions

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

for i in range(n-2, -1, -1):
    r[i] = fractions.gcd(r[i+1], dat_a[i+1])
for i in range(n):
    res[i] = fractions.gcd(l[i], r[i])

print(max(res))

Submission status: Accepted

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

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

if N <= 2:
    print(max(A))
    exit()

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

ans = 1
left_gcd = [0]
left_gcd[0] = A[0]
right_gcd = [0]
right_gcd[0] = A[N-1]

for i in range(1,N):
    if left_gcd[i-1] > A[i]:
        left_gcd.append(gcd(left_gcd[i-1], A[i]))
    else:
        left_gcd.append(gcd(A[i], left_gcd[i-1]))
    if right_gcd[i-1] > A[N-1-i]:
        right_gcd.append(gcd(right_gcd[i-1], A[N-1-i]))
    else:
        right_gcd.append(gcd(A[N-1-i], right_gcd[i-1]))
right_gcd.reverse()

g = [left_gcd[N-2], right_gcd[1]]

for i in range(1,N-1):
    l = left_gcd[i-1]
    r = right_gcd[i+1]
    if l > r:
        g.append(gcd(l,r))
    else:
        g.append(gcd(r,l))

print(max(g))

Submission status: Accepted

Solution code:
import sys
import fractions

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MAT(h): return [list(map(int, input().split())) for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = INT()
    A = LIST()
    L = [0] * N
    R = [0] * N
    M = [0] * N
    for i in range(N - 1):
        L[i + 1] = fractions.gcd(L[i], A[i])
        R[N - i - 2] = fractions.gcd(R[N - i - 1], A[N - i - 1])
    maxv = 0
    for i in range(N):
        M[i] = fractions.gcd(L[i], R[i])
        if maxv < M[i]:
            maxv = M[i]
    print(maxv)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MAT(h): return [list(map(int, input().split())) for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = INT()
    A = LIST()
    L = [0] * N
    R = [0] * N
    M = [0] * 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])
    maxv = 0
    for i in range(N):
        M[i] = gcd(L[i], R[i])
        if maxv < M[i]:
            maxv = M[i]
    print(maxv)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#C
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+2)
R=[0]*(N+2)
L[0] = A[0]
R[N-1] = A[N-1]
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 = 0
for i in range(N):
    ans = max(ans,gcd(L[i-1],R[i+1]))
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ri = lambda: int(input())
#rl = lambda: [int(x) if x.isdecimal() else x for x in input().split()]
#rl = lambda: list(input().split()))
rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

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

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

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

def main():
  n=I()
  l=LI()

  l1=l[:]
  l2=l[:]

  for i in range(1,n):
    l1[i]=gcd(l1[i-1],l1[i])

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

  n+=1
  l1=[0]+l1
  l2=l2+[0]

  # print(l1)
  # print(l2)

  mx=0
  for i in range(n-1):
    mx=max(mx,gcd(l1[i],l2[i+1]))

  return mx

print(main())

Submission status: Accepted

Solution code:
from fractions import gcd
#input
N = int(input())
A = [int(i) for i in input().split()]
#cluc left and right gcd 
left = [0]*N
right = [0]*N
count = 0
left[0] = A[0]
right[-1] = A[-1]
Ans = []

for i in A[1:]:
    left[count+1] = gcd(left[count],i)
    count += 1

for i in reversed(A[:-1]):
    right[count-1] = gcd(right[count],i)
    count -= 1
# make ans
Ans.append(right[1])
Ans.append(left[-2])
for i in range(N-2):
    Ans.append(gcd(left[i],right[i+2]))
print(max(Ans))

Submission status: Accepted

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

import sys
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd

def input(): return sys.stdin.readline().strip()
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

class SegTree:
    """
    以下のクエリを処理する
    1.update:  i番目の値をxに更新する
    2.get_val: 区間[l, r)の値を得る
    """
 
    def __init__(self, n, func, init):
        """
        :param n: 要素数
        :param func: 値の操作に使う関数(min, max, addなど)
        :param init: 要素の初期値
        """
        n += 1
        self.n = n
        self.func = func
        self.init = init
        # nより大きい2の冪数
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.init] * (n2 << 1)
 
    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.n2
        self.tree[i] = x
        while i > 1:
            self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1])
            i >>= 1
 
    def get_val(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        return self._get_val(a, b, 1, 0, self.n2)
 
    def _get_val(self, a, b, k, l, r):
        """
        [a, b)の値を得る内部関数
        :param k: 現在調べている区間のtree内index
        :param l, r: kが表す区間の左右端index [l, r)
        :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値
        """
        # 範囲外なら初期値
        if r <= a or b <= l:
            return self.init
        # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用
        if a <= l and r <= b:
            return self.tree[k]
        # 一部だけ範囲内なら2つに分けて再帰的に調査
        m = (l + r) // 2
        return self.func(
            self._get_val(a, b, k << 1, l, m),
            self._get_val(a, b, (k << 1) + 1, m, r)
        )

N=INT()
A=LIST()

st=SegTree(N+2, gcd, 0)
for i in range(N):
    st.update(i+1, A[i])

ans=0
for i in range(1, N+1):
    ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2)))
print(ans)

Submission status: Accepted

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

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

ans = [0]*n
lileft = [0]*(n+1)
liright = [0]*(n+1)
for i in range(n):
  lileft[i+1] = gcd(lileft[i],li[i])
for i in range(n):
  liright[i+1] = gcd(liright[i],li[n-i-1])
for i in range(n):
  ans[i] = gcd(lileft[i],liright[n-i-1])
print(max(ans))

Submission status: Accepted

Solution code:
import copy

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

max_a = max(a)


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


# 事前処理
# あらかじめ、左端からi個でgcdを計算しておく
# 同様に右からも
l = [0] * n
r = [0] * n

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

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

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


tmp = 0
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import fractions as fr

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

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

ans = 0
for i in range(N):
    ans = max(ans, fr.gcd(l[i],r[-i-1]))
print(ans)

Submission status: Accepted

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

l = []
r = []
import fractions as fr
l.append(0)
l.append(A[0])
for i in range(1,N-1):
    l.append(fr.gcd(l[i],A[i]))
r.append(0)
r.append(A[-1])
for i in range(1,N-1):
    r.append(fr.gcd(r[i],A[-i-1]))

ans = 0
for i in range(N):
    ans = max(ans, fr.gcd(l[i],r[-i-1]))
print(ans)

Submission status: Accepted

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

class SegAccumCalc:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def set_input(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new
    
    def get_input(self, n):
        return self.node[(self.N-1)+n]

    def get_output(self,
                   a=0, b=-1,
                   k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left = self.get_output(a,b,2*k+1,l,(l+r)//2)
            right = self.get_output(a,b,2*k+2,(l+r)//2,r)
            return self.calc(left,right)

N = int(input())
A = inpl()
sac = SegAccumCalc(A,calc=gcd)
ans = 0
for i in range(N):
    sac.set_input(i,0)
    g = sac.get_output()
    if g > ans:
        ans = g
    sac.set_input(i,A[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions

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

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

Submission status: Accepted

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

class SegAccumCalc:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def set_value(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new

    def get(self,
            a=0, b=-1,
            k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left = self.get(a,b,2*k+1,l,(l+r)//2)
            right = self.get(a,b,2*k+2,(l+r)//2,r)
            return self.calc(left,right)

N = int(input())
A = inpl()
sac = SegAccumCalc(A,calc=gcd)
ans = 0
for i in range(N):
    if i == 0:
        c = sac.get(1,N)
    elif i == N-1:
        c = sac.get(0,N-1)
    else:
        c = gcd(sac.get(0,i),sac.get(i+1,N))
    if c > ans:
        ans = c
print(ans)

Submission status: Accepted

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

class SegCalcTree:
    def __init__(self, value, N=0, calc=lambda x,y: x+y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [None] * (2*N-1)
        for i, v in enumerate(value):
            self.node[i+N-1] = v
        self.calc = lambda x, y: x if y is None else y if x is None else calc(x,y)
        for i in range(N-2,-1,-1):
            left, right = self.node[2*i+1], self.node[2*i+2]
            self.node[i] = self.calc(left, right)

    def set_value(self, n, v):
        i = (self.N-1) + n
        self.node[i] = v
        while i > 0:
            i = (i-1)//2
            left, right = self.node[2*i+1], self.node[2*i+2]
            new = self.calc(left,right)
            if self.node[i] == new:
                break
            else:
                self.node[i] = new

    def get(self,
            a=0, b=-1,
            k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left = self.get(a,b,2*k+1,l,(l+r)//2)
            right = self.get(a,b,2*k+2,(l+r)//2,r)
            return self.calc(left,right)

N = int(input())
A = inpl()
sct = SegCalcTree(A,calc=gcd)
ans = 0
for i in range(N):
    a = A[i]
    sct.set_value(i,None)
    c = sct.get()
    if c > ans:
        ans = c
    sct.set_value(i,a)
print(ans)

Submission status: Accepted

Solution code:
import fractions
from functools import reduce

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

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

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

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

Submission status: Accepted

Solution code:
from fractions import gcd

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

L = []
R = []
L.append(a[0])
R.append(a[-1])

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

ans = max(L[-1], R[-1])
for i in range(N-2):
    tmp = gcd(L[i], R[-(i+2)])
    if tmp > ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
INF = float('inf')
MOD = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ass = set()
def div(x):
    for i in range(1,int(x**0.5)+1):
        if x%i == 0:
            ass.add(i)
            ass.add(x//i)
div(A[0])
div(A[1])
ans = 1
for i in ass:
    cnt = 0
    for a in A:
        if a%i == 0:
            cnt += 1
    if cnt >= N-1:
        ans = max(ans,i)
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))
Gleft = [A[0]]
Gright = [A[-1]]
for i in range(1,N):
    Gleft.append(gcd(Gleft[-1],A[i]))
    Gright.append(gcd(Gright[-1],A[-1-i]))
Gright = Gright[::-1]
res = max(Gright[1],Gleft[-2])
for i in range(1,N-1):
    res = max(res,gcd(Gleft[i-1],Gright[i+1]))
print(res)

Submission status: Accepted

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

s1=a[0]
s2=a[-1]
su=[s1]
sd=[s2]
for i in range(1,n-1):
    s1=gcd(s1,a[i])
    s2=gcd(s2,a[(i+1)*(-1)])
    su+=[s1]
    sd+=[s2]
sd.reverse()

ans=[su[-1],sd[0]]
for i in range(n-2):
    ans+=[gcd(su[i],sd[i+1])]
print(max(ans))

Submission status: Accepted

Solution code:
def larger(x, y):
	if x > y: return x
	return y

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

N = int(input())
A = list(map(int, input().split()))
left = [A[0]]
for i in range(1, N):
	left.append(euc(left[-1], A[i]))
right = [A[-1]]
for i in range(1, N):
	right.append(euc(right[-1], A[-1-i]))
right.reverse()

max = larger(left[N-2], right[1])
for i in range(1, N-1):
	max = larger(max, euc(left[i-1], right[i+1]))
print(max)

Submission status: Accepted

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

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

L = [0]
R = [0]
for n in range(N-1):
    L.append(gcd(L[-1],A[n]))
    R.append(gcd(R[-1],A[N-n-1]))
    
GCDs = []
for n in range(N):
    GCDs.append(gcd(L[n], R[N-n-1]))
    
print(max(GCDs))

Submission status: Accepted

Solution code:
from functools import reduce
from itertools import combinations
from fractions import gcd

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


Left = [0] * (N + 1)
Right = [0] * (N + 1)

for i in range(N):
    Left[i + 1] = gcd(Left[i], A[i])
    Right[N - i - 1] = gcd(Right[N - i], A[N - i - 1])
ans = 0
for i in range(N):
    ans = max(ans, gcd(Left[i], Right[i + 1]))
    
print(ans)

Submission status: Accepted

Solution code:
import fractions
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
l_gcd = [0] * (len(data) + 1)
r_gcd = [0] * (len(data) + 1)
for ind,(i,j) in enumerate(zip(data,reversed(data))):
    l_gcd[ind + 1] = fractions.gcd(l_gcd[ind],i)
    r_gcd[ind + 1] = fractions.gcd(r_gcd[ind],j)
r_gcd = list(reversed(r_gcd))
ans = 0
for k in range(N):
    ans = max(fractions.gcd(l_gcd[k],r_gcd[k+1]),ans)
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
import numpy as np

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

left = np.zeros(n + 1, dtype=np.int)
right = np.zeros(n + 1, dtype=np.int)

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

last = np.zeros(n, dtype=np.int)

for i in range(n):
    last[i] = gcd(left[i], right[i + 1])


print(max(last))

Submission status: Accepted

Solution code:
from fractions import gcd
# from functools import reduce
# def gcd_list(numbers):
#     return reduce(math.gcd, numbers)

n=int(input())

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

left=[0]*(n+1)
right=[0]*(n+1)

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

# print(left)
# print(right)
last=[0]*n

for i in range(n):
    last[i]=(gcd(left[i],right[i+1]))


print(max(last))

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()))
output=0
KA=[0 for i in range(N)]
LA=[0 for i in range(N)]
GCD = []

for k in range(1,N):
        LA[k]=gcd(A[k-1],LA[k-1])

for k in range(1,N):
        KA[k]=gcd(A[N-k],KA[k-1])


for k in range(N):
        GCD.append(gcd(LA[k],KA[N-k-1]))
output = max(GCD)
print(output)

Submission status: Accepted

Solution code:
N=int(input())
A=sorted(map(int,input().split()))
ans=1
yakusu=[[],[]]

for i in range(2):
    for j in range(2,int(A[i]**0.5)+1):
        if A[i]%j==0:
            yakusu[i].append(j)
            yakusu[i].append(A[i]//j)
    yakusu[i].append(A[i])

yakusum=yakusu[0]
yakusum.extend(yakusu[1])
yakusum=sorted(set(yakusum))

for i in yakusum:
    counter=0
    for j in range(N):
        if A[j]%i!=0:
            counter+=1
            if counter==2:
                break
    if counter<=1:
        ans=i
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=1
Amax=max(A)
yakusu=[[],[]]

for i in range(2):
    for j in range(2,int(A[i]**0.5)+1):
        if A[i]%j==0:
            yakusu[i].append(j)
            yakusu[i].append(A[i]//j)
    yakusu[i].append(A[i])

yakusum=yakusu[0]
yakusum.extend(yakusu[1])
yakusum=sorted(set(yakusum))

for i in yakusum:
    counter=0
    for j in range(N):
        if A[j]%i!=0:
            counter+=1
            if counter==2:
                break
    if counter<=1:
        ans=i
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
n=int(input())
a=[int(i) for i in 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])
r=r[::-1]
g=[]
for i in range(n):
    g.append(gcd(l[i],r[i]))
print(max(g))

Submission status: Accepted

Solution code:
from fractions import gcd

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

L = [0 for _ in range(N+1)]
R = [0 for _ in range(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])

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

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

print(max(M))

Submission status: Accepted

Solution code:
n1 = int(input())
nl1 = [int(i1) for i1 in input().split()]
 
r1 = 0
nl1.sort()

for i5 in range(2):
    c1 = 0
    for i2 in range(n1):
        if nl1[i2] % nl1[i5] != 0:
            c1 += 1
        if c1 >= 2:
             break
        if i2 == (n1 - 1):
            r1 = nl1[i5] 
 
i3 = nl1[1] // 2
 
while r1 == 0:
    c1 = 0
    for i4 in range(n1):
        if nl1[i4] % i3 != 0:
            c1 += 1
        if c1 >= 2:
            break
        if i4 == (n1 - 1):
            r1 = i3
    i3 -= 1
 
print(str(r1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions
import sys

n1 = int(input())
nl1 = [int(i1) for i1 in input().split()]

if n1 == 2:
    print(max(nl1))
    sys.exit()

r1 = 0

nl1.sort()

if n1 >= 3:
    n2 = fractions.gcd(nl1[0], nl1[1]) 
    n2 = max(n2, fractions.gcd(nl1[1], nl1[2]))
    n2 = max(n2, fractions.gcd(nl1[0], nl1[2]))

c1 = 0
for i2 in range(n1):
    if nl1[i2] % n2 != 0:
        c1 += 1
    if c1 >= 2:
        break
    if i2 == (n1 - 1):
        r1 = n2

i3 = n2

while r1 == 0:
    c1 = 0
    for i4 in range(n1):
        if nl1[i4] % i3 != 0:
            c1 += 1
        if c1 >= 2:
            break
        if i4 == (n1 - 1):
            r1 = i3
    i3 -= 1

print(str(r1))

Submission status: Accepted

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

def gcd(x, y):
    if x < 0 and y < 0:
        return -1
    if x < 0:
        return y
    if y < 0:
        return x


    x, y = max(x, y), min(x, y)
    r = x % y
    if r == 0:
        return y
    else:
        return gcd(r, y)

# Li = gcd(A1, A2, ..., Ai-1)
# Ri = gcd(Ai+1, Ai+2, ..., AN)

L = [-1] * N
R = [-1] * N

L[1] = A[0]
R[N-2] = A[N-1]

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

G = [gcd(L[i], R[i]) for i in range(N)]
print(max(G))

Submission status: Accepted

Solution code:
import fractions as f

n = int(input())
A = [int(_) for _ in input().split()]

left = []
right = []

left.append(A[0])
for i in range(1,n):
    left.append(f.gcd(left[i-1],A[i]))

right.append(A[n-1])
for i in range(1,n):
    right.append(f.gcd(right[i-1],A[n-1-i]))

ans = 0
for i in range(n):
    if i == 0:
        ans = right[n-2]
    if i >= 1 and i <= n-2:
        ans = max(ans,f.gcd(left[i-1],right[n-2-i]))
    if i == n-1:
        ans = max(ans,left[n-2])
print(ans)

Submission status: Accepted

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

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

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

maxGCD = 1
for j in range(N):
    maxGCD = max(maxGCD, gcd(L[j], R[N-j-1]))
print(maxGCD)

Submission status: Accepted

Solution code:
from fractions import gcd

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

left = [0 for i in range(N+1)]
right = [0 for i in range(N+1)]

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

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

ans = 1

for i in range(N):
    l = left[i-1]
    r = right[i+1]
    if gcd(l, r) > ans:
        ans = gcd(l, r)

print(ans)

Submission status: Accepted

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

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

S1 = [A[0]]
s = A[0]
for i in range(1,N-1):
    s=gcd(s,A[i])
    S1.append(s)

S2 = [A[N-1]]
s = A[N-1]
for i in range(1,N-1):
    s=gcd(s,A[N-i-1])
    S2.append(s)

max_gcd = 1
for i in range(N):
    if i==0:
        max_gcd=max(max_gcd,S2[N-2])
    elif i==N-1:
        max_gcd=max(max_gcd,S1[N-2])
    else:
        max_gcd=max(max_gcd,gcd(S1[i-1],S2[N-2-i]))

print(max_gcd)

Submission status: Accepted

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

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

for i in range(a[1]):
  flag = 2
  for j in range(n):
    if a[j] % (a[1]-i) != 0:
      flag = flag - 1
    if flag == 0:
      break
  else:
    print(a[1]-i)
    break
else:
  print(1)

Submission status: Accepted

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


n = int(input())
a = list(map(int,input().split()))
l = [0 for x in range(n+1)]
r = [0 for x in range(n+1)]

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

##
#
#print(a)
#print(l,r)
ans = 0
for x in range(n):
    #print(ans,l[x],r[x+1],gcd(l[x],r[x+1]))
    ans = max(ans,gcd(l[x],r[x+1]))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
nums = [int(i) for i in input().split()]
nums_sorted = sorted(list((set(nums))), reverse=True)
def compute_divs(n):
    divs = []
    if n == 1: return [1]
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            divs.append(i)
            if i != n // i:
                divs.append(n//i)
    divs.append(n)
    return divs
ans = 1
if len(nums_sorted) == 1: ans = nums[0]
else:
    divs1 = compute_divs(nums_sorted[0])
    divs2 = compute_divs(nums_sorted[1])
    divs = sorted(list(set(divs1+divs2)))
    for i in reversed(divs):
        c= 0
        for j in nums:
            if j % i != 0:
                c += 1
                if c == 2:
                    break
        if c <= 1:
            ans = i
            break
print(ans)