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: Time Limit Exceeded

Solution code:
from fractions import gcd
from itertools import combinations
from functools import reduce
import random

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

origina_gcd = reduce(gcd, a_list)

n_1_combis = combinations(a_list, N - 1)

max_gcd = 1
for combi in n_1_combis:
    tmp_gcd = reduce(gcd, combi)
    max_gcd = max(max_gcd, tmp_gcd)
print(max_gcd)

Submission status: Time Limit Exceeded

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
for d in range(B[0], 0, -1):
    count = 0
    for b in B:
        if b % d != 0:
            count += 1
            if count >= 2:
                break
    if count < 2:
        ans = d
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools

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

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

ansl = []

for j in range(len(al)):
    a = list(al[j])
    ans = a[0]
    for i in range(1, n-1):
        ans = fractions.gcd(ans, a[i])
    ansl.append(ans)
    
print(max(ansl))

Submission status: Time Limit Exceeded

Solution code:
import functools
n = int(input())
a_list = list(map(int,input().split()))

def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if y in res:
        return y
    if x%y==0:
        return y
    else:
        while x%y!=0:
            x,y = y,x%y
        return y

res = []
tmp = []
for _ in range(n):
    e = a_list.pop(0)
    if e in tmp:
        continue
    res.append(functools.reduce(gcd, a_list))
    a_list.append(e)
    tmp.append(e)

print(max(res))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)

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


g_max = 1
for i in range(n):
    d = a_l.pop(0)

    if n-1==1:
        g = a_l[0]
    else:
        g = gcd(a_l[0], a_l[1])
        if n-1!=2:
            for j in range(2,n-1):
                g=gcd(g,a_l[j])
    # print(g)
    g_max = max(g, g_max)
    # print(d)
    a_l.append(d)

print(g_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions
N = int(input())
As = list(map(int, input().split()))
As.sort()
ans = 1
 
for i in range(N):
    flag = True
    if i == 0:
        a = As[1]
    else:
        a = As[0]
    for j in range(N):
        if j != i:
            a = fractions.gcd(a, As[j])
            if a <= ans:
                flag = False
                break
    if flag:
        ans = a
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import Fraction
import fractions
import copy
from functools import reduce

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

N = int(input())
A = list(map(int, input().split()))
ans = []
for i in range(N):
   B = copy.copy(A)
   del B[i]
   ans.append(gcd(B))
print(max(ans))

Submission status: Time Limit Exceeded

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

def factorize(x):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    d = 2
    while x > 1:
        while x % d == 0:
            if d not in f:
                f[d] = 0
            f[d] += 1
            x //= d
        d += 1
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    # l = [min(ls[:i]) for i in range(len(ls))]
    l = [float('inf')] * len(ls)
    for i in range(1, len(ls)):
        l[i] = min(l[i - 1], ls[i - 1])
    # r = [min(ls[i + 1:]) for i in range(len(ls))]
    r = [float('inf')] * len(ls)
    for i in range(len(ls) - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p

# 各A_iの素因数
F = [factorize(a) for a in A]

# 素因数の集合 (2要素に出現しない素因数は無視)
D =  set(F[0].keys())
D.update(F[1].keys())

# Dの要素 d → dが各A_iに含まれる個数のリスト
X = {d: [f[d] if d in f else 0 for f in F] for d in D}

# Dの要素 d → A_iを削除したときのdの個数の最小値のリスト
X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D if X[d][i] > 0) for i in range(N)]
print(max(S))

Submission status: Time Limit Exceeded

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

from functools import reduce
import fractions

def gcd(num_list):
    #return reduce(math.gcd, num_list)
    return reduce(fractions.gcd, num_list)

max_kys = 0

for i in range(N):
    B = A.copy()
    B.pop(i)
    max_kys = max(max_kys, gcd(B))

print(max_kys)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3

import collections
import math


def solve(n, xs):
    divisors = collections.Counter()
    for x in xs:
        for y in range(1, math.floor(math.sqrt(x)) + 1):
            d, m = divmod(x, y)
            if m == 0:
                divisors[y] += 1
                if y != d:
                    divisors[d] += 1
    for i in range(1, max(xs) + 1)[::-1]:
        if divisors[i] >= n - 1:
            return i


def main():
    n = int(input())
    xs = [int(x) for x in input().split()]
    print(solve(n, xs))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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)
c = 1
for x in a:
    d = make_divisors(x)
    for y in d:
        if divs[y] >= c - 2:
            divs[y] += 1
    c += 1
la = len(a)
maxdiv = 1
for k, v in divs.items():
    if v >= la - 1:
        maxdiv = max(maxdiv, k)
print(maxdiv)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
def gcd(x,y):
  while y != 0:
    k = x
    x = y
    y = k%y
  return  x
ans = 1
for i in range(N):
  flag = True
  if i == 0:
    p = A[1]
    for j in range(2,N):
      p = gcd(A[j],p)
      if ans >= p:
        flag = False
        break
    if flag:
      ans = p
  else:
    p = A[0]
    if p != 1:
      for j in range(1,N):
        if j != i:
          p = gcd(A[j],p)
        if ans >= p:
          flag = False
          break
      if flag:
        ans = p
    else:
      p = A[1]
      for j in range(2,N):
        if j != i:
          p = gcd(A[i],p)
        if ans >= p:
          flag = False
          break
      if flag:
        ans = p
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

def gcd_base(x, y):
    if x >= y:
        while y != 0:
            x, y = y, x % y
        return x
    else:
        while x != 0:
            y, x = x, y % x
        return y

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

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

for i in range(0, N):
    a = A.pop(0)
    GCD.append(gcd(A))
    A.append(a)

GCD.sort()

print(GCD[N-1])

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

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

ans = gcd_list(ls)

ls = [i // ans for i in ls]

tempx = 1


for i in range(N):
	temp = ls[:]
	temp.pop(i)
	a = gcd(temp[0], temp[1])
	if a == 1:
		continue
	for j in range(2, N - 1):
		a = gcd(a, temp[j])
		if a == 1:
			break
	if a > tempx:
		tempx = a
print(ans * tempx)

Submission status: Time Limit Exceeded

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
	for j in range(N):
		if(i == j):
			continue
		else:
			if(t == 0):
				t = A[j]
			else:
				t = gcd(t,A[j])
	B.append(t)
print(max(B))

Submission status: Time Limit Exceeded

Solution code:
n = input()
A = input().split(" ")

n = int(n)
A_tmp = []
for a in A:
    A_tmp.append(int(a))
A = A_tmp

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

if n == 2:
    if A[0] > A[1]:
        print(A[0])
    else:
        print(A[1])
        
else:
    biggest = 0
    for i in range(n):
        index = []
        for j in range(n):
            if i != j:
                index.append(j)

        tmp = euclid(A[index[0]], A[index[1]])
        for j in range(n-1):
            tmp = euclid(tmp ,A[index[j]])

        if tmp > biggest:
            biggest = tmp
    print(biggest)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
divsors=[]
for i in range(n):
  for j in range(1,int(A[i]**(1/2)+1)):
    if A[i]%j==0:
      divsors.append(j)
      if j!=A[i]//j:
        divsors.append(A[i]//j)
set_divsors=sorted(list(set(divsors)))[::-1]
for i in range(len(set_divsors)):
  a=divsors.count(set_divsors[i])
  if a==n or a==n-1:
    print(set_divsors[i])
    break

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def list_gcd(a):
    tmp = list(set(a))
    ans = tmp[0]
    for i in tmp[1:]:
        ans = gcd(ans, i)
    return ans

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

result = []
for i in range(n):
    result.append(list_gcd(a[0:i]+a[i+1:]))

print(max(result))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
import numpy as np
N=int(input())
A=list(map(int,input().split(' ')))
leftgcd = [A[0]]
rightgcd = [A[-1]]

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

leftgcd.append(rightgcd[-1])
rightgcd = [leftgcd[0]] + rightgcd

g = fractions.gcd(leftgcd[0], A[-1])

leftgcd = np.array(leftgcd)//g
rightgcd = np.array(rightgcd)//g
result = 1

for i in range(N):
	x = fractions.gcd(leftgcd[i], rightgcd[i])
	result = x if x > result else result

print(int(g*result))

Submission status: Time Limit Exceeded

Solution code:
n = input()
lst = [int(s) for s in input().split()]

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

def gcdm(*arg):
    if len(arg) == 0:
        return 0
    arg = list(arg)
    a = arg.pop()
    for b in arg:
        a = gcd(a,b)
    return a

res = list()
if len(lst) == 2:
    res.append(gcd(*lst))
for i in range(len(lst)):
    temp=lst.pop(i)
    res.append(gcdm(*lst))
    lst.insert(i,temp)
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
from functools import reduce
from fractions import gcd
ans=[]
B=A.copy
for i in range(N):
    tmp=A[i]
    del A[i]
    ans.append(reduce(gcd,A))
    A.insert(i,tmp)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
def p(*a):
  s=" ".join(map(str,a))
  #print(s)
  sys.stderr.write(s+"\n")

import fractions
from functools import reduce

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

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



N=int(input())
A = [int(i) for i in input().split()]	# 1 2 3 4 5 ...

#p(N)
#p(A)

ans=0
for i in range(len(A)):
	#p(i, A[i], A[:i], A[i+1:])
	B=A[:i]+A[i+1:]
	#p(B)
	c=gcd_list(B)
	ans=max(ans,c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce


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


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


n = int(input())
a = list(int(n) for n in input().split())

# print(gcd_list(a))

max = 0

for i in range(n):
    j = a.pop(0)
#    print(j)
#    print(a)
    gcd = gcd_list(a)
    if max < gcd:
        max = gcd
#    print(gcd_list(a))
    a.append(j)
#    print(a)

print(max)

Submission status: Time Limit Exceeded

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

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

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

if __name__ == '__main__':
	N=int(input())
	A=list(map(int,input().split()))

	ans=0
	for i in range(N):
		l=copy.deepcopy(A)
		l.pop(int(i))
		tmp_gcd=gcd_list(l)	
		if(tmp_gcd > ans):
			ans= tmp_gcd
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
n = int(input())
a = list(map(int,input().split()))
ans = 1
def gcd_list(numbers):
    return reduce(fractions.gcd,numbers)
gcd = gcd_list(a)
a = [i//gcd for i in a]
for i in range(n):
    ans = max(ans,gcd_list(a[:i] + a[i+1:]))
print(ans * gcd)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()])

res = np.where(A % 2 == 0)
for i in range(max(A), -1, -1):
    if len(A[np.where(A % i == 0)]) >= len(A)-1:
        res = i
        break

print(res)

Submission status: Time Limit Exceeded

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

def gcd(m,n):
    if m > n:
        x = m
        y = n
    else:
        x = n
        y = m
    if x % y == 0:
        return y
    else:
        while x % y != 0:
            z = x % y
            x = y
            y = z
        else:
            return z

if N == 2:
    if A[0] > A[1]:
        print(A[0])
    else:
        print(A[1])
else:
    gcd_max = 0
    for i in range(N):
        if i != 0:
            gcd_temp = A[0]
        else:
            gcd_temp = A[1]
        j = 0
        while j < N:
            if j != i:
                gcd_temp = gcd(gcd_temp, A[j])
            if gcd_temp == 1:
                j += N
            j += 1
        if gcd_max < gcd_temp:
            gcd_max = gcd_temp
    print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions # python 3.4.3
# import copy
from functools import reduce

N=int(input())

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


def gcd(arr):
    return reduce(fractions.gcd, arr)

ans=gcd(A)

poped=None

for n in range(N):
	if poped != None:
		A.insert(n-1,poped)
	poped=A.pop(n)
	# print(A)
	ans=max(gcd(A),ans)

print(ans)

Submission status: Time Limit Exceeded

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

maxnum=1
for i in range(1,max(nums)+1):
    counter=0
    
    for j in range(n):
        if nums[j]%i != 0:
            counter+=1
    
    if counter==0 or counter==1:
        maxnum=i
        
        
        
print(maxnum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


if N == 2:
    print(max(A[0], A[1]))
else:
    m = min(A)
    ans = 1
    for i in range(m, 1, -1):
        count = 0
        for n in range(N):
            if count > 1:
                break
            if A[n] % i != 0:
                count += 1
        if count <= 1:
            ans = i
            break

    print(ans)

Submission status: Time Limit Exceeded

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

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):
    tmpA = A[0:i]+A[i+1:]
    tmpAL = len(tmpA)
    if tmpAL== 1:
      tmp = tmpA[0]
    else:
      tmp = gcd(tmpA[0], tmpA[1])
      if tmpAL != 2:
        for j in range(2,tmpAL):
            tmp = gcd(tmp,tmpA[j])

    if tmp > x:
      x = tmp
  
  print(x)
  
main()

Submission status: Time Limit Exceeded

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

if n==2:
    print(b)
    exit()

def gsd(a,b):
    if b==0:
        return a

    return gsd(b,a%b)

Y=[]
for i in range(n):
    list=[j for j in a if j != a[i]]
    tmpY=0
    for j in list:
        if tmpY == 0:
            tmpY=j
            continue
        tmpY=gsd(tmpY,j)
    Y.append(tmpY)

print(max(Y))

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
import fractions
from functools import reduce
# python3.4なのでfractions利用


def main():
    N = int(input())
    B = list(map(int, input().split()))

    if(N == 2):
        print(max(B))
        return

    result = 1
    for i in range(N):
        chB = copy.copy(B)
        chB.pop(i)
        current_gcd = gcd_list(chB)
        if(result < current_gcd):
            result = current_gcd
    print(result)


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


if(__name__ == '__main__'):
    main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy
def gcd(numbers):
    return reduce(fractions.gcd, numbers)
a = int(input())
b = list(map(int, input().split()))
c = []
d = copy.deepcopy(b)
for i in range(a):
  b = copy.deepcopy(d)
  if a == 2:
    c.append(max(b))
  elif a >= 3:
    b.pop(i)
    c.append(gcd(b))
if a <= 3:
  print(max(c))
elif a > 3:
  if max(c) == 1:
    print(1)
  else:
    print(max(c))

Submission status: Time Limit Exceeded

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

from functools import reduce
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def gcds(*numbers):
    return reduce(gcd, numbers)

ans = 0
for i in range(len(a)):
    aa = a.copy()
    aa.pop(i)
    g = gcds(*aa)
    ans = g if g > ans else ans
print(ans)

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
N = int(input())
As=input().split()
A = np.array([int(s) for s in As])
del(As)
#minA = np.amin(A)
min2A = A[A.argsort()[1]]
for i in range(min2A+1)[::-1]:
    if np.sum((A%i)==0)>=N-1:
        print(i)
        break

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce


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


N = int(input())
a_list = list(map(int, input().split()))
max_gcd_a = reduce(gcd, a_list[1:], a_list[1])
for idx in range(1, N):
    gcd_1 = reduce(gcd, a_list[1:idx], a_list[0])
    gcd_a = reduce(gcd, a_list[idx + 1:], gcd_1)
    if max_gcd_a < gcd_a:
        max_gcd_a = gcd_a

print(max_gcd_a)

# どれか１つを除いた時の最大公約数で最大のもの

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc125/tasks/abc125_c

import itertools
from collections import Counter
from collections import defaultdict
import bisect

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

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

    answers = []
    # half
    m = N // 2
    ans1 = 0
    for i in range(m):
        ans1 = gcd(A[i], ans1)

    for i in range(m, N):
        ans = ans1
        for j in range(m, N):
            if i == j:
                continue
            ans = gcd(A[j], ans)
        answers.append(ans)

    ans2 = 0
    for i in range(m, N):
        ans2 = gcd(A[i], ans2)

    for i in range(m):
        ans = ans2
        for j in range(m):
            if i == j:
                continue
            ans = gcd(A[j], ans)
        answers.append(ans)

    print(max(answers))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

if __name__ == '__main__':
	N=int(input())
	A=list(map(int,input().split()))

	ans=0
	for i in range(N):
		l=copy.deepcopy(A)
		l.pop(int(i))
		tmp_gcd=gcd_list(l)	
		if(tmp_gcd > ans):
			ans= tmp_gcd
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
#ユークリッド互除法
def gcd(x,y):
    a = max(x,y)
    b = min(x,y)
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

def gcdl(l):
    if len(l) == 1:
        return l[0]
    x = l[0]
    for i in l[1:]:
        x = gcd(i,x)
    return x

can = [1]*N
for i in range(N):
    can[i] = gcdl(A[0:i]+A[i+1:])
print(max(can))

Submission status: Time Limit Exceeded

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


N = int(input())
A = list(map(int, input().split(' ')))
A.sort()
result1 = reduce(fractions.gcd, A)
for i in range(len(A)):
    A_list = copy(A)
    a = A_list.pop(i)
    if result1 > a:
        break
    result = reduce(fractions.gcd, A_list)
    if result > result1:
        result1 = result
print(result1)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
N = int(input())
l = list(map(int,input().split()))


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

if len(l) == 2:
    memo = max(l)
else:
    memo = 1
    for i in range(N):
        g = gcd(*(l[:i]+l[i+1:]))
        if g > memo:
            memo = g

print(memo)

Submission status: Time Limit Exceeded

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

ans = []

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

def gcd_list(numbers):
    while len(numbers) > 2:
        x1 = numbers.pop()
        y1 = numbers.pop()
        numbers.append(gcd(x1, y1))
    return gcd(numbers[0], numbers[1])

if n == 2:
    print(max(a))
else:
    for i in range(n):
        c = a.copy()
        c.remove(a[i])
        ans.append(gcd_list(c))

    print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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)

    # divisors.sort()
    return divisors


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

cnt_div = dict()
for i, _a in enumerate(a):
    for d in make_divisors(_a):
        if d in cnt_div:
            cnt_div[d] += 1
        elif i < 2:
            cnt_div[d] = 1

max_div = 1
for r, c in cnt_div.items():
    if c >= n - 1:
        max_div = max(max_div, r)

print(max_div)

Submission status: Time Limit Exceeded

Solution code:
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
N=int(input())
A=list(map(int, input().split()))
Z=0
for i in range(N):
    if i==0:
        ans=A[1]
        for j in range(2,N):
            ans=gcd(ans,A[j])


    else :
        B=A.copy()
        B[i]=B[0]
        Z=B[0]
        for k in range(1,N):
            Z =gcd(Z,B[k])


    if ans >Z:
        ans=ans
    else:
        ans=Z

print(ans)

Submission status: Time Limit Exceeded

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

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

def gcd_list(X, N):
  g = X[0]
  for i in range(N):
    g = gcd(g, X[i])
  return g

num = 1
for n in range(N):
  a_n = A[n]
  for i in range(max(A)):
    A[n] = i
    num = max(num, gcd_list(A, N))
  A[n] =a_n

print(num)

Submission status: Time Limit Exceeded

Solution code:
import fractions


def solve(N, A):
    max_gcd = 0
    gcd1memo = None
    for i in range(N):
        gcd1 = None
        if gcd1memo is not None:
            gcd1 = fractions.gcd(gcd1memo, A[i - 1])
        else:
            for a in A[0:i]:
                gcd1 = fractions.gcd(gcd1, a) if gcd1 is not None else a
        gcd1memo = gcd1
        if i == N - 1:
            gcd = gcd1
        else:
            gcd2 = None
            for a in A[i + 1: N]:
                gcd2 = fractions.gcd(gcd2, a) if gcd2 is not None else a
            if gcd1 is None:
                gcd = gcd2
            elif gcd2 is None:
                gcd = gcd1
            else:
                gcd = fractions.gcd(gcd1, gcd2)
        if gcd > max_gcd:
            max_gcd = gcd
    return max_gcd


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

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
# 整数の入力
a = int(input())
# 数値のリスト化
xList = [int(i) for i in input().split()]
keisanList = deepcopy(xList)
saidai = max(xList)
saidaiKoyakuSu = 0
koyakuSu = 0
listIndex = 0

for i in range(a):
    keisanList = deepcopy(xList)
    keisanList[i] = saidai
    koyakuSu = gcd_list(keisanList)
    if(saidaiKoyakuSu < koyakuSu):
        saidaiKoyakuSu = koyakuSu
        listIndex = i

xList[listIndex] = saidai
print(gcd_list(xList))

Submission status: Time Limit Exceeded

Solution code:

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 = dict()
c = 1
for x in a:
    d = make_divisors(x)
    for y in d:
        if y in divs:
            if divs[y] >= c - 2:
                divs[y] += 1
        else:
            if c <= 2:
                divs[y] = 1
    c += 1
la = len(a)
maxdiv = 1
for k, v in divs.items():
    if v >= la - 1:
        maxdiv = max(maxdiv, k)
print(maxdiv)

Submission status: Time Limit Exceeded

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

nums = A[:]
import fractions

gcd_queue = [
    (nums[0], True),
    (nums[1], True),
    (fractions.gcd(nums[0], nums[1]), False),
]
for n in nums[2:]:
    tmp_queue = []
    for q in gcd_queue:
        if q[1] == True:
            tmp_queue.append((fractions.gcd(q[0], n), True))
        else:
            tmp_queue.append((fractions.gcd(q[0], n), False))
            tmp_queue.append((q[0], True))
    gcd_queue = tmp_queue[:]
#print(gcd_queue)

ans = 0
for q in gcd_queue:
    ans = max(ans, q[0])
print(ans)

Submission status: Time Limit Exceeded

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

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)

    divisors.sort()
    return divisors
    
    
kosuu = int(input())
nums = list(map(int,input().split()))

temp_arr=[]
for temp in nums:
    temp_arr.extend(make_divisors(temp))
    
temp_arr2 = list(set(temp_arr))
temp_arr2.sort(reverse=True)
temp_arr.sort(reverse=True)
for temp in temp_arr2:
    suuzi = temp_arr.count(temp)
    if suuzi == kosuu -1 or suuzi == kosuu:
        print(temp)
        break

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

input()
numbers = [int(x) for x in input().split()]
maximum = max(numbers)

gcd_max = 0

for i in range(len(numbers)):
    num = numbers[:]
    num[i] = maximum
    result = reduce(gcd, num)
    if result > gcd_max:
        gcd_max = result
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

def gcd2(_a):
    buff = 0
    for n in range(N):
        buff = fractions.gcd(buff, _a[n])
    return buff


ans = 0
_ans = 0
for i in range(N):
    buff = A.copy()
    buff[i] = 0
    _ans = gcd2(buff)
    ans = max(_ans, ans)


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import fractions
from functools import reduce

N = int(input())
A = np.array(input().split()).astype(int)

def gcd(numbers):
    return reduce(fractions.gcd, numbers)
rt = 1
for i in range(N):
    a = A[i]
    A[i] = 0
    f = gcd(A[A>0])
    A[i] = a
    if rt < f:
        rt = f
print(rt)

Submission status: Time Limit Exceeded

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

def gcd(m,n):
    if m > n:
        x = m
        y = n
    else:
        x = n
        y = m
    if x % y == 0:
        return y
    else:
        while x % y != 0:
            z = x % y
            x = y
            y = z
        else:
            return z

if N == 2:
    if A[0] > A[1]:
        print(A[0])
    else:
        print(A[1])
else:
    gcd_max = 0
    for i in range(N):
        if i != 0:
            gcd_temp = A[0]
        else:
            gcd_temp = A[1]
        for j in range(N):
            if j != i:
                gcd_temp = gcd(gcd_temp, A[j])
        if gcd_max < gcd_temp:
            gcd_max = gcd_temp
    print(gcd_max)

Submission status: Time Limit Exceeded

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

def gcd_l(A):
    if len(A) == 1:
        return A[0]
    x = gcd(A[0], A[1])
    if len(A) == 2:
        return x
    for i in range(2, len(A)):
        x = gcd(x, A[i])
    return x

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

B = []
x = A[0]
for i in range(N):
    x = gcd(x, A[i])
    B.append(x)

ans = 1
for i in range(1, N-1):
    ans = max(ans, gcd(B[i-1], gcd_l(A[i+1:])))
ans = max(ans, B[N-1], gcd_l(A[1:]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
from functools import reduce

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

n = int(input())
a = list(map(int,input().split())) #input from array of integer like "1 2 3"

ans = []

max = 0
maxi = 0

for i in range(n):
  l = copy.deepcopy(a)
  l.pop(i)
  x = gcd_list(l)
  ans.append(x)
  if max < x:
    max = x
    maxi = i

print(max)