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: Runtime Error

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])
    print(ans*max(d.items(),key=lambda x:x[1])[1])

Submission status: Runtime Error

Solution code:
def break_primes(n):
    pn = n
    cnt = 0
    d = set()
    while pn % 2 == 0:
        pn //= 2
        d.add(2)
    p = 3
    while(p*p <= pn):
        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)]
    onemiss = [0 for _ in range(P)]
    for p in range(P):
        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]
            elif vec_n[p] < onemiss[p]:
                onemiss[p] = vec_n[p]
    ans = 1
    for p in range(P):
        ans *= primes[p] ** onemiss[p]
    print(ans)

Submission status: Runtime Error

Solution code:
import fractions
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
a.sort()
gcd_list = []
ans = 0
for i in range(1,n):
  gcd_list.append(fractions.gcd(a[0],a[i]))
gcd_list.sort()
gcd_list.remove(gcd_list[0])
for j in range(1,n-2):
  ans = max(ans,fractions.gcd(gcd_list[0],gcd_list[i]))
print(ans)

Submission status: Runtime Error

Solution code:
import math
#import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
a = 0
for i in range(n):
	L,R = 0,0
	for j in range(0,i):
		L = math.gcd(L,list1[j])
	for j in range(i+1,n):
		R = math.gcd(R,list1[j])
	a = max(a,math.gcd(L,R))

print(a)

Submission status: Runtime Error

Solution code:
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))

a = list1[0]
b = 0

for i in range(n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
if b == 1:
	if math.gcd(list1[0],list1[2]) < math.gcd(list1[1],list1[2]):
		b = 0
del list1[b]
a = list1[0]
for i in range(len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Runtime Error

Solution code:
import math
#import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
if math.gcd(list1[0],list1[1]) < math.gcd(list1[0],list1[-1]):
	a = math.gcd(list1[0],list1[-1])
	b = 0
else:
	a = math.gcd(list1[0],list1[1])
	b = -1

for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Runtime Error

Solution code:
import math
#import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
a = math.gcd(list1[0],list1[-1])
b = 0
for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Runtime Error

Solution code:
from math 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: Runtime Error

Solution code:
from math import gcd

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])
    
ans = max(toRight[-2], toLeft[2])
for i in range(3, N-3 + 1):
    now = gcd(toRight[i-1], toLeft[-i-1])
    ans = max(ans, now)

print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import fractions
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(int(n))
  return table
n=int(input())
a=list(map(int,input().split()))
a.sort()
x=[]
count=0
count1=0
count2=0
ans=1
tmp2=[]
for k in range(n):
    tmp=list(set(prime_decomposition(a[k])))
    tmp.sort()
    tmp2.append(list(prime_decomposition(a[k])))
    if count<len(tmp):
        count=len(tmp)
        x=tmp
        count1+=1
    elif count==len(tmp):
        for i in range(count):
            if x[i]<tmp[i]:
                x=tmp
                count1+=1
for l in range(len(tmp2[count1])):
    ans*=tmp2[count1][l]
for i in range(len(tmp2[count1])):
    for j in range(n):
        if a[j]%tmp2[count1][i]!=0:
            count2+=1
            if count2==2:
                break
            if j==n-1:
                ans*=tmp2[count1][i]
        if a[j]%tmp2[count1][i]==0:
            if j==n-1:
                ans*=tmp2[count1][i]
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import fractions as ft
n=int(input())
a=list(map(int,input().split()))
my_max=max(a)
b=[0]*(my_max+1)
def md(n):
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            b[i]+=1
            if i != n // i:
                b[n//i]+=1

def sosuu(n):
    for p in range(2, n):
        if n % p == 0:
            #print(str(n) + ' is composite.')
            return False
    return True

ans=0
for i in range(n):
    md(a[i])

m=n-1
cnt=0
for i in range(my_max,0,-1):
    if b[i]==m:
        cnt=i
        break
    if cnt==0:
        if b[i]==n:
            cnt=i
            
print(cnt)

Submission status: Runtime Error

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]
	elif ma < fractions.gcd(left[i-1], right[n-2-i]):
		ma = fractions.gcd(left[i-1], right[n-2-i])
		left.append(fractions.gcd(left[i-1], a[i]))

print(ma)

Submission status: Runtime Error

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]
	elif ma < math.gcd(left[i-1], right[n-2-i]):
		ma = fractions.gcd(left[i-1], right[n-2-i])
		left.append(fractions.gcd(left[i-1], a[i]))

print(ma)

Submission status: Runtime Error

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-1):
	if i == 0:
		ma = right[n-2]
	elif ma < fractions.gcd(left[i-1], right[n-2-i]):
		ma = fractions.gcd(left[i-1], right[n-2-i])
		left.append(fractions.gcd(left[i-1], a[i]))

print(ma)

Submission status: Runtime Error

Solution code:
import math

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: Runtime Error

Solution code:
from math 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[0]), gcd(0,...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: Runtime Error

Solution code:
from math import gcd
from functools import reduce
N = int(input())
a = list(map(int, input().split()))

list_ans = []

for i in range(N):
    remove_a = a.pop(0)
    #print("a is",a)
    ans = reduce(gcd, a)
    list_ans.append(ans)
    a.append(remove_a)
    #print("fix a is",a)

print(max(list_ans))

Submission status: Runtime Error

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: Runtime Error

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()))
    tmp = 0
    if gcd(A) == max(A):
        print(gcd(A))
        exit()
    else:
        for i in range(N):
            if i == 0 :
                tmp.append(gcd(A[1:]))
            elif i == N-1:
                tmp.append(gcd(A[:N-1]))
            else:
                tmp.append(gcd(A[:i]+A[i+1:]))
    print(max(tmp))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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()))
    tmp = 0
    if gcd(A) == max(A):
        print(gcd(A))
        exit()
    else:
        for i in range(N):
            if i == 0 :
                tmp=max(tmp,gcd(A[1:]))
            elif i == N-1:
                tmp=max(tmp,gcd(A[:N-1]))
            else:
                tmp=max(tmp,gcd(A[:i]+A[i+1:]))
    print(tmp)
 if __name__ == "__main__":
     main()

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))+[0]
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]
    
    for j in range(len(a)):
        if j==0:
            L=math.gcd(a[j],a[j+1])
        else:
            L=math.gcd(L,a[j])

    for k in range(len(b)):
        if k==0:
            R=math.gcd(b[k],b[k+1])
        else:
            R=math.gcd(R,b[k])
            
    ans.append(math.gcd(L,R))
    L=0
    R=0

print(max(ans))

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))+[0]
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]
    
    for j in range(len(a)):
        if j==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j])

    for k in range(len(b)):
        if k==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k])
            
    ans.append(gcd(L,R))
    L=0
    R=0
print(max(ans))

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
A=list(map(int,input().split()))+[0]
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]

    for j in range(len(a)):
        if j==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j])

    for k in range(len(b)):
        if k==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k])
        
    ans.append(gcd(L,R))
    L=0
    R=0
        
            
print(max(ans))

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
A=list(map(int,input().split()))+[0]
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]

    for j in range(len(a)-1):
        if L==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j+1])

    for k in range(len(b)-1):
        if R==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k+1])
        
    ans.append(gcd(L,R))
    L=0
    R=0
        
            
print(max(ans))

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
A=list(map(int,input().split()))
L=0
R=0
ans=[]
for i in range(N-1):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]

    for j in range(len(a)-1):
        if L==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j+1])

    for k in range(len(b)-1):
        if R==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k+1])
        
    ans.append(gcd(L,R))
    L=0
    R=0
        
            
print(max(ans))

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
A=list(map(int,input().split()))
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]

    for j in range(len(a)-1):
        if L==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j+1])

    for k in range(len(b)-1):
        if R==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k+1])
        
    ans.append(gcd(L,R))
    L=0
    R=0
        
            
print(max(ans))

Submission status: Runtime Error

Solution code:
from sys import stdin
from fractions import gcd
#from math import gcd

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

def gcd_all(list):
    return gcd_all([gcd(*list[:2])] + list[2:]) if len(list) > 1 else list[0]

ma = -50
for i in range(n):
    lin = [li[s] for s in range(n) if s != i]
    ma = max(ma,gcd_all(lin))

print(ma)

Submission status: Runtime Error

Solution code:
from sys import stdin
import fractions

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

def gcd_all(list):
    return gcd_all([fractions.gcd(*list[:2])] + list[2:]) if len(list) > 1 else list[0]

ma = 0
for i in range(n):
    lin = [li[s] for s in range(n) if s != i]
    ma = max(ma,gcd_all(lin))

print(ma)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

def gcd_all(list):
    return gcd_all([math.gcd(*list[:2])] + list[2:]) if len(list) > 1 else list[0]

ma = 0
for i in range(n):
    lin = [li[s] for s in range(n) if s != i]
    ma = max(ma,gcd_all(lin))

print(ma)

Submission status: Runtime Error

Solution code:
n=int(input())
lista = [int(i) for i in input().split()]

import fractions

list_ans=[]

for i in range(n):
	listb=lista.copy()
	listb.pop(i)
#	print(listb)

	ans= listb[0]

	for i in range(1,n-1):
		ans=fractions.gcd(ans,listb[i])
#	print(ans)
	list_ans.append(ans)

print(int(max(list_ans)))

Submission status: Runtime Error

Solution code:
n=int(input())
lista = [int(i) for i in input().split()]

import fractions

list_ans=[]

for i in range(n):
	listb=lista.copy()
	listb.pop(i)
#	print(listb)

	ans= listb[0]

	for i in range(1,n-1):
		ans=fractions.gcd(ans,listb[i])
#	print(ans)
	list_ans.append(ans)

print(max(list_ans))

Submission status: Runtime Error

Solution code:
n=int(input())
lista = [int(i) for i in input().split()]

import fractions

list_ans=[]

for i in range(n):
	listb=lista.copy()
	listb.pop(i)
#	print(listb)

	ans= listb[0]

	for i in range(1,n-1):
		ans=fractions.gcd(ans,listb[i])
#	print(ans)
	list_ans.append(ans)
	print(max(list_ans))

Submission status: Runtime Error

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[N]
    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: Runtime Error

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[N]
    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: Runtime Error

Solution code:
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[N]
    else:
        tmp = gcd(right[i] , left[i - 1])
        if tmp > max_val:
            max_val = tmp
print(max_val)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce

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

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

gcd_box=[]

for i in range(1,n-1):
    L_gcd=gcd_list(a[:i])
    R_gcd=gcd_list(a[i+1:])
    gcd_box.append(fractions.gcd(L_gcd,R_gcd))
    
print(max(gcd_box))

Submission status: Runtime Error

Solution code:
import math,copy

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

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

for i in range(N):
    A_rest = A.copy()
    A_rest.remove(A_rest[i])
    pre = A_rest[0]
    for j in range(1,N-1):
        gcd_tmp = math.gcd(pre,A_rest[j])
        pre = gcd_tmp
    gcd_list.append(pre)

print(max(gcd_list))

Submission status: Runtime Error

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

res = 0
for i in range(n):
    aa = a[:i] + a[i+1:]
    gcd = reduce(math.gcd, aa)
    res = max(res,gcd)

print(res)

Submission status: Runtime Error

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

res = 0
for i in range(n):
    aa = a[:i] + a[i+1:]
    gcd = functools.reduce(math.gcd, aa)
    res = max(res,gcd)

print(res)

Submission status: Runtime Error

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(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: Runtime Error

Solution code:
import 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)])
l[0] = r[0] = l[-1] = r[-1] = 0

ans = [math.gcd(l[i], r[i+2]) for i in range(n)]
print(max(ans))

Submission status: Runtime Error

Solution code:
from functools import reduce
from math import gcd

n = int(input())
a = [int(i) for i in input().split()]
ans = -1
for i in range(1,n+1):
    ans = max(ans,reduce(gcd,a[:i-1]+a[i:]))
print(ans)

Submission status: Runtime Error

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

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


dp=[0]*N
dp[1]=max(A[0],A[1])
dp[2]=max(gcd(A[0],A[1]),gcd(A[1],A[2]),gcd(A[2],A[0]))
for i in range(3,N):
  dp[i]=gcd(dp[i-1],A[i])

print(dp[N-1])

Submission status: Runtime Error

Solution code:
from fractions import gcd
from math import gcd
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
ans = 0

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

Submission status: Runtime Error

Solution code:
from fractions import gcd
from math import gcd
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
ans = 0

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

Submission status: Runtime Error

Solution code:
import math

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

Prefix=[0 for i in range(n + 2)] 
Suffix=[0 for i in range(n + 2)] 

Prefix[1] = a[0] 
for i in range(2,n+1): 
    Prefix[i] = math.gcd(Prefix[i - 1], a[i - 1]) 

Suffix[n] = a[n - 1] 

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

ans = max(Suffix[2], Prefix[n - 1]) 

for i in range(2,n): 
    ans = max(ans, math.gcd(Prefix[i - 1], Suffix[i + 1])) 

print(ans)

Submission status: Runtime Error

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


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    A_sorted = sorted(list(set(A)))

    if len(A_sorted) <= 2:
        print(max(A_sorted))
        exit()

    min = 10 ** 10
    min_index = -1
    for i in range(N - 1):
        x = fractions.gcd(A_sorted[i], A_sorted[i + 1])
        if min > x:
            min = x
            min_index = i

    gcd = A_sorted[0]
    if min_index == 0:
        gcd = A_sorted[2]

    for i in range(N):
        if i == min_index or i == min_index + 1:
            continue
        gcd = fractions.gcd(gcd, A_sorted[i])

    x = fractions.gcd(gcd, A_sorted[min_index])
    y = fractions.gcd(gcd, A_sorted[min_index + 1])

    print(max([x, y]))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    A_sorted = sorted(set(A))

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

    min = 10 ** 10
    min_index = -1
    for i in range(N - 1):
        x = fractions.gcd(A_sorted[i], A_sorted[i + 1])
        if min > x:
            min = x
            min_index = i

    gcd = A_sorted[0]
    if min_index == 0:
        gcd = A_sorted[2]

    for i in range(N):
        if i == min_index or i == min_index + 1:
            continue
        gcd = fractions.gcd(gcd, A_sorted[i])

    x = fractions.gcd(gcd, A_sorted[min_index])
    y = fractions.gcd(gcd, A_sorted[min_index + 1])

    print(max([x, y]))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys

from math import gcd

#+++++
		
		
def main2():
	#print(gcd(100,55))
	
	n = int(input())
	al = list(map(int, input().split()))
	
	if n == 2:
		return max(al)
	
	nu=[0]*len(al)
	fi=0
	snd=0
	for i, _ in enumerate(nu):
		vv=al[0]
		for j, v in enumerate(al):
			if i==j:
				continue
			vv=gcd(vv, v)
		ans = max(ans, vv)
	
	print(ans)
	
def main():
	#print(gcd(100,55))
	
	n = int(input())
	al = list(map(int, input().split()))
	
	if n == 2:
		return max(al)
	
	from_left=[0]*len(al)
	kk=al[0]
	for i, v in enumerate(al):
		kk=gcd(kk,v)
		from_left[i]=kk
	
	from_right=[0]*len(al)
	kk=al[-1]
	for i,v in enumerate(al[::-1]):
		kk=gcd(kk,v)
		from_right[i]=kk
	from_right=from_right[::-1]
	
	ans=0
	#pa(from_right)
	#pa(from_left)
	#sayuukara
	for i,_ in enumerate(al):
		if i == 0:
			vv=from_right[1]
		elif i == len(al)-1:
			vv=from_left[-2]
		else:
			a=from_right[i+1]
			b=from_left[i-1]
			#pa((a,b))
			vv=gcd(a,b)
		ans = max(ans, vv)
	
	print(ans)




#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from math import gcd
n=int(input())
a=list(map(int,input().split()))
m=[0]*n
L,R=[0]*(n+1),[0]*(n+1)
for i in range(1,n+1):
    L[i]=gcd(L[i-1],a[i-1])
    R[n-i]=gcd(R[n-i+1],a[n-i])
for i in range(n):
    m[i]=gcd(L[i],R[i+1])
print(max(m))

Submission status: Runtime Error

Solution code:
#C
import sys
from math import gcd
n=int(input())
a=list(map(int,input().split()))
if len(a)==2:
    print(max(a))
    sys.exit()
g=gcd(a[0],a[1])
b=[]
for i in range(2,n):
    l=gcd(g,a[i])
    if l!=g:
        b.append(i)
        g=l
s=[]
g=gcd(a[0],a[2])
for i in range(3,n):
    g=gcd(g,a[i])
s.append(g)
g=gcd(a[1],a[2])
for i in range(3,n):
    g=gcd(g,a[i])
s.append(g)
for i in range(len(b)):
    g=gcd(a[0],a[1])
    for j in range(2,n):
        if j==b[i]:
            continue
        g=gcd(g,a[j])
    s.append(g)
print(max(s))

Submission status: Runtime Error

Solution code:
from fractions import gcd
N = int(input())
array = list(map(int,input().split()))
gcd_arr = [array[0]]

for i in range(N-1):
  gcd_arr.append(gcd(gcd_arr[-1],array[i+1]))
  
gcd_arr.reverse()

for i in range(N):
  if gcd_arr[i+1] > gcd_arr[i]:
    break
    
if (N-i-1) == 1:
  new_gcd1 = array[1]
  for i in range(N-2):
    new_gcd1 = gcd(new_gcd1,array[i+2])
  array.pop(1)
  new_gcd2 = array[0]
  for i in range(N-2):
    new_gcd2 = gcd(new_gcd2,array[i+1])
  print(max(new_gcd1,new_gcd2))
  
else:
  array.pop(N-i-1)  
  new_gcd = array[0]
  for i in range(N-2):
    new_gcd = gcd(new_gcd,array[i+1])

  print(new_gcd)