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:
class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  # build
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  # set value at position p (0-indexed)
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  # result on interval [l, r) (0-indexed)
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        res = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                res = f(res, dat[l])
                l += 1
            if r & 1:
                r -= 1
                res = f(dat[r], res)
            l >>= 1
            r >>= 1
        return res


def gcd(x, y):
    if x * y == 0:
        return x + y
    elif x > y:
        return gcd(y, x)
    else:
        return gcd(y % x, x)


N = int(input())
A = [int(_) for _ in input().split()]
ST = SegmentTree(array=A, f=gcd, ti=0)
ans = 0
for i in range(N):
    ans = max(ans, gcd(ST.query(0, i), ST.query(i + 1, N)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce


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


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


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


main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[21]:


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


# In[22]:


maxans = 0
for j in range(N):
    Anew = []
    for i in range(N):
        Anew.append(A[i])
    Anew.pop(j)    
    ans = Anew[0]
    for i in range(1,N-1):
        ans = fractions.gcd(ans, Anew[i]) 
    if ans > maxans:
        maxans = ans
print(maxans)

Submission status: Time Limit Exceeded

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

list_ans = []

for i in range(N):
    remove_a = a.pop(0)
    #print("a is",a)
    ans = reduce(gcd, a)
    list_ans.append(ans)
    a.append(remove_a)
    #print("fix a is",a)

print(max(list_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(numbers):
    return reduce(fractions.gcd, numbers)

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

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = []
    if gcd(A) == max(A):
        print(gcd(A))
        exit()
    else:
        for i in range(N):
            if i == 0 :
                tmp.append(gcd(A[1:]))
            elif i == N-1:
                tmp.append(gcd(A[:N-1]))
            else:
                tmp.append(gcd(A[:i]+A[i+1:]))
    print(max(tmp))
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = 0
    if gcd(A) == max(A):
        print(gcd(A))
        exit()
    else:
        for i in range(N):
            if i == 0 :
                tmp=max(tmp,gcd(A[1:]))
            elif i == N-1:
                tmp=max(tmp,gcd(A[:N-1]))
            else:
                tmp=max(tmp,gcd(A[:i]+A[i+1:]))
    print(tmp)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

N=int(input())
A=list(map(int,input().split()))+[0]
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]
    
    for j in range(len(a)):
        if j==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j])

    for k in range(len(b)):
        if k==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k])
            
    ans.append(gcd(L,R))

print(max(ans))

Submission status: Time Limit Exceeded

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

N=int(input())
A=list(map(int,input().split()))+[0]
L=0
R=0
ans=[]
for i in range(N):
    a=[0,0]+A[:i]
    b=[0,0]+A[i+1:]
    
    for j in range(len(a)):
        if j==0:
            L=gcd(a[j],a[j+1])
        else:
            L=gcd(L,a[j])

    for k in range(len(b)):
        if k==0:
            R=gcd(b[k],b[k+1])
        else:
            R=gcd(R,b[k])
            
    ans.append(gcd(L,R))
    L=0
    R=0

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
N = int(input())
A = list(map(int, input().split()))
tmp = 0
if gcd(A) == max(A):
    print(gcd(A))
    exit()
else:
    for i in range(N):
        if i == 0 :
            tmp=max(tmp,gcd(A[1:]))
        elif i == N-1:
            tmp=max(tmp,gcd(A[:N-1]))
        else:
            tmp=max(tmp,gcd(A[:i]+A[i+1:]))
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
N = int(input())
A = list(map(int, input().split()))
tmp = []
if gcd(A) == max(A):
    print(gcd(A))
    exit()
else:
    for i in range(N):
        if i == 0 :
            tmp.append(gcd(A[1:]))
        elif i == N-1:
            tmp.append(gcd(A[:N-1]))
        else:
            tmp.append(gcd(A[:i]+A[i+1:]))
print(max(tmp))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
N = int(input())
A = list(map(int, input().split()))
tmp = []
for i in range(N):
    if i == 0 :
        tmp.append(gcd(A[1:]))
    elif i == N-1:
        tmp.append(gcd(A[:N-1]))
    else:
        tmp.append(gcd(A[:i]+A[i+1:]))
print(max(tmp))

Submission status: Time Limit Exceeded

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

from fractions import gcd

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

ans = []
for n in range(N):
    if n == 0:
        x = A[1]
    else:
        x = A[0]

    for i, ai in enumerate(A):
        if i == n:
            continue
        x = gcd(x, ai)
    ans.append(x)

print(max(ans))

Submission status: Time Limit Exceeded

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

import fractions
import copy

list_ans=[]

for i in range(n):
	listb=copy.deepcopy(lista)
	listb.pop(i)
#	print(listb)

	ans= listb[0]

	for i in range(1,n-1):
		ans=fractions.gcd(ans,listb[i])
#	print(ans)
	list_ans.append(ans)

print(int(max(list_ans)))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


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

G = []

for x in range(N):
    B = A[:x] + A[x+1:]
    g = B[0]
    for i in range(1, len(B)):
        g = gcd(g, B[i])
    G.append(g)

print(max(G))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
from functools import reduce
num = int(input())
nums = [int(s) for s in input().split()]

def gcd_list(numbers):
    return reduce(fractions.gcd, nums)
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
answer = []
for i in range(num):
  ls = copy.deepcopy(nums)
  del ls[i]
  answer.append(gcd(*ls))
print(max(answer))

Submission status: Time Limit Exceeded

Solution code:
def gcd(x , y):
    while y != 0:
        x , y = y , x % y
    return x
N = int(input())
A = list(map(int ,input().split()))
max_val = 0
if N == 2:
    print(max(A[1], A[0]))
    exit()
for i in range(N):
    work = A[:i] + A[i + 1:]
    for j in range(1 , len(work)):
        if j == 1:
            tmp = gcd(work[0],work[1])
        else:
            tmp = gcd(tmp , work[j])
    if max_val < tmp:
        max_val = tmp
print(max_val)

Submission status: Time Limit Exceeded

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

# 書き換える必要ある？
# 2 36 72　などの場合,2を36にしたら最大公約数は大きくなる
# ならば、１つを除いた全ての数の組み合わせにおける最大公約数の最大値が答えでは？

import itertools
import fractions
from functools import reduce

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

all_pairs=list(itertools.combinations(a,n-1))
gcd_box=[]
for lis in all_pairs:
    gcd_box.append(gcd_list(lis))
print(max(gcd_box))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

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

# 1つの数値を順に取り除いて、残りの最大公約数を求める
gcd_list = []

for i in range(N):
    A_rest = A.copy()
    A_rest.remove(A_rest[i])
    pre = A_rest[0]
    for j in range(1,N-1):
        gcd_tmp = gcd(pre,A_rest[j])
        pre = gcd_tmp
    gcd_list.append(pre)

print(max(gcd_list))

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions
import functools
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
a = LIST()

res = 0
for i in range(n):
    aa = a[:i] + a[i+1:]
    gcd = functools.reduce(fractions.gcd, aa)
    res = max(res,gcd)

print(res)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
N = int(input())
array = list(map(int,input().split()))
all_gcd = [0]*N

def calc_gcd(idx,array):
  gcd_arr = []
  for i in range(idx):
    if gcd_arr == []:
      gcd_arr.append(array[i])
    else:
      gcd_arr.append(gcd(gcd_arr[-1],array[i]))
  for j in range(idx+1,N,1):
    if gcd_arr == []:
      gcd_arr.append(array[j])
    else:
      gcd_arr.append(gcd(gcd_arr[-1],array[j]))
  return(gcd_arr[-1])

for k in range(N):
  all_gcd[k] = calc_gcd(k,array)

print(max(all_gcd))

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree:
    def __init__(self, seq, func="min"):
        if func=="min":
            self.f = min
            self.e = float('inf')
        if func=="max":
            self.f = max
            self.e = -float('inf')
        if func=="gcd":
            self.f = gcd
            self.e = 0
        self._n = len(seq)
        self.n_seg = 2**(self._n-1).bit_length()
        self.seg = [self.e]*2*self.n_seg
        
        for i in range(self._n):
            self.seg[self.n_seg+i-1] = seq[i]
            
        for i in range(self.n_seg-2, -1, -1):
            self.seg[i] = self.f(self.seg[2*i+1], self.seg[2*i+2])
        
    def update(k, val):
        k += self.n_seg -1
        self.seg[k] = val
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.f(self.seg[k*2+1], self.seg[k*2+2])
        
    def query(self, a, b):
        if b<=a:
            return self.e
        a += self.n_seg-1
        b += self.n_seg-2
        res = self.e
        while b-a>1:
            if a&1==0:
                res = self.f(res, self.seg[a])
            if b&1==1:
                res = self.f(res, self.seg[b])
                b -= 1
            a = a//2
            b = (b-1)//2
        if a==b:
            res = self.f(res, self.seg[a])
        else:
            res = self.f(self.f(res, self.seg[a]), self.seg[b])
        return res
    
    def __repr__(self):
        ret = str(self.seg[self._n-1:])
        return ret

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

if __name__=="__main__":
    N = int(input())
    A = list(map(int, input().split()))
    A_seg = SegmentTree(A, func="gcd")
    ans = -1
    for i in range(N):
        ans = max(ans, gcd(A_seg.query(0, i), A_seg.query(i+1, N)))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

n = int(input())
a = [int(i) for i in input().split()]
ans = -1
for i in range(1,n+1):
    ans = max(ans,reduce(gcd,a[:i-1]+a[i:]))
print(ans)

Submission status: Time Limit Exceeded

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

def gcd_list(L):
    b=min(L)
    for i in L:
        while True:
            if i%b==0:
                break
            c=b
            b=i%b
            i=c
    return(b)

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

print(ans)

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(lst):
    return reduce(gcd, lst)


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

Submission status: Time Limit Exceeded

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

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

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

gcds = list()
for i in range(0,N):
    B = copy.deepcopy(A)
    B.pop(i)
    tmp = gcd_list(B)
    gcds.append(tmp)

print(max(gcds))

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 = [int(i) for i in input().split()]
ans = 0
for i in range(N):
  B = A[:]
  B.remove(B[i])
  ans = max(ans, gcd_list(B))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
# from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

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

def main():
    """ main """
    N = int(input())
    AS = list(map(int, input().split()))
    dp = [0]*N
    dp[0] = AS[1]
    for i in range(1,N):
        for j in range(0,i):
            dp[j] = gcd(dp[j],AS[i])
        i_gcd = AS[0]
        for j in range(1,i):
            i_gcd = gcd(AS[j],i_gcd)
        dp[i] = i_gcd
    print(max(dp))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import fractions
from functools import reduce
n = int(input())
a = list(map(int, input().split()))
print(max([reduce(fractions.gcd, x) for x in itertools.combinations(a,n-1)]))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

N = int(input())
A = list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
  tmpA = A[i]
  A[i] = A[i+1] if i != N-1 else A[i-1]
  ans = max(ans, reduce(gcd,A))
  A[i] = tmpA
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))
class SegTree:
  def segfunc(self, x, y):
    return gcd(x, y) #ここ判定関数（ただし結合則があるもの）
  def __init__(self, n, ide_ele, init_val):
    #####単位元######
    self.ide_ele = ide_ele
    #num:n以上の最小の2のべき乗
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    #set_val
    for i in range(N):
      self.seg[i + self.num - 1] = init_val[i]    
    #built
    for i in range(self.num - 2, -1, -1) :
      self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) 
  def update(self, k, x):
    k += self.num - 1
    self.seg[k] = x
    while k + 1:
      k = (k - 1) // 2
      self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) 
  def query(self, p, q):
    if q <= p:
      return self.ide_ele
    p += self.num - 1
    q += self.num - 2
    res = self.ide_ele
    while q - p > 1:
      if p & 1 == 0:
        res = self.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res
sg = SegTree(N, 0, A)
res = 0
for i in range(N):
  res = max(res, gcd(sg.query(0, i), sg.query(i + 1, N)))
print(res)

Submission status: Time Limit Exceeded

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

ans = 1
# 1つ使わない中で最大公約数が最大のものを求める
for i in range(0, n):
    if i == 0:
        gcd_num = a[1]
    else:
        gcd_num = a[0]
    for j in range(1, n):
        if i == j:
            continue
        gcd_num = fractions.gcd(gcd_num, a[j])
    ans = max(ans, gcd_num)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
ans = 1
for i in range(0, N):
    if i == 0:
        tmp = A[1]
    else:
        tmp = A[0]
    for j in range(0, N):
        if i != j:
            tmp = gcd(A[j], tmp)
        if tmp < ans:
            break
    ans = max(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
ans = 1
for i in range(0, N):
    if i == 0:
        tmp = A[1]
    else:
        tmp = A[0]
    for j in range(0, N):
        if i != j:
            tmp = gcd(A[j], tmp)
        if tmp == 1:
            break
    ans = max(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
ans = 1
for i in range(0, N):
    if i == 0:
        tmp = A[1]
    else:
        tmp = A[0]
    for j in range(0, N):
        if i != j:
            tmp = gcd(A[j], tmp)
    ans = max(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

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

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

X=tuple(X)
Y=tuple(Y)

Z=[fractions.gcd(X[i], Y[i]) for i in range(N)]
    
print(max(Z))

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

X=[A[0]]
for i in range(1, N-1):
  X.append(fractions.gcd(X[-1], A[i]))
  
Y=[A[-1]]
for i in range(N-2, 0, -1):
  Y=[fractions.gcd(Y[0], A[i])]+Y
  
X=[Y[0]]+X
Y=Y+[X[-1]]
Z=[fractions.gcd(X[i], Y[i]) for i in range(N)]
    
print(max(Z))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce


def find_gcd(list_):
    x = reduce(gcd, list_)
    return x


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

ans = 0
for i in range(n):
    a_ = a.copy()
    a_.pop(i)
    ans = max(ans, find_gcd(a_))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

def gcd(numbers):
  return reduce(fractions.gcd, numbers)
n=int(input())
a=list(map(int,input().split()))

result=[]
gcdV=0
for i in range(0,n):
  gcdV=max(gcdV,gcd(a[:i]+a[i+1:]))
print(gcdV)

Submission status: Time Limit Exceeded

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


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



max_saidaikouyakusu = 0
for i in range(n):#nums[i]がない時の最大公約数を求める
    saidaikouyakusu = None
    for j in range(n):
        if j== i:continue
        else:
            if saidaikouyakusu==None:
                saidaikouyakusu = nums[j]
            else:
                saidaikouyakusu = gcd(saidaikouyakusu,nums[j])
    if max_saidaikouyakusu < saidaikouyakusu:
        max_saidaikouyakusu=saidaikouyakusu


print(max_saidaikouyakusu)

Submission status: Time Limit Exceeded

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

max_gcd = 0
for i in range(N):
  temp_A = A[:i] + A[i+1:]
  temp_gcd = temp_A[0]
  for j in range(N-1):
    temp_gcd = gcd(temp_gcd,temp_A[j])

  if temp_gcd > max_gcd:
    max_gcd = temp_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))

comb = list(combinations(A,len(A)-1))
max_gcd = 0
for i in range(len(comb)):
  temp_gcd = comb[i][0]
  for j in range(1,len(comb[i])):
    temp_gcd = gcd(temp_gcd,comb[i][j])

  if temp_gcd > max_gcd:
    max_gcd = temp_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))

comb = list(combinations(A,len(A)-1))
max_gcd = 0
for i in range(len(comb)):
  temp_gcd = comb[i][0]
  for j in range(1,len(comb[i])):
    temp_gcd = gcd(temp_gcd,comb[i][j])
    if temp_gcd < max_gcd:
      break
  max_gcd = max(max_gcd,temp_gcd)

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))

comb = list(combinations(A,len(A)-1))
max_gcd = 0
for i in range(len(comb)):
  temp_gcd = comb[i][0]
  for j in range(1,len(comb[i])):
    temp_gcd = gcd(temp_gcd,comb[i][j])
  max_gcd = max(max_gcd,temp_gcd)

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import fractions
import sys
import functools
sys.setrecursionlimit(10 ** 7)


def solve(N: int, A: "List[int]"):
    ans = 0
    for i in range(N):
        ans = max(ans, functools.reduce(fractions.gcd, A[0:i] + A[i + 1:]))
    print(ans)
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools
# (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

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

ans = 0
for i in range(n):
  g = 0
  for j in range(n):
    if i != j:
      g = gcd(g, a[j])
  ans = max(ans, g)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import functools
import math
import fractions
N = int(input())
A = list(map(int, input().split()))
gcd_max = 1
for i in range(N):
    gcd = functools.reduce(fractions.gcd, A[i+1:] + A[:i])
    if gcd > gcd_max:
        gcd_max = gcd

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions as m

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

gstackl = [0]
gstackr = [0]

ans = 0

for i in range(0, n):
	gstackl.append(m.gcd(gstackl[i], al[i]))

for i in reversed(range(1, n)):
	gstackr = [m.gcd(gstackr[0], al[i])] + gstackr

gstackr.append(0)

#print(gstackl)
#print(gstackr)

for i in range(n):
	l = gstackl[i]
	r = gstackr[i]
	#print("{} {}".format(l, r))
	a = m.gcd(l, r)
	#print(a)
	if a > ans:
		ans = a

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
max_v = max(a)
for i in range(max_v, 0, -1):
    a_t = tuple(map(lambda x: x % i == 0, a))
    if a_t.count(False) <= 1 and ans < i:
        ans = i
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

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

def resolve():
    n = int(input())
    ans = 0
    aaa = list(map(int, input().split()))
    for i in range(n):
        l = aaa[:i] + aaa[i+1:]
        g = gcd_list(l)
        ans = max(ans, g)
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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

for i in range(max(a),0,-1):
  flg=0
  for j in range(n):
    if a[j]%i !=0 and flg==0:
      flg=1
    elif a[j]%i !=0 and flg==1:
      flg=2
      break
  if flg<2:
    print(i)
    break

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):
  ans=max(gcd_list(a[:i]+a[i+1:]),ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N =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

A = sorted(A)

def del_dicts(dicts,i):
    del_list=[]
    for k,v in dicts.items():
        if v<i:
            del_list.append(k)
    for d in del_list:
        del dicts[d]
    return dicts

dicts = {}

for i in range(len(A)):
    if i<=2:
        for num in make_divisors(A[i]):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    
    else:
        del_list=[]
        
        for k in dicts.keys():
            if A[i]%k == 0:
                dicts[k]+=1
    dicts = del_dicts(dicts,i)
print(max(dicts.keys()))

Submission status: Time Limit Exceeded

Solution code:
N =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

A = sorted(A)

dicts = {}

for i in range(len(A)):
    for num in make_divisors(A[i]):
        if num in dicts:
            dicts[num]+=1
        else:
            dicts[num]=1
    
    if i>=2:
        del_list=[]
        for k,v in dicts.items():
            if v<i:
                del_list.append(k)
        for d in del_list:
            del dicts[d]

print(max(dicts.keys()))

Submission status: Time Limit Exceeded

Solution code:
N =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

dicts = {}
for i in A:
    for num in make_divisors(i):
        if num in dicts:
            dicts[num]+=1
        else:
            dicts[num]=1


max_value=0
for k,v in dicts.items():
    if v>=N-1 and max_value<k:
        max_value=k

print(max_value)

Submission status: Time Limit Exceeded

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


def compute_gcd(ns):
    a = gcd(ns[0], ns[1])
    for i in range(2, len(ns)):
        a = gcd(a, ns[i])
    return a


n = int(input())
nums = list(map(int, input().split()))
if n == 2:
    print(max(nums))
else:
    gcds = []
    for i in range(n):
        ns = [nums[j] for j in range(n) if j != i]
        a = compute_gcd(ns)
        gcds.append(a)
    print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import fractions
import copy


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

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

    ans = 0
    for a in A:
        B = copy.deepcopy(A)
        B.remove(a)
        ans = max(ans,gcd(B))

    print(ans)






main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

gcd_max = 0

for i, _ in enumerate(A):
    gcd_max = max(gcd_max, gcd(A[:i] + A[i+1:]))
    
print(gcd_max)

# 考え中。。。

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

gcd_max = 0

for i, _ in enumerate(A):
    gcd_max = max(gcd_max, gcd(A[:i] + A[i+1:]))
    
print(gcd_max)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy

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

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

ans = 0

for i in range(N):
  A_copy.pop(i)
  gcd = gcd_list(A_copy)
  if  gcd > ans:
    ans = gcd
  A_copy = copy.deepcopy(A)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#また、50ぷんはかかった、しかもTLE
#マジで死にたくなってくる
import copy
import fractions
from functools import reduce

n=int(input())
#この方法だと、参照型なので全部変えられてしまう
#a1=[[int(i) for i in input().split()]]*n
a1=[int(i) for i in input().split()]
a2=[[a1[j] for j in range(n) if i!=j] for i in range(n)]
#print(a2)
#高階関数reduce
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

#def gcd_list(numbers):
#    nul = numbers[0]
#    for i in range(1, len(numbers)):
#        nul = fractions.gcd(nul, numbers[i])
#    return nul

c=1

for i in range(n):
    c=max([c,gcd_list(a2[i])])
print(c)