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個の整数 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
"""
import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_map = {}
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : return gcd_sub(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[1:])
        return value

def gcd_sub(a , b) : 
    if (a , b) in gcd_map : return gcd_map[(a,b)]
    else : 
        rst = gcd(a , b)
        gcd_map[(a,b)] = rst
        return rst

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

Submission status: Runtime Error

Solution code:
#C
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
N=int(input())
A = [int(j) for j in input().split()]
A=list(set(A))
A.sort()
ans=0
 
for a in A:
    if len(A)>2:
        tmp=A.copy()
        tmp.remove(a)
        ans=max(ans,gcd(*tmp))
    elif len(A)==1:
        ans=A[0]
    else:
        ans=gcd(A[0],A[1])
print(ans)

Submission status: Runtime Error

Solution code:
import math

def gcd(numlist):
    l = len(numlist)
    re = math.gcd(numlist[0], numlist[1])
    for i in range(2,l):
        re = math.gcd(re, numlist[i])
    return re

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

ans = 0

maxA = max(Alist)
for i in range(N):
    Ai = Alist[i]
    Alist[i] = maxA
    Agcd = gcd(Alist)
    if ans < Agcd:
        ans = Agcd
    Alist[i] = Ai

print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
N=int(input())
A=list(map(int,input().split()))
g=[]
def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
B=A[:]
for i in range(N):
    del B[i]
    g.append(gcd_list(B))
    B=A[:]
print(max(g))

Submission status: Runtime Error

Solution code:
import functools
import math

n=int(input())
v=list(map(int,input().split()))
tmp = 3
for i in v:
        if i % 2 == 0:
            tmp = i
for i in v:
    if i % 2 != 0:
        v[v.index(i)] = tmp

gcd = functools.reduce(math.gcd, v)

print(gcd)

Submission status: Runtime Error

Solution code:
n = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(max(a), 1, -1):
  count = len(a)
  for value in a:
    if value%i == 0 :
      count -= 1
  if count <= 1 :
    break
print(str(i))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import copy
import fractions
from functools import reduce


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


n = input()
a = list(map(int, input().split()))
a.sort()
b = []
c = 0
for i in range(len(a) / 2):
    b = copy.copy(a)
    b[i] = max(a)
    t = gcd(*b)
    if c < t:
        c = t

print(c)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

l = []
for i in range(n):
    a_reduced = a[:i] + a[i+1:]
    l.append(gcd_mul(*a_reduced))

print(max(l))

Submission status: Runtime Error

Solution code:
import fraction
from functools import reduce

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

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

  x = 0
  for i in range(N):
    tmp = gcd_list(A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

Submission status: Runtime Error

Solution code:
import math
import fractions
from collections import Counter as ct

def gcd_list(l):
    for n,i in enumerate(l):
        if n==0:cur=i
        cur=fractions.gcd(cur,i)
    return cur

def yakusulist(n):
    ans=[]
    for i in range(1,int(math.sqrt(n+1))+1):
        if n%i==0:
            ans.append(i)
            ans.append(n//i)
    return(set(ans))
    
def warennnokaomaera(n):
    if n==1:return(True)
    for k,i in enumerate(A):
        if k<3:continue
        if i%n:return(False)
    return(True)

N=int(input())
data=[int(i) for i in input().split()]
datagcd=gcd_list(data)
A=[i//datagcd for i in data]
if N>2:
    alpha=sorted(ct(A).keys())
    min3=[alpha[0],alpha[1],alpha[2]]
    p,q,r=[yakusulist(n) for n in min3]
    pq=p&q
    qr=q&r
    rp=r&p
    overtwo=pq|qr|rp
    overtwo=sorted(list(overtwo),reverse=True)
    for i in overtwo:
        if warennnokaomaera(i):
            print(datagcd*i)
            break
else:print(max(data))

Submission status: Runtime Error

Solution code:
from math import gcd

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


ans = -1
for i in range(N):
    flg = 0
    tmp = A[:i] + A[i+1:]
    Gcd = tmp[0]
    for j in range(N-2):
        g = gcd(tmp[j], tmp[j+1])
        Gcd = gcd(Gcd, g)
        if Gcd < ans:
            flg = 1
            break
    if flg:
        continue
    ans = max(ans, Gcd)

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import fractions

N = int(input()) 
a = list(int(_) for _ in input().split()) 

ma = list(itertools.combinations(a,N-1))
if N >=3:
    print(max([fractions.gcd(*ma[i]) for i in range(N)]))
else:
    print(max(a))

Submission status: Runtime Error

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

import random
N_s = random.randint(0,N)

max = 1e9+1
change = random.randint(1, max)

V[N_s] = change

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

res = V[0]
for c in V[1::]:
    res = gcd(res , c)
print(res)

Submission status: Runtime Error

Solution code:
import collections

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

    # divisors.sort()
    return divisors
  
def check(num, kouho, N, i):
  for val in kouho.keys():
    if num%val == 0:
      kouho[val] += 1
  return {k: v for k, v in kouho.items() if v > i-2}
  
  
N = int(input())
A= list(map(int, input().split()))
pri_flag = True

divs = []
for i in range(3):
  divs.extend(cal_divs(A[i]))
kouho = {}
c = collections.Counter(divs)
for div, n in c.most_common():
	kouho[div] = n
    
kouho = {k: v for k, v in kouho.items() if v > 1}

for i,num in enumerate(A[3:]):
  kouho = check(num, kouho, N, i+3)
  if len(kouho) == 1:
    print(1)
    pri_flag = False
    break

if pri_flag:
  print(max(kouho))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

l = []
for i in range(n):
    a_reduced = a[:i] + a[i+1:]
    l.append(gcd(*a_reduced))

print(max(l))

Submission status: Runtime Error

Solution code:
import math, copy, sys
def gcd(list):
    if len(list) == 2:
        return math.gcd(list[0], list[1])
    else:
        return math.gcd(list[0], gcd(list[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):
        list = copy.copy(A)
        del list[i]
        if tmp < gcd(list):
            tmp = gcd(list)
    print(tmp)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
import numpy as np
def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = int(input())
A = list(map(int, input().split()))
ans=0
for i in range(N):
    a = A[0]
    del A[0]
    
    b = gcd_list(A)
    if ans < b:
        ans = b
    A.append(a)

print(ans)

Submission status: Runtime Error

Solution code:
import fraction

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

if n == 2:
    a.append(math.gcd(a[0],a[1]))

g0 = [math.gcd(a[0],a[1]),math.gcd(a[0],a[2]),math.gcd(a[1],a[2])]
s_g0 = sorted(g0,reverse=True)

maxg1 = s_g0[0]
maxg2 = s_g0[1]

if n >= 4:
    for i in range(n - 3):
        maxg1 = math.gcd(maxg1,a[i + 3])
        maxg2 = math.gcd(maxg2,a[i + 3])

if maxg1 >= maxg2:
    print(maxg1)
else:
    print(maxg2)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
a = [int(i) for i in input().split()]
if len(a) == 2:
    print(math.gcd(a[0],a[1]))
    return
minval = min(a)
maxgcd = max(math.gcd(a[0],a[1]),math.gcd(a[1],a[2]),math.gcd(a[0],a[2]))
maxgcd2 = maxgcd
for index, value in enumerate(a):
    if value == minval:
        continue
    temp = math.gcd(minval, value)
    if temp < maxgcd:
        maxgcd2 = maxgcd
        maxgcd = temp
print(maxgcd2)

Submission status: Runtime Error

Solution code:
import functools
import math

n=int(input())
v=list(map(int,input().split()))
tmp = 3
for i in v:
        if i % 2 == 0:
            tmp = i
    for i in v:
        if i % 2 != 0:
            v[v.index(i)] = tmp

gcd = functools.reduce(math.gcd, v)

print(gcd)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce

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

gcd_left = []
gcd_left.append(A[0])
for i in range(N-1):
    gcd_left.append(fractions.gcd(gcd_left[i],A[i+1]))

gcd_right = []
gcd_right.append(A[0])
for i in range(N-1,0):
    gcd_right.insert(0,fractions.gcd(gcd_right[0],A[i+1]))


min_gcd = 1
if min_gcd < gcd_right[1]:
    min_gcd = gcd_right[1]
if min_gcd < gcd_left[N-2]:
    min_gcd = gcd_left[N-2]
for i in range(0,N-2):
    tmp = fractions.gcd(gcd_left[i],gcd_right[i+2])
    if tmp > min_gcd:
        min_gcd = tmp
        
print(min_gcd)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

if max(v) % 2 ==0:
	b = True
else:
	b = False
		
v.sort()

for index,_ in enumerate(v):
	if _  % 2 ==0:
		if not b:
			v[index] = v[-1]
			#print("書き換え:" + str(_) + " 書き換え後:"  + str(v[-1]))
			break
			
	else:
		if b:
			v[index] = v[-1]
			#print("書き換え:" + str(_) + " 書き換え後:"  + str(v[-1]))			
			break
			
#print(v)

#固定
print(reduce(math.gcd,v))

Submission status: Runtime Error

Solution code:
import math
n=int(input())
array=list(map(int,input().split()))
max=0

for i in range(0,n):
	for j in range(i,n):
		if i!=j:
			# print(array[i])
			# print(array[j])
			num=math.gcd(array[i],array[j])
			# print(num)
			if max<num:
				max=num

print(max)

Submission status: Runtime Error

Solution code:
import math
#from functools import reduce
num=input()
list=list(map(int,input().split()))
pop_num=0
answer=[]
for x in range(len(list)):
    pop_num=list.pop(x)
    #print(list)
    #answer.append(reduce(math.gcd,list))
    answer.append(math.gcd(*list))
    #print(answer)
    list=[pop_num]+list
answer.sort()
#print(answer)
print(answer[-1])

Submission status: Runtime Error

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

N = int(input())
nums = [int(i) for i in input().split()]
nums2 = copy.copy(nums)
ans = []

def gcds(numlist):
    return reduce(math.gcd, numlist)


for i in range(N):
   nums.pop(i)
   ans.append(gcds(nums))
   nums = copy.copy(nums2)

print(max(ans))

Submission status: Runtime Error

Solution code:
import math

n = int(input())
a = []
tmp = input().split()
for i in range(n):
    a.append(int(tmp[i]))

if n == 2:
    if a[0] > a[1]:
        print(a[0])
    else:
        print(a[1])
    exit()

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

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

ans = math.gcd(a[0], rev[-2])
if ans < rev[-1]:
    ans = rev[-1]
        
prev_gcd = a[0]
for i in range(1, n-1):
    prev_gcd = math.gcd(prev_gcd, a[i])
    cand_ans = math.gcd(prev_gcd, rev[-1 - i])
    if cand_ans > ans:
        ans = cand_ans

print(ans)

Submission status: Runtime Error

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

from math import gcd

num_list = [0] * n

for i in range(n - 1):
    for j in range(i+1, n):
        num_list[i] = max(num_list[i], gcd(a[i], a[j]))
        num_list[j] = max(num_list[j], gcd(a[i], a[j]))

print(max(num_list))

Submission status: Runtime Error

Solution code:
memo = {}

def gcd(a, b):
    if (a, b) in memo.keys():
        return memo[(a, b)]
    if b == 0:
        return a
    return gcd(b, a % b)

n = input()
a_list = list(map(int, input().split()))
max_g = 0
for exclude in range(n):
    g = a_list[0] if exclude > 0 else a_list[1]
    for i, a in enumerate(a_list):
        if i == 0 or i == exclude:
            continue
        g_new = gcd(g, a)
        if (g, a) not in memo:
            memo[(g, a)] = g_new
        g = g_new
        if g <= max_g:
            break
    if g > max_g:
        max_g = g
print(max_g)

Submission status: Runtime Error

Solution code:
import math

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

if N == 2:
  print(max(A))
else:
  G = math.gcd(A[0], A[1])
  H = math.gcd(A[1], A[2])
  I = math.gcd(A[2], A[0])

  print(max(G, H, I))

Submission status: Runtime Error

Solution code:
import math

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

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

gcd.append(math.gcd(A[0], A[number-1]))

gcd.sort()

if gcd[0] == gcd[number-1]:
    print(gcd[0])

elif gcd[number-2] == gcd[number-1] and gcd[number-3] != gcd[number-2]:
    print(gcd[number-3])

else:
    print(gcd[number-1])

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
from copy import copy
N=int(input())
A=list(map(int,input().split()))
g=[]
def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
B=copy(A)
for i in range(N):
    del B[i]
    g.append(gcd_list(B))
    B=copy(A)
print(max(g))

Submission status: Runtime Error

Solution code:
import math
from collections import Counter as ct

def gcd_list(l):
    for n,i in enumerate(l):
        if n==0:cur=i
        cur=math.gcd(cur,i)
    return cur

def yakusulist(n):
    ans=[]
    for i in range(1,int(math.sqrt(n+1))+1):
        if n%i==0:
            ans.append(i)
            ans.append(n//i)
    return(set(ans))
    
def warennnokaomaera(n):
    if n==1:return(True)
    for k,i in enumerate(A):
        if k<3:continue
        if i%n:return(False)
    return(True)

N=int(input())
data=[int(i) for i in input().split()]
datagcd=gcd_list(data)
A=[i//datagcd for i in data]
if N>2:
    alpha=sorted(ct(A).keys())
    min3=[alpha[0],alpha[1],alpha[2]]
    p,q,r=[yakusulist(n) for n in min3]
    pq=p&q
    qr=q&r
    rp=r&p
    overtwo=pq|qr|rp
    overtwo=sorted(list(overtwo),reverse=True)
    for i in overtwo:
        if warennnokaomaera(i):
            print(datagcd*i)
            break
else:print(max(data))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(sys.getrecursionlimit())
N = int(input())
A = list(map(int,input().split(" ")))

def gcd (a: int,b: int) -> int:
    if a < b: a,b = b,a
    return a if b == 0 else gcd(b,a%b)
def lcm (a: int,b: int) -> int:
    return a // gcd(a,b) * b

memo = [-1] * N 
def dfs(i,v,flg):
    if i == N-1:
        if flg:
            return gcd(A[N-1],v)
        else:
            return v
    if flg:
        if memo[i] != -1:
            return memo[i]
        else:
            memo[i] = dfs(i+1,gcd(A[i],v),True)
            return memo[i]
    else:
        return max(dfs(i+1,gcd(v,A[i]),False),dfs(i+1,v,True))

if N == 2:
    print(max(A))
    exit(0)
else:
    print(max(dfs(1,A[0],False),dfs(2,A[1],True)))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
a = list(map(int,input().split()))
err = 8
ma1 = 1
for i in range(1,N):
    f = math.gcd(a[0],a[i])
    for j in range(1,N):
        if a[j]%f != 0:
            err = err + 1
        if err == 10:
            break
    if err < 10 and f > 1:
        ma1 = f
arr = 8
ma2 = 1
tmp = a[N-1]
a[N-1]=a[0]
a[0]=tmp


for i in range(1,N):
    f = math.gcd(a[0],a[i])
    for j in range(1,N):
        if a[j]%f != 0:
            arr = arr + 1
        if arr == 10:
            break
    if arr < 10 and f > 1 :
        ma2 = f



if ma2 > ma1:
    print(ma2)
else:
    print(ma1)

Submission status: Runtime Error

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            m = n // i
            if i != m:
                divisors.append(m)

    return divisors

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

div = [0] * (max(A) + 1)
for a in A:
    for i in make_divisors(a):
        div[i] += 1

for i, num in zip(range(max(A), 0, -1), reversed(div)):
    if num > N - 2:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
import math
from sys import stdin
from functools import reduce

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

def GCD(numbers):
    if len(numbers)==1:
        return number[0]
    else:
        e = k(numbers[0],numbers[1])
        if len(numbers)==2:
            return e
        else:
            for j in range(2, len(numbers)):
                ele=k(e,numbers[i])
            return e

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
max = 0
for i in range(N):
    if i==0:
        g=GCD(A[1:])
    elif i==N-1:
        g=GCD(A[:N-1])
    else:
        g=GCD(A[:i]+A[i+1:])
    if g>max:
        max=g
print(max)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

  x = 0
  for i in range(N):
    tmp = gcd_list(A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

Submission status: Runtime Error

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

def hcfnaive(a,b): 
  if(b==0): 
    return a 
  else: 
    return hcfnaive(b,a%b) 
      
def gcd(numbers):
  if len(numbers)==2:
    return hcfnaive(numbers[0],numbers[1])
  elif len(numbers)==1:
    return numbers[0]
  else:
    numbers[0]=hcfnaive(numbers[0],numbers[1])
    numbers.pop(1)
  return gcd(numbers)

ans=0
for i in range(n):
  num2=copy.deepcopy(num)
  num2.pop(i)
  ab=gcd(num2)
  if ab>ans:
    ans=ab
print(ans)

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)

N = int(input())
A = (input()).split()
# N = int('2')
# A = ('1000000000 1000000000').split()
A = [int(a) for a in A]
max=0

for i in range(N):
    B = A.copy()
    del B[i]
    for j in range(N-1):
        if max < gcd(*B):
            max = gcd(*B)
            
print(max)

Submission status: Runtime Error

Solution code:
import sys
# gcd
# from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
# from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
import math
from functools import reduce


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


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


#
# input関係の定義
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


N = ii()
A = lmi()
old_gcd = gcd_list(A)
for i in range(N):
    A[i] = A[i] // old_gcd
i = N // 2
prev = 0
while i != prev:
    if gcd_list(A[i:N]) == 1:
        prev = i
        i = (i + N - 1) // 2
        continue
    else:
        i = (prev + i) // 2
        continue
A.pop(i)
print(old_gcd * gcd_list(A))
# print(A)
#
# num = N
# ans = []
# while num != 2:
#     ans = []
#     if num % 2 == 0:
#         num = -(-num // 2)
#         for i in range(num):
#             ans.append(math.gcd(A[2 * i], A[2 * i + 1]))
#     else:
#         num = -(-num // 2)
#         for i in range(num - 1):
#             ans.append(math.gcd(A[2 * i], A[2 * i + 1]))
#             ans.append(A[-1])
#     if ans.count(1) >= 2:
#         print(old_gcd)
#         break
# else:
#     print(max(ans))

Submission status: Runtime Error

Solution code:
import fractions

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

gcd =[]
for i in range(number-1):
    gcd.append(fractions.gcd(A[i], A[i+1]))

gcd.append(math.gcd(A[0], A[number-1]))

gcd.sort()

if gcd[0] == gcd[number-1]:
    print(gcd[0])

elif gcd[number-2] == gcd[number-1] and gcd[number-3] != gcd[number-2]:
    print(gcd[number-3])

else:
    print(gcd[number-1])

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

b = None
#かきかえる段階
if max(v) % 2 ==0:
	#print("最大の値は偶数")
	b = True
else:
	#print("最大の値は奇数")
	b = False
		
v.sort()

for index,_ in enumerate(v):
	if _  % 2 ==0:
		if not b:
			v[index] = v[-1]
			#print("書き換え:" + str(_) + " 書き換え後:"  + str(v[-1]))
			break
			
	else:
		if b:
			v[index] = v[-1]
			#print("書き換え:" + str(_) + " 書き換え後:"  + str(v[-1]))			
			break
			
#print(v)

#固定
print(reduce(math.gcd,v))

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(" ")))
gcd_max = 0

for n in range(len(A)):
    temp_A = list(A)
    del temp_A[n]
    gcd_num = gcd_list(temp_A)
    if gcd_num > gcd_max:
        gcd_max = gcd_num

print(gcd_max)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
a = [int(i) for i in input().split()]
m = []
if len(a) == 2:
    print(max(a))

else:
    for i in range(n-1):
        m.append(math.gcd(a[i],a[i+1]))
    m.append(math.gcd(a[0],a[i+1]))
    m = list(set(m))
    m.sort()
    print(m[1])

Submission status: Runtime Error

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

min=10000000000000000000
for i in range(N):
    if A[i] < min:
        min=A[i]
min2=10000000000000000000
for i in range(N):
     if A[i] < min2 and A[i] != min:
         min2=A[i]

cnt=0
for i in range(N):
    if A[i] % min2 == 0:
        cnt+=1
if cnt == len(A) or cnt == len(A)-1:
    print(a)
    quit()

a = min
for i in range(min):
    cnt=0
    for i in range(N):
        if A[i] % a == 0:
            cnt+=1
        if cnt == len(A) or cnt == len(A)-1:
            print(a)
            quit()
    a -= 1

Submission status: Runtime Error

Solution code:
import functools
import math

n=int(input())
v=list(map(int,input().split()))
p = 0
tmp = 3
g = len([i for i in v if i % 2 == 0])

if g > 1:
    for i in v:
        if i % 2 == 0:
            tmp = i
    for i in v:
        if i % 2 != 0:
            v[v.index(i)] = tmp
else:
    for i in v:
        if i % 2 != 0:
            tmp = i
    for i in v:
        if i % 2 == 0:
            v[v.index(i)] = tmp

gcd = functools.reduce(math.gcd, v)

print(gcd)

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())
a = list(map(int,input().split()))
li = []
li2 = []
for i in range(n-1):
    li.append(gcd(a[i],a[i+1]))
for i in range(n-2):
    li2.append(gcd(a[i],a[i+2]))
if n <= 3:
    li = set(li) | set(li2)
    ans = max(li)
    print(ans)
else:
    li = set(li) & set(li2)
    ans = max(li)
    print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

n = int(input())
a_l =[int(i) for i in input().split()]
a_l.append(a_l[n-2])
max_gcd=reduce(math.gcd, a_l)
for i in range(n):
    tmp = a_l[i]
    a_l[i] = a_l[i+1]
    max_gcd=max(max_gcd,reduce(math.gcd, a_l))
    a_l[i] = tmp
print(max_gcd)

Submission status: Runtime Error

Solution code:
import math

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

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

G = math.gcd(A[0], A[1])
H = math.gcd(A[1], A[2])
I = math.gcd(A[2], A[0])

print(max(G, H, I))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include<vector>
#include <algorithm>
#include <cstdlib>
long gcd(long a, long b) {
    if (a == 0 || a == -1) 
        return b;
    if (b == -1) {
        return a;
    }
    return gcd(b % a, a); 
}

long gcd_bw(long left, long right, std::vector<long> h,  long skip) {
    if (right - left == 1) {
        if (left == skip) {
            return -1;
        }
        return h[left];
    }
    long middle = (right - left ) / 2 + left;
    return gcd(gcd_bw(left, middle, h, skip), gcd_bw(middle, right, h,  skip)); 
}

int main() {
    long n, k;
    std::cin >> n;
    std::vector<long> h(n);
    for(long i=0;i<n;i++) {
        std::cin >> h[i];
    }
    long max_gcd = 0;
    for(long i=0;i<n;i++) {
        long current = gcd_bw(0, n, h, i);
        if (current > max_gcd) {
            max_gcd = current;
        }
    }

    std::cout << max_gcd << std::endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

n=int(input())
array = list(map(int, input().split())) 
array.sort(reverse=True)


max=-100

for i in range(n):
    copy_array=array.copy()
    if(max>array[i]):
        break
    copy_array.pop(i)
    tmp_gcd=gcd(*copy_array)
    # print(copy_array)
    if(max<tmp_gcd):
        max=tmp_gcd
    
print(max)

Submission status: Runtime Error

Solution code:
# vim: set fileencoding=utf-8:
import fractions
from functools import reduce


def main():
    N = int(input())
    A = [int(ele) for ele in input().split()]
    gcds = [-1] * N

    # ans = tle_solver(A)
    ans = ac_solver(A)
    print(ans)


def ac_solver(A):
    a_min = min(A)
    a_min_divisors = sorted(make_divisors(a_min), reverse=True)
    kouho = []
    for d in a_min_divisors:
        ct = 0
        for a in A:
            if a % d != 0:
                ct += 1

            if 2 < ct:
                break

        if ct == 1:
            kouho.append(d)

    return max(kouho)


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


def tle_solver(A):
    gcds = [-1] * len(A)
    for idx in range(len(A)):
        tmp = A[:idx] + A[idx+1:]
        gcds[idx] = gcd(*tmp)

    return max(gcds)


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


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from functools import reduce

def gcd(m, n):
    # 入力を m, n (m ≧ n) とする。
    tmp = max(m,n)
    n = min(m,n)
    m = tmp
    # n = 0 なら、 m を出力してアルゴリズムを終了する。
    if n == 0:
        return m
    # m を n で割った余りを新たに n とし、更に 元のnを新たにm とし 2. に戻る。
    return gcd(n, m % n)

def gcda(arr, left, right):
    return reduce((lambda a,b:gcd(a,b)), arr[left:right+1], gcd(arr[left], arr[right]))

n = int(input())
nums = [ int(s) for s in input().split(" ")]

if n == 2:
    print(max(nums))
    exit()

allgcd = gcda(nums, 0, n-1)

found = 0
delete_kouho = -1
for i in range(1,n):
    if gcd(nums[0], nums[i]) == allgcd:
        found += 1
        delete_kouho = i

if found >= 2:
    print(gcda(nums, 1, n-1))
    exit()
else:
    print(gcd(gcda(nums, 0, delete_kouho-1), gcda(nums, delete_kouho+1, n-1)))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def gcdList(num):
    return reduce(math.gcd, num)

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

for i in range(n):
    ans = max(ans, gcdList(a[:i]+a[i+1:]))
print(ans)

Submission status: Runtime Error

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

def gcd_list(numbers):
    return reduce(math.gcd,numbers)
ans = 0
count = 0
for i in range(N):
    B = copy.copy(A)
    a = B.pop(i)
    gcd = gcd_list(B)
    ans = max(ans,gcd)

print(ans)

Submission status: Runtime Error

Solution code:
import fractions as math
import sys


def gcd2(array):
    gcd = math.gcd(array[0], array[1])
    for i in range(2, len(array)-1):
        gcd = math.gcd(gcd, array[i])
    return gcd


n = int(input())
a = [int(x) for x in input().split(' ')]
m = max(a)

i = sys.maxsize
l = 0
for k in range(n):
    if i > math.gcd(a[k], m):
        i = math.gcd(a[k], m)
        l = k
    elif i == math.gcd(a[k], m):
        del a[k]
        print(gcd2(a))
        sys.exit(0)
del a[l]
print(gcd2(a))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
import fraction

result = 0

n = int(input())
A = list(map(int, input().split()))
for i in range(n):
    result = max(result, reduce(fraction.gcd, A[:i]+A[i+1:]))

print(result)