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:
import numpy as np
import copy

N = int(input())
A = [int(i) for i in input().split()]
A = list(set(A))

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a  
if N<=2:
    print(max(A))
else:
    gcds = []
    for n in range(N):
        if n == 0:
            lgcd = None
        else:
            lgcd = A[0]
            for i in range(n):
                lgcd = gcd(lgcd,A[i])
        if n == N-1:
            rgcd = None
        else:
            rgcd = A[n+1]
            for i in range(n+1,N):
                rgcd = gcd(rgcd,A[i])
        if lgcd == None:
            gcds.append(rgcd)
        elif rgcd == None:
            gcds.append(lgcd)
        else:
            gcds.append(gcd(lgcd,rgcd))
print(max(gcds))

Submission status: Runtime Error

Solution code:
def gcd(a, b):
    min_ = min(a, b)
    max_ = max(a, b)
    if max_ == min_:
        return 1
    else:
        
        if max_ % min_ == 0:
            return min_
        else:
            return gcd(min_, max_ % min_) 

N = int(input())
A = list(map(int, input().split()))
re = [0]*(N**2)
for i in range(N):
    for j in range(N):
        re[i*N+j] = gcd(A[i], A[j])
        
print(max(re))

Submission status: Runtime Error

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
x,y,z=[a[0]],[a[N-1]],[]
for i in range(N-1):
	x.append(fr.gcd(x[i],a[i+1]))
	y.append(fr.gcd(y[i],a[N-1-i]))
for i in range(1,N-1):
	z.append(fr.gcd(x[i-1],y[N-i]))
print(max(z))

Submission status: Runtime Error

Solution code:
import fractions
from itertools import combinations
from functools import reduce
 
def gcd(numbers):
    return reduce(fractions.gcd, numbers)
 
 
N = int(input())
A_list = map(int, input().split())
 
ans = 0
 
for A_product in combinations(A_list, N-1):
    if gcd(list(A_product)) > ans:
        ans = gcd(A_product)
 
print(ans)
import math
from itertools import combinations
from functools import reduce

def gcd(numbers):
    return reduce(math.gcd, numbers)


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

ans = 0

for A_product in combinations(A_list, N-1):
    if gcd(list(A_product)) > ans:
        ans = gcd(A_product)

print(ans)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
number = list(map(int,input().split(' ')))

leftgcd = [0 for i in range(n)]
leftgcd[0] = number[0]
rightgcd = [0 for i in range(n)]
rightgcd[-1] = number[-1]


for i in range(1,n):
    leftgcd[i] = math.gcd(leftgcd[i-1],number[i])

for i in reversed(range(n-1)):
    rightgcd[i] = math.gcd(rightgcd[i+1],number[i])

ans = list()
for i in range(n):
    if i==0:
        ans.append(rightgcd[i+1])
    elif i == n-1:
        ans.append(leftgcd[i-1])
    else:
        ans.append(math.gcd(leftgcd[i-1],rightgcd[i+1]))

# print(leftgcd,rightgcd,ans,sep='\n')
print(max(ans))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
number = list(map(int,input().split(' ')))

leftgcd = [0 for i in range(n)]
leftgcd[0] = number[0]
rightgcd = [0 for i in range(n)]
rightgcd[-1] = number[-1]


for i in range(1,n):
    leftgcd[i] = math.gcd(leftgcd[i-1],number[i])

for i in reversed(range(n-1)):
    rightgcd[i] = math.gcd(rightgcd[i+1],number[i])

ans = list()
for i in range(n):
    if i==0:
        ans.append(rightgcd[i+1])
    elif i == n-1:
        ans.append(leftgcd[i-1])
    else:
        ans.append(math.gcd(leftgcd[i-1],rightgcd[i+1]))

print(leftgcd,rightgcd,ans,sep='\n')
print(max(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
answer = sorted(a)[1]
for b in range(answer).reverse():
    cnt = 2
    flag = True
    for i in range(n):
        if a[i] % answer != 0:
            cnt -= 1
        if cnt == 0:
            flag = False
    if flag:
        break
    answer -= 1
print(answer)

Submission status: Runtime Error

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

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

data_ans=[]
data1=[A[0]]
data2=[A[N-1]]
l=A[0]
m=A[N-1]
for i in range(1,N-3):
  l=gcd(l,A[i])
  m=gcd(m,A[N-i])
  data1.append(l)
  data2.append(m)
l=gcd(l,A[N-1])
m=gcd(m,A[1])
data_ans.append(l)
data_ans.append(m)
for i in range(0,N-3):
  data_ans.append(data1[i],data2[N-2-i])
print(max(data_ans))

Submission status: Runtime Error

Solution code:
import math

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

gcd_l = [0]
gcd_r = [0]

for i in range(N):
    gcd_l.append(math.gcd(gcd_l[i], A[i]))
    gcd_r.append(math.gcd(gcd_r[i], A[-(i+1)]))
gcd_r = gcd_r[::-1]

ans = []
for i in range(N):
    ans.append(math.gcd(gcd_l[i], gcd_r[i+1]))
ans_2 = max(ans)
print(ans_2)

Submission status: Runtime Error

Solution code:
import math

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

gcd_l = [0]
gcd_r = [0]

for i in range(N):
    gcd_l.append(math.gcd(gcd_l[i], A[i]))
    gcd_r.append(math.gcd(gcd_r[i], A[-(i+1)]))
gcd_r = gcd_r[::-1]

ans = []
for i in range(N):
    ans.append(math.gcd(gcd_l[i], gcd_r[i+1]))
print(max(ans))

Submission status: Runtime Error

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

def gcd(m, n):
  while n:
    m, n = n, m%n
  return m

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

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

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

Submission status: Runtime Error

Solution code:
from math import gcd
N = int(input())
A = [int(s) for s in input().split()]
ans = 0
L = [0] * (N)
R = [0] * (N)
for n in range(0, N):
    if n != N-1:
        L[n+1] = gcd(A[n], L[n])
        R[-(n+2)] = gcd(A[-(n+1)], R[-(n+1)])
for n in range(0, N):
    temp = gcd(L[n], R[n])
    ans = max(temp, ans)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import os
sys.stdin = open('input.txt', 'r')

from fractions import gcd
N = int(input())
A = [int(s) for s in input().split()]
ans = 0
L = [0] * (N)
R = [0] * (N)
for n in range(0, N):
    if n != N-1:
        L[n+1] = gcd(A[n], L[n])
        R[-(n+2)] = gcd(A[-(n+1)], R[-(n+1)])
for n in range(0, N):
    temp = gcd(L[n], R[n])
    ans = max(temp, ans)
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd
N = int(input())
A = [int(s) for s in input().split()]
ans = 0
L = [0] * (N)
R = [0] * (N)
for n in range(0, N):
    if n != N-1:
        L[n+1] = gcd(A[n], L[n])
        R[-(n+2)] = gcd(A[-(n+1)], R[-(n+1)])
print(L, R)
for n in range(1, N):
    temp = gcd(L[n], R[n])
    ans = max(temp, ans)
print(ans)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
A = [int(s) for s in input().split()]
ans = 0
L = [A[0]] * (N)
R = [A[-1]] * (N)
for n in range(1, N):
    L[n] = math.gcd(A[n], L[n-1])
    R[-(n+1)] = math.gcd(A[-(n+1)], R[-n])
for n in range(1, N-1):
    temp = math.gcd(L[n-1], R[n+1])
    ans = max(temp, ans)
print(ans)

Submission status: Runtime Error

Solution code:
import math

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

d = A[0]
flg = False
flg2 = False

for i in range(N):
    pd = d
    d = math.gcd(d, A[i])
    if d < pd and (not flg):
        d = pd
        flg = True

dd = A[1]
for i in range(N):
    pdd = dd
    dd = math.gcd(dd, A[i])
    if dd < pdd and (not flg2):
        dd = pdd
        flg2 = True

print(max(d, dd))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
A = list(map(int, input().split()))
d = A[0]
flg = False
flg2 = False
for i in range(N):
    pd = d
    d = math.gcd(d, A[i])
    if d < pd and not flg:
        d=pd
        flg = True
dd = A[1]
for i in range(N):
    pdd = dd
    dd = math.gcd(dd, A[i])
    if dd < pdd and not flg2:
        dd=pdd
        flg2 = True
print(max(d, dd))

Submission status: Runtime Error

Solution code:
from fraction import gcd
n = int(input())
A = list(map(int,input().split()))
left = [0]*(n+1)
right = [0]*(n+2)
for a,aa,i in zip(A,A[::-1],range(1,n+1)):
    left[i] = gcd(a,left[i-1])
    right[n+1-i] = gcd(aa,right[n-i+2])
ans = 1
for i in range(1,n+1):
    if i ==1:
        if ans < right[i+1]:
            ans = right[i+1]
    elif i == n:
        if ans < left[i-1]:
            ans = left[i-1]
    else:
        if ans < gcd(left[i-1],right[i+1]):
            ans = gcd(left[i-1],right[i+1])

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
l_list = [0]
r_list = [0]
for i in range(n):
    l = gcd(l_list[i], a[i])
    l_list.append(l)
    r = gcd(r_list[-1], a[-i-1])
    r_list.append(r)
r_list = list(reversed(r_list))
r_list = r_list[1:]
l_list = l_list[:n]
c_list = []
for i in range(n):
    c_list.append(gcd(l_list[i], r_list[i]))
print(max(c_list))

Submission status: Runtime Error

Solution code:
import math
import copy

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

def gcd_list(numbers):
    g = numbers[0]
    for m in numbers:
        g = math.gcd(g, m)
    return g

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

gcds = list()
for a in A:
    B = copy.copy(A)
    B.remove(a)
    g = gcd_list(B)
    gcds.append(g)
print(max(gcds))

Submission status: Runtime Error

Solution code:

import math
import copy

def gcd_list(numbers):
    g = numbers[0]
    for m in numbers:
        g = math.gcd(g, m)
    return g

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

gcds = list()
for a in A:
    # B = copy.deepcopy(A)
    # B.remove(a)
    gcd = gcd_list(A)
    gcds.append(gcd)
    # del B
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
import copy

def gcd_list(numbers):
    g = numbers[0]
    for m in numbers:
        g = math.gcd(g, m)
    return g

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

gcds = list()
for a in A:
    B = copy.deepcopy(A)
    B.remove(a)
    gcd = gcd_list(B)
    gcds.append(gcd)
    del B
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
import copy

def gcd_list(numbers):
    gcd = numbers[0]
    for m in numbers:
        gcd = math.gcd(gcd, m)
    return gcd

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

gcds = list()
for a in A:
    B = copy.deepcopy(A)
    B.remove(a)
    gcd = gcd_list(B)
    gcds.append(gcd)
    del B
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
# from functools import reduce
from copy import deepcopy

def gcd_list(numbers):
    gcd = numbers[0]
    for m in numbers[1:]:
        gcd = math.gcd(gcd, m)
    return gcd
    # return reduce(math.gcd, numbers)

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

gcds = list()
for a in A:
    B = deepcopy(A)
    B.remove(a)
    gcd = gcd_list(B)
    gcds.append(gcd)
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
from copy import deepcopy

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

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

gcds = list()
for a in A:
    B = deepcopy(A)
    B.remove(a)
    gcd = gcd_list(B)
    gcds.append(gcd)
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
A = list(map(int, input().split()))
B = [0]
C = [0]
for i in range(N):
  B.append(math.gcd(B[-1],A[i]))
  C.append(math.gcd(C[-1], A[-1-i]))

D = []
for i in range(N):
    D.append(math.gcd(B[i],C[N-i-1]))

print(max(D))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
from copy import *

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

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

gcds = list()
for a in A:
    B = deepcopy(A)
    B.remove(a)
    gcd = gcd_list(B)
    gcds.append(gcd)
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
from copy import *

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
gcds = list()
for a in A:
    B = deepcopy(A)
    B.remove(a)
    gcd = gcd_list(B)
    gcds.append(gcd)
gcds.remove(min(gcds))
print(min(gcds))

Submission status: Runtime Error

Solution code:
import numpy as np
import math
from functools import reduce

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

n = input()
a = input().split()
N = int(n)
A = np.array(a, dtype=int)
max_gcd = 0

for i in range(N):
    X = A[:i]
    Y = A[i+1:]
    X = np.hstack((X,Y))
    gcd_X = gcd(*X)
    if gcd_X > max_gcd:
        max_gcd = gcd_X
print(max_gcd)

Submission status: Runtime Error

Solution code:
#最大公約数を求める再帰関数-->計算量O(log(max(x))
def gcd(x, y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x
    return gcd(x-y, y)


#本文
N = int(input())
list_A = list(map(int, input().split()))

#配列初期化
list_gcd = [0] * N
list_gcd_rev = [0] * N

#以下　間違いを発見できず

##配列の最初のgcdを求める。定義
#list_gcd[0] = list_A[0]
#list_gcd_rev[N-1] = list_A[N-1]
##print (list_gcd, list_gcd_rev)

##配列にgcdを代入していく
#for i in range(1,N):
#    list_gcd[i] = gcd(list_gcd[i-1], list_A[i])
##print (list_gcd)
#for j in range(1,N):
#    list_gcd_rev[N-1-j] = gcd(list_gcd_rev[N-j], list_A[N-1-j])
##print (list_gcd_rev)

##print (list_gcd, list_gcd_rev)

##k番目までの両端からのgcdを求める
#if N == 2:
#    ans = gcd(list_gcd[0], list_gcd_rev[1])
#else:
#    ans = max(list_gcd[N-2], list_gcd_rev[1]) #両端の文字を除いたときの答え
##print (ans)
#for k in range(1,N-1):
#    a = list_gcd[k-1]
#    b = list_gcd_rev[k+1]
#    ans = max(ans, gcd(a, b))
#print (ans)

#以下、正答

for i in range(1,N):
    list_gcd[i]=gcd(list_gcd[i-1],list_A[i-1])
for i in range(N-1)[::-1]:
    list_gcd_rev[i]=gcd(list_gcd_rev[i+1],list_A[i+1])
Z = [gcd(list_gcd[i], list_gcd_rev[i]) for i in range(N)]
print (max(Z))

Submission status: Runtime Error

Solution code:
import math


def solute(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        gcd_right.append((math.gcd(gcd_right[i], a[-(i+1)])))
    gcd_right = gcd_right[::-1]

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i+1]))
    return max(ans)


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

Submission status: Runtime Error

Solution code:
#最大公約数を求める再帰関数-->計算量O(log(max(x))
def gcd(x, y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x
    return gcd(x-y, y)


#本文
N = int(input())
list_A = list(map(int, input().split()))

#配列初期化
list_gcd = [0] * N
list_gcd_rev = [0] * N

#配列の最初のgcdを求める。定義
list_gcd[0] = list_A[0]
list_gcd_rev[N-1] = list_A[N-1]
#print (list_gcd, list_gcd_rev)

#配列にgcdを代入していく
for i in range(1,N):
    list_gcd[i] = gcd(list_gcd[i-1], list_A[i])
#print (list_gcd)
for j in range(1,N):
    list_gcd_rev[N-1-j] = gcd(list_gcd_rev[N-j], list_A[N-1-j])
#print (list_gcd_rev)

#print (list_gcd, list_gcd_rev)

#k番目までの両端からのgcdを求める
if N == 2:
    ans = gcd(list_gcd[0], list_gcd_rev[1])
else:
    ans = max(list_gcd[N-2], list_gcd_rev[1]) #両端の文字を除いたときの答え
#print (ans)
for k in range(1,N-1):
    a = list_gcd[k-1]
    b = list_gcd_rev[k+1]
    ans = max(ans, gcd(a, b))
print (ans)

Submission status: Runtime Error

Solution code:
#最大公約数を求める再帰関数-->計算量O(log(max(x))
def gcd(x, y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x
    return gcd(x-y, y)


#本文
N = int(input())
list_A = list(map(int, input().split()))

#配列初期化
list_gcd = [0] * N
list_gcd_rev = [0] * N

#配列の最初のgcdを求める。定義
list_gcd[0] = list_A[0]
list_gcd_rev[N-1] = list_A[N-1]
#print (list_gcd, list_gcd_rev)

#配列にgcdを代入していく
for i in range(1,N):
    list_gcd[i] = gcd(list_gcd[i-1], list_A[i])
#print (list_gcd)
for j in range(1,N):
    list_gcd_rev[N-1-j] = gcd(list_gcd_rev[N-j], list_A[N-1-j])
#print (list_gcd_rev)

#print (list_gcd, list_gcd_rev)

#k番目までの両端からのgcdを求める
if N == 2:
    ans = gcd(list_gcd[N-2], list_gcd_rev[1])
else:
    ans = max(list_gcd[N-2], list_gcd_rev[1]) #両端の文字を除いたときの答え
#print (ans)
for k in range(1,N-1):
    a = list_gcd[k-1]
    b = list_gcd_rev[k+1]
    ans = max(ans, gcd(a, b))
print (ans)

Submission status: Runtime Error

Solution code:
#最大公約数を求める再帰関数-->計算量O(log(max(x))
def gcd(x, y):
    if x < y:
        x, y = y, x
    if y == 0:
        return x
    return gcd(x-y, y)


#本文
N = int(input())
list_A = list(map(int, input().split()))

#配列初期化
list_gcd = [0] * N
list_gcd_rev = [0] * N

#配列の最初のgcdを求める。定義
list_gcd[0] = list_A[0]
list_gcd_rev[N-1] = list_A[N-1]
#print (list_gcd, list_gcd_rev)

#配列にgcdを代入していく
for i in range(1,N):
    list_gcd[i] = gcd(list_gcd[i-1], list_A[i])
#print (list_gcd)
for j in range(1,N,):
    list_gcd_rev[N-1-j] = gcd(list_gcd_rev[N-j], list_A[N-1-j])
#print (list_gcd_rev)

#print (list_gcd, list_gcd_rev)

#k番目までの両端からのgcdを求める
ans = max(list_gcd[N-2], list_gcd_rev[1]) #両端の文字を除いたときの答え
#print (ans)
for k in range(1,N-1):
    a = list_gcd[k-1]
    b = list_gcd_rev[k+1]
    ans = max(ans, gcd(list_gcd[k-1], list_gcd_rev[k+1]))
print (ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
using namespace std;

int gcd(int a, int b);

int main() {
    int N; cin >> N;
    int A[N]; for (int i = 0; i < N; i++) cin >> A[i];
    int left, right, max = 0;
    for (int n = 0; n < N; n++) {
        left = 0;
        right = 0;
        for (int i = 0; i < n; i++) left = gcd(left, A[i]);
        for (int i = N-1; i > n; i--) right = gcd(right, A[i]);
        if (gcd(left, right) > max) max = gcd(left, right);
    }
    cout << max << endl;
}

int gcd(int a, int b) {
    if (a < b) swap(a, b);
    if (b == 0) return a;
    int r = a % b;
    while (r != 0) {
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

Submission status: Runtime Error

Solution code:
import math

n = int(input())
a = list(map(int, input().split()))
print(solution(n, a))  
gcd_left = [0]
gcd_right = [0]
for i in range(n):
    gcd_left.append(math.gcd(gcd_left[i], a[i]))
    # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
    # 要は右端から順にgcdを求めている
    gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

ans = []
for i in range(n):
    ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


print(max(ans))

Submission status: Runtime Error

Solution code:
import math

n = int(input())
a = list(map(int, input().split()))
print(solution(n, a))  
gcd_left = [0]
gcd_right = [0]
for i in range(n):
    gcd_left.append(math.gcd(gcd_left[i], a[i]))
    # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
    # 要は右端から順にgcdを求めている
    gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

ans = []
for i in range(n):
    ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


return max(ans)

Submission status: Runtime Error

Solution code:
import math


def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))  
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)

if __name__ == '__main__':
  
    main()

Submission status: Runtime Error

Solution code:
import math


def main():
  
	n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))  
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)

if __name__ == '__main__':
  
    main()

Submission status: Runtime Error

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


def cul(n,a):
    l=len(a)
    b=[a[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=10
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0
for i in range( math.ceil((M+1)**0.5) ):
    if M%i==0:
        cnt=0
        for j in range(p,N):
            if a[i]%i!=0:
                cnt+=1
                if cnt==2: break
            elif i==N-1 and i>ans: ans=i
        
        x=M//i
        cnt=0
        for j in range(p,N):
            if a[i]%x!=0:
                cnt+=1
                if cnt==2: break
            elif i==N-1 and x>ans: ans=i

r=a[N-1]
for i in range(N-1,p-1,-1):
    r=gcd(r,a[i])
if r>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),r)
        if s>ans: ans=s

print(ans)

Submission status: Runtime Error

Solution code:
import math


def main():
	n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))  
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)

main()

Submission status: Runtime Error

Solution code:
import math


def main():
	n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))  
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)

if __name__ == '__main__':
  
    main()

Submission status: Runtime Error

Solution code:
import unittest
import math


def solution(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)


class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertEqual(solution(3, [7, 6, 8]), 2)

    def test2(self):
        self.assertEqual(solution(3, [12, 15, 18]), 6)

    def test3(self):
        self.assertEqual(solution(2, [1000000000, 1000000000]), 1000000000)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))


main()

Submission status: Runtime Error

Solution code:
import unittest
import math


def solution(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)


class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertEqual(solution(3, [7, 6, 8]), 2)

    def test2(self):
        self.assertEqual(solution(3, [12, 15, 18]), 6)

    def test3(self):
        self.assertEqual(solution(2, [1000000000, 1000000000]), 1000000000)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))


if __name__ == '__main__':
  
    main()
    unittest.main()

Submission status: Runtime Error

Solution code:
import math

def solution(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def cul(n,a):
    l=len(a)
    b=[a[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=10
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0
for i in range( math.ceil((M+1)**0.5) ):
    if M%i==0:
        
        for j in range(p,N):
            if a[i]%i!=0: break
            elif i==N-1 and i>ans: ans=i
        x=M//i
        for j in range(p,N):
            if a[i]%x!=0: break
            elif i==N-1 and x>ans: ans=i

r=a[N-1]
for i in range(N-1,p-1,-1):
    r=gcd(r,a[i])
if r>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),r)
        if s>ans: ans=s

print(ans)

Submission status: Runtime Error

Solution code:
import unittest
import math


def solution(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))


    return max(ans)


class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertEqual(solution(3, [7, 6, 8]), 2)

    def test2(self):
        self.assertEqual(solution(3, [12, 15, 18]), 6)

    def test3(self):
        self.assertEqual(solution(2, [1000000000, 1000000000]), 1000000000)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def cul(n,a):
    l=len(a)
    b=[a[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=10
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(tmp,a[i])

ans=0
for i in range( math.ceil((M+1)**0.5) ):
    if M%i==0:
        
        for j in range(p,N):
            if a[i]%i!=0: break
            elif i==N-1 and i>ans: ans=i
        x=M//i
        for j in range(p,N):
            if a[i]%x!=0: break
            elif i==N-1 and x>ans: ans=i

r=a[N-1]
for i in range(N-1,p-1,-1):
    r=gcd(r,a[i])
if r>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),r)
        if s>ans: ans=s

print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin, exit
from math import gcd


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

# List if length of 2 or shorter
if len(A) <= 2:
    print(max(A))
    exit()

# Left Side = [A0, 0, 0, 0, 0]
left_side = [0 for _ in range(N)]
left_side[0] = A[0]

# Right Side = [0, 0, 0, 0, An]
right_side = [0 for _ in range(N)]
right_side[-1] = A[-1]

for i in range(N - 1):  # DP: L1 = gcd(L0, A1); Maximum GCD on very RIGHT of list  (Dir -->)
    left_side[i + 1] = gcd(left_side[i], A[i + 1])

for i in range(1, N):   # DP: Rn-2 = gcd(An-2, Rn-1); Maximum GCD on very LEFT of list  (Dir <--)
    right_side[-1 * (i+1)] = gcd(A[-1*(i+1)], right_side[-1*i])

max_gcd = 1

# Left
max_gcd = max(max_gcd, right_side[1])
# Max of Right and Left
max_gcd = max(max_gcd, left_side[-2])

for i in range(1, N - 1):
    left = left_side[i - 1]
    # We exclude the number at i here, and compare whether new gcd is greater than previous
    right = right_side[i + 1]

    max_gcd = max(max_gcd, gcd(left, right))

print(max_gcd)

Submission status: Runtime Error

Solution code:
import math

def solution(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        # 順に(0とa[-1]), (a[-1]とa[-2]),...の比較
        # 要は右端から順にgcdを求めている
        gcd_right.append(math.gcd(gcd_right[i], a[-(i + 1)]))

    gcd_right = gcd_right[::-1]  # gcd_right[i]がi番目以降のgcdとなるように反転

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i + 1]))
        
    return max(ans)

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    print(solution(n, a))