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 fractions
def gcd(l):
	if len(l)<1:
		return False
	elif len(l)==1:
		return l[0]
	elif len(l)==2:
		return fractions.gcd(l[0],l[1])
	else:
		lst=l[::]
		k=math.fractions(lst[0],lst[1])
		lst.pop(0)
		lst.pop(0)
		lst.append(k)
		return gcd(lst)

N=int(input())
A=list(map(int,input().split()))
mx=0
for i in A:
	L=A[::]
	L.remove(i)
	mx=max(gcd(L),mx)
print(mx)

Submission status: Runtime Error

Solution code:
def gcd(l):
	if len(l)<1:
		return False
	elif len(l)==1:
		return l[0]
	elif len(l)==2:
		return math.gcd(l[0],l[1])
	else:
		lst=l[::]
		k=math.gcd(lst[0],lst[1])
		lst.pop(0)
		lst.pop(0)
		lst.append(k)
		return gcd(lst)

N=int(input())
A=list(map(int,input().split()))
mx=0
for i in A:
	L=A[::]
	L.remove(i)
	mx=max(gcd(L),mx)
print(mx)

Submission status: Runtime Error

Solution code:
import math
def gcd(l):
	if len(l)<1:
		return False
	elif len(l)==1:
		return l[0]
	elif len(l)==2:
		return math.gcd(l[0],l[1])
	else:
		k=math.gcd(l[0],l[1])
		l.pop(0)
		l.pop(0)
		l.append(k)
		return gcd(l)

N=int(input())
A=list(map(int,input().split()))
mx=0
for i in A:
	L=A[::]
	L.remove(i)
	mx=max(gcd(L),mx)
print(mx)

Submission status: Runtime Error

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

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

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

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

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

for n in range(N):
    GCDs[n] = gcd(L[n], R[N-1-n])

print(max(GCDs))

Submission status: Runtime Error

Solution code:
import sys

def gcd(x, y, *val):
    val = [x, y] + list(val)
    val.sort(reverse=True)
    count = len(val)

    if count == 2:
        mod = val[0] % val[1]
        if mod == 0:
            return val[1]
        elif mod == 1:
            return 1
        else:
            return gcd(val[1], mod)
    else:
        if count % 2 == 1:
            val.append(min(val))

        return gcd(*[gcd(val[i], val[i + 1]) for i in range(0, count, 2)])
        

def main():
    sinputl = sys.stdin.readline

    num = int(sinputl().strip())
    input = [int(x) for x in sinputl().split()]

    gcd_max = 1
    input_mem = []

    if num == 2:
        print(max(input))
        return

    for i in range(num):
        mem = input.pop(num - 1 - i)
        gcd_max = max(gcd_max, gcd(*input, *input_mem))
        input_mem.append(mem)

    print(gcd_max)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

def gcd(x, y, *val):
    val = [x, y] + list(val)
    val.sort(reverse=True)

    if len(val) == 2:
        mod = val[0] % val[1]
        if mod == 0:
            return val[1]
        elif mod == 1:
            return 1
        else:
            return gcd(val[1], mod)
    else:
        length = len(val)
        if length % 2 == 1:
            val.append(val[-1])

        return gcd(*[gcd(val[i], val[i + 1]) for i in range(0, length, 2)])
        

def main():
    sinputl = sys.stdin.readline

    num = int(sinputl().strip())
    input = [int(x) for x in sinputl().split()]

    gcd_max = 1
    input_mem = []

    if num == 2:
        print(max(input))
        return

    for i in range(num):
        mem = input.pop(num - 1 - i)
        gcd_max = max(gcd_max, gcd(*input, *input_mem))
        input_mem.append(mem)

    print(gcd_max)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

def gcd(x, y, *val):
    val = [x, y] + list(val)
    val.sort(reverse=True)

    if len(val) == 2:
        mod = val[0] % val[1]
        if mod == 0:
            return val[1]
        elif mod == 1:
            return 1
        else:
            return gcd(val[1], mod)
    else:
        length = len(val)
        if length % 2 == 1:
            val.append(val[-1])

        return gcd(*[gcd(val[i], val[i + 1]) for i in range(0, length, 2)])
        

def main():
    sinputl = sys.stdin.readline

    num = int(sinputl().strip())
    input = [int(x) for x in sinputl().split()]

    gcd_max = 1
    input_mem = []

    for i in range(num):
        mem = input.pop(num - 1 - i)
        gcd_max = max(gcd_max, gcd(*input, *input_mem))
        input_mem.append(mem)

    print(gcd_max)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

def gcd(x, y, *val):
    val = [x, y] + list(val)
    val.sort(reverse=True)

    if len(val) == 2:
        mod = val[0] % val[1]
        if mod == 0:
            return val[1]
        elif mod == 1:
            return 1
        else:
            return gcd(val[1], mod)
    else:
        return gcd(*[gcd(val[i], val[i + 1]) for i in range(0, len(val), 2)], val[-1])
        

def main():
    sinputl = sys.stdin.readline

    num = int(sinputl().strip())
    input = [int(x) for x in sinputl().split()]

    gcd_max = 1
    input_mem = []

    for i in range(num):
        mem = input.pop(num - 1 - i)
        gcd_max = max(gcd_max, gcd(*input, *input_mem))
        input_mem.append(mem)

    print(gcd_max)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce
n = int(input())
a = list(map(int,input().split()))
gcds = 0
for N in range(n):
    gcds = max(gcds,reduce(fractions.gcd, a[:N],a[N+1:]))
print(gcds)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce
n = int(input())
a = list(map(int,input().split()))
gcds = 0
for N in range(n):
    b = a.copy()
    b.remove(b[N])
    gcds = max(gcds,reduce(fractions.gcd, b))
print(max(gcds))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import math

n = int(input())

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

gcd = s[0]
r = [gcd]
for i in s[1:]:
    gcd = math.gcd(gcd,i)
    r.append(gcd)

gcd = s[-1]
l = [gcd]
for i in list(reversed(s))[1:]:
    gcd = math.gcd(gcd,i)
    l.append(gcd)
l.reverse()

ans = []

for i in range(n):
    li = r[:i]+l[i+1:]
    gcd = li[0]
    for i in li[1:]:
        gcd = math.gcd(gcd,i)
    ans.append(gcd)

#    ans.append((math.gcd(min())))
    
print(max(ans))

Submission status: Runtime Error

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

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

def exgcd(arg):
    tmp=gcd(arg[0],arg[1])
    for i in range(2,n-1):
        tmp=gcd(tmp,arg[i])
    return tmp

max_gcd=0
for i in range(n+1):
    tmp_list=copy.copy(a[0:i])
    tmp_list.extend(a[i+1:n])
    tmp_gcd=exgcd(tmp_list)
    if tmp_gcd>max_gcd:
        max_gcd=tmp_gcd

print(max_gcd)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [x for x in A]
A.sort(reverse=True)
k = 2

while len(B) > 1:
    B = [x for x in B if x % k != 0]
    k += 1

if len(B) == 1:
    del A[A.index(B[0])]

ans = A[0]

for l in A:
    ans = math.gcd(l, ans)

print(ans)

Submission status: Runtime Error

Solution code:
mport fractions
from functools import reduce

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

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

res = 0
for i in range(N):
    tmp = gcd_list(A[:i] + A[i+1:])
    res = max(res,tmp)

print(res)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
n = int(input())
s  = [int(i) for i in input().split()]
s2 = tuple(s)
def gcd(numbers):
        return reduce(math.gcd, numbers)
ans = 1
for i in range(n):
    del s[i]
    if gcd(s)>ans:
        ans = gcd(s)
    s = list(s2)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s  = [int(i) for i in input().split()]
s2 = tuple(s)
def gcd(numbers):
        return reduce(math.gcd, numbers)
ans = 1
for i in range(n):
    del s[i]
    if gcd(s)>ans:
        ans = gcd(s)
    s = list(s2)
print(ans)

Submission status: Runtime Error

Solution code:
import functools
import math
import copy

N = int(input())
A = list(map(int, input().split()))
maxGcd = functools.reduce(math.gcd, A)
for i in range(N):
    tempA = copy.deepcopy(A)
    tempA.pop(i)
    gcd = functools.reduce(math.gcd, tempA)
    maxGcd = max(maxGcd, gcd)
print(maxGcd)

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
A=list(map(int,input().split()))
left,right=[0],[0]
[(left.append(gcd(left[i],A[i])),right.append(gcd(right[i],A[N-i-1]))) for i in range(N-1)]
right.reverse()
print(max([gcd(a,b) for a,b in zip(left,right)]))

Submission status: Runtime Error

Solution code:
from math import gcd
N=int(input())
A=list(map(int,input().split()))
left,right=[0],[0]
[(left.append(gcd(left[i],A[i])),right.append(gcd(right[i],A[N-i-1]))) for i in range(N-1)]
right.reverse()
max([gcd(a,b) for a,b in zip(left,right)])

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
import itertools

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

l = list(itertools.combinations(a,n-1))
ans = 0

for itr in l:
    ans = max(ans, reduce(math.gcd, list(itr)))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

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

count=0
ans=0

for i in range(N):
    if a[i]<0:
        count+=1
    	a[i]=abs(a[i])

if count%2==0:
    ans=sum(a)

else:
    a.sort()
    ans=sum(a)-a[0]*2

print(ans)

Submission status: Runtime Error

Solution code:
import math

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

ans = 1

if N == 1 :
    ans = A[0]
else :
    for i in range(N) :
        B = A[:i-1] + A[i:] # i番目を除く
        stat = B[0]
        for b in B[1:] :
            stat = math.gcd(stat,b)
            if stat == 1:
                break
        else :
            ans = max(ans,stat)

print(ans)

Submission status: Runtime Error

Solution code:
import math

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

ans = 1

if N == 1 :
    ans = A[0]
else :
    for i in range(N) :
        B = A[:i-1] + A[i:] # i番目を除く
        gcd = B[0]
        for b in B[1:] :
            gcd = math.gcd(gcd,b)
            if gcd == 1:
                break
        else :
            ans = max(ans,gcd)

print(ans)

Submission status: Runtime Error

Solution code:
import math

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

ans = 1

for i in range(N) :
    B = A[:i-1] + A[i:]
    gcd = B[0]
    for b in B[1:] :
        gcd = math.gcd(gcd,b)
        if gcd == 1:
            break
    else :
        ans = max(ans,gcd)

print(ans)

Submission status: Runtime Error

Solution code:
from functools import reduce
from fractions import gcd
#from math import gcd

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

gcd_all = reduce(gcd, A)
list = []

for i in range(N):
    #gcd_1 = reduce(gcd, (A[:i]+A[i+1:]))
    for j in range(N):
        if i == j:
            continue
        list.append(gcd(A[i],A[j]))
    #print(list)
    #print(max(list))
    if gcd_all == max(list):
        index = i
        #print(index)
    list = []

    #if gcd_1 >= gcd_all:
     #   gcd_all = gcd_1
A.pop(index)
print(reduce(gcd, A))
#print(gcd_all)

Submission status: Runtime Error

Solution code:
from functools import reduce
from fractions import gcd
from math import gcd

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

gcd_all = reduce(gcd, A)
list = []

for i in range(N):
    #gcd_1 = reduce(gcd, (A[:i]+A[i+1:]))
    for j in range(N):
        if i == j:
            continue
        list.append(gcd(A[i],A[j]))
    #print(list)
    #print(max(list))
    if gcd_all == max(list):
        index = i
        #print(index)
    list = []

    #if gcd_1 >= gcd_all:
     #   gcd_all = gcd_1
A.pop(index)
print(reduce(gcd, A))
#print(gcd_all)

Submission status: Runtime Error

Solution code:
import functools
import math
import fractions

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

#gcd_all = functools.reduce(math.gcd, A)
gcd_all = functools.reduce(fractions.gcd, A)
#print(gcd_all)

for i in range(N):
    list = A[:i] + A[i+1:]
    #print(list)
    #gcd_1 = functools.reduce(math.gcd, list)
    gcd_1 = functools.reduce(fractions.gcd, list)
    #print(gcd_1)
    if gcd_1 >= gcd_all:
        index = i
        value = gcd_1
        gcd_all = gcd_1

A[index] = value

print(functools.reduce(math.gcd, A))

Submission status: Runtime Error

Solution code:
import functools
import math

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

gcd_all = functools.reduce(math.gcd, A)
#print(gcd_all)

for i in range(N):
    list = A[:i] + A[i+1:]
    #print(list)
    gcd_1 =  functools.reduce(math.gcd, list)
    #print(gcd_1)
    if gcd_1 >= gcd_all:
        index = i
        value = gcd_1
        gcd_all = gcd_1

A[index] = value

print(functools.reduce(math.gcd, A))

Submission status: Runtime Error

Solution code:
import math

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

g_l = [1 for i in range(N)]
g_r = [1 for i in range(N)]

g_l[0] = A[0]
g_r[-1] = A[-1]

for i in range(1, N):
    g_l[i] = math.gcd(A[i], g_l[i-1])
    g_r[-i-1] = math.gcd(A[-i-1], g_r[-i])

ans = max(g_r[1], g_l[-2])
for i in range(1, N-1):
    ans = max(ans, math.gcd(A[i-1], A[i+1]))

print(ans)

Submission status: Runtime Error

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

max_gcd_list = []
for i in range(N):
    new_list = a_list[:i] + (a_list[(i+1):] if i < N-1 else [])
    max_gcd = reduce(gcd, new_list[1:], new_list[0])
    max_gcd_list.append(max_gcd)
print(max(max_gcd_list))

Submission status: Runtime Error

Solution code:
import fractions  
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
b = 1
if N == 2:
  b = max(A)
else :
  for i in range(N):
    L= sorted(A[:i]+A[i+1:],reverse=true)
    for j in range(N-1):
      if j == 0:
        C = fractions.gcd(L[0],L[1])
        if C <= b :
          break
      elif  j == N-2:
        C = fractions.gcd(C,L[j])
        if C <= b :
          break
        elif C > b:
          b = C  
      else :
        C = fractions.gcd(C,L[j])
        if C <= b :
          break
print(b)

Submission status: Runtime Error

Solution code:
import fractions  
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
b = 1
if N == 2:
  b = max(A)
else :
  for i in range(N):
    L= sorted(A[:i]+A[i+1:],reverse=true)
    for j in range(N-1):
      if A[i-1]==A[i]:
        continue
      else:
        if j == 0:
          C = fractions.gcd(L[0],L[1])
          if C <= b :
            break
        elif  j == N-2:
          C = fractions.gcd(C,L[j])
          if C <= b :
            break
          elif C > b:
            b = C  
        else :
           if  A[i-1]!=A[i]:
            C = fractions.gcd(C,L[j])
            if C <= b :
              break
	        else:
              continue
          
print(b)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
a=input().split(" ")
a=[int(j) for j in a]
divised=0
import math
from functools import reduce
divisor=reduce(math.gcd,a)
b=[]

for i in range(n):
    b=a[:i]+a[i+1:]
    divised=reduce(math.gcd,b)
    if divisor<divised:
        divisor=divised
        
print(divisor)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
a=input().split(" ")
a=[int(j) for j in a]
divised=0
import math
from functools import reduce
divisor=reduce(math.gcd,a)
b=[]

for i in range(n):
    b=a[:i]+a[i+1:]
    print(b)
    divised=reduce(math.gcd,b)
    if divisor<divised:
        divisor=divised
        
print(divisor)

Submission status: Runtime Error

Solution code:
from fractions import*
N＝int(input())
a  = list(map(int,input().split()))
B=[0]
a=range(N)
for x in a:B+=[gcd(B[-1],A[x])]
w=M=0
for x in a[::-1]:M=max(M,gcd(w,B[x]));w=gcd(w,A[x])
print(M)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
A = list(map(int, input().split()))
M = []
for i in range(n):
    target_index = i
    if target_index == 0:
        target = A[1]
    else:
        target = A[0]
    for index, number in enumerate(A):
        if index == target_index:
            continue
        target = math.gcd(target, number)
    M.append(target)
print(max(M))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from math import gcd

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



# L[0] = 0
# L[i] (i=1, ..., N)にはA_1, ..., A_iの最大公約数が格納されている

L = []
L.append(A[0])

for i in range(N):
    L.append(gcd(L[i], A[i]))

#  for i in range(N+1):
    #  print(L[i])


# R[N+1] = 0
# R[i] (i=N, N-1, ..., 1)にはA_i, ..., A_Nの最大公約数が格納されている

R = [0] * (N+2)
# R = [0, 0, 0, ... , 0] (0がN+2個)
R[N+1] = A[N-1]

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

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



# M[i] (i=0, ..., N-1)にはA_(i+1)を除いた整数の最大公約数が格納されている

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

print(max(M))

Submission status: Runtime Error

Solution code:
from math import gcd

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



# L[0] = 0
# L[i] (i=1, ..., N)にはA_1, ..., A_iの最大公約数が格納されている

L = []
L.append(0)

for i in range(N):
    L.append(gcd(L[i], A[i]))

#  for i in range(N+1):
    #  print(L[i])


# R[N+1] = 0
# R[i] (i=N, N-1, ..., 1)にはA_i, ..., A_Nの最大公約数が格納されている

R = [0] * (N+2)
# R = [0, 0, 0, ... , 0] (0がN+2個)

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

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



# M[i] (i=0, ..., N-1)にはA_(i+1)を除いた整数の最大公約数が格納されている

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

print(max(M))

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

if N<=2:
	print(max(a))
else:
	ans = gcd_list(a[1:])
	for i in range(1,N):
		temp_gcd = gcd_list(a[0:i]+a[i+1:])

		ans = max(ans , temp_gcd)

print(ans)

Submission status: Runtime Error

Solution code:
from fractions import gcd
from itertools import combinations

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

b = list(accumulate([0] + a, gcd))
c = list(accumulate([0] + a[::-1], gcd))
l = list(gcd(s, t) for s, t in zip(b[:-1], c[::-1][1:]))
print(max(l))

Submission status: Runtime Error

Solution code:
from fractions import gcd
from itertools import combinations

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

l = set(gcd(x, y) for x, y in list(combinations(a, 2)))
if 1 in l:
    remove(1)
print(max(l))

Submission status: Runtime Error

Solution code:
from math import gcd
# from fractions import gcd
from itertools import combinations

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

l = set(gcd(x, y) for x, y in list(combinations(a, 2)))
if 1 in l:
    remove(1)
print(max(l))

Submission status: Runtime Error

Solution code:
from fractions import gcd
from itertools import permutations

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

l = set(gcd(x, y) for x, y in list(permutations(a, 2)))
if 1 in l:
    remove(1)
print(min(l))

Submission status: Runtime Error

Solution code:
from math import gcd
from itertools import permutations

N = 3
a = [12, 15, 18]

l = set(gcd(x, y) for x, y in list(permutations(a, 2)))
if 1 in l:
    remove(1)
print(min(l))

Submission status: Runtime Error

Solution code:
from math import gcd
from itertools import permutations

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

l = set(gcd(x, y) for x, y in list(permutations(a, 2)))
if 1 in l:
    remove(1)
print(min(l))

Submission status: Runtime Error

Solution code:
n = int(input())
def kouyakusuu(lst):
    m = lst[0]
    while lst[1] % m != 0:
        m -= 1
    for i in range(len(lst)):
        while lst[i+2] % m != 0:
            m -= 1
    return m

a = []
for i in map(int, input().split()):
    a.append(i)

ans = kouyakusuu(a)
print(ans)

Submission status: Runtime Error

Solution code:
import math
print(math.gcd(2, 5))

Submission status: Runtime Error

Solution code:
from math import gcd as GCD

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

# GCD_LtoR[i] = GCD(A0, A1, ... , A(i-1))
GCD_LtoR = [None] * N
# GCD_LtoR[i] = GCD(Ai, A(i+1), ... , AN)
GCD_RtoL = [None] * N

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

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

GCD_max = GCD_RtoL[1]
for i in range(0, N-1):
    # Aに含まれるAi以外の要素のGCDを計算する
    GCD_ex_i = GCD(GCD_LtoR[i-1], GCD_RtoL[i+1])
    GCD_max = max(GCD_ex_i, GCD_max)
GCD_max = max(GCD_LtoR[N-2], GCD_max)
print(GCD_max)

Submission status: Runtime Error

Solution code:
import math
#import fractions as math



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

L = [0]
R = [0]
#Find GCD from right and left
for i in range(1,N+1):
    gcdL = math.gcd(L[i-1],A[i-1])
    L.append(gcdL)
    gcdR = math.gcd(R[i-1],A[N-i])
    R.append(gcdR)

#reverse R
R = R[::-1]

#find GCD of N-1 elements
GCD = []
for i in range(N-1):
    gcdcombination = math.gcd(L[i],R[i+1])
    GCD.append(gcdcombination)


#the answer
print(max(GCD))

Submission status: Runtime Error

Solution code:
n = int(input())
in_num = input().split()
lis_num = [int(s) for s in in_num]
m = max(lis_num)
 
x = [lis_num[i] % m for i in range(n)]
sx = max(x.remove(max(x)))
m2 = max(lis_num.remove(m))
y = [lis_num[i] % m2 for i in range(n)]
sy = max(y.remove(max(y)))
if sx == 0:
  ans = m
elif sy == 0:
  ans = m2
else:
  for i in range((m+1)//2, 0, -1):
    x = [lis_num[j] % i for j in range(n)]
    sx = max(x.remove(max(x)))
    if sx == 0:
      ans = i
      break
    else:
      continue
print(ans)

Submission status: Runtime Error

Solution code:
import fractions  
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
b = 1
if N == 2:
  b = max(A)
else :
  for i in range(N):
    L= sorted(A[:i]+A[i+1:])
    for j in range(N-1):
      if A[i-1]==A[i]:
        continue
      else:
        if j == 0:
          C = fractions.gcd(L[0],L[1])
          if C <= b :
            break
        elif  j == N-2:
          C = fractions.gcd(C,L[j])
          if C <= b :
            break
          elif C > b:
            b = C  
        else :
           if  A[i-1]!=A[i]:
            C = fractions.gcd(C,L[j])
            if C <= b :
              break
	        else:
              continue
          
print(b)

Submission status: Runtime Error

Solution code:
import fractions  
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
b = reduce(fractions.gcd,A[:0]+A[1:])
if N == 2:
  b = A.max()
else :
  for i in range(1,N):
    L= A[:i]+A[i+1:]
    for j in range(N-1):
      if j == 0:
        C = fractions.gcd(L[0],L[1])
        if C < b :
          break
      elif  j == N-2:
        C = fractions.gcd(C,L[j])
        if C <= b :
          break
        elif C > b:
          b = C  
      else :
        C = fractions.gcd(C,L[j])
        if C <= b :
          break
print(b)

Submission status: Runtime Error

Solution code:
import fractions  
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
b = reduce(fractions.gcd,A[:0]+A[1:])
Z = reduce(fractions.gcd,A[:0]+A[1:])
for i in range(1,N):
  L= A[:i]+A[i+1:]
  for j in range(N-1):
    if j == 0:
      C = fractions.gcd(L[0],L[1])
      if C < b :
        break
    elif  j == N-2:
      C = fractions.gcd(C,L[j])
      if C <= b :
        break
      elif C > b:
        b = C  
    else :
      C = fractions.gcd(C,L[j])
      if C <= b :
        break
print(b)

Submission status: Runtime Error

Solution code:
import math
import functools


def shift(index: int, l: list) -> list:
    return l[index:] + l[:index]


def not_see_one(index: int, a: list) -> iter:
    ret = shift(index, a)
    return ret[1:]


def gcd_list(numbers: list) -> int:
    return functools.reduce(math.gcd, numbers)


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

result = max(map(gcd_list, map(functools.partial(not_see_one, a=a_list), range(n))))
print(result)

Submission status: Runtime Error

Solution code:
import math
import functools
from functools import reduce


def shift(index: int, l: list) -> list:
    return l[index:] + l[:index]


def not_see_one(index: int, a: list) -> iter:
    ret = shift(index, a)
    return ret[1:]


def gcd_list(numbers: list) -> int:
    return reduce(math.gcd, numbers)


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

result = max(map(gcd_list, map(functools.partial(not_see_one, a=a_list), range(n))))
print(result)

Submission status: Runtime Error

Solution code:
from functools import reduce


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


def lcm(a, b):
    return a * b // gcd(a, b)


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

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

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

m = [gcd(l[i], r[i]) for i in range(n)]

print(reduce(max, m))

Submission status: Runtime Error

Solution code:
import math

N = int(input())
A = input().split()
a = [int(A[i]) for i in range(len(A))]
b = []
for i in range(N):
    b.append(math.gcd(a[i], a[0 if (i+1) == N else i+1]))

print(max(b))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

N = int(input())
A = input().split()
a = [int(A[i]) for i in range(len(A))]
b = []
for i in range(N):
    b.append(math.gcd(a[i], a[0 if (i+1) == N else i+1]))

print(max(b))

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce 

def mygcd(n,nlist) :
	if len(nlist) > 2 :
		l0 = nlist.pop(0)
		l1 = nlist.pop(0)
		g = fractions.gcd(l0,l1)
		if g == 1 :
			return 1
		else :
			if n != 0 :
				g = fractions.gcd(n,g)
			return(mygcd(g,nlist))
	elif len(nlist) == 2 :
		l0 = nlist.pop(0)
		l1 = nlist.pop(0)
		g = fractions.gcd(l0,l1)
		if g == 1 :
			return 1
		else :
			return(fractions.gcd(n,g))
	elif len(nlist) == 0 :
		return fractions.gcd(n,nlist.pop(0))

N = int(input())
A = list(map(int,input().split()))
res = 0
for i in range(N) :
	new = A.copy()
	del new[i]
	a = mygcd(0,new)
	if a > res :
		res = a
print(res)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce 

def mygcd(nlist) :
	return reduce(math.gcd,nlist)

N = int(input())
A = list(map(int,input().split()))
res = 0
for i in range(N) :
	new = A.copy()
	del new[i]
	if mygcd(new) > res and res != 1 :
		res = mygcd(new)
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = 0
def gcd(a, b):
    r = a % b
    if r == 0:
        return b
    else:
        return gcd(b, a % b)
for i in range(N):
  a = A[:i]+A[i+1:]
  b=gcd(a[0],a[1])
  for j in range(2,N-1):
    b = gcd(b,a[j])
  if C < b:
    C = b
print(C)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce
N=int(input())
A=set(map(int,input().split()))
l=[]
for i in A:
    if A-{i}!={}:
        l.append(reduce(fractions.gcd,A-{i}))
    else:
        l.append(i)
print(max(l))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
N=int(input())
A=set(map(int,input().split()))
l=[]
for i in A:
    l.append(reduce(math.gcd,A-{i},0))
print(max(l))

Submission status: Runtime Error

Solution code:
from math import gcd

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

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

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

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

Submission status: Runtime Error

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
max_gcd = 1

def left_gcd(i):
    # i >= 1
    index = 0
    current_gcd = a[0]
    while index < i:
        current_gcd = math.gcd(current_gcd, a[index])
        index += 1

    return current_gcd

def right_gcd(i):
    # i < n-1
    index = n-1
    current_gcd = a[index]
    while index > i:
        current_gcd = math.gcd(current_gcd, a[index])
        index -= 1
    return current_gcd

for i in range(n):
    temp_gcd = 1
    if i == 0:
        temp_gcd = right_gcd(i)
    elif i == n-1:
        temp_gcd = left_gcd(i)
    else: 
        temp_gcd = math.gcd(left_gcd(i), right_gcd(i))
    max_gcd = max(temp_gcd, max_gcd)

print(max_gcd)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
a.sort()
max_gcd = 1

def gcd_3(n1,n2,n3):
    return math.gcd(math.gcd(n1,n2),math.gcd(n1,n3))

if len(a) == 2:
    max_gcd = max(a)
elif len(a) == 3:
    gcd1 = math.gcd(a[0],a[1])
    gcd2 = math.gcd(a[1],a[2])
    gcd3 = math.gcd(a[0],a[2])
    max_gcd = max(gcd1,gcd2,gcd3)
else:
    gcd1 = gcd_3(a[0], a[1],a[2])
    gcd2 = gcd_3(a[1], a[2],a[3])
    gcd3 = gcd_3(a[2], a[0],a[3])
    gcd4 = gcd_3(a[0],a[1],a[3])
    max_gcd = max(gcd1,gcd2, gcd3, gcd4)

print(max_gcd)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
a.sort()
max_gcd = 1
if len(a) == 2:
    max_gcd = max(a)
else:
    gcd1 = math.gcd(a[0], a[1],a[2])
    gcd2 = math.gcd(a[1], a[2],a[3])
    gcd3 = math.gcd(a[2], a[0],a[3])
    gcd4 = math.gcd(a[0],a[1],a[3])
    max_gcd = max(gcd1,gcd2, gcd3, gcd4)

print(max_gcd)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
a.sort()
max_gcd = 1
if len(a) == 2:
    max_gcd = max(a)
else:
    gcd1 = math.gcd(a[0], a[1])
    gcd2 = math.gcd(a[1], a[2])
    gcd3 = math.gcd(a[2], a[0])
    max_gcd = max(gcd1,gcd2, gcd3)

print(max_gcd)