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 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)
    return divisors
  
def check(num, kouho, N, i):
    ret = {}
    for val in kouho.keys():
        if num%val == 0:
            ret[val] = kouho[val] + 1
        else ret[val] == i:
            ret[val] = kouho[val]
        
    return ret
  
N = int(input())
A= list(map(int, input().split()))
pri_flag = True

if N > 4:
    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:]):
        if len(kouho) == 1:
            print(1)
            pri_flag = False
            break
        kouho = check(num, kouho, N, i+3)
    
    if pri_flag:
        print(max(kouho))
        
else:
    divs = []
    kouho = {}
    for i in range(N):
        divs.extend(cal_divs(A[i]))
        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 > N-2}
    print(max(kouho))

Submission status: Runtime Error

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

def factors(num):
    L = []
    for i in range(1, min(t, int(num**(1/2))+1)):
        if num%i == 0:
            L.append(i)
            L.append(num//i)
    return L

t = 10**5
first = a.pop(0)
second = a.pop(0)
in_all = [i for i in factors(first) if i in factors(second)]
in_almost_all = [i for i in factors(second)+factors(first) if i not in in_all]
t = max(in_all + in_almost_all)

while len(a) > 0:
    x = a.pop(0)
    x_factors = factors(x)
    in_all = [i for i in in_all if i in x_factors]
    in_almost_all = [i for i in in_almost_all if i in x_factors]
    t = max(in_all + in_almost_all)

print(t)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
if n == 2:
    print(a[1])
else:
    dif = set()
    for i in range(n - 1):
        dif.append(a[i - 1] - a[i])
    d = list(dif)
    d.sort()
    d.reverse()
    f=False
    for i in range(len(d)):
        if d[i] == 0:
            break
        ok = True
        flag = False
        for j in range(n):
            if a[j] % d[i] != 0:
                if flag:
                    break
                else:
                    ok=False
        if ok:
            f=True
            print(i)
            break
    if not f:
        print(1)

Submission status: Runtime Error

Solution code:
import fractions
import functools

n = int(input())
g = []
k = []
for x in input().split():
    y = int(x)
    if y%2 == 0:
        g.append(y)
    else:
        k.append(y)
if len(g) >= 2 and len(k) >= 2:
    print(1)
elif len(g) >= 2 and len(k) == 1:
    print(functools.reduce(fractions.gcd, g))
elif len(g) == 1 and len(k) == 0:
    print(10**9)
elif len(g) == 1 and len(k) == 1:
    print(max(g[0],k[0]))
elif len(g) == 1 and len(k) >= 1:
    gcd = functools.reduce(fractions.gcd, k)
    if gcd == 1:
        print(1)
    elif gcd >= 2:
        print(gcd)
elif len(g) >= 2 and len(k) == 0:
    cn = 0
    while len(g)>0:
        for i in range(len(g)):
            g[i] /= 2
            cn += 1
            if g[i]%2 != 0:
                k.append(g.pop(i))
    if len(g) >= 2 and len(k) >= 2:
        print(1)
    elif len(g) >= 2 and len(k) == 1:
        print(functools.reduce(fractions.gcd, g))
    elif len(g) == 1 and len(k) == 0:
        print(10**9)
    elif len(g) == 1 and len(k) == 1:
        print(max(g[0],k[0]))
    elif len(g) == 1 and len(k) >= 1:
        gcd = functools.reduce(fractions.gcd, k)
        if gcd == 1:
            print(1)
        elif gcd >= 2:
            print(gcd)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
t=0
ans=[0]*N
while t<N:
    u=0
    if t!=0:
        ans[t]=A[0]
        while u<N-1:
            if (u+1)!=t:
                ans[t]=math.gcd(ans[t],A[u+1])
            u=u+1
    else:
        ans[t]=A[1]
        y=1
        while y<N-1:
            ans[t]=math.gcd(ans[t],A[y+1])
            y=y+1
    t=t+1
print(max(ans))

Submission status: Runtime Error

Solution code:
import fractions

N=int(input())
a_list = [int(i) for i in input().split()]
a_list.sort()
b_list = a_list[:]
del b_list[-1]
ans1 = b_list[0]
for i in range(N-1):
	ans1 = fractions.gcd(ans1, b_list[i])

max_number = a_list[-1]
min_number = a_list[-1]
min_index = len(N)
for i in range(N-1):
	gcd = fractions.gcd(max_number,a_list[i])
	if gcd < min_number:
		min_index = i
		min_number = gcd

del a_list[min_index]
ans2 = a_list[0]
for j in range(N-1):
	ans2 = fractions.gcd(ans2, a_list[j])
print(max(ans1,ans2))

Submission status: Runtime Error

Solution code:
from functools import reduce

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


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


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


tmp = 0
gcd_max = 0
for n in range(N):
    tmp = A[n]
    A.remove(A[n])
    gcd = gcd_list(A)
    if gcd >= gcd_max:
        gcd_max = gcd

    A.insert(n, tmp)

print(gcd_max)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
N = int(input())
A = input().split()
A = [int(a) for a in A]
B = []
for n in range(N):
  for i in range(1, (10^9)+1):
    A[n] = i
    b = reduce(math.gcd, A)
    B.append(b)
B_max = max(b)
print(B_max)

Submission status: Runtime Error

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


N = int(input())
A = list(map(int, input().split()))
# gcd_list = []
# hoge = gcd(A[0], A[1])
# for i in range(2, N):
#     gcd_list.append(gcd(hoge, A[i]))
# gcd_list.sort()
# print(gcd_list[len(gcd_list)-2])
A.sort()
max_A = max(A)
gcd_list = []
for i in range(int(N/2):
    tmp = A[i]
    A[i] = max_A
    hoge = gcd(A[0], A[1])
    for j in range(2, int(N/2):
        hoge = gcd(A[j], hoge)
    gcd_list.append(hoge)
    A[i] = tmp

print(max(gcd_list))
# 最大の値と変えてもいい

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

ans=0


for i in range(n):
    del a[i]
    k = gcd_list(a)

    if k > ans:
        ans = k

print(ans)

Submission status: Runtime Error

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


N = int(input())
A = input().split()
for i in range(N):
    A[i] = int(A[i])
    
if N == 2:
    print(max(A[0],A[1]))
if N >= 3:
    gcd_all = gcd(A[0], A[1])
    gcd_without_1 = A[0]
    gcd_without_2 = A[1]
    gcd_without_1 = max(gcd_all, max( gcd(gcd_without_1, A[i]),gcd(gcd_without_2, A[i])))
    gcd_all = math.gcd(gcd_all,A[2])
    for i in range(3,N):
        gcd_without_1 = max(gcd_all, gcd(gcd_without_1, A[i]))
        gcd_all = gcd(gcd_all,A[i])
    print(max(gcd_all, gcd_without_1))

Submission status: Runtime Error

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


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

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

n = int(input())
lis = list(map(int,input().split()))
#lis = list(set(lis))
reslis = []
reslis.append(gcd_lis2(lis))
if len(lis) == 1:
    print(lis[0])
elif len(lis) == 2:
    print(max(lis))
elif len(lis) != 2:
    for i in range(n):
        tmp = copy.deepcopy(lis)
        del tmp[i]
        reslis.append(gcd_lis2(tmp))
    print(max(reslis))

Submission status: Runtime Error

Solution code:
import math

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

not_replaced_max = A[0]

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

# def divisor(n):
#     limit = int(math.sqrt(n))
#     for i in range(2, limit):
#         if n % i == 0:
#             result.append(i)
#     return result

# def merge(A, B):

# divisor_list = divisor(A[1])
# for i in range(1, N):
#     not_replaced_max = gcd(not_replaced_max, A[i])
# print(replaced_max)
#
A.sort()

def func(n):
    limit = int(math.sqrt(n))
    divisor = []
    for i in range(1, limit):
        if n % i == 0:
            divisor.append(i)
    return divisor

def merge(A, B):
    divisor_list = [a for a in A]
    for b in B:
        if b not in A:
            divisor_list.append(b)
    return divisor_list

divisor_list = merge(func(A[0], A[1]))
divisor_list.sort(reverse=True)
is_end = False
for d in divisor_list:
    count = 0
    for a in A:
        if a % d != 0:
            count += 1
            if count > 1:
                break
    if count == 0 or is_end:
        print(d)
    else:
        is_end = True

Submission status: Runtime Error

Solution code:
import fractions
import functools
import sys
sys.setrecursionlimit(1000000)

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

if len(a) == 2:
  print(max(a))
  sys.exit()
  
gcd_a = functools.reduce(fractions.gcd, a)
a=[i//gcd_a for i in a]

cnt = 0
gcd_0 = functools.reduce(fractions.gcd, a[1:])
for i in range(1,len(a)):
  if fractions.gcd(a[0],a[i]) == 1:
    so = i
    cnt+=1
    if cnt == 2:
      print(max(gcd_a,gcd_0*gcd_a))
      sys.exit()

a_i = [i for i in a if i != a[so]]
gcd_i = functools.reduce(fractions.gcd, a_i)

print(max(gcd_0,gcd_i)*gcd_a)

Submission status: Runtime Error

Solution code:
n = int(input(), 10)
a = list(map(int, input().split()))
greatCDlist = []

for i in range(n):
  pop = a.pop(i)
  greatCD = 1
  for j in range(n):
    if j == 0:
      greatCD = gcd(a[0], a[1])
    else:
      greatCD = gcd(greatCD, a[j])
  a.insert(i, pop)
  greatCDlist.append(greatCD)

maxg = 1
for i in range(n):
  if maxg > greatCDlist[i]:
    maxg = greatCDlist[i]

print(maxg)


def gcd(m, n):
  if m < n:
    m ^= n
    n ^= m
    m ^= n
  if n == 0:
    return m
  else:
    return gcd(n, n % m)

Submission status: Runtime Error

Solution code:
import fractions
import functools

n = int(input())
g = []
k = []
for x in input().split():
    y = int(x)
    if y%2 == 0:
        g.append(y)
    else:
        k.append(y)
if len(g) >= 2 and len(k) >= 2:
    print(1)
elif len(g) >= 2 and len(k) == 1:
    print(functools.reduce(fractions.gcd, g))
elif len(g) == 1 and len(k) == 0:
    print(10**9)
elif len(g) == 1 and len(k) == 1:
    print(max(g[0],k[0]))
elif len(g) == 1 and len(k) >= 1:
    gcd = functools.reduce(fractions.gcd, k)
    if gcd == 1:
        print(1)
    elif gcd >= 2:
        print(gcd)
elif len(g) >= 2 and len(k) == 0:
    cn = 0
    while len(g)>0:
        for i in range len(g):
            g[i] /= 2
            cn += 1
            if g[i]%2 != 0:
                k.append(g.pop(i))
    if len(g) >= 2 and len(k) >= 2:
        print(1)
    elif len(g) >= 2 and len(k) == 1:
        print(functools.reduce(fractions.gcd, g))
    elif len(g) == 1 and len(k) == 0:
        print(10**9)
    elif len(g) == 1 and len(k) == 1:
        print(max(g[0],k[0]))
    elif len(g) == 1 and len(k) >= 1:
        gcd = functools.reduce(fractions.gcd, k)
        if gcd == 1:
            print(1)
        elif gcd >= 2:
            print(gcd)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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


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


max_gcd = 0

for i in range(N):
    popped = A.pop(i)
    tmp_max_gcd = gcd_list(A)
    if tmp_max_gcd > max_gcd:
        max_gcd = tmp_max_gcd
    A.append(popped)

print(max_gcd)

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 = []
        temp_l += l[0 : i]
        temp_l += l[i+1 : len_l]
        gcd_temp_l = get_gcd_l(temp_l)
        answers.append(gcd_temp_l)
    ans = max(answers)
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
numbers = tuple(map(int,input().split()))

import math
from functools import reduce

ans = []

if n == 2:
    a = list(numbers)
    ans.appned(max(numbers))

else:
    for i in range(n):
        a = list(numbers)
        a.append(max(a))
        del a[i]
        ans.append(reduce(math.gcd, a))

print(max(ans))

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(lambda x, y: math.gcd(x, y), removed))
print(max(gcd_list))

Submission status: Runtime Error

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

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

if n==2:
	print(max(A))
	quit()

a = list(set(A))

gcds = []
for i in range(len(a)):
	temp = copy(a)
	temp.pop(i)

	num1 = temp[0]
	num2 = temp[1]
	result = gcd(num1, num2)
	for j in range(2, len(temp)):
		if result in gcds:
			break
		result = gcd(result, temp[j])
	
	if result not in gcds:
		gcds.append(result)

gcds = sorted(gcds)

if len(gcds)==1:
	print(gcds[0])
else:
	gcds = sorted(gcds)
	if len(a)==len(A):
		print(gcds[1])
	else:
		print(gcds[0])

Submission status: Runtime Error

Solution code:
num = int ( input())
n = list (map (int , input().split()))
m = min(n)
ans = 1
while True:
    for d in range (1,m+1) :
        i = m-d
        con = 0
        for x in n :
            if x % i != 0 :
                con += 1
        if con <= 1 :
             ans = i
             break
    break
print (ans)

Submission status: Runtime Error

Solution code:
from math import gcd
from functools import reduce

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

n = int(input())
a = [int(_) for _ in input().split()]
a.sort()
ir = [a[0]]
for i in range(1,n):
    ir.append(gcd(a[0], a[i]))
mi = ir.index(min(ir))
mr = []
for i in range(n):
    mr.append(gcd(a[mi], a[i]))
if ir.count(min(ir)) > mr.count(min(mr)):
    print(gcd_list(a[1::]))
elif ir.count(min(ir)) == mr.count(min(mr)):
    print(a[0])
elif ir.count(min(ir)) < mr.count(min(mr)):
    a.pop(mi)
    print(gcd_list(a))

Submission status: Runtime Error

Solution code:
import sys

n = int(input())
a = list(map(int, input().split()))
divisors = []
k = 0
count = 0
answer = 1
removes = 0

for i in range(2,max(a)):

    for j in range(n):
        if a[j] % i != 0:
            count += 1
            memory = j
            if count >= 2:
                break
    if count == 1:
        a[memory]=0
    if count <=1:
        divisors.append(i)

for i in range(n):
    a[i] = a[i] /divisors[-1]

for i in divisors:
    answer=answer*i

print(answer)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
ans = 0
A = sorted(A)
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
ans = 0
ans_ = 0
if N == 2:
  print(max(A))
elif N == 3:
  tmp = [A[0],A[1],A[N-1]]
  for i in range(len(tmp)):
    tmp_ = tmp[:]
    tmp_.pop(i)
    ans_ = gcd_list(tmp_)
    if ans < ans_:
      ans = ans_
  print(ans)
else:
  tmp = [A[0],A[1],A[N-2],A[N-1]]
  if len(list(set(A))) == 2:
    print(max(A))
  else:
    count = 0
    ans = gcd_list(tmp)
    for j in range(N):
      if A[j]%ans != 0:
        count+=1
    if count = 1:
      print(ans)
    else:
      for i in range(len(tmp)):
        tmp_ = tmp[:]
        tmp_.pop(i)
        ans_ = gcd_list(tmp_)
        if ans < ans_:
          ans = ans_
      print(ans)

Submission status: Runtime Error

Solution code:
from fractions import gcd as g

r=reduce


n=int(raw_input())
a=map(int,raw_input().split())
if len(set(a))<n:
	print r(g,a)
	exit()

e,o=[],[]
n1,n2=0,0
for i in a:
	if i&1:
		o.append(i)
		n2+=1
	else:
		e.append(i)
		n1+=1

if n1>1 and n2>1:
	print 1
elif n1==1 and n2==1:
	print max(e+o)
else:
	if n1==1:
		print r(g,o)
		exit()
	if n2==1:
		print r(g,e)
		exit()

	a.sort()
	d=[]
	s=set()
	for i in xrange(1,n):
		e=g(a[0],a[i])
		s.add(e)
		d.append(e)

	if len(s)>2:
		print -1
	else:
		n1,n2=list(s)
		n1,n2=d.count(n1),n2(n2)
		if n1>0 and n2>0:
			print r(d,a)
		elif n1==1 or n2==1:
			print max(list(s))
		else:
			print 1

Submission status: Runtime Error

Solution code:
import math
import random

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

tmpgcd = list()
for i in range(2):
    sample = random.choices(a, k=2)
    tmpgcd.append(math.gcd(sample[0], sample[1]))

# print('hey')

tmpgcd = sorted(tmpgcd)
mini = tmpgcd[1]
# print(mini, tmpgcd)
result = 0
for i in range(mini, -1, -1):
    tmp = [x % i for x in a]
    tmp = [x for x in tmp if x != 0]
    if len(tmp) <= 1:
        result = i
        break

print(result)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

a = list(map(int, input().split()))
box = [0]*10**9

def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            divisors.append(i)
            if i != k // i:
                divisors.append(k//i)
    return divisors
for i in a:
    for j in make_divisors(i):
        box[10**9-j]+=1 
if n-1 in box:
    print(10**9 - box.index(n-1))
else:
    print(10**9 - box.index(n))

Submission status: Runtime Error

Solution code:
def GetBCG(A, ignore):
    Amin = min(A)
    B = []
    for i in range(A):
        if i == ignore:
            continue

        Bi = A[i]
        count = 0
        if Bi % Amin == 0:
            count += 1
        else:
            B.append(Bi % Amin)

    if count >= 2:
        return Amin
    else:
        return GetBCG(B, -1)


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

AnsList = [GetBCG(InputList, i) for i in range(N)]

print(max(AnsList))

Submission status: Runtime Error

Solution code:
import functools
import math

def map_all(es):
    return all([e == es[0] for e in es[1:]]) if es else False

n = int(input())
a = list(map(int, input().split(" ")))
gcf = functools.reduce(math.gcd, a)

ans = []
a_tmp = a

tmp = 0
if map_all(a):
    print(gcf)
else:
    for i in range(n):
        a_tmp = a
        tmp = a[i]
        for j in range(1, max(a)+1):
            a_tmp[i] = j
            gcd = functools.reduce(math.gcd, a_tmp)
            #print(gcd)
            ans.append(gcd)
            #print(a_tmp)
            a_tmp[i] = tmp
    
    print(max(ans))

Submission status: Runtime Error

Solution code:
import sys
def gcd(num_list,a,N):
  return max([ i for i in range(min(num_list),a,-1) if len([0 for o in num_list if o%i == 0]) == N or len([0 for o in num_list if o%i == 0]) == N-1])

def gcd2(num_list,a,N):
  return max([ i for i in range(min(num_list),a,-1) if len([0 for o in num_list if o%i == 0]) == N-1])


N = int(sys.stdin.readline())
A = list(map(int,sys.stdin.readline().split()))
ans1 = gcd(A,0,N)
temp = min(A)
A.remove(min(A))
ans2 = gcd2(A,temp,N)
print(max(ans1,ans2))

Submission status: Runtime Error

Solution code:
import fractions
import functools
N=int(input())
A=[int(i) for i in input().split()] 
#max_gcd=functools.reduce(fractions.gcd, A)
for i in range(N):
	tmp=A.copy()
	tmp.pop(i)
	gcd=functools.reduce(fractions.gcd, tmp)
	if max_gcd<gcd:
		max_gcd=gcd
print(max_gcd)

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_map = {}
def GCD(numbers , startIndex , endIndex):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        set_index = (startIndex , endIndex)
        if set_index in gcd_map : 
            return gcd_map[set_index]
        value = GCD(numbers[1:] , startIndex + 1 , endIndex)
        result = gcd(value , numbers[0])
        gcd_map[set_index] = 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(prev_list , 1 , N - 1)
        elif i == N - 1 : 
            after_list = a_list[:N-1]
            rst = GCD(after_list , 0 , N - 2)
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
            rst = gcd(GCD(prev_list , 0 , i - 1) , GCD(after_list , i + 1 , N - 1))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()

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)
    return divisors
  
def check(num, kouho, N, i):
    ret = {}
    for val in kouho.keys():
        if num%val == 0:
            ret[val] = kouho[val] + 1
        else ret[val] == i:
            ret[val] = kouho[val]
        
    return ret
  
  
N = int(input())
A= list(map(int, input().split()))
pri_flag = True

if N > 4:
  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:]):
    if len(kouho) == 1:
      print(1)
      pri_flag = False
      break
    kouho = check(num, kouho, N, i+3)

  if pri_flag:
      print(max(kouho))
        
else:
    divs = []
    kouho = {}
    for i in range(N):
        divs.extend(cal_divs(A[i]))
        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 > N-2}
    print(max(kouho))

Submission status: Runtime Error

Solution code:
N = map(int, input().split())
A = map(int, input().split())
print(A[0])

Submission status: Runtime Error

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

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

def gcd_all(a_list):
    gcd_num = gcd(a_list[0],a_list[1])
    for i in range(2, len(a_list)):
        gcd_num = gcd(gcd_num, a[i])
    return gcd_num

max_gcd = 0
for i in range(N):
    a_cp = cp.copy(a)
    del a_cp[i]
    max_gcd = max(max_gcd, gcd_all(a_cp))
print(max_gcd)

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):
            gcd_temp = gcd(A_temp[i],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:
import math
import copy

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

if N==2:
    print(max(A))
    exit()
else:
    max=0
    for i in range(N):
        x = copy.deepcopy(A)
        del x[i]
        c = math.gcd(x[0], x[1])
        for i in range(2,N-1):
            c = math.gcd(c,x[i])
        if c >= max:
            max = c
        x.clear()
    print(max)

Submission status: Runtime Error

Solution code:

from collections import defaultdict


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

    return divisors


n = int(input())
a = list(map(int, input().split()))
divs = defaultdict(int)
divs_ = defaultdict(int)
la = len(a)
maxdiv = 1
for y in make_divisors(a[0]):
    divs[y] += 1
for y in make_divisors(a[1]):
    divs[y] += 1
    maxdiv = max(maxdiv, y)
if la == 2:
    print(maxdiv)
    exit()
for y in make_divisors(a[2]):
    divs[y] += 1
divs_ = {k: v for k, v in divs.items() if v >= 2}
c = 4
for x in a[3:]:
    d = make_divisors(x)
    for y in d:
        if divs_[y] >= c - 2:
            divs_[y] += 1
    c += 1
for k, v in divs_.items():
    if v >= la - 1:
        maxdiv = max(maxdiv, k)
print(maxdiv)

Submission status: Runtime Error

Solution code:
def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a % b)
N = int(input())
A = list(map(int, input().split()))
s = set()
m = []
for i in range(1,N):
  for j in range(i+1,N):
    m.append(gcd(A[i-1], A[j]))
print(max(m))

Submission status: Runtime Error

Solution code:
import fractions
import itertools

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

al = list(set(list(itertools.combinations(l, n-1))))

c = []

for j in range(len(al)):
    a = list(al[j])
    ans = a[0]
    c.append((list(map(lambda x: fractions.gcd(ans,x), a[1:]))))

print(max(c)[0])

Submission status: Runtime Error

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

if len(A) == 1:
    print(A[0])
    exit()

ls = []
for i in range(len(A)):
    for j in range(len(A)):
        tmp = fractions.gcd(A[i], A[j])
        if tmp not in A:
            ls.append(tmp)

print(max(ls))

Submission status: Runtime Error

Solution code:
def c_gcd_on_blackboard(N, A):
    from functools import reduce
    from fractions import gcd
    odd, even = set(), set()
    for a in A:
        if a % 2 == 0:
            even.add(a)
        else:
            odd.add(a)
    if len(odd) >= 3:
        return 1
    if len(odd) == 0:
        even = sorted(even)
        even[0] = even[-1]
        return reduce(gcd, even)
    ans = 1
    for o in odd:
        ans = max(ans, reduce(gcd, even))
    return ans

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

Submission status: Runtime Error

Solution code:
import math
n = int(input())
a_list = list(map(int, input().split()))
a_list.sort()
result = 0
for i in range(n):
    r = a_list[0]
    for j in range(n):
        if i == j: continue
        r = math.gcd(a_list[j], r)
    if r > result: result  = r
print(result)

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)
    return divisors
  
def check(num, kouho, N, i):
    ret = {}
    for val in kouho.keys():
        if num%val == 0:
            ret[val] = kouho[val] + 1
        else ret[val] == i:
            ret[val] = kouho[val]
        
  return ret
  
  
N = int(input())
A= list(map(int, input().split()))
pri_flag = True

if N > 4:
  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:]):
    if len(kouho) == 1:
      print(1)
      pri_flag = False
      break
    kouho = check(num, kouho, N, i+3)

  if pri_flag:
      print(max(kouho))
        
else:
    divs = []
    kouho = {}
    for i in range(N):
        divs.extend(cal_divs(A[i]))
        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 > N-2}
    print(max(kouho))

Submission status: Runtime Error

Solution code:
import numpy as np
huga = list(map(int, input().split()))
unchi=list(map(int, input().split()))
n=len(unchi)

import sys
if n==2:
  if unchi[0]>=unchi[1]:
    print(unchi[0])
    sys.exit()
  else:
    print(unchi[1])
    sys.exit()    

def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
kouyaku=1
geri =[[0 for i in range(n)] for j in range(n)]
for i in range(n):
  for j in range(n):
    geri[i][j]=unchi[j]


    
for i in range(n):
  sore=geri[i]
  sore.pop(i)
  if len(sore)==2:
    u=gcd(sore[0],sore[1])
  else:
    u=sore[0]
    k=(sore)
    for j in range(1,k):
      u=gcd(u,sore[j])

  if u>=kouyaku:
  	kouyaku=u
print(kouyaku)

Submission status: Runtime Error

Solution code:
n = int(input(), 10)
a = list(map(int, input().split()))
greatCDlist = []

for i in range(n):
  pop = a.pop(i)
  greatCD = 1
  for j in range(n):
    if j == 0:
		greatCD = gcd(a[0], a[1])
    else:
      greatCD = gcd(greatCD, a[j])
  a.insert(i, pop)
  greatCDlist.append(greatCD)

maxg = 1
for i in range(n):
  if maxg > greatCDlist[i]:
    maxg = greatCDlist[i]

print(maxg)


def gcd(m, n):
  if m < n:
    m ^= n
    n ^= m
    m ^= n
  if n == 0:
    return m
  else:
    return gcd(n, n % m)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def mygcd(*nums):
    return reduce(math.gcd, nums)

max = 0

N = int(input())

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

for i in range(N):
    A_ = A[:]
    A_.pop(i)
    A_without_ith = A_
    gcd_ = mygcd(*A_without_ith)
    max = gcd_ if gcd_ > max else max

print(max)