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:
from sys import stdin

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

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

left = []
right = []

for i in range(n):
  if(i==0):
    before = an[i]
  else:
    before = gcd(before, an[i])
  left.append(before)

for i in range(n):
  if(i==0):
    before = an[n-1-i]
  else:
    before = gcd(before, an[n-1-i])
  right.append(before)

max = 0
for i in range(n):
  if(i==0):
    before = right[n-2];
  elif(i==(n-1)):
    before = left[n-2];
  else:
    before = gcd(left[i-1],right[n-2-i])
  max = max(before,max)

print(max)

Submission status: Runtime Error

Solution code:
import math

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

def list_gcd(l):
    if len(l) == 0:
      return 0
    elif len(l) == 1:
      return l[0]
    elif len(l) == 2:
      return gcd(l[0], l[1])
    else:
      m = l[1:]
      return gcd(l[0], list_gcd(m))

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

if N == 2:
  print(max(A))
else:
  m = -1
  for i in range(N):
    L = [A[j] for j in range(0, i)]
    R = [A[j] for j in range(i+1, N)]
    m = max(m, gcd(list_gcd(L),list_gcd(R)))
  print(m)

Submission status: Runtime Error

Solution code:
import math

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

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

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

print(max(G))

Submission status: Runtime Error

Solution code:
import math
import sys

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

if N == 2:
    print(max(A))
    sys.exit(0)

L = [0 for i in range(N)]
R = [0 for i in range(N)]
L[0] = A[0]
R[-1] = A[-1]

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

# print(L)
# print(R)

max_gcd = max(L[-2], R[1])
for i in range(1, N-1):
    #print(math.gcd(L[i-1], R[i+1]))
    max_gcd = max(max_gcd, math.gcd(L[i-1], R[i+1]))

print(max_gcd)
#min_g = max(G1[-1], G2[1])
#min_g2 = [math.gcd(G1[i], G2[i+1]) for i in range(N-2)]

Submission status: Runtime Error

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

L = len(A)
from functools import reduce
from fractions import gcd

g = reduce(gcd, A)

class SegTree():
  import math
  def __init__(self, func, A, identity=1):
    self._func = func
    self._N = 1
    while self._N < len(A):
      self._N *= 2
       # 長さ 7 なら N=3 (2^3=8)
    self._identity = identity # maxなら-1e9, sumなら0, productなら1

    self._tree = [self._identity] * (2*self._N -1) 
    self._tree[self._N-1:self._N-1+ len(A)] = A #最下段に元の配列を
    for i in range(self._N-1+len(A), -1, -1):
      self._update_single_node(i)
    ########
    # N=3: #
    # 0000 #
    # 1122 #
    # 3456 #
    ########
    self._node_range = [(i-self._N+1,i-self._N+2) for i in range(2*self._N-1)] #right_exclusive
    for i in range(self._N-2, -1, -1):
      self._node_range[i] = (self._node_range[2*i+1][0], self._node_range[2*i+2][1])

  def _update_single_node(self,i):
    self._tree[i] = self._func(self._tree[2*i+1], self._tree[2*i+2])

  def point_update(self,i,x):
    # A[i] = x (addではないので注意)
    j = self._N-1+i
    self._tree[j] = x
    while j>0:
      j = (j-1)//2
      self._update_single_node(j)

  def range_query(self, l, r, on=0):
    # reduce(func, A[l:r])
    # onはノード[on]上と重なる部分の，ということ
    onl, onr = self._node_range[on]
    
    # ノード[on]上はすべて覆う
    if l<=onl and onr<=r:
      return self._tree[on]
    
    # 重なりなし
    elif onr<=l or r<=onl:
      return self._identity
    
    else:
      return self._func(self.range_query(l, r, on=2*on+1),
                        self.range_query(l, r, on=2*on+2))

st = SegTree(gcd, A, identity=l)

answer = 0
for i in range(len(A)):
  st.point_update(i, A[i-1])
  answer = max(answer, st._tree[0])
  st.point_update(i, A[i])

print(answer)

Submission status: Runtime Error

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

L = len(A)
from functools import reduce
from fractions import gcd

g = reduce(gcd, A)

class SegTree():
  import math
  def __init__(self, func, A, identity=0):
    self._func = func
    self._N = 1
    while self._N < len(A):
      self._N *= 2
       # 長さ 7 なら N=3 (2^3=8)
    self._identity = identity # maxなら-1e9, sumなら0, productなら1

    self._tree = [self._identity] * (2*self._N -1) 
    self._tree[self._N-1:self._N-1+ len(A)] = A #最下段に元の配列を
    for i in range(self._N-1+len(A), -1, -1):
      self._update_single_node(i)
    ########
    # N=3: #
    # 0000 #
    # 1122 #
    # 3456 #
    ########
    self._node_range = [(i-self._N+1,i-self._N+2) for i in range(2*self._N-1)] #right_exclusive
    for i in range(self._N-2, -1, -1):
      self._node_range[i] = (self._node_range[2*i+1][0], self._node_range[2*i+2][1])

  def _update_single_node(self,i):
    self._tree[i] = self._func(self._tree[2*i+1], self._tree[2*i+2])

  def point_update(self,i,x):
    # A[i] = x (addではないので注意)
    j = self._N-1+i
    self._tree[j] = x
    while j>0:
      j = (j-1)//2
      self._update_single_node(j)

  def range_query(self, l, r, on=0):
    # reduce(func, A[l:r])
    # onはノード[on]上と重なる部分の，ということ
    onl, onr = self._node_range[on]
    
    # ノード[on]上はすべて覆う
    if l<=onl and onr<=r:
      return self._tree[on]
    
    # 重なりなし
    elif onr<=l or r<=onl:
      return self._identity
    
    else:
      return self._func(self.range_query(l, r, on=2*on+1),
                        self.range_query(l, r, on=2*on+2))

st = SegTree(gcd, A, identity=l)

answer = 0
for i in range(len(A)):
  st.point_update(i, A[i-1])
  answer = max(answer, st._tree[0])
  st.point_update(i, A[i])

print(answer)

Submission status: Runtime Error

Solution code:
from fractions import gcd
N = int(input())
Alist = list(map(int, input().split()))
Rlist = [0]*(N+2)
Llist = [0]*(N+1)
for i in range(N):
    Llist[i+1] = gcd(Llist[i], Alist[i])
    Rlist[N-i] = gcd(Rlist[N-i+1], Alist[N-i+1])
print(max([gcd(Rlist[i], Llist[i]) for i in range(N)]))

Submission status: Runtime Error

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

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

num = []
for i in range(N):
    temp = A[0:i] + A[i+1:]
    num.append(gcd(temp))

print(max(num))

Submission status: Runtime Error

Solution code:
"""
問題文

N個の整数 A1,A2,...,ANが黒板に書かれています。
あなたはこの中から整数を 1つ選んで、1 以上 109以下の好きな整数に書き換えます。
元の整数と同じ整数に書き換えても構いません。書き換えた後のN個の整数の最大公約数の最大値を求めてください。

制約
入力は全て整数である。
2≤N≤105
1≤Ai≤109
"""

"""
3 5 7
最大公約数: 1
最大の最大公約数: 1

8 4 6
最大公約数: 2
最大の最大公約数: 4
『aとbとcの最大公約数』　＝　『aとbの最大公約数』と『c』　の最大公約数

A1 , A2 , A3 ... An

gcd(A1 , A2 , A3 ... An-1) * N
= gcd * N * N
= gcd * N^2

gcd(A1 , A2 , A3 ... An-1)
gcd(A1 , A2 , A3 ... An-2 , An) = gcd(A1 , A2 , A3 ... An-2) , An
"""
#from math import gcd
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_L_map = {}
def GCD_L(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        if len(numbers) in gcd_L_map : 
            return gcd_L_map[len(numbers)]
        value = GCD_L(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        gcd_L_map[len(numbers)] = result
        return result

gcd_R_map = {}
def GCD_R(numbers):
    numbers = list(reversed(numbers))
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        if len(numbers) in gcd_L_map : 
            return gcd_R_map[len(numbers)]
        value = GCD_L(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        gcd_R_map[len(numbers)] = result
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : 
            prev_list = a_list[1:]
            rst = GCD_R(prev_list )
        elif i == N - 1 : 
            after_list = a_list[:N-1]
            rst = GCD_L(after_list)
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
            rst = gcd(GCD_L(prev_list ) , GCD_R(after_list ))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
N = int(input())
Alist = list(map(int, input().split()))
Rlist = [0]*(N+2)
Llist = [0]*(N+1)
for i in range(N):
    Llist[i+1] = gcd(Llist[i], Alist[i])
    Rlist[N-i] = gcd(Rlist[N-i+1], Alist[N-i+1])
print(max([gcd(Rlist[i], Llist[i]) for i in range(N)])

Submission status: Runtime Error

Solution code:
import fractions

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

def mass_gcd(L):
    # num is 1
    if len(L) == 1:
        return L[0]
    else:
        max_gcd = fractions.gcd(L[0], L[1])

    # solve gcd
    for _ in range(1, len(L) - 1):
        max_gcd = max(max_gcd, fractions.gcd(L[i], L[i + 1]))
    return max_gcd

ans = mass_gcd(A)
for i in range(0, N):
    left = A[:i]
    right = A[(i + 1):]
    if len(left) == 0:
        pass
    elif len(right) == 0:
        pass
    else:
        ans = max(ans, fractions.gcd(mass_gcd(left), mass_gcd(right)))
print(ans)

Submission status: Runtime Error

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

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

num = []
for i in range(N):
    temp = A[0:i] + A[i+1:]
    num.append(gcd(*temp))

print(max(num))

Submission status: Runtime Error

Solution code:
import math
import sys

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

if N == 2:
    print(max(A))
    sys.exit(0)

L = [0 for i in range(N)]
R = [0 for i in range(N)]
L[0] = A[0]
R[-1] = A[-1]

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

# print(L)
# print(R)

max_gcd = max(L[-2], R[1])
for i in range(1, N-1):
    print(math.gcd(L[i-1], R[i+1]))
    max_gcd = max(max_gcd, math.gcd(L[i-1], R[i+1]))

print(max_gcd)
#min_g = max(G1[-1], G2[1])
#min_g2 = [math.gcd(G1[i], G2[i+1]) for i in range(N-2)]

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N =int(input())
A = list(map(int,input().split()))
A.sort()

if len(A) < 2:
    A.append(1)
    A.append(1)
import fractions

ans = 0
for i in range(4):
    for j in range(4):
        if i == j:
            continue
        ans = max(ans,fractions.gcd(A[i],A[j]))
        
print(ans)

Submission status: Runtime Error

Solution code:
import fractions

def list_gcd(l):
    if len(l) == 0:
      return 0
    elif len(l) == 1:
      return l[0]
    elif len(l) == 2:
      return fractions.gcd(l[0], l[1])
    else:
      m = l[1:]
      return fractions.gcd(l[0],list_gcd(m))

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

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

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N =int(input())
A = list(map(int,input().split()))
A.sort()

if len(A) > 2:
    A.append(1)
import fractions

ans = 0
for i in range(3):
    for j in range(3):
        if i == j:
            continue
        ans = max(ans,fractions.gcd(A[i],A[j]))
        
print(ans)

Submission status: Runtime Error

Solution code:
mport fractions

n = int(input())
a = list(map(int, input().split()))
t = []
l = 0
r = 0

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

print(max(t))

Submission status: Runtime Error

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


def gcd(vs):
    vs = list(set(vs))
    while True:
        m = min(vs)
        vs_ = [0] * len(vs)
        for v in vs:
            if v % m != 0:
                vs_.append(v % m)
        vs_ = vs_ + [m]
        vs = vs_
        if len(vs) == 1:
            return vs[0]


gcds = []
for i in range(len(As)):
    gcds.append(gcd(As[:i] + As[i + 1:]))

print(max(gcds))

Submission status: Runtime Error

Solution code:
#C問題　累積和でやってみよう
N = int(input())
A = [int(i) for i in input().split()]

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

num = []
for i in range(N):
    temp = A[0:i] + A[i+1:]
    num.append(gcd(*temp))

print(max(num))

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:
        a = b
        max = functools.reduce(math.gcd, a)
    elif len(a) != 1:
        max = functools.reduce(math.gcd, b)

    print(a)
    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)
    print(max)
    print(a)
    a_resu = (math.gcd(max,a))
    b_resu = (functools.reduce(math.gcd,b))
    if a_resu < b_resu:
        a_new.remove(a_resu)
        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:
import functools
import math
N = int(input())
A = [int(i) for i in input().split()]

num = 1

if len(A) == 2:
  print(max(A))
else:
  for i in range(len(A)):
    B = A[ : i] + A[i + 1:]
    g = functools.reduce(math.gcd, B)
    if (g > num):
      num = g
  print(num)

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
def gcdover3(a):
    res = max(a)
    for idx in range(len(a)-1):
        res = min([gcd(a[idx],a[idx+1]),res])
    return res

if N<=2:
    print(max(A))
else:
    gcdss = []
    for i,a in enumerate(A):
        A_temp = copy.deepcopy(A)
        A_temp.pop(i)
        gcdss.append(gcdover3(A_temp))
    print(max(gcdss))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
ans = 0
index = [0] * N
max = 0
temp = [0] * (10**9 + 1)
for i in range(N):
    for d in range(1, int(A[i]/2)):
        if A[i] % d == 0:
            temp[d] += 1
    temp[A[i]+ 1] += 1
for i in range(N-1, 0, -1):
    if N - 1 <= temp[i]:
        ans = i
        break
print(ans)

Submission status: Runtime Error

Solution code:
import functools
import math
N = int(input())
A = [int(i) for i in input().split()]

num = 1

if len(A) == 2:
  print(max(A))
  for i in range(len(A)):
    B = A[ : i] + A[i + 1:]
    g = functools.reduce(math.gcd, B)
    if (g > num):
      num = g
  print(num)

Submission status: Runtime Error

Solution code:
import math

def gcd(l):
    if len(l) == 0:
      return 0
    elif len(l) == 1:
      return l[0]
    elif len(l) == 2:
      return math.gcd(l[0], l[1])
    else:
      m = l[1:]
      return math.gcd(l[0],gcd(m))

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

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

Submission status: Runtime Error

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

N= int(input())
t = list(map(int, input().split()))
soin = []
temp=0
nokori =[]

for i in range(N):
 nokori = t[0:i]+t[i+1:]
 for j in range(N-2):
     soin.append(gcd(nokori[j],nokori[j+1]))
print(max(soin))

Submission status: Runtime Error

Solution code:
import functools
import math
n = int(input())
a = list(map(int,input().split()))
odd_tmp = []
ans = 1
oddcnt = 0
a.sort()

for i in range(n):
    if a[i] % 2 != 0:
        oddcnt += 1
        odd_tmp.append(int(a[i]))
if oddcnt < 2:
    if oddcnt == 1:
        a.remove(odd_tmp[0])
    print(functools.reduce(math.gcd, a))
    exit()
for i in range(oddcnt):
    tmp = [] + a
    tmp.remove(odd_tmp[i])
    ans = max(functools.reduce(math.gcd, tmp), ans)

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:
        a = b
        max = functools.reduce(math.gcd, a)
    elif len(a) != 1:
        max = functools.reduce(math.gcd, b)

    print(a)
    while True:
        if len(a) == 1:
            break
        b = a[N//2:]
        a = a[:N//2]
        if len(a) == 1:
            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:
        a_new.remove(a_resu)
        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())
A = list(map(int, input().split()))
ans = 0
index = [0] * N
max = 0
temp = [0] * (10**9 + 1)
for i in range(N):
    for d in range(1, int(A[i]/2)):
        if A[i] % d == 0:
            temp[d] += 1
    temp[A[i]+ 1] += 1
for i in range(N-1, 0, -1):
    if N - 1 <= temp[i]:
        ans = i
        break
print(ans)

Submission status: Runtime Error

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

def spaceinput():
    return list(map(int,input().split(" ")))


N=int(input())
a=sorted(spaceinput())
#,reverse=True)


notrewrite=[-1]*N
rewrite=[-1]*N
best=[-1]*N

notrewrite[0]=rewrite[0]=best[0]=a[0]
for i in range(1,N):
    notrewrite[i]=gcd(notrewrite[i-1],a[i])
    rewrite[i]=notrewrite[i-1]
    best[i]=max([rewrite[i],gcd(a[i],best[i-1])])
    #print([notrewrite[i],rewrite[i]])


if N==2:
    print(max(a))
else:
    print(best[N])

Submission status: Runtime Error

Solution code:
"""
python3 main.py < input.txt
"""
import math
import itertools as it
import heapq


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

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

m=max(R[1], L[-2])
for i in range(1, N-1):
        m=max(m, math.gcd(L[i-1], R[i+1]))
print(m)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = [int(i) for i in input().split()]
B = sorted(A.copy(), reverse=True)

ans = 1
counts = [N] * (B[0]+1)
for d in range(B[0], B[-1]//2-1, -1):
    for b in B:
        if b % d == 0:
            counts[d] -= 1
            if d != b // d:
                counts[b//d] -= 1

    if counts[d] < 2:
        ans = d
        break

if ans == 1:
    for i in range(B[-1]//2, 0, -1):
        if counts[i] < 2:
            ans = i
            break

print(ans)

Submission status: Runtime Error

Solution code:
import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
    A = list(set(A))
    N2 = len(A) + 1
  else:
    # N = 3
    # A = [7,6,8]
    # N = 3
    # A = [12,15,18]
    N = 3
    N2 = 2
    A = [1,1000000000,1000000000]

  x = 0
  for i in range(N2):
    if N == 1:
      tmp  = 1000000000
    elif N2 == 2 :
      tmp = A[i]
      # tmp = functools.reduce(fractions.gcd, A[i])
    else:
      tmp = functools.reduce(fractions.gcd, A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
def cf(a,b):
    if a == b:
        return a
    while a != 0 and b != 0:
        if a > b:
            a = a % b
        else:
            b = b % a
    if a == 0:
        n = b
    else: 
        n = a
    return n
ans = 1
for y in range(N):
    lst = A[:]
    lst.pop(y)
    mcf = cf(lst[0],lst[1])
    if N <= 2:
        break
    for x in range(N-3):
        mcf = cf(mcf,lst[x+1])
    if ans < mcf:
        ans = mcf
print(ans)

Submission status: Runtime Error

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
N = input()
A = map(int, raw_input().split())
B = []
for i in range(N):
	t = 0
	m = min(B)
	for j in range(N):
		if(i == j):
			continue
		else:
			if(t == 0):
				t = A[j]
			else:
				t = gcd(t,A[j])
		if(t <= m):
			break
	B.append(t)
print(max(B))

Submission status: Runtime Error

Solution code:
import fractions

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

ans=[]

for i in range(N):
    if i<N-1:
        if fractions.gcd(A[i], A[i+1]) != 1:
            ans.append(fractions.gcd(A[i], A[i+1]))

minimum=min(ans)

if minimum % 2 != 0:
    print(minimum*2)
else:
    print(minimum)

Submission status: Runtime Error

Solution code:
from copy import deepcopy
from fractions import gcd
from functools import reduce
import sys
N = int(input())
A = [1] + list(set([int(i) for i in input().split()])) + [1]
 
def gcd_(*numbers):
    return reduce(gcd, numbers)

ans = []
for i in range(1, N+1):
    ans.append(gcd_(gcd_(*A[:i]), gcd_(*A[i+1:])))
print(max(ans))

Submission status: Runtime Error

Solution code:
import fractions
import sys
import copy
 
def gcd2(array):
    gcd = fractions.gcd(array[0], array[1])
    for i in range(2, len(array)-1):
        gcd = fractions.gcd(gcd, array[i])
    return gcd
 
 
n = int(input())
a = sorted([int(x) for x in input().split(' ')])
 
gs = []
for i in range(n-1):
    gs.append((fractions.gcd(a[i], a[i+1]), i))
gs.append((fractions.gcd(a[n-1], a[0]), n-1))
 
_, l = min(gs)
b = copy.copy(a)
c = copy.copy(a)
 
del b[l]
del c[l+1]
bd = gcd2(b)
cd = gcd2(c)
print(max(bd, cd))

Submission status: Runtime Error

Solution code:
import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
    A = list(set(A))
    N2 = len(A) + 1
  else:
    # N = 3
    # A = [7,6,8]
    # N = 3
    # A = [12,15,18]
    N = 2
    N2 = 2
    A = [1000000000,1000000000]

  x = 0
  for i in range(N2):
    if N == 1:
      tmp  = 1000000000
    # elif N2 == 2 :
    #   tmp = functools.reduce(fractions.gcd, A)
    else:
      tmp = functools.reduce(fractions.gcd, A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

Submission status: Runtime Error

Solution code:
# C
import functools
def gcd(a,b):
    if a<b:a,b=b,a
    if a%b==0:return b
    return gcd(b,a%b)

n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
pre_a = -1

for i in range(n):
    if pre_a != a[i]:
        if len(a[:i]) >= 1 and len(a[i+1:] >= 1):
            res = gcd(functools.reduce(gcd, a[:i]), functools.reduce(gcd, a[i+1:]))
        else:
            values = a[:i] + a[i+1:]
            res = functools.reduce(gcd, values)
        ans = max(ans, res)
    pre_a = a[i]
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a.sort(reverse=True)

flag = 0

for i in reversed(range(a[0])):
    for aa in a:
        if aa % i != 0:
            flag = 1

    if flag == 0:
        print(i)
        break

    flag = 0

Submission status: Runtime Error

Solution code:
import numpy

def gcd(a,b,mem):
    str12 = "{}.{}".format(a, b)
    if b==0:
        return a
    elif str12 in mem.keys():
        return mem[str12]
    else:
        mem[str12] = gcd(b,a%b,mem)
        return  mem[str12]




N = int(input())
A = numpy.array([int(x) for x in input().split()])
a2 = list(A)
max = 0
mem = {}
for i,_ in enumerate(A):
    A.pop(i)
    res = A[0]
    for i in A[1:]:
        res = gcd(i,res,mem)
    max = res if res >= max else max
    A = list(a2)


print(max)

Submission status: Runtime Error

Solution code:
import math
import copy
from functools import reduce

max_gcf=0

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

for i in range(N):
  list_tmp = copy.deepcopy(list_A)
  del list_tmp[i]
  gcf = reduce(math.gcd, list_tmp)
  if gcf > max_gcf:
    max_gcf = gcf

print(max_gcf)

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:
    gcdss = []
    for i,a in enumerate(A):
        A_temp = copy.deepcopy(A)
        A_temp.pop(i)
        gcds = []
        for i in range(len(A_temp)-1):
            if i == 0:
                gcd_temp = gcd(A_temp[i],A_temp[i+1])
            else:
                gcd_temp = gcd(gcd_temp[-1],A_temp[i+1])
            gcds.append(gcd_temp)
            if gcd_temp == 1:
                break
        gcdss.append(min(gcds))
    print(max(gcdss))

Submission status: Runtime Error

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

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

list_koyaku1=[]
list_koyaku11=[]       
         
if N==2:
    ans=max(A)

else:
    b=min(A)
    c=max(A)
    if gcd(c,b)==1:
        b=A[-2]
    
    for a in A:
        list_koyaku1.append(gcd(a,b))

    for a in list_koyaku1:
        list_koyaku11.append(gcd(a,c))
    
    number_prime=list_koyaku11.count(1)
    
    if number_prime>1:
        ans=1
    else:
        ans=max(list_koyaku11)

print(ans)

Submission status: Runtime Error

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

 ans = 0
 valid = []
 def gcd(a,b):
     if b==0:return a
     return gcd(b,a%b)


 for i in range(N):
   valid = A[:]
   valid.pop(i-1)
   valid.sort()
   valid_number = valid[0]
   for j in range(N-1):
     valid_number = gcd(valid_number,valid[j])
   ans = max(ans,valid_number)

 print(ans)

Submission status: Runtime Error

Solution code:
# GCD on Blackboard
"""
リストのどの要素を抜いた時に最大公約数が最大値をとるか調べて、
その時の最大公約数を出力する。
"""

from functools import reduce

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


def get_gcd(a, b):
    """
    ユークリッド互除法により最大公約数を求める。
    """
    if a < b:
        a, b = b, a

    elif a % b == 0:
        return b

    return get_gcd(b, a % b)


def get_gcd_l(l):
    """
    リストの要素の最大公約数を求める。
    """
    return reduce(get_gcd, l)


def answer(l):
    """
    どの要素を抜いた時に最大公約数が最大値をとるか調べる。
    """
    len_l = len(l)
    answers = []  # 答えの案

    for i in range(len_l): # 要素を1個ずつ消してみる。
        temp_l_left = l[0 : i]
        temp_l_right = l[i+1 : len_l]
        gcd_temp_l_left = get_gcd_l(temp_l_left)
        gcd_temp_l_right = get_gcd_l(temp_l_right)
        answers.append(max(gcd_temp_l_left, gcd_temp_l_right))
    ans = max(answers)
    return ans


def main():
    """
    答えを出力する。
    """
    print(answer(A))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict

data = {}


def gcv(a, b):
    global data
    # if a >= b:
    #     a, b = b, a
    if data.get('{}:{}'.format(a, b), None):
        return data['{}:{}'.format(a, b)]
    elif data.get('{}:{}'.format(b, a), None):
        return data['{}:{}'.format(b, a)]

    if b == 0:
        data['{}:{}'.format(a, b)] = a
        return a
    else:
        # print(a, b)
        data['{}:{}'.format(a, b)] = gcv(b, a % b)

        return data['{}:{}'.format(a, b)]


def solve(n, a):
    if n == 2 and a[0] == a[1]:
        return a[0]
    n_dict = defaultdict(int)
    for i in range(len(a) - 1):
        for j in range(len(a) - i -  1):
            # print(i, j + i + 1)
            n_dict[gcv(a[i], a[j + i + 1])] += 1
    n_new = sorted(n_dict.items(), key=lambda x: x[1], reverse=True)
    if n_new[0][0] == 1:
        try:

            return n_new[1][0]
        except:
            return n_new[0][0]
    if n_new[1][1] == n-2:
        return n_new[1][0]
    return n_new[0][0]



def input_from_console():
    n = int(input())
    a = list(map(int, input().split()))
    return n, a


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8

import math
from functools import reduce

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

gcd_list = []
for i in range(N):
    removed = A[0:i] + A[i + 1:N]
    gcd_list.append(reduce(math.gcd, removed))
print(max(gcd_list))

Submission status: Runtime Error

Solution code:
import collections

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

divisor = []
for i in range(N):
    for j in range(1,(int(A[i])+1)):
        if A[i] % j == 0:
            divisor.append(j)

count_divisor = collections.Counter(divisor)

max = 1
for i in range(len(count_divisor)):
    if count_divisor[i][1] >= N-1:
        if count_divisor[i][0] >= max:
            max = count_divisor[i][0]
print(max)