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:
#また、50ぷんはかかった、しかもTLE
#マジで死にたくなってくる
import copy
import fractions
from functools import reduce

n=int(input())
a1=[int(i) for i in input().split()]
#高階関数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):
    a2=copy.deepcopy(a1)
    a2.pop(i)
    c=max([c,gcd_list(a2)])
print(c)

Submission status: Time Limit Exceeded

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

n=int(input())
a1=[int(i) for i in input().split()]
#高階関数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):
    a2=copy.deepcopy(a1)
    a2.pop(i)
    c=max([c,gcd_list(a2)])
print(c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A_list = list(map(int, input().split())) # case1 : [7, 6, 8]

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

ans = 0

for i in range(N):
    left_gcd = 0
    right_gcd = 0

    for j in range(0, i):
        left_gcd = gcd(A_list[j], left_gcd)

    for k in range(i+1, N):
        right_gcd = gcd(A_list[k], right_gcd)

    i_gcd = gcd(left_gcd, right_gcd)

    if ans < i_gcd:
        ans = i_gcd

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A_list = list(map(int, input().split())) # case1 : [7, 6, 8]

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

ans = 0

for i in range(N):
    left_gcd = 0
    right_gcd = 0

    for j in range(0, i):
        left_gcd = gcd(A_list[j], left_gcd)

    for k in range(i+1, N):
        right_gcd = gcd(A_list[k], right_gcd)

    i_gcd = gcd(left_gcd, right_gcd)

    if ans < i_gcd:
        ans = gcd(left_gcd, right_gcd)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions as frac
from functools import reduce

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

def gcd_with_replace(numbers, replace_index) :
    stash = numbers[replace_index]
    numbers[replace_index] = 0
    li = gcd_list(numbers)
    numbers[replace_index] = stash
    return li

def main() :
    N = int(input())
    A_list = [int(i) for i in input().split()]
    # A_list.reverse()
    max_gcd_value = 0
    for i in range(N) :
        gcd_value = gcd_with_replace(A_list,i)
        if (max_gcd_value < gcd_value) :
            max_gcd_value = gcd_value
    
    print(max_gcd_value)



main()

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()))
gcd_A_all = []
for i in range(n):
    A_pop = [a for a in A[0:i] + A[i+1:n]]
    gcd_A = A_pop[0]
    for j in range(n-1):
        gcd_A = gcd(gcd_A, A_pop[j])
    gcd_A_all.append(gcd_A)
print(max(gcd_A_all))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
import copy

N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A)):
    if i == 0:
        lst = A[1:]
    else:
        lst = A[:i] + A[i+1::]
    if len(lst) > 1:
        ans = max(ans, functools.reduce(fractions.gcd, lst))
    else:
        ans = max(ans, lst[0])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
import copy

N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A)):
    lst = copy.copy(A)
    lst.pop(i)
    if len(lst) > 1:
        ans = max(ans, functools.reduce(fractions.gcd, lst))
    else:
        ans = max(ans, lst[0])

print(ans)

Submission status: Time Limit Exceeded

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

L = [0]
R = [0]
for i in range(N):
    L.append(fractions.gcd(L[i], lst[i]))
    R.insert(0, fractions.gcd(R[0], lst[N-1-i]))
ans = 0
for i in range(N):
    ans = max(ans, fractions.gcd(L[i], R[i+1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import heapq
import copy
N = int(input())
a = list(map(int, input().split()))
heap = []
for b in a:
    c = copy.deepcopy(a)
    c.remove(b)
    ans = c[0]
    for i in range(1, N-1):
        ans = fractions.gcd(ans, c[i])
        if ans == 1:
            break
    heapq.heappush(heap, -ans)
print(-heapq.heappop(heap))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import heapq
import copy
N = int(input())
a = list(map(int, input().split()))
heap = []
for b in a:
    c = copy.deepcopy(a)
    c.remove(b)
    ans = c[0]
    for i in range(1, N-1):
        ans = fractions.gcd(ans, c[i])
    heapq.heappush(heap, -ans)
print(-heapq.heappop(heap))

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]*N
for i in range(0, N):
    tmp = a.pop(0)
    ans[i] = gcd_list(a)
    a.append(tmp)
 
ans.sort()
print(ans[N-1])

Submission status: Time Limit Exceeded

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

L=[0]
R=[0]
for i in range(n-1):
    L.append(gcd(L[i],a[i]))
    R.insert(0,gcd(R[-1-i],a[-1-i]))
#print(L)
#print(R)
ans = 0
for i in range(n):
  ans = max(ans,gcd(L[i],R[i]))
print(ans)

Submission status: Time Limit Exceeded

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

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, = map(int, input().split())
data = getInputListInt()

ans = 0

for i in range(n):
    tmp = []
    tmp = copy.deepcopy(data)
    tmp.pop(i)

    gcdans = reduce(fractions.gcd, tmp)

    ans = max(ans, gcdans)

print(ans)

Submission status: Time Limit Exceeded

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

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, = map(int, input().split())
data = getInputListInt()

data.sort()

ans = 0

for i in range(n):
    tmp = []
    tmp = copy.deepcopy(data)
    tmp.pop(i)

    gcdans = reduce(fractions.gcd, tmp)

    ans = max(ans, gcdans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

dic = defaultdict(int)


def count_divisors(n):
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            dic[i] += 1
            if i != n // i:
                dic[n//i] += 1


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

for a in A:
    count_divisors(a)

for key in sorted(dic.keys(), reverse=True):
    if dic[key] in [n, n-1]:
        print(key)
        break

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

dic = defaultdict(int)


def count_divisors(n):
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            dic[i] += 1
            if i != n // i:
                dic[n//i] += 1


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

for a in A:
    count_divisors(a)

for key in sorted(dic.keys(), reverse=True):
    if dic[key] in [n, n-1]:
        print(key)
        break

Submission status: Time Limit Exceeded

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

if len(A) > 1:
    result = sorted(A)[1]
else:
    result = min(A)

while True:
    counter = 0
    should_continue = False
    for e in A:
        if e % result != 0:
            counter += 1
            if counter > 1:
                should_continue = True
                break
    if should_continue:
        continue
    if counter <= 1:  # 1つは書き換えられる
        break
    else:
        result -= 1

print(result)

Submission status: Time Limit Exceeded

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

if len(A) > 1:
    result = sorted(A)[1]
else:
    result = min(A)

while True:
    counter = 0
    for e in A:
        if e % result != 0:
            counter += 1
    if counter <= 1:  # 1つは書き換えられる
        break
    else:
        result -= 1

print(result)

Submission status: Time Limit Exceeded

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

# 最大公約数
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

for i in range(N):
    if i != 0:
        temp_dcg = A[0]
    else:
        temp_dcg = A[1]
    for j in range(i):
        temp_dcg = gcd(temp_dcg, A[j])
    for k in range(i + 1, N):
        temp_dcg = gcd(temp_dcg, A[k])
    dcg_list[i] = temp_dcg

print(max(dcg_list))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions
from functools import reduce

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

max = 0
for i in range(n):
    p = a[:i]
    q = a[i + 1:]
    r = reduce(fractions.gcd,  p + q)
    if max < r :
        max = r
print(max)

Submission status: Time Limit Exceeded

Solution code:
import fractions;

N = int(input());
aList = list(map(int, input().split()));

lList = [0];
rList = [0];
newL = 0;
newR = 0;
for i in range(0, N):
    newL = fractions.gcd(newL, aList[i]);
    lList.append(newL)
    newR = fractions.gcd(newR, aList[N - i - 1]);
    rList.insert(0, newR);

print(max(fractions.gcd(lList[i], rList[i + 1]) for i in range(N)));

Submission status: Time Limit Exceeded

Solution code:
import fractions;

N = int(input());
aList = list(map(int, input().split()));

lList = [0];
rList = [0];
newL = 0;
newR = 0;
for i in range(0, N):
    newL = fractions.gcd(newL, aList[i]);
    lList.append(newL)
    newR = fractions.gcd(newR, aList[N - i - 1]);
    rList.insert(0, newR);

mMax = 0;
for i in range(N):
    result = fractions.gcd(lList[i], rList[i + 1]);
    if mMax < result:
        mMax = result;


print(mMax);

Submission status: Time Limit Exceeded

Solution code:
import fractions;

N = int(input());
aList = list(map(int, input().split()));

lList = [0];
l = 0;
for i in range(N):
    result = fractions.gcd(l, aList[i])
    lList.append(result);
    l = result;

rList = [0];
r = 0;
for i in range(N - 1, -1, -1):
    result = fractions.gcd(r, aList[i]);
    rList.insert(0, result);
    r = result;

mMax = 0;
for i in range(N):
    result = fractions.gcd(lList[i], rList[i + 1]);
    if mMax < result:
        mMax = result;


print(mMax);

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

for j in range(N):
    B = A[:]
    B.pop(j)
    tmp = max(B)
    for i in B:
        tmp = fractions.gcd(tmp, i)
    if tmp > ans:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(1,int(a[i]**0.5)+1):
        if a[i] % j == 0:
            try:
                d[j].add(i)
            except:
                d[j] = set()
                d[j].add(i)
            try:
                d[a[i]//j].add(i)
            except:
                d[a[i]//j] = set()
                d[a[i]//j].add(i)

ans = 0
for i in d.keys():
    if len(d[i]) >= n-1:
        ans = max(ans, i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
for i in range(n):
  b=a[:i]+a[i+1:]
  ans[i] = b[0]
  for j in range(1, n-1):
    ans[i] = fractions.gcd(ans[i], b[j])
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import *

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

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

ans = 1
for i in range(len(A)):
    l = A.copy()
    l.pop(i)
    ans = max(ans,gcd_list(l))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache, reduce
from bisect import bisect
n = int(input())
aa = list(map(int, input().split()))

lencache = []
cache = []

@lru_cache(maxsize=10**9)
def gcd2(a, b) :
    if a%b : return gcd2(b, a%b)
    else : return b

@lru_cache(maxsize=10**9)
def gcdn(fs) :
    global cache, lencache
    l = len(fs)
    i = bisect(lencache, l)
    lencache.insert(i, l)
    cache.insert(i, fs)
    if l == 1 :
        return list(fs)[0]
    elif l < 10 :
        return reduce(gcd2, fs)
    else :
        for c in cache[::-1] :
            if c in fs :
                return gcd2(gcdn(c), gcdn(fs-c))
        else :
            c = frozenset({v for i, v in enumerate(fs) if i<len(fs)/2})
            return gcd2(gcdn(c), gcdn(fs-c))

m = 1
for i in range(n) :
    s = frozenset(aa[:i]+aa[i+1:])
    g = gcdn(s)
    if g > m :
        m = g
else :
    print(m)

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions
from functools import reduce

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

tmp1 = 0
for j in range(n):
  tmp0 = copy.copy(a)
  tmp0[j] = 0
  #print(a)
  ans = tmp0[0]
  #print(tmp0)
  for i in range(1, n):
    ans = fractions.gcd(ans, tmp0[i])
  tmp1 = max(tmp1,ans)
print(tmp1)

Submission status: Time Limit Exceeded

Solution code:
import functools
 
n = int(input())
lst = list(map(int, input().split()))
 
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
 
lst_2 = lst[:]
res = 0
for i in range(n):
    lst_2 = lst[:]
    lst_2.pop(i)
    res = max(res, functools.reduce(gcd, lst_2))
print(res)

Submission status: Time Limit Exceeded

Solution code:
import functools

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

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

lst_2 = lst[:]
res = 0
for i in range(n):
    lst_2 = lst[:]
    lst_2.pop(i)
    res = max(res, functools.reduce(gcd, lst_2))
print(res)

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=0
for i in range(n):
    ans=max(ans,reduce(gcd,a[:i]+a[i+1:]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
try:
    from math import gcd
except:
    from fractions import gcd
import operator
from collections import deque


class Segtree:
    """
    st = Segtree(4, f=operator.add, e=0)
    st[1:] = [4, 9, 16]
    st[0] = -99
    assert st[-1] == 16
    assert st[:] == [-99, 4, 9, 16]
    assert st.grasp(1, 3) == 13
    """

    def __init__(self, n, f=operator.add, e=0):
        """all(f(v, e) == f(e, v) == v for v in data)"""
        self.r = range(n)
        self.size = 2 ** ((n - 1).bit_length())
        self.tree = [e] * (2 * self.size)
        self.f = f
        self.iden = e

    def __len__(self):
        return len(self.r)

    def __list__(self):
        return self.tree[self.size-1:self.size-1+len(self.r)]

    def __repr__(self):
        return repr(list(self))

    def grasp(self, i=0, j=None):
        """reduce(f, data[i:j], e)"""
        if j is None:
            j = len(self)
        i = self.size - 1 + max(i, 0)
        j = self.size - 1 + min(j, len(self))
        ans = self.iden
        while j - i > 0:
            if i & 1 is 0:
                ans = self.f(self.tree[i], ans)
            if j & 1 is 0:
                ans = self.f(ans, self.tree[j-1])
            i = i >> 1
            j = (j - 1) >> 1
        return ans

    def __getitem__(self, i):
        r = self.r[i]
        if isinstance(r, int):
            return self.tree[self.size - 1 + r]
        else:
            return [self.tree[self.size - 1 + ri] for ri in r]

    def __setitem__(self, i, v):
        """Extra elements will be ignored when i is a slice."""
        r = self.r[i]
        if isinstance(r, int):
            r = [r]
            v = [v]
        q = deque()
        s = set()

        def addnext(j):
            k = (j - 1) // 2
            if k >= 0 and k not in s:
                q.append(k)
                s.add(k)

        for ri, vi in zip(r, v):
            j = self.size - 1 + ri
            self.tree[j] = vi
            addnext(j)
        while len(q):
            j = q.popleft()
            s.remove(j)
            vl = self.tree[2 * j + 1]
            vr = self.tree[2 * j + 2]
            self.tree[j] = self.f(vl, vr)
            addnext(j)


N = int(input())
A = [int(s) for s in input().split()]
st = Segtree(N, f=gcd, e=0)
st[:] = A
ans = max([gcd(st.grasp(0, i), st.grasp(i+1)) for i in range(N)])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#前準備
import fractions
n=int(input())
a=[int(i) for i in input().split()]
total=1
for i in a:
    total*=i
ans=1    

#i番目を抜いた最大公約数を考える
for i in range(n):
    gcd=total
    
    #i以外のn-1個を確認
    for j in range(n):
        if i==j:
            continue
        
        #i以外の時に、gcdとの最大公約数をとる
        gcd=fractions.gcd(a[j],gcd)
        
        #この時点で答えより小さい場合、考える価値なし
        if gcd<=ans:
            break
        
    if gcd>ans:
        ans=gcd
#     print(ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
*A, = map(int, input().split())
for _ in range(n-2):
    A = [gcd(A[i-1], A[i]) for i in range(n)]
print(max(A))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
n = int(input())
*A, = map(int, input().split())
print(max([reduce(gcd, A[:i]+A[i+1:]) for i in range(n)]))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from itertools import accumulate

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

m_gcd = 0
for i in range(N):
	Lt = L[:]
	Lt.pop(i)
	m_gcd = max(m_gcd, list(accumulate(Lt, gcd))[N - 2])

print(m_gcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
N=int(input())
A=list(map(int,input().split()))
ans=0
def gcd(n):
  return reduce(fractions.gcd, n)

for i in range(N):
  li=A[:i]+A[i+1:]
  x=gcd(li)
  if x>ans:
    ans=x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
from functools import reduce
N=int(input())
A=list(map(int,input().split()))
B=[]
c=0
def gcd(*n):
  return reduce(fractions.gcd, n)
for i in range(N):
  c=copy.deepcopy(A[i])
  A.pop(i)
  B.append(gcd(*A))
  A.insert(i,c)
  c=0
print(max(B))

Submission status: Time Limit Exceeded

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

import fractions
from functools import reduce

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

ans = 0
for i in range(n):
    b = a[:]
    b.pop(i)
    if ans < gcd_list(b):
        ans = gcd_list(b)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# Atcoder forces me to use fractions.gcd instead of math.gec because the contest accepts only python 3.4

import fractions
from functools import reduce


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


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

if len(A) == 1:
    ans = A[0]
else:
    ans = 1
    for i in range(len(A)):
        ans = max(ans, gcd_list(A[:i] + A[i + 1:]))

print(ans)

Submission status: Time Limit Exceeded

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


def R(r):
    r_list = [a[i] for i in range(n) if i >= r]
    if r_list == []:
        return reduce(fractions.gcd, [a[i] for i in range(n) if i != n - 1])
    else:
        return reduce(fractions.gcd, r_list)


def L(l):
    l_list = [a[i] for i in range(n) if i <= l]
    if l_list == []:
        return reduce(fractions.gcd, [a[i] for i in range(n) if i != 0])
    else:
        return reduce(fractions.gcd, l_list)


def M(m):
    return fractions.gcd(L(m-1), R(m + 1))


t = []
for j in range(n):
    t.append(M(j))
print(max(t))

Submission status: Time Limit Exceeded

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

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

gcd_list =[]

for i, a in enumerate(A):
    A_except_a = copy.deepcopy(A)
    A_except_a.pop(i)
    
    gcd = reduce(fractions.gcd, A_except_a)
    gcd_list.append(gcd)

print(max(gcd_list))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

m1 = [0]
for i in a:
  m1.append(fractions.gcd(m1[-1], i))

m2 = [0]
for i in reversed(a):
  m2.append(fractions.gcd(m2[-1], i))

m = []
for i in range(len(a)):
  m.append(fractions.gcd(m1[i], m2[::-1][i + 1]))

print(max(m))

Submission status: Time Limit Exceeded

Solution code:
import fractions
def resolve():
    N = int(input())
    A = list(map(int,input().split()))    
#    print()
    maxans = 0
    for j in range(N):
        if j==0:
            ans = A[1]
        else:
            ans = A[0]
        for i in range(1, N):
            if i==j:
                continue
            ans = fractions.gcd(ans, A[i])
        maxans = max(maxans,ans)
    print(maxans)
        #for i in range(ans,max(A)):
        #    if ans


resolve()

Submission status: Time Limit Exceeded

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

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

divisor_dict = {}
for i in range(len(A)):
    div = divisor(A[i])
    for j in div:
        if j in divisor_dict:
            divisor_dict[j] += 1
        else:
            divisor_dict[j] = 1

max_val = 0
for key, val in divisor_dict.items():
    if val >= N-1 and key > max_val:
        max_val = key

print(max_val)

Submission status: Time Limit Exceeded

Solution code:
#125
import fractions
n = int(input())
a = [int(x) for x in input().split(' ')]
all_m = []
for i in range(1,n-1):
    a_1 = a[:i]
    a_2 = a[i+1:]
    m_1 = a_1[0]
    m_2 = a_2[0]
    for j in range(len(a_1)-1):
        m_1 = fractions.gcd(m_1,a_1[j+1])
    for j in range(len(a_2)-1):
        m_2 = fractions.gcd(m_2,a_2[j+1])
    all_m.append(fractions.gcd(m_1,m_2))
m = a[1]
for j in range(1,n-1):
    m = fractions.gcd(m,a[j+1])
all_m.append(m)
m = a[0]
for j in range(n-2):
    m = fractions.gcd(m,a[j+1])
all_m.append(m)
print(max(all_m))

Submission status: Time Limit Exceeded

Solution code:
import fractions
n = int(input())
a = [int(x) for x in input().split(' ')]
all_m = []
for i in range(n):
    get_a = a[:i] + a[i+1:]
    m = get_a[0]
    for j in range(n-2):
        m = fractions.gcd(m,get_a[j+1])
    all_m.append(m)
print(max(all_m))

Submission status: Time Limit Exceeded

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

    return gcd(b % a, a)


N = int(input())
A = [int(v) for v in input().split()]
max_gcd = -1

for i in range(N):
    tmp = None
    for j in range(N):
        if j == i: continue
        if tmp is None:
            tmp = A[j]
            continue

        # print(tmp, A[j], gcd(tmp, A[j]))
        tmp = gcd(tmp, A[j])
    max_gcd = max(tmp, max_gcd)
if max_gcd < 0:
    print(A[0])
else:
    print(max_gcd)

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(n):
  if i == 0:
    tmp = a[1]
  else:
    tmp = a[0]
  for j in range(n):
    if j == i:
      continue
    else:
      tmp = gcd(tmp, a[j])
  ans = max(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

l_gcd = [0]
for i in range(n):
    l_gcd = l_gcd + [fractions.gcd(l_gcd[-1], a[i])]
# print(l_gcd)

r_gcd = [0]
for i in range(n):
    r_gcd.append(fractions.gcd(r_gcd[-1], a[-(i+1)]))
r_gcd.reverse()
# print(r_gcd)

for i in range(n):
    ans = max(ans, fractions.gcd(l_gcd[i], r_gcd[i+1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

l_gcd = [0]
for i in range(n):
    l_gcd = l_gcd + [fractions.gcd(l_gcd[-1], a[i])]
# print(l_gcd)

r_gcd = [0]
for i in range(n):
    r_gcd = [fractions.gcd(r_gcd[0], a[-(i+1)])] + r_gcd
# print(r_gcd)

for i in range(n):
    ans = max(ans, fractions.gcd(l_gcd[i], r_gcd[i+1]))
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 = 0

for i in range(n):
    b = a[:i]+a[i+1:]
    ans = max(reduce(fractions.gcd, b), 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()))
max_gcd = 0
for i in range(n):
    if i == 0:
        Gcd = reduce(gcd,a[1:])
    elif i == n-1:
        Gcd = reduce(gcd,a[:n-1])
    else:
        Gcd = reduce(gcd,a[:i] + a[i+1:])
    max_gcd = max(max_gcd,Gcd)
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

gcd_max=1

import functools

prefix_gcd = 0
post = A
for i in range(N):
    a = post.popleft()
    if len(post) == 0:
        postfix_gcd = 0
    elif len(post) == 1:
        postfix_gcd = post[0]
    else:
        postfix_gcd = functools.reduce(gcd, post)
    if postfix_gcd == 0:
        gcd_max = max(gcd_max, prefix_gcd)
    if prefix_gcd > 0:
        gcd_max = max(gcd_max, gcd(prefix_gcd, postfix_gcd))
        prefix_gcd = gcd(prefix_gcd, a)
    else:
        gcd_max = max(gcd_max, postfix_gcd)
        prefix_gcd = a

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

gcd_max=1

import functools
import math

for i in range(N):
    a = A.popleft()
    gcd_max = max(gcd_max, functools.reduce(gcd, A))
    A.append(a)

print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

def GCD_modify(a,b):
  if a==0:
    return b
  elif b==0:
    return a
  else:
    return fractions.gcd(a,b)

def RE_F(i):
  L,R = 0,0
  for j in range(i):
    L = GCD_modify(L,num_arr[j])
  for k in range(N-i-1):
    R = GCD_modify(R,num_arr[k+i+1])
    #print(R)
  return GCD_modify(L,R)

GCD_arr = [RE_F(l) for l in range(N)]

print(max(GCD_arr))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
import  copy
N = int(input())
A = list(map(int,input().split()))
B = copy.deepcopy(A)
gcdlist = []
for i in range(N):
    del B[i]
    ans = B[0]
    for k in range(1,len(B)):
        ans = gcd(ans,B[k])
    gcdlist.append(ans)
    B = copy.deepcopy(A)
print(max(gcdlist))

Submission status: Time Limit Exceeded

Solution code:
# GCD on Blackboad
import fractions

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

def get_gcd(a):
	d = a[0]
	for x in a[1:]:
		d = fractions.gcd(d, x)
	return d
	
gcd = [0]*N
for n in range(N):
	A_ = A[:]
	del A_[n]
	gcd[n] = get_gcd(A_)

print(max(gcd))

Submission status: Time Limit Exceeded

Solution code:
import fractions

n = int(input())
lst = [int(i) for i in input().split()]
gcd = []

for i in range(n):
    f = 0
    for j in range(n):
        if f==0 and j!=i:
            f = lst[j]
        elif j!=i:
            f = fractions.gcd(f,lst[j])
    gcd.append(f)
print(max(gcd))

Submission status: Time Limit Exceeded

Solution code:
#最大公約数
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

#最小公倍数
def lcm(a, b):
    return a * b // gcd(a, b)

#最大公約数（複数）
def gcds(l):
    if len(l) == 1:
        return l[0]
    elif len(l) == 2:
        return gcd(l[0], l[1])
    else:
        n = len(l) // 2
        L = l[:n]
        R = l[n:]
        return gcd(gcds(L), gcds(R))

#最小公倍数(複数)
def lcms(l):
	if len(l) == 1:
		return l[0]
	elif len(l) == 2:
		return lcm(l[0], l[1])
	else:
		n = len(l) // 2
		L = l[:n]
		R = l[n:]
		return lcm(lcms(L), lcms(R))

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

mm = []
import copy

for i in range(n):
	temp = copy.deepcopy(a)
	temp.pop(i)
	mm.append(gcds(temp))
print(max(mm))

Submission status: Time Limit Exceeded

Solution code:
import fractions

def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    ans = 1
    for i_out in range(len(A)):
        A_ith = [A[i] for i in range(len(A))]
        del A_ith[i_out]
        ans = max(ans, cal_all_gcb(A_ith))
    print(ans)

def cal_all_gcb(A):
    gcd = A[0]
    for j in range(1, len(A)):
        gcd = fractions.gcd(gcd, A[j])
    return gcd

main()