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:
N = int(input())
A = list(map(int, input().split()))

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

from functools import lru_cache
@lru_cache(maxsize = None)

def L(x):
    if x <= 0:
        return 0
    else:
        return gcd(L(x-1), A[x-1])

def R(x):
    if x >= N - 1:
        return 0
    else:
        return gcd(A[x+1], R(x+1))

for i in range(N-1, -1, -1):
    R(i)

w = 1
for i in range(N):
    w = max(w, gcd(L(i), R(i)))

print(w)

Submission status: Runtime Error

Solution code:
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[N - i - 1]))

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

print(ans)

Submission status: Runtime Error

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

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

from functools import lru_cache
@lru_cache(maxsize = 10005)

def L(x):
    if x <= 0:
        return 0
    else:
        return gcd(L(x-1), A[x-1])

def R(x):
    if x >= N - 1:
        return 0
    else:
        return gcd(A[x+1], R(x+1))

w = 1
for i in range(N):
    w = max(w, gcd(L(i), R(i)))

print(w)

Submission status: Runtime Error

Solution code:
from math import gcd

n = int(input())
a = list(map(int, input().split()))
fromLeft = [0] * n
fromLeft[0] = a[0]
fromRight = [0] * n
fromRight[0] = a[n - 1]
for i in range(1, n):
    fromLeft[i] = gcd(fromLeft[i - 1], a[i])
    fromRight[i] = gcd(fromRight[i - 1], a[n - i - 1])
ret = fromLeft[n - 1]
for i in range(n):
    if i == 0:
        curGcd = fromRight[n - 2]
    elif i == n - 1:
        curGcd = fromLeft[n - 2]
    else:
        curGcd = gcd(fromLeft[i - 1], fromRight[n - i - 2])
    ret = max(ret, curGcd)
print(ret)

Submission status: Runtime Error

Solution code:
import fractions
 
n = int(input())
a = [int(value) for value in input().split(" ")]
 
from_right_gcd = []
from_left_gcd = []
 
# 左から
for index in range(a.__len__):
    if index == 0:
        from_left_gcd.append(a[index])
        continue
    from_left_gcd.append(fractions.gcd(from_left_gcd[index - 1], a[index]))
 
# 右から
for index in range(a.__len__ - 1, -1, -1):
    if index == a.__len__ - 1:
        from_right_gcd.append(a[index])
        continue
    from_right_gcd.append(fractions.gcd(from_right_gcd[a.__len__ - index - 1 - 1], a[index]))
 
maybe_answer_list = []
 
for index in range(a.__len__):
    if index == 0:
        maybe_answer_list.append(from_right_gcd[a.__len__ - 1 - 1])
        continue
    if index == a.__len__ - 1:
        maybe_answer_list.append(from_left_gcd[index - 1])
        continue
    maybe_answer_list.append(fractions.gcd(from_left_gcd[index - 1], from_right_gcd[a.__len__ - index - 1 - 1]))
 
print(max(maybe_answer_list))

Submission status: Runtime Error

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

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

from functools import lru_cache
@lru_cache(maxsize = None)

def L(x):
    if x <= 0:
        return 0
    else:
        return gcd(L(x-1), A[x-1])

def R(x):
    if x >= N - 1:
        return 0
    else:
        return gcd(A[x+1], R(x+1))

w = 1
for i in range(N):
    w = max(w, gcd(L(i), R(i)))

print(w)

Submission status: Runtime Error

Solution code:
import copy
import math
from functools import reduce

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

gcd_list = []

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

for i in range(len(A)):
    tmp = copy.copy(A)
    del tmp[i]
    gcd_list.append(gcd(*tmp))

print(max(gcd_list))

Submission status: Runtime Error

Solution code:
import math

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

gcdlistL = [0]
for i in range(N):
    gcdlistL.append(math.gcd(gcdlistL[-1], A[i]))
#    print("gcdlistL[-1]={},A[{}]={}".format(gcdlistL[-1],i,A[i]))
#print("gcdlistL={}".format(gcdlistL))

gcdlistR = [0]
for i in range(N):
    gcdlistR.insert(0, math.gcd(A[N-1-i], gcdlistR[-1-i]))
#    print("gcdlistR[-1-{}]={},A[{}]={}".format(gcdlistR[-1-i],i,i,A[N-1-i]))
#print("gcdlistR={}".format(gcdlistR))

gcdmax = 0
for i in range(N):
    gcdmax = max(gcdmax, math.gcd(gcdlistL[i], gcdlistR[i+1]))
#    print("gcmax={},gcdL[{}]={},gcdR[{}]={}".format(gcdmax,gcdlistL[i],gcdlistR[-1-i],i,i,A[N-1-i]))

print(gcdmax)

Submission status: Runtime Error

Solution code:
N  = int(input())
As = [int(i) for i in input().split()]

# 約数
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

devs = make_divisors(A[0])
devs.extend(make_divisors(A[1]))

devs = list(set(devs))
cnt = 1
for dev in sorted(devs)[::-1]:
    a = sum([a%dev==0 for a in As])
    if N - a == 0 or N -a == 1:
        print(dev)
        break

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

for i in range(n):
    max = gcd_list(a[-i])
    if max > ans:
        ans = max
        
print(ans)

Submission status: Runtime Error

Solution code:
import functools
import copy

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

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

gcds = []
if N == 2:
    n = max(A)
else:
    for i in range(N-1):
        if i == 0:
            continue
        l = A[0:i]
        r = A[i+1:N]

        l_gcd = functools.reduce(lambda x, y: gcd(x, y), l)
        r_gcd = functools.reduce(lambda x, y: gcd(x, y), r)
        gcds.append(gcd(l_gcd, r_gcd))

print(max(gcds))

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))


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

list1=[0]
L=0
for a in A:
    L=gcd(L,a)
    list1.append(L)

list2=[0]
R=0
for a in reversed(A):
    R=gcd(R,a)
    list2.append(R)

list2.reverse()

ans=[]

for i in range(N+1):
    ans.append(gcd(list1[i],list2[i+1]))

print(max(ans))

Submission status: Runtime Error

Solution code:
import math, copy
def left(list, index):
    if index == 0:
        return 0
    else:
        return math.gcd(left(list, index-1), list[index-1])
def right(list, index):
    if index == len(list):
        return 0
    else:
        return math.gcd(right(list, index+1), list[index])
def ex_gcd(list, index):
    return math.gcd(left(list,index), right(list,index+1))

N = int(input())
A = [int(x) for x in input().split()]
if N == 2:
    print(max(A[0], A[1]))
else:
    tmp = 0
    for i in range(N):
        if tmp < ex_gcd(A, i):
            tmp = ex_gcd(A, i)
    print(tmp)

Submission status: Runtime Error

Solution code:
import math
import copy
from functools import reduce
n = int(input())
try:
    a = list(map(int,input().split()))
except EOFError:
    break
ans = reduce(math.gcd,a)
if n==2 or (min(a)==max(a)):
    print(max(a))
else:
    for i in range(n):
        _a = copy.copy(a)
        _a.pop(i)
        tmp = reduce(math.gcd,_a)
        if tmp > ans:
            print(tmp)
            break
        else:
            if i != n-1:
                pass
            else:
                print(ans)

Submission status: Runtime Error

Solution code:
import fractions
"""
import math
import queue

N = int(input())
As = sorted(list(map(int, input().split())))

backet = [1 for i in range(As[-1]+1)]
ans = 1

for i, Ai in enumerate(As):
    for j in range(2,int(Ai/2)+1) :
        if (Ai % j == 0) :
            backet[j] += 1

flag = True
while flag :
    flag = False
    for i, bc in enumerate(backet):
        if (bc >= N) :
            backet[i] -= N
            ans *= (bc-1)
            flag = True
            continue

print(ans)
"""

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


def L(i) :
    if (i-1 < 0) :
        return 0
    return fractions.gcd(L(i-1), A[i-1])

def R(i) :
    if (i >= N) :
        return 0
    return fractions.gcd(R(i+1), A[i])

M = 0
for i in range(N) :
    M = max(M, fractions.gcd(L(i), R(i+1)))


print(M)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
def gcd(X):
    tmp = X
    X.sort()
    m = min(X)
    for i in range(len(X)):
        if X[i] != m:
            X[i] = X[i]%m
    if X == tmp:
        return m
    else:
        while 0 in X:
            X.remove(0)
        return gcd(X)

ans = 1
tmp = A
for i in range(len(A)-1):
    A.pop(i)
    if gcd(A) > ans:
        ans = gcd(A)
    A = tmp
print(ans)

Submission status: Runtime Error

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

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

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


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

M.sort()
print(M[N-1])

Submission status: Runtime Error

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

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

def L(i) :
    if (i-1 < 0) :
        return 0
    return gcd(L(i-1), A[i-1])

def R(i) :
    if (i >= N) :
        return 0
    return gcd(R(i+1), A[i])

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


print(M)

Submission status: Runtime Error

Solution code:
#C
from math import gcd
N = int(input())
A = list(map(int, input().split()))

A.append(0)
A.append(0)
A[0],A[-1] = 0, A[0]
def calcgcd(L, ind):
    if len(L) == 1:
        return L[0]
    elif len(L) == 2:
        return max(L[0], L[1])
    else:
        l, r = L[0], L[-1]
        for i in range(0, ind):
            l = gcd(l, L[i])
        for i in range(ind + 1, N):
            r = gcd(r, L[i])
        return gcd(l,r)
M = []
for i in range(N):
    M.append(calcgcd(A,i))
print(max(M))

Submission status: Runtime Error

Solution code:
import math

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

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


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

M.sort()
print(M[N-1])

Submission status: Runtime Error

Solution code:
import math
import copy
from functools import reduce
n = int(input())
a = list(map(int,input().split()))
ans = reduce(math.gcd,a)
if n==2 or (min(a)==max(a)):
    print(max(a))
else:
    for i in range(n):
        _a = copy.copy(a)
        _a.pop(i)
        tmp = reduce(math.gcd,_a)
        if tmp > ans:
            print(tmp)
            break
        else:
            if i != n-1:
                pass
            else:
                print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd

n = int(input())
a = list(map(int, input().split()))
fromLeft = [0] * n
fromLeft[0] = a[0]
for i in range(1, n):
    fromLeft[i] = gcd(fromLeft[i - 1], a[i])
fromRight = [0] * n
fromRight[n - 1] = a[n - 1]
for i in range(n - 2, -1, -1):
    fromRight[i] = gcd(fromRight[i + 1], a[i])
ret = 0
for i in range(n):
    if i == 0:
        curGcd = fromRight[i + 1]
    elif i == n - 1:
        curGcd = fromLeft[n - 2]
    else:
        curGcd = gcd(fromLeft[i - 1], fromRight[n - i])
    ret = max(ret, curGcd)
print(ret)

Submission status: Runtime Error

Solution code:
import math

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

gcdlistL = [0]
for i in range(N):
    gcdlistL.append(math.gcd(gcdlistL[-1], A[i]))
#    print("gcdlistL[-1]={},A[{}]={}".format(gcdlistL[-1],i,A[i]))
#print("gcdlistL={}".format(gcdlistL))

gcdlistR = [0]
for i in range(N):
    gcdlistR.insert(0, math.gcd(A[N-1-i], gcdlistR[-1-i]))
#    print("gcdlistR[-1-{}]={},A[{}]={}".format(gcdlistR[-1-i],i,i,A[N-1-i]))
#print("gcdlistR={}".format(gcdlistR))

gcdmax = 0
for i in range(N):
    gcdmax = max(gcdmax, math.gcd(gcdlistL[i], gcdlistR[i+1]))
#    print("gcmax={},gcdL[{}]={},gcdR[{}]={}".format(gcdmax,gcdlistL[i],gcdlistR[-1-i],i,i,A[N-1-i]))

print(gcdmax)

Submission status: Runtime Error

Solution code:
from math import gcd

n = int(input())
a = list(map(int, input().split()))
fromLeft = [0] * n
fromLeft[0] = a[0]
for i in range(1, n):
    fromLeft[i] = gcd(fromLeft[i - 1], a[i])
fromRight = [0] * n
fromRight[n - 1] = a[n - 1]
for i in range(n - 2, -1, -1):
    fromRight[i] = gcd(fromRight[i + 1], a[i])
ret = 0
for i in range(n):
    if i == 0:
        curGcd = fromRight[n - 2]
    elif i == n - 1:
        curGcd = fromLeft[n - 2]
    else:
        curGcd = gcd(fromLeft[i - 1], fromRight[n - i])
    ret = max(ret, curGcd)
print(ret)

Submission status: Runtime Error

Solution code:
import math, copy
def ex_gcd(list, index):
    if index == 0:
        ans = list[1]
    else:
        ans = list[0]
    for i in range(1, len(list)):
        if i != index:
            ans = math.gcd(ans, list[i])
    return ans
N = int(input())
A = [int(x) for x in input().split()]
if N == 2:
    print(max(A[0], A[1]))
else:
    tmp = 0
    for i in range(N):
        if tmp < ex_gcd(A, i):
            tmp = ex_gcd(A, i)
    print(tmp)

Submission status: Runtime Error

Solution code:
import math
n=int(input())
l = list(map(int, input().split()))
ans=1;
for i in range(n):
	ans=max(math.gcd(math.gcd(l[0],l[i-1]),math.gcd(l[i],l[n-1])),ans)
print(ans)

Submission status: Runtime Error

Solution code:
import functools
import fractions


N = int(input())
A = list(map(int, input().split()))
A = list(set(A))
ans = [functools.reduce(fractions.gcd, A)]
if len(A) == 1:
    print(A[0])
    exit()
else:
    for i in A:
        b = sorted(A)
        b.remove(i)
        if a.coount(i) == 1:
            ans.append(functools.reduce(fractions.gcd, b))
print(max(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
gcd_list=[]
reverse_gcd_list = []


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

gcd_list.append(A[0])
reverse_gcd_list.append(A[-1])

for i in range(1,N):
    gcd_list.append(gcd(gcd_list[i-1], A[i]))
    reverse_gcd_list.insert(0,gcd(reverse_gcd_list[0], A[-1-i]))

num = [gcd_list[-2], reverse_gcd_list[1]]
for i in range(N-2):
    num = num.append(gcd(gcd_list[i], reverse_gcd_list[i+2]))

print(max(num))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
def gcd(X):
    X.sorted
    m = min(X)
    for i in len(X):
        X[i] = X[i]%m
    if X == [0] * len(X):
        return m
    else:
        while 0 in X:
            X.remove(0)
        return gcd(X)

ans = 1
tmp = A
for i in range(N):
    A.pop(i)
    if gcd(A) > ans:
        ans = gcd(A)
    A = tmp
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
def gcd(n,m):
  l0=[]
  if n==0:
    return m
  elif m==0;
  return n
  else:
    for i in range(1,n//2):
      if n%i==0:
        l0.append(i)
    l0.append(n)
    g=1
    for i in l0:
      if m%i==0:
        g=i
    return(g)
l1=[]
for i in range(n):
  ll1=gcd(0,l[0])
  ll2=gcd(0,l[n-1])
  if i==0:
    ll1=gcd(0,l[1])
    for j in range(1,n-1):
      ll1=gcd(ll1,l[j])
    l1.append(ll1)
  elif i==n-1:
    ll2=gcd(0,l[0])
    for j in range(n-1):
      ll2=gcd(ll2,l[j])
    l1.append(ll2)
  else:
      for j in range(i):
        ll1=gcd(ll1,l[j])
      for j in range(n-1,i:-1):
        ll2=gcd(ll2,l[j])
      l1.append(gcd(ll2,ll1))
print(max(l1))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
def gcd(X):
    X.sorted
    m = min(X)
    for i in len(X):
        X[i] = X[i]%m
    if X == [0] * len(X):
        return m
    else:
        while 0 in X:
            X.remove(0)
        return gcd(X)

ans = 1
tmp = A
for i in range(N):
    A.pop(i)
    if gcd(A) > ans:
        gcd(A) = ans
    A = tmp
print(ans)

Submission status: Runtime Error

Solution code:
import fractions
from functions import reduce

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

n = int(input())
a = [int(i) for i in input().split()]
ans = 0
l,r = [],[]
for i in range(n):
	l = gcd_list([a[i] for m in range(i)])
	r = gcd_list([a[i] for m in range(n-1,i,-1)])
	ans = max(ans,l,r)
print(ans)

Submission status: Runtime Error

Solution code:
import functools
import fractions


N = int(input())
A = list(map(int, input().split()))
A = list(set(A))
ans = [functools.reduce(fractions.gcd, A)]
if len(A) == 1:
    print(A[0])
    exit()
else:
    b = sorted(A)
    b.remove(i)
    if a.coount(i) == 1:
        ans.append(functools.reduce(fractions.gcd, b))
print(max(ans))

Submission status: Runtime Error

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

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

def L(i) :
    if (i <= 0) :
        return 0
    return gcd(L(i-1), A[i])

def R(i) :
    if (i >= N) :
        return 0
    return gcd(R(i+1), A[i])

M = 0
for i in range(N) :
    M = max(M, gcd(L(i), R(i)))


print(M)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

import numpy as np 
N = int(input())
A = list(map(int, input().strip().split()))
i = 0
B = 0
while i <= N - 1:
    Aa = np.delete(A, i)
    B1 = gcd_list(Aa)
    if B1 > B:
        B = B1
    i += 1
print(B)

Submission status: Runtime Error

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


def gcd_lis(l):
    res = 0
    for i in range(len(l)-1):
        if i == 0:
            res = gcd(l[i],l[i+1])
        else:
            res = gcd(res,l[i+1])
    return res

def gcd_lis2(l):
    tmpd = copy.deepcopy(l)
    if len(l) == 1:
        return l[0]
    else:
        tmpd.append(gcd(tmpd.pop(),tmpd.pop()))
        return gcd_lis2(tmpd)

n = int(input())
lis = list(map(int,input().split()))
#lis = list(set(lis))
res = gcd_lis2(lis)
#print(res)

if len(lis) == 2:
    print(max(lis))
elif len(lis) != 2:
    for i in range(n):
        tmp = copy.deepcopy(lis)
        del tmp[i]
        if i == 0:
            #print(lis[1:])
            res = max(res,(gcd_lis2(lis[1:])))

        elif i == n-1:
            res = max(res,(gcd_lis2(lis[:n-1])))
        else:
            res = max(res,
            gcd(gcd_lis2(lis[i+1:]),gcd_lis2(lis[:i]))
            )
        #print(res)

    print(res)

Submission status: Runtime Error

Solution code:
import math
import copy
from functools import reduce
n = int(input())
a = list(map(int,input().split()))
ans = reduce(math.gcd,a)
if n==2 or (min(a)==max(a)):
    print(max(a))
else:
    for i in range(n):
        _a = copy.copy(a)
        _a.pop(i)
        tmp = reduce(math.gcd,_a)
        if tmp > ans:
            print(tmp)
            break
        else:
            pass

Submission status: Runtime Error

Solution code:
from math import gcd

N = int(input())
A = list(map(int, input().split()))
L = [0]*(N+1)
R = [0]*(N+1)

for i in range(N):
    j = N - i
    L[i+1] = gcd(L[i], A[i])
    R[j-1] = gcd(R[j], A[j-1])
res = 0
for i in range(N-1):
    res = max(res, gcd(L[i], R[i+1]))
print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
num_unique = list(set(num_list))
num_unique.sort()
num_unique.reverse()
max_num = 0
flag = False
for i in range(num_unique[-2], 0, -1):
    if i == 1:
        max_num = 1
        break
    elif flag and count < 2:
        break
    else:
        flag = True
        count = 0
        max_num = i
    for num in num_unique:
        # print("-------------------------------------------")
        # print("最大公約数候補: {0}, 数: {1}, カウント: {2}".format(i, num, count))
        # print("割る： {0}, あまり: {1}".format(num / max_num, num % max_num))
        if num / max_num < 1 or num % max_num != 0:
            count += 1
            if count > 1:
                break
print(max_num)

Submission status: Runtime Error

Solution code:
from math import gcd

N = 3
A = [7, 6, 8]

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

for i in range(N):
    j = N - i
    L[i+1] = gcd(L[i], A[i])
    R[j-1] = gcd(R[j], A[j-1])
res = 0
for i in range(N-1):
    res = max(res, gcd(L[i], R[i+1]))
print(res)

Submission status: Runtime Error

Solution code:
import functools
import math
import copy
import fractions
N = int(input())
a_new = list(map(int,input().split()))
a = copy.deepcopy(a_new)
b = a[N//2:]
a = a[:N//2]
N = N//2
if len(a_new) >= 4:
    if functools.reduce(fractions.gcd, b) < functools.reduce(fractions.gcd, a) and len(a) !=1:
        maxa = functools.reduce(fractions.gcd, a)
        a = b
    elif len(a) != 1:
        maxa = functools.reduce(fractions.gcd, b)
    while True:
        if len(a) == 1:
            break
        b = a[N//2:]
        a = a[:N//2]
        if len(a) == 1:
            a = int(a[0])
            break
        if functools.reduce(fractions.gcd,b) < functools.reduce(fractions.gcd,a):
            a = copy.deepcopy(b)
        N = len(a)


    b_old = copy.deepcopy(b)
    b.append(maxa)
    a_resu = (fractions.gcd(maxa,a))
    b_resu = (functools.reduce(fractions.gcd,b))
    if a_resu == b_resu:
        print(maxa)
    elif a_resu < b_resu:
        a_new.remove(a)
        print(functools.reduce(fractions.gcd,a_new))
    else:
        a = [b_old[0],maxa]
        b = [b_old[1],maxa]
        if functools.reduce(fractions.gcd, b) < functools.reduce(fractions.gcd, a):
            a_new.remove(b_old[1])
            print(functools.reduce(fractions.gcd, a_new))
        else:
            a_new.remove(b_old[0])
            print(functools.reduce(fractions.gcd, a_new))
elif len(a_new) == 3:
    print(max(fractions.gcd(a_new[0], a_new[1]),fractions.gcd(a_new[0], a_new[2]),fractions.gcd(a_new[1],a_new[2])))
else:
    print(max(a_new[0],a_new[1]))

Submission status: Runtime Error

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
num_unique = list(set(num_list))
num_unique.sort()
num_unique.reverse()
max_num = 0
flag = False
for i in range(num_unique[-2], 0, -1):
    if i == 1:
        max_num = 1
        break
    elif flag and count < 2:
        break
    else:
        flag = True
        count = 0
        max_num = i
    for num in num_unique:
        print("最大公約数候補: {0}, 数: {1}, カウント: {2}".format(i, num, count))
        print("割る： {0}, あまり: {1}".format(num / max_num, num % max_num))
        if num / max_num < 1 or num % max_num != 0:
            count += 1
            if count > 1:
                break
print(max_num)

Submission status: Runtime Error

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

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

L = [0]*N
R = [0]*N

L[0] = A[0]
r[-1] = A[-1]

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

#残りの N − 1 個の整数の最大公約数
mx = 0
for i in range(N):
	if i==N-1:	#後ろから1個めが抜かれた時
		v = L[N-2]
	elif i==0:	#前から1個めが抜かれた時
		v = R[1]
	else:		#どれでもない時
		v = gcd(R[i+1], L[i-1])
	mx = max(v, mx)
print(mx)

Submission status: Runtime Error

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

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

L = [0]*N
R = [0]*N

L[0] = A[0]
r[-1] = A[-1]

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

#残りの N − 1 個の整数の最大公約数
mx = 0
for i in range(N):
	if i==N-1:	#後ろから1個めが抜かれた時
		v = L[N-2]
	elif i==0:	#前から1個めが抜かれた時
		v = R[1]
	else:		#どれでもない時
		v = gcd(L[i+1], R[i-1])
	mx = max(v, mx)
print(mx)

Submission status: Runtime Error

Solution code:
import functools
import math
import copy
N = int(input())
a_new = list(map(int,input().split()))
a = copy.deepcopy(a_new)
b = a[N//2:]
a = a[:N//2]
N = N//2
if len(a_new) >= 4:
    if functools.reduce(math.gcd, b) < functools.reduce(math.gcd, a) and len(a) !=1:
        maxa = functools.reduce(math.gcd, a)
        a = b
    elif len(a) != 1:
        maxa = functools.reduce(math.gcd, b)
    while True:
        if len(a) == 1:
            break
        b = a[N//2:]
        a = a[:N//2]
        if len(a) == 1:
            a = int(a[0])
            break
        if functools.reduce(math.gcd,b) < functools.reduce(math.gcd,a):
            a = copy.deepcopy(b)
        N = len(a)


    b_old = copy.deepcopy(b)
    b.append(maxa)
    a_resu = (math.gcd(maxa,a))
    b_resu = (functools.reduce(math.gcd,b))
    if a_resu == b_resu:
        print(maxa)
    elif a_resu < b_resu:
        a_new.remove(a)
        print(functools.reduce(math.gcd,a_new))
    else:
        a = [b_old[0],maxa]
        b = [b_old[1],maxa]
        if functools.reduce(math.gcd, b) < functools.reduce(math.gcd, a):
            a_new.remove(b_old[1])
            print(functools.reduce(math.gcd, a_new))
        else:
            a_new.remove(b_old[0])
            print(functools.reduce(math.gcd, a_new))
elif len(a_new) == 3:
    print(max(math.gcd(a_new[0], a_new[1]),math.gcd(a_new[0], a_new[2]),math.gcd(a_new[1],a_new[2])))
else:
    print(max(a_new[0],a_new[1]))

Submission status: Runtime Error

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

def L(i,A):
  if i==0:
    return 0
  else:
    return gcd(L(i-1,A),A[i-1])

def R(i,A,N):
  if i==N+1:
    return 0
  else:
    return gcd(R(i+1,A,N),A[i])
    
N=int(input())
A=[0]
A.extend(list(map(int,input().split())))
A.append(0)
ans=1
for i in range(1,N+1):
  M=gcd(L(i,A),R(i+1,A,N))
  ans=max(ans,M)
print(ans)

Submission status: Runtime Error

Solution code:
from fraction import gcd

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

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

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

res = 1
for i in range(n):
    if i - 1 < 0:
        res = max(res, q[i + 1])
    elif i + 1 >= n:
        res = max(res, p[i - 1])
    else:
        res = max(res, gcd(p[i - 1], q[i + 1]))

print(res)

Submission status: Runtime Error

Solution code:
from math import gcd

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

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

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

res = 1
for i in range(n):
    if i - 1 < 0:
        res = max(res, q[i + 1])
    elif i + 1 >= n:
        res = max(res, p[i - 1])
    else:
        res = max(res, gcd(p[i - 1], q[i + 1]))

print(res)

Submission status: Runtime Error

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

def L(i,A):
  if i==0:
    return 0
  else:
    return gcd(L(i-1,A),A[i-1])

def R(i,A,N):
  if i==N+1:
    return 0
  else:
    return gcd(R(i+1,A,N),A[i])
    
N=int(input())
A=[0]
A.extend(list(map(int,input().split())))
A.append(0)
ans=1
for i in range(0,N+1):
  M=gcd(L(i,A),R(i+1,A,N))
  ans=max(ans,M)
print(ans)

Submission status: Runtime Error

Solution code:
import functools
import math
import copy
N = int(input())
a_new = list(map(int,input().split()))
a = copy.deepcopy(a_new)
b = a[N//2:]
a = a[:N//2]
N = N//2
if len(a_new) >= 4:
    if functools.reduce(math.gcd, b) < functools.reduce(math.gcd, a) and len(a) !=1:
        max = functools.reduce(math.gcd, a)
        a = b
    elif len(a) != 1:
        max = functools.reduce(math.gcd, b)
    while True:
        if len(a) == 1:
            break
        b = a[N//2:]
        a = a[:N//2]
        if len(a) == 1:
            a = int(a[0])
            break
        if functools.reduce(math.gcd,b) < functools.reduce(math.gcd,a):
            a = copy.deepcopy(b)
        N = len(a)


    b_old = copy.deepcopy(b)
    b.append(max)
    a_resu = (math.gcd(max,a))
    b_resu = (functools.reduce(math.gcd,b))
    if a_resu == b_resu:
        print(max)
    elif a_resu < b_resu:
        a_new.remove(a)
        print(functools.reduce(math.gcd,a_new))
    else:
        a = [b_old[0],max]
        b = [b_old[1],max]
        if functools.reduce(math.gcd, b) < functools.reduce(math.gcd, a):
            a_new.remove(b_old[1])
            print(functools.reduce(math.gcd, a_new))
        else:
            a_new.remove(b_old[0])
            print(functools.reduce(math.gcd, a_new))
elif len(a_new) == 3:
    print(max(math.gcd(a_new[0], a_new[1]),math.gcd(a_new[0], a_new[2]),math.gcd(a_new[1],a_new[2])))
else:
    print(max(a_new[0],a_new[1]))

Submission status: Runtime Error

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
num_unique = list(set(num_list))
num_unique.sort()
num_unique.reverse()
max_num = 0
flag = False
for i in range(num_unique[-2], 0, -1):
    if i == 1:
        max_num = 1
        break
    elif flag and count < 2:
        break
    else:
        flag = True
        count = 0
        max_num = i
    for num in num_unique:
        if int(num / max_num) < 1 or num % max_num != 0:
            count += 1
            if count > 1:
                break
print(max_num)