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:
import math
#約数を列挙する関数
def return_child(num,result):
  for i in range(1, int(math.sqrt(num))+1, 1):
    if num % i == 0:
      result.append(i)
      if i != num//i:
        result.append(num // i)    
  return result

N = int(input())

nums = list(map(int, input().split(' ')))
nums.sort()

tmp = []
x = return_child(nums[0],tmp)
tmp = []
y = return_child(nums[1],tmp)

possible = []
for i in range(len(x)):
  possible.append((x[i], 1))

second_possible = []
for i in range(len(y)):
  second_possible.append((y[i], 0))

for i in range(1,N,1):
  result = []
  facts = return_child(nums[i],result)
  facts.sort()
  for j in range(len(possible)):
    if possible[j][0] in facts:
      continue
    else:
      if possible[j][1] == 0:
        possible[j] = (possible[j][0], -1)
      elif possible[j][1] == 1:
        possible[j] = (possible[j][0], 0)
          
  possible = [tup for tup in possible if tup[1] >= 0]
    

for i in range(2,N,1):
  result = []
  facts = return_child(nums[i],result)
  facts.sort()
  for j in range(len(second_possible)):
    if second_possible[j][0] in facts:
      continue
    else:
      if second_possible[j][1] == 0:
        second_possible[j] = (second_possible[j][0], -1)
      
  second_possible = [tup for tup in second_possible if tup[1] >= 0]
    
default = []
challenge = []

for i in range(len(possible)):
  if possible[i][1] == 0:
    challenge.append(possible[i][0])
  elif possible[i][0] == 1:
    default.append(possible[i][0])
    
for i in range(len(second_possible)):
  challenge.append(second_possible[i][0])

default.sort()
challenge.sort()

if challenge == []:
  print(default[-1])
else:
  print(challenge[-1])

Submission status: Time Limit Exceeded

Solution code:
import math
#約数を列挙する関数
def return_child(num,result):
  for i in range(1, int(math.sqrt(num))+1, 1):
    if num % i == 0:
      result.append(i)
      if i != num//i:
        result.append(num // i)    
  return result

N = int(input())

nums = list(map(int, input().split(' ')))

factarized = []
for i in range(N):
  result = []
  facts = return_child(nums[i],result)
  facts.sort()
  factarized.append(facts)

default = []
challenge = []

for i in range(len(factarized[0])):
  count = 0
  for k in range(len(factarized)):
    if factarized[0][i] not in factarized[k]:
      count += 1
    if count == 2:
      break
  if count == 0:
    default.append(factarized[0][i])
  elif count ==1:
    challenge.append(factarized[0][i])
    
for i in range(len(factarized[1])):
  count = 0
  for k in range(len(factarized)):
    if factarized[1][i] not in factarized[k]:
      count += 1
    if count == 2:
      break
  if count == 0:
    default.append(factarized[1][i])
  elif count ==1:
    challenge.append(factarized[1][i])

default.sort()
challenge.sort()

if challenge == []:
  print(default[-1])
else:
  print(challenge[-1])

Submission status: Time Limit Exceeded

Solution code:
import math
#約数を列挙する関数
def return_child(num,result):
  for i in range(1, int(math.sqrt(num))+1, 1):
    if num % i == 0:
      result.append(i)
      if i != num//i:
        result.append(num // i)    
  return result

N = int(input())

nums = list(map(int, input().split(' ')))
min_value_index = nums.index(min(nums))
max_value_index = nums.index(max(nums))

factarized = []
for i in range(N):
  result = []
  facts = return_child(nums[i],result)
  facts.sort()
  factarized.append(facts)

default = []
challenge = []
for i in range(len(factarized[min_value_index])):
  count = 0
  for k in range(len(factarized)):
    if factarized[min_value_index][i] not in factarized[k]:
      count += 1
    if count == 2:
      break
  if count == 0:
    default.append(factarized[min_value_index][i])
  elif count ==1:
    challenge.append(factarized[min_value_index][i])
    
for i in range(len(factarized[max_value_index])):
  count = 0
  for k in range(len(factarized)):
    if factarized[max_value_index][i] not in factarized[k]:
      count += 1
    if count == 2:
      break
  if count == 0:
    default.append(factarized[max_value_index][i])
  elif count ==1:
    challenge.append(factarized[max_value_index][i])

default.sort()
challenge.sort()

if challenge == []:
  print(default[-1])
else:
  print(challenge[-1])

Submission status: Time Limit Exceeded

Solution code:
import math
#約数を列挙する関数
def return_child(num,result):
  for i in range(1, int(math.sqrt(num))+1, 1):
    if num % i == 0:
      result.append(i)
      if i != num//i:
        result.append(num // i)    
  return result

N = int(input())

nums = list(map(int, input().split(' ')))
min_value = min(nums)

factarized = []
for i in range(N):
  result = []
  facts = return_child(nums[i],result)
  facts.sort()
  factarized.append(facts)

default = []
challenge = []
for i in range(1,min_value+1,1):
  count = 0
  for k in range(len(factarized)):
    if i not in factarized[k]:
      count += 1
    if count == 2:
      break
  if count == 0:
    default.append(i)
  elif count ==1:
    challenge.append(i)

if challenge == []:
  print(default[-1])
else:
  print(challenge[-1])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

from fractions import gcd

l_gcd = [0 for _ in range(n)]
r_gcd = [0 for _ in range(n)]

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

ret = 1
for i in range(1,n+1):
    ret = max(ret,gcd(l_gcd[i-1],r_gcd[-1::-1][i+1]))
print(ret)

Submission status: Time Limit Exceeded

Solution code:
#ABC-125-C
N = int(input())
A = list(map(int, input().split()))

def gcd(x, y):
    while y:
        x, y = y, x%y
    return x

ans_gcd = -1

for i in range(N):
    check_gcd = -1
    for j in range(N):
#         print("i, j", i, j)
        if i == j:
            continue
        elif check_gcd == -1:
            check_gcd = A[j]
#             print("check_gcd", check_gcd)
        else:
            check_gcd = gcd(check_gcd, A[j])
#     print("i", i, "ans", ans_gcd, "check", check_gcd)
    ans_gcd = max(ans_gcd, check_gcd)
    
print(ans_gcd)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A_base=list(map(int,input().split()))
A=A_base.copy()
M=[]
def gcd(a,b):
  if a<b:a,b=b,a
  while a%b!=0:a,b=b,a%b
  return b

for i in range(N):
  A.pop(i)
  base=A[0]
  for j in range(1,len(A)):
    base = gcd(base,A[j])
  else:
    M.append(base)
    A=A_base.copy()
print(max(M))

Submission status: Time Limit Exceeded

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

from functools import lru_cache
@lru_cache(maxsize=None)
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

ans = 1
for i in range(N):
    if i != 0:
        a = A[0]
    else:
        a = A[1]
    for j in range(N):
        if i == j:
            continue
        a = gcd(a, A[j])
    ans = max(ans, a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce


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


def solve():
    N, *A = map(int, open(0).read().split())
    
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(A[:i]+A[i+1:]))
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

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

Submission status: Time Limit Exceeded

Solution code:


from fractions import gcd
from functools import reduce # reduceの使い方覚える、ついでにaccumulateも

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

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

result = []
for i in range(n):
  numbers = A[:]
  numbers.pop(i)
  result.append(divider(numbers))
print(max(result))

Submission status: Time Limit Exceeded

Solution code:
import functools, fractions

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

gcd_max = 0

for i in range(n):
#  ad = a[:]
  ad = a[:i] + a[i + 1:]
#  if i == 0:
#    ad[0] = ad[1]
#  else:
#    ad[i] = ad[i - 1]
  gcd = functools.reduce(fractions.gcd, ad)
  if gcd > gcd_max:
    gcd_max = gcd

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import functools, fractions

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

gcd_max = 0

for i in range(n):
  ad = a[:]
  if i == 0:
    ad[0] = ad[1]
  else:
    ad[i] = ad[i - 1]
  gcd = functools.reduce(fractions.gcd, ad)
  if gcd > gcd_max:
    gcd_max = gcd

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions
N=int(input())
A=list(map(int,input().split()))
ans=[]
def dfs(A):
  l=[]
  if len(A)==1:
    return A[0]
  for i in range(0,len(A),2):
    if i+1 != len(A):
      l.append(fractions.gcd(A[i],A[i+1]))
    else:
      l.append(A[i])
  return dfs(l)
  
  
for i in range(N):
  s=copy.deepcopy(A)
  s.pop(i)
  ans.append(dfs(s))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import functools, fractions

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

gcd_max = 0

for i in range(n):
  ad = a[:]
  ad[i] = ad[i - 1]
  gcd = functools.reduce(fractions.gcd, ad)
  if gcd > gcd_max:
    gcd_max = gcd

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import functools, fractions

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

gcd_max = 0

for i in range(n):
  ad = a[:i] + a[i + 1:]
  gcd = functools.reduce(fractions.gcd, ad)
  if gcd > gcd_max:
    gcd_max = gcd

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import functools, fractions

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

gcd_max = 0

for i in range(n):
  ad = a[:i] + a[i + 1:]
  gcd = functools.reduce(fractions.gcd, ad)
  if gcd >= gcd_max:
    gcd_max = gcd

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from functools import reduce
import fractions


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


n = int(input())
lst = list(map(int, input().split()))
min_v = min(lst)

max_gcd = 0
for i in range(n):
  arr = lst[:i] + lst[i + 1:len(lst)]
  if min_v < max_gcd:
    break
  wk_gcd = gcd(*arr)
  max_gcd = wk_gcd if wk_gcd > max_gcd else max_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from functools import reduce
import fractions


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


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

max_gcd = 0
for i in range(n):
  arr = lst[:i] + lst[i + 1:len(lst)]
  # if min(arr) < max_gcd:
  #   break
  wk_gcd = gcd(*arr)
  max_gcd = wk_gcd if wk_gcd > max_gcd else max_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from functools import reduce
import fractions


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


n = int(input())
lst = list(map(int, input().split()))
lst.sort()

max_gcd = 0
for i in reversed(range(n)):
  arr = lst[:i] + lst[i + 1:len(lst)]
  if min(arr) < max_gcd:
    break
  wk_gcd = gcd(*arr)
  max_gcd = wk_gcd if wk_gcd > max_gcd else max_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from functools import reduce
import fractions


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


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

max_gcd = 0
for i in reversed(range(n)):
  wk_gcd = gcd(*(lst[:i] + lst[i + 1:len(lst)]))
  max_gcd = wk_gcd if wk_gcd > max_gcd else max_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from functools import reduce
import fractions


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


n = int(input())
lst = list(map(int, input().split()))
lst.sort()

v = 0
index = 0
# 2値の最大公約数を求める
for i in range(n):
  arr = lst[:i] + lst[i + 1:len(lst)]
  g = gcd(*arr)
  if g > v:
    v = g
    index = i

print(v)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
gcda = []
if N == 2:
    gcda.append(max(A))
else:
    
    for i in range(N):
        A2 = A.copy()
        A2.pop(i)
        element = gcd(A2[0],A2[1])
        for j in range(2,N-1):
            element = gcd(element,A2[j])
        gcda.append(element)
print(max(gcda))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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


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

ans = max(gcd_list(a[1:]), gcd_list(a[:n - 1]))
for i in range(1, n - 1):
    ans = max(ans, fractions.gcd(gcd_list(a[:i]), gcd_list(a[i + 1:])))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
def init(init_val,n):
    #set_val
    for i in range(n):
        seg[i+num-1]=init_val[i]
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2])

def segfunc(x,y):
    if x*y == 0:
        return max([x,y])
    return gcd(x,y)

def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])

def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res
N = int(input())
a = list(map(int,input().split()))
#####単位元######
ide_ele = 0

#num:n以上の最小の2のべき乗
num =2**(N-1).bit_length()
seg=[ide_ele]*2*num

init(a,N)
res = 0
for i in range(N):
    tmp = gcd(query(0,i),query(i+1,N))
    if tmp >= res:
        res = tmp
print(res)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
def init(init_val,n):
    #set_val
    for i in range(n):
        seg[i+num-1]=init_val[i]
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2])

def segfunc(x,y):
    if x*y == 0:
        return max([x,y])
    return gcd(x,y)

def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])

def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res
N = int(input())
a = list(map(int,input().split()))
#####単位元######
ide_ele = 0

#num:n以上の最小の2のべき乗
num =2**(N-1).bit_length()
seg=[ide_ele]*2*num

init(a,N)
res = 0
for i,elem in enumerate(a):
    update(i,0)
    tmp = query(0,N)
    if tmp >= res:
        res = tmp
    update(i,a[i])
print(res)

Submission status: Time Limit Exceeded

Solution code:

#NOT AC

import fractions
import functools

import sys
input = sys.stdin.readline

# 整数の入力
n = input()
n = int(n)

# いくつあるかわからないときのスペース区切りの整数の入力
a = input().split()
a = list(map(int,a))

gcdlist = [0] * n

i = 0
for i in range(0,n):
    check = [0] * n
    check[:] = a[:] #リストのコピーの時の作法
    del check[i]
    #print(check)
    #ここでcheckのリストの最大公約数をもとめればよい
    gcdlist[i] = functools.reduce(fractions.gcd, check)
    #print(gcdlist)

#print(gcdlist)

j = max(gcdlist)

print(j)

#a[j] = gcdlist[j]

#print(functools.reduce(math.gcd,a))

Submission status: Time Limit Exceeded

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

for i in range(1,A[-1]+1):
  count=0
  for j in A:
    if j%i==0:
      count+=1
  else:
    if count>=N-1:
      high=i

print(high)

Submission status: Time Limit Exceeded

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

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

N=int(input())
A=list(map(int,input().split()))
org_A=A[:]
#左から最大公約数作る右もね
ans=0
for i in range(N):
    l=A[:i]
    for j in range(len(l)-1):
        l[j+1]=euclid(l[j+1],l[j])
    
    r=A[i+1:]
    for j in range(len(r)-1):
        r[-j-2]=euclid(r[-j-1],r[-j-2])
        
    ans=max(ans,euclid(r[0] if len(r)>0 else l[-1],l[-1] if len(l)>0 else r[0]))
    
print(ans)

Submission status: Time Limit Exceeded

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

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

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

ans=0
for n in range(N):
    temp=A[:]
    temp.pop(n)
    for i in range(N-2):
        temp[i+1]=euclid(temp[i],temp[i+1])
    ans=max(temp[-1],ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()

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

#print(mgcd(A))

ans = []
ans.append(mgcd(A))
for i in range(N):
	tmp = A[:i]+A[i+1:]
	ans.append(mgcd(tmp))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
i=a[1]
while i > 0:
  flg=0
  for j in a:
    if j%i!=0:
      flg+=1
  if flg<=1:
    print(i)
    break
  else:
    i-=1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as c
m=int(input())
a=list(map(int,input().split()))

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

arr=[]
for i in a:
  arr+=make_divisors(i)

commonarr=c(arr).most_common()
ans=0
for i in commonarr:
  if i[1]>=m-1:
    ans=max(ans,i[0])
  else:
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

import fractions
from functools import reduce

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

left = [0]*n
right = [0]*n

for i in range(1, n):
    left[i] = gcd_list(a[:i])

for i in range(n-2, -1, -1):
    right[i] = gcd_list(a[i+1:])

ans = 0
for i in range(n):
    ans = max(ans, gcd_list([left[i], right[i]]))
print(ans)

Submission status: Time Limit Exceeded

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

from fractions import gcd
from functools import reduce
max_gcd = 0

for i in range(n+1):
  max_gcd = max(max_gcd, reduce(gcd, A[:i] + A[i+1:]))
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

n = int(input())
list_ = list(map(int, input().strip().split()))

reductive_gcd = lambda list_: list_[0] if len(list_) == 1 else reduce(gcd, list_)

ans = 0
for i in range(n):
    temp = list_.pop(0)
    gcd_ = reductive_gcd(list_)
    list_.append(temp)
    if gcd_ > ans:
        ans = gcd_

print(ans)

Submission status: Time Limit Exceeded

Solution code:

from functools import reduce
from fractions import gcd

n = int(input())
list_ = list(map(int, input().strip().split()))

reductive_gcd = lambda list_: list_[0] if len(list_) == 1 else reduce(gcd, list_)

ans = 0
for i in range(n):
    temp = list_.pop(0)
    gcd_ = reductive_gcd(list_)
    list_.append(temp)
    if gcd_ > ans:
        ans = gcd_

print(ans)

Submission status: Time Limit Exceeded

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

n = int(input())
array = np.array(list(map(int, input().strip().split())))

reductive_gcd = lambda list_: list_[0] if len(list_) == 1 else reduce(gcd, list_)

print(max([reductive_gcd([array[i] for i in range(n) if i != j]) for j in range(n)]))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
def gcd_l(a):
  tmp=a[0]
  for i in range(len(a)):
    tmp=gcd(tmp,a[i])
  return tmp
tmp=gcd_l(a)
for i in range(n):
  b=[a[j] for j in range(len(a)) if i!=j]
  tmp=max(gcd_l(b),tmp)
print(tmp)

Submission status: Time Limit Exceeded

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

import copy
from fractions import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)
ans=0
for i in range(n):
  aco=copy.copy(a)
  aco.pop(i)
  ans=max(ans,gcd_list(aco))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import fractions

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

ans_max = 0
for i in range(N):
    if i == 0:
        ans = 0
    else:
        ans = a[0]
    for j in range(1, N):
        if i == j:
            ans = fractions.gcd(ans, 0)
        else:
            ans = fractions.gcd(ans, a[j])
    ans_max = max(ans, ans_max)

print(ans_max)

Submission status: Time Limit Exceeded

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

A_deque = deque(A)


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

ans = 0
for _ in range(N):
    a = A_deque.popleft()
    ans = max(ans, func_gcd(A_deque))
    A_deque.append(a)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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


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

if (n == 2):
    print(max(a))
else:
    a.sort()
    max = 0
    for i in range(n):      
        b = a[0:i]
        c = a[i+1:n]        
        b.extend(c)
        gcd = gcd_list(b)   
        if max < gcd:       
            max = gcd       
    print(max)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
def print_ans(N, a_list):
    """Test Case
    >>> print_ans(3, [7, 6, 8])
    2
    >>> print_ans(3, [12, 15, 18])
    6
    >>> print_ans(2, [1000000000, 1000000000])
    1000000000
    """
    m_list = []
    a_list.append(0)
    for i in range(N):
        m = 0
        for k in range(i):
            m = gcd(max(m, a_list[k]), min(m, a_list[k]))
        for l in range(i + 1, N + 1):
            m = gcd(max(m, a_list[l]), min(m, a_list[l]))
        m_list.append(m)
    print(max(m_list))

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

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
a = list(map(int, input().split()))
if n == 2:
    print(max(a))
    exit()
x = [a[0], gcd(a[0], a[1])]
y = [gcd(a[-2], a[-1]), a[-1]]
for i in range(1, n - 2):
    x.append(gcd(x[i], a[i + 1]))
    y.insert(0, gcd(y[0], a[-i - 2]))
ans = max(x[-1], y[0])
for i in range(n - 2):
    ans = max(ans, gcd(x[i], y[i + 1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
a = list(map(int, input().split()))
if n == 2:
    print(max(a))
    exit()
x = [a[0], gcd(a[0], a[1])]
y = [gcd(a[-2], a[-1]), a[-1]]
for i in range(1, n - 2):
    x.append(gcd(x[i], a[i + 1]))
    y.insert(0, gcd(y[0], a[-i - 2]))
x.insert(0, y[0])
y.append(x[-1])
ans = 0
for i in range(n):
    ans = max(ans, gcd(x[i], y[i]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
a = list(map(int, input().split()))
if n == 2:
    print(max(a))
    exit()
x = [a[0], gcd(a[0], a[1])]
for i in range(1, n - 2):
    x.append(gcd(x[i], a[i + 1]))
y = [gcd(a[-2], a[-1]), a[-1]]
for i in range(3, n):
    y.insert(0, gcd(y[0], a[-i]))
x.insert(0, y[0])
y.append(x[-1])
ans = 0
for i in range(n):
    ans = max(ans, gcd(x[i], y[i]))
print(ans)

Submission status: Time Limit Exceeded

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

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
ans=0 
for i in range(n):
  a=A[i]
  A[i]=0
  X=gcd(*A)
  ans=max(ans,X)
  A[i]=a
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


def p_c():
    from sys import stdin
    input = stdin.readline
    n = int(input())
    *a, = map(int, input().split())
    ans = 0
    for i in range(n):
        tmp = a[i]
        a[i] = 0
        g = a[0]
        for j in range(1, n):
            g = gcd(g, a[j])
        a[i] = tmp
        if ans < g:
            ans = g
    print(ans)
    
if __name__ == '__main__':
    p_c()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


def p_c():
    n = int(input())
    *a, = map(int, input().split())
    ans = []
    for i in range(n):
        tmp = a[i]
        a[i] = 0
        g = a[0]
        for j in range(1, n):
            g = gcd(g, a[j])
        ans.append(g)
        a[i] = tmp
    print(max(ans))
    
p_c()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions as f


n = int(input())
a = list(map(int, input().split()))
b = copy.deepcopy(a)
ans = 1

for e in b:
    if (len(a) == 1):
        ans = a[0]
    elif (len(a) == 2):
        ans = max(a[0], a[1])
    else:
        a.remove(e)
        rep = a[0]

        for elem in a:
            rep = f.gcd(rep, elem)

        if (ans < rep):
            ans = rep

        a.append(e)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

n = int(input())
A = list(map(int, input().split()))
c = 1
for i in range(n):
  g = s_gcd(A[:i] + A[i+1:])
  c = max(c, g)
  
print(c)

Submission status: Time Limit Exceeded

Solution code:
def gca(x, y):
    if x == 0:
        return y
    return gca(y % x, x)

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

dic = {}



maxg = 0
for ignore in  range(N):
    g = A[0]
    if ignore == 0:
        g = A[1]
    for i in range(N):
        if i == ignore:
            continue
        x = g
        y = A[i]
        if y > x : x, y = y, x
        if (x, y) in dic:
            g = dic[(x, y)]
        else:
            g = gca(g, A[i])
        dic[(x, y)] = g

    maxg = max(g, maxg)

print(maxg)

Submission status: Time Limit Exceeded

Solution code:
def gca(x, y):
    if x == 0:
        return y
    return gca(y % x, x)

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

maxg = 0
for ignore in  range(N):
    g = A[0]
    if ignore == 0:
        g = A[1]
    for i in range(N):
        if i == ignore:
            continue
        g = gca(g, A[i])

    maxg = max(g, maxg)

print(maxg)

Submission status: Time Limit Exceeded

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

if n == 2:
    print(max(a))
else:
    l = [-1]
    r = []
    
    def gcd(x,y):
        while y != 0:
            x, y = y, x%y
    #        print(x,y)
        return(x)
    
    for i in range(n-1):
        m = a[i+1:][0]
        if len(a[i+1:]) == 1:
            m = a[i+1:][0]
        elif len(a[i+1:]) > 1:
            for j in a[i+1:]:
                m = gcd(m,j)
        r.append(m)
        m2 = a[:i+1][0]
        if len(a[:i+1]) == 1:
            m2 = a[:i+1][0]
        elif len(a[:i+1]) > 1:
            for j in a[:i+1]:
                m2 = gcd(m2,j)
        l.append(m2)
    r.append(-1)
 #   print(r)
  #  print(l)
    ans = 0
    for i in range(n):
        if l[i] == -1:
            ans = max(ans,r[i])
        elif r[i] == -1:
            ans = max(ans,l[i])
        else:
            ans = max(ans,gcd(l[i],r[i]))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
from math import sqrt
from functools import reduce
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def gcd_list(li): return reduce(gcd, li, 0)

N = INT()
A = LIST()

# 平方分割
D = ceil(sqrt(N))
bucket = [[] for i in range(D)]
gcds = [0] * D
for i in range(N):
    bucket[i//D].append(A[i])
# 各バケットで全体のGCDを予め取っておく
for i in range(D):
    gcds[i] = gcd_list(bucket[i])

ans = 0
for i in range(D):
    for j in range(len(bucket[i])):
        tmp = 0
        # バケット内でa以外全ての要素とGCD
        for k, a in enumerate(bucket[i]):
            if j == k:
                continue
            tmp = gcd(tmp, a)
        # aを含むバケット以外の全てのバケットとGCD
        for k in range(D):
            if i == k:
                continue
            tmp = gcd(tmp, gcds[k])
        ans = max(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

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

GCD = 0

for i in range(0,N):
  current = A[i]
  A[i] = A[i - 1]
  div = reduce(gcd,A)
  if div > GCD:
    GCD = div
  A[i] = current
  
print(GCD)