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 fractions
import numpy as np
from functools import reduce

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

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

n=int(input())

arr = np.array(list(map(int,input().split()))).astype(np.int)

ans=0

for i in range(n):
    ind = np.ones(n, dtype=bool)
    ind[i] = False
    #print(arr[ind])
    ans=max(ans,gcd_list(arr[ind]) )

print(ans)

Submission status: Time Limit Exceeded

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

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

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

p = l.pop(0)
ans = 1
for j in range(1,N+1):
    element = gcd(l[0], l[1])
    for i in range(2,N-1):
        element = gcd(element,l[i])
    ans = max(ans, element)
    l.append(p)
    p = l.pop(0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

def gcd_list(a):
  tmp = a[0]
  for i in range(len(a)-1):
    tmp = gcd ( a[i+1] , tmp )
  return tmp

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

c=[]
for i in range(n):
	c.append(gcd_list(v[:i]+v[i+1:]))

print(max(c))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
num_list.sort()
num_list.reverse()
answer_list = []
max_num = 0
flag = False
for i in range(num_list[0], 0, -1):
    if flag and count < 2:
        break
    else:
        flag = True
        count = 0
        max_num = i
    for j in range(n):
        if num_list[j] / max_num < 1 or num_list[j] % max_num != 0:
            count += 1
            if count > 1:
                break
print(max_num)

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)
                #print('i,inlist:',i , n//i)

    divisors.sort()
    return divisors

adic = {}
for a1 in a:
    diva = make_divisors(a1)
    for diva1 in diva:
        if diva1 in adic.keys():
            adic[diva1] += 1
        else:
            adic[diva1] = 1
#print(adic)

g = 1
for key1 in adic.keys():
    s = adic[key1]
    if s >= N-1:
        g = max(g,key1)
print(g)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
A = list(set(a))
ans = [reduce(gcd, A)]

if len(A) == 1:
    print(A[0])
    exit()
for i in a:
    b = sorted(A)
    b.remove(i)
    if a.count(i) == 1:
        ans.append(reduce(gcd, b))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import numpy as np

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

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

n = int(input())
a_list = np.asarray(list(map(int, input().split())))
ans = 0

for i in range(n):
    ind = np.ones(n, dtype=np.bool)
    ind[i] = False
    ans = max(ans, gcd_list(a_list[ind]))

print(ans)

Submission status: Time Limit Exceeded

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


def gcd(vs):
    while True:
        m = min(vs)
        vs = [(v - 1) % m + 1 for v in vs]
        if all([v == vs[0] for v in vs]):
            return vs[0]


gcds = []
for i in range(len(As)):
    gcds.append(gcd(As[:i] + As[i + 1:]))

print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def gcd(a, b):
    minV = min(a, b)
    gcd = 0
    for i in range(1, minV + 1):
        if int(a) % i == 0 and int(b) % i == 0:
            gcd = i
    return gcd

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

maxg = 0
if N == 2:
    print(max(Alist))
else:
    for i in range(0, N):
        tmp = np.array(Alist)
        g = 0
        if i != N - 1:
            tmp[i] = tmp[i + 1]
        else:
            tmp[i] = tmp[i - 1]
        for j in range(1, N):
            if j == 1:
                g = gcd(tmp[0], tmp[1])
            else:
                g = gcd(g, tmp[j])
        if maxg < g:
            maxg = g
    print(maxg)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd  # in math module python >= 3.5
from functools import reduce


def gcdall(numlist):
    return reduce(gcd, numlist)


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    if len(a) < 3:
        print(max(a))
        return

    gcds = []
    for i in range(len(a)):
        excepted = [a[j] for j in range(len(a)) if j != i]
        b = 0
        while all([k % 2 == 0 for k in excepted]):
            b += 1
            excepted = [k // 2 for k in excepted]

        if b > 0:
            gcds.append(gcdall(excepted) * 2 * b)
        else:
            gcds.append(gcdall(excepted))
    print(max(gcds))


main()

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
A = list(set(a))
ans = [reduce(gcd, a)]

if len(A) == 1:
    print(A[0])
    exit()
for i in a:
    b = sorted(A)
    b.remove(i)
    if a.count(i) == 1:
        ans.append(reduce(gcd, b))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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


import math
import fractions
from functools import reduce

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

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

ans=1
temp=[]
aa = gcd_list(a)
for i in range(aa,max(a)+1):
    cnt=0
    for j in a:
        if j % i != 0:
            cnt = cnt+1
    if cnt == 1 or cnt == 0:
        temp.append(i)

print(max(temp))

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()))
max = 0
for i in range(n):
    li = a[:i] + a[i + 1:]
    tmp = gcd_list(li)
    if tmp > max:
        max = tmp
print(max)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
ans=1    
n=int(input())
a=[int(s) for s in input().split()]
for i in range(len(a)):
    b=a[:]
    b[i]=max(a)
    g=gcd_list(b)
    if g>ans:
        ans=g
print(ans)

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()))
tempA = []
maxynum = 0

for i in range(N):
    tempA.extend(A[0:i])
    tempA.extend(A[i+1:])
    ynum = gcd_list(tempA)
    if ynum > maxynum:
        maxynum = ynum
    tempA.clear()

print(maxynum)

Submission status: Time Limit Exceeded

Solution code:
# AtCoder Beginner Contest 125
# https://atcoder.jp/contests/abc125
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nnn = lambda n: ss2nnn(ii2ss(n))

cache = {}


def gcd(a, b):
    if a not in cache:
        cache[a] = {}
    if b in cache[a]:
        return cache[a][b]
    if b == 0:
        cache[a][b] = a
        return a
    n = gcd(b, a % b)
    cache[a][b] = n
    return n

def main(N, An):
    if N == 2:
        n = max(An[0], An[1])
        print(n)
        return
    nn = [An[0]] * N
    nn[0] = An[1]
    for i in range(1, N):
        for j in range(N):
            if i == j:
                continue
            nn[j] = gcd(nn[j], An[i])
    print(int(max(nn)))

N = i2n()
An = i2nn()
main(N, An)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())

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


from functools import lru_cache
import sys
if n == 2:
    print(max(a))
    sys.exit()


@lru_cache(maxsize=100000)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def gcd_list(li):
    g = gcd(li[0], li[1])
    for i in range(2, n):
        g = gcd(g, a[i])
    return g


def gcd_list_2(li):
    g = gcd(li[0], li[1])
    if g == 1:
        return max(gcd_list(li[0] + li[2:]), gcd_list(li[1:]))
    for i in range(2, n):
        if gcd(g, a[i]) == 1:
            return gcd_list([g] + li[i + 1:])
        else:
            g = gcd(g, a[i])
    return g


def max_gcd(a):
    g = gcd_list(a)
    a = [a[i]//g for i in range(n)]

    m = 1
    for i in range(n):
        b = a[:]
        del b[i]
        gg = gcd_list(b)
        m = max(m, gg)

    print(g * m)


max_gcd(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
values = list(map(int, input().split()))
m = 1


def gcd(x, y):
    if x < y:
        x += y
        y = x - y
        x -= y

    while y > 0:
        r = x % y
        x = y
        y = r

    return x


for i in range(n):
    a = values[:]
    a.pop(i)
    g = a[0]
    for j in range(n-1):
        if a[j] % g == 0:
            pass
        else:
            g = gcd(g, a[j])
    if m < g:
        m = g

print(m)

Submission status: Time Limit Exceeded

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

import fractions
from functools import reduce

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


ans=0
GCD_target=gcd_list(A)

for i in range(N):
   copied_A = A[:]
   copied_A.pop(i)
   GCD_current=gcd_list(copied_A)
   ans=max(ans,GCD_current)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2000)

memo_gcm = {}
def uclid_gcm(a, b):
  if a < b:
    a += b
    b = a-b
    a=a-b
  if b == 0:
    return a
  elif (a, b) in memo_gcm.keys():
    return memo_gcm[(a, b)]
  else:
    r = a%b
    memo_gcm[(a, b)] = b if r == 0 else uclid_gcm(b, r)
    return memo_gcm[(a, b)]

def gcm_all(array):
  l = len(array)
  if l == 1:
    return array[0]
  else:
    tmp = []
    for i in range(l//2):
      tmp.append(uclid_gcm(array[i*2], array[i*2+1]))
    if l %2 == 1:
      tmp.append(uclid_gcm(array[-2],array[-1]))
    return gcm_all(tmp)

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

ans = 0
for i in range(N):
  t  = A[i]
  A[i] = 0
  ans = max(ans, gcm_all(A))
  A[i] = t
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions
from functools import reduce
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
input = sys.stdin.readline
n = int(input())
a_s = input().split()
a = [int(s) for s in a_s]
max = 0
for i in range(n):
    temp = a[:i] + a[i+1:]
    k = gcd_list(temp)
    if k > max:
        max = k
print(max)

Submission status: Time Limit Exceeded

Solution code:
from collections import*
def dv(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 Counter(divisors)
 
n,*a=map(int,open(0).read().split())
ans=Counter()
for i in range(n):
    ans+=dv(a[i])
l=[]
for k,v in ans.items():
    if v==n-1 or v==n:
        l.append(k)
print(max(l))

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
import fractions


def main():
    N = int(input())
    num = list(map(int, input().split()))
    # num.sort()
    # print(num)
    # print(num[0:0])
    gcd = 0
    for i in range(len(num)):
        temp_list = num[0:i]+num[i+1:]
        temp_gcd = temp_list[0]
        for i in temp_list:
            temp_gcd = fractions.gcd(temp_gcd, i)
        # temp_gcd = functools.reduce(math.gcd, temp_list)
        if temp_gcd > gcd:
            gcd = temp_gcd
    print(gcd)
    # ans = 0
    # print()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# ユークリッドの互除法
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

ans = 0
if n==2:
    print(max(aa))
else:
    for i in range(n):
        temp_list = aa.copy()
        temp_list.pop(i)
        temp = temp_list[0]
        for j in range(n-1):
            temp = gcd(temp, temp_list[j])
        # print(aa[i], temp)
        ans = max(ans, temp)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
    if a > b:
        while b != 0:
            a, b = b, a%b
        return(a)
    else:
        while a != 0:
            b, a = a, b%a
        return(b)        

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

temp = 1
for i in range(n):
    temp = temp * a[i]

temp_ = temp
ans = 0
for i in range(n):
    for j in range(n):
        if j != i:
            temp_ = gcd(temp_, a[j])
    if ans < temp_:
        ans = temp_
    temp_ = temp

print(ans)

Submission status: Time Limit Exceeded

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

from copy import copy
from fractions import gcd

def all_gcd(numbers):
    result = numbers[0]
    for v in numbers:
        result = gcd(result, v)
    return result

def main():
    n = int(input())
    a = list(map(int, input().split()))
    max_gcd = 0
    del_number = -1
    for i in range(n):
        b = copy(a)
        del b[i]
        bgcd = all_gcd(b)
        if max_gcd < bgcd:
            max_gcd = bgcd
            del_number = a[i]
    print(max_gcd)




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(A[1]-1):

    count = 0

    for j in range(N):
        if A[j] % (A[1] - i) !=0:
            count += 1
        
    if count == 1:
        b = A[1] - i
        break
    else:
        continue

if b == 0:
    print(A[0])
else :
    print(b)

Submission status: Time Limit Exceeded

Solution code:
def hcf(x, y): 
    for i in range(1,min(x,y) + 1):
        if((x % i == 0) and (y % i == 0)):
            hcf = i
    return hcf

def blackbord(n,a):
    if n==2 and a[0]==a[1]:
        return a[0]
    else:
        return max(list(hcf(i,j) for i in a for j in list(filter(lambda b:b!=i,a))))
 
n = int(input())
a=list(map(int, input().split()))
print(blackbord(n,a))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
 
N=int(input())
a_list = list(map(int, input().split()))
lst = []
for i, a in enumerate(a_list):
    tmp = a_list[:i] + a_list[i+1:]
    lst.append(reduce(gcd, tmp))
print(max(lst))

Submission status: Time Limit Exceeded

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

gcd_max = 1

if N == 2:
    print(max(A_array[0], A_array[1]))
    exit()
for i in range(N):
    gcd = 0
    for j, A in enumerate(A_array):
        if i == j:
            continue
        if gcd == 0:
            gcd = A
            continue
        # print(A,gcd)
        gcd = min(gcd, fractions.gcd(A, gcd))
        if gcd <= gcd_max:
            break
    gcd_max = max(gcd_max, gcd)
print(gcd_max)

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)

gcdA = [0 for i in range(N)]
for i in range(N):
    A_=A.copy()
    A_.pop(i)
    gcdA[i] = gcd(A_)

print(max(gcdA))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import functools
n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
for i in range(n):
  ans[i] = functools.reduce(gcd, a[:i] + a[i+1:])
print(max(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()))
L = []
ANS = [0]*N

for i in range(N):
    L = A[:i]+A[i+1:]
    ANS[i] = gcd(*L)

if N == 2:
    print(max(A))
else:
    print(max(ANS))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
s=[[0]for _ in range(n)]
#print(s)

import math
import fractions
from functools import reduce

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
    
for i in range(n):
    if i==0:
        s[0]=l[1:]
    
    else:
        s[i]=l[:i]+l[i+1:]
ans=1
for i in s:
    ans=max(gcd_list(i),ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

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

ans = -9999
for i in range(N):
    tmp = [A[j] for j in range(N) if j != i]
    if reduce(gcd, tmp) > ans:
        ans = reduce(gcd, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
"""
問題文

N個の整数 A1,A2,...,ANが黒板に書かれています。
あなたはこの中から整数を 1つ選んで、1 以上 109以下の好きな整数に書き換えます。
元の整数と同じ整数に書き換えても構いません。書き換えた後のN個の整数の最大公約数の最大値を求めてください。

制約
入力は全て整数である。
2≤N≤105
1≤Ai≤109
"""

"""
3 5 7
最大公約数: 1
最大の最大公約数: 1

8 4 6
最大公約数: 2
最大の最大公約数: 4
『aとbとcの最大公約数』　＝　『aとbの最大公約数』と『c』　の最大公約数

a B c
gcd1 = gcd(a , B)
gcd2 = gcd(B , c)

"""
import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce

gcd_map = {}
def GCD(numbers):
    x = numbers[0]
    for i in range(len(numbers) - 1) : 
        x = gcd_sub(x , numbers[i + 1])
    return x

def gcd_sub(a , b) : 
    if (a , b) in gcd_map : return gcd_map[(a,b)]
    else : 
        rst = gcd(a , b)
        gcd_map[(a,b)] = rst
        return rst



def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : sub_a_list = a_list[1:]
        elif i == N - 1 : sub_a_list = a_list[:N-1]
        else : sub_a_list = a_list[0 : i ] + a_list[i + 1 :] 
        rst = GCD(sub_a_list)
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools

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

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

max_gcd = -float('inf')

for v in itertools.permutations(list,N-1):
    max_gcd = max(functools.reduce(gcd,v),max_gcd)

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy

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

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

ans = 0
for i in range(N):
    a = copy.copy(A)
    a.pop(i)
    kouyaku = gcd_list(*a)
    ans = max(ans, kouyaku)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
a = min(A)
A1 = np.array(A)
count1 = []
for i in range(1, a+1):
    x = A1%i
    count1.append(sum(x==0))


ans1 = []
for i in range(a):
    if count1[i] == N or count1[i] == N-1:
        ans1.append(i+1)

A.remove(a)
b = min(A)
A2 = np.array(A)
for i in range(1, b+1):
    y = A2%i
    count1.append(sum(y==0))


ans2 = []
for i in range(b):
    if count1[i] == N:
        ans2.append(i+1)


    

print(max(max(ans1), max(ans2)))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

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

def gcd_list(numbers):
    return reduce(hcfnaive, numbers)
ans = []
  
for i in range(N):
  ans.append(gcd_list(A[i+1:]+A[:i]))
  
ans.sort()
print(ans[-1])

Submission status: Time Limit Exceeded

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


n = int(input())
a = list(map(int, input().split()))
ans = a[0]
for i in range(n):
    ans = gcd(i,ans)
for i in range(n):
    if i==0:
        t = a[1]
    else:
        t = a[0]
    for j in range(n):
        if j==i:
            continue
        t = gcd(a[j],t)
        if t<=ans:
            break
    ans = max(t,ans)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def gcdl(l):
    res = l[0]
    for i in l:
        res = gcd(res, i)
    return res

n = int(input())
ass = [int(x) for x in input().split()]

m = 1
for i in range(n):
    tmp = ass[:]
    tmp.pop(i)

    mm = gcdl(tmp)
    if mm > m:
        m = mm
        
print(m)

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 n in range(N):
    tmp = A.copy()
    _ = tmp.pop(n)
    tmp_gcd = gcd(tmp)
    if max_gcd < tmp_gcd:
        max_gcd = tmp_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
values = list(map(int, input().split()))
m = 1


def gcd(x, y):
    if x < y:
        x += y
        y = x - y
        x -= y

    while y > 0:
        r = x % y
        x = y
        y = r

    return x


for i in range(n):
    a = values[:]
    a.pop(i)
    g = a[0]
    for j in range(n-1):
        g = gcd(g, a[j])
    if m < g:
        m = g

print(m)

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=[int(x) for x in input().split()]
tmp=0
for i in range(n):
    gcd=gcd_list(a[1:])
    if gcd>tmp:
        tmp=gcd
    a.append(a.pop(0))
print(tmp)

Submission status: Time Limit Exceeded

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

for i in range(1, N):
    B.append(gcd(B[i - 1], A[i]))

ans = 0
for i in range(N):
    if i != 0:
        ans0 = B[i - 1]
    else:
        ans0 = A[1]
    for j in range(i + 1, N):
        ans0 = gcd(ans0, A[j])
    ans = max(ans, ans0)
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()))
ans=[]
for i in range(n):
  s=a.pop(i)
  x=reduce(gcd,a)
  ans.append(x)
  a.insert(i,s)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def check(ary, div):
    c = 0
    for i in range(len(ary)):
        if ary[i] % div == 0:
            c += 1
    if c >= len(ary) - 1:
        return True
    else:
        return False
 
N = int(input())
data = list(map(int, input().split()))
ans = 1
for i in range(1, max(data) + 1)[::-1]:
    if check(data, i):
        ans = i
        break
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().rstrip().split(' ')))
    def gcd_bw_except(left, right, skip):
        if right - left == 1:
            if left == skip:
                return None
            else:
                return a[left]
        else:
            return gcd(gcd_bw_except(left, (right - left)//2 + left , skip), gcd_bw_except((right - left)//2 + left, right, skip))
    max_gcd = 0
    for i in range(n):
        prev = gcd_bw_except(0, n, i)
        if max_gcd < prev:
            max_gcd = prev
    print(max_gcd)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#import math
import fractions as math

N = int(input())
A = list(map(int, input().split()))
memo = {}
x = A[1]
for a in A[2:]:
    n = math.gcd(x, a)
    memo[(x, a)] = n
    x = n
max_gcd = x

for i in range(N):
    x = A[0]
    for j in range(N):
        if i == j:
            pass
        else:
            if (x, A[j]) in memo:
                x = memo[(x, A[j])]
            else:
                n = math.gcd(x, A[j])
                memo[(x, A[j])] = n
                x = n
    max_gcd = max(max_gcd, x)
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
c=list(map(int,input().split()))
count=0
for i in range(max(c),0,-1):
  for j in range(n):
    if c[j]%i!=0:
      count=count+1
  if count==1 or count==0:
    print(i)
    break
  count=0

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


def mygcd(enum):
    if len(enum) == 1:
        return enum[0]
    ret = 0
    for i in range(len(enum)):
        if i == 0:
            ret = gcd(enum[0], enum[1])
        else:
            ret = gcd(ret, enum[i])
    return ret


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

ans = 0
for i in range(n):
    li = a[:i] + a[i+1:]
    po = mygcd(li)
    if ans < po:
        ans = po

print(ans)

Submission status: Time Limit Exceeded

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

from functools import reduce
def gcd(a,b):
    if a==0:
        return b 
    else:
        return gcd(b%a,a)
#A = [12, 24, 27, 30, 36]
#gcdp = reduce(lambda x,y:gcd(x,y),A)

B = []
for i in range(N):
  if(i == 0): G = A[i]
  else: G = gcd(G,A[i])
  a = A.copy()
  a.pop(i)
  b = reduce(lambda x,y:gcd(x,y),a)
  B.append(b)
  
ans = max(B)
ans = max(ans,G)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import functools

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

n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
    b = a[i]
    a.remove(b)
    c = functools.reduce(gcd, a)
    if ans < c:
        ans = c
    a.insert(i,b)
print(ans)

Submission status: Time Limit Exceeded

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

gcd_max = 1

if N == 2:
    print(max(A_array[0], A_array[1]))
    exit()
for i in range(N):
    gcd = 0
    for j, A in enumerate(A_array):
        if i == j:
            continue
        if gcd == 0:
            gcd = A
            continue
        # print(A,gcd)
        gcd = min(gcd, fractions.gcd(A, gcd))
        if gcd < gcd_max:
            continue
    gcd_max = max(gcd_max, gcd)
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np

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)
    return divisors
  
def set_list(es):
  return len(list(set(es))) == 1
  
n = int(input())
n_list = [int(i) for i in input().split()] 

np_list = np.array(n_list)
min1_idx = np.argmin(np_list)
min1 = min(np_list)
np_list[min1_idx] = 10000000001
min2_idx = np.argmin(np_list)
min2 = min(np_list)

div_list = make_divisors(min1) + make_divisors(min2)
div_set = set(div_list)

for s in reversed(sorted(list(div_set))):
  if set_list(n_list):
    print(n_list[0])
    break  
  count = 0
  j = 0
  for x in n_list:
    if n_list[j] % s == 0:
      #print(s)
      #print(n_list[j] % s)
      count += 1
    j += 1
  if count >= n - 1:
    print(s)
    break

Submission status: Time Limit Exceeded

Solution code:
import functools
import math

def GCD(m, n):
    while n:
        m, n = n, m % n
    return m

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

gcda = 0
for i in range(N):
    list2 = list1.copy()
    del list2[i]
    gcd = functools.reduce(GCD, list2)
    if gcd > gcda:
        gcda = gcd

print(gcda)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools

N = int(input())
A_ = list(map(int,input().split()))
gcd_max = 0

if N > 2:
  for i in range(N):
    A_list = [A_[j] for j in range(N) if j != i]
    tmp = fractions.gcd(A_list[0],A_list[1])
    for j in A_list[2:]:
      tmp = fractions.gcd(tmp,j)
    if tmp > gcd_max:
      gcd_max = tmp
else:
  gcd_max = max(A_[0],A_[1])
  
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import sys

sys.setrecursionlimit(1000000)

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

g_list = []

if len(l) == 2:
  g_list.append(max(l[0],l[1]))
  
else:
    g = fractions.gcd(l[1],l[2])
    for j in range(3,N):
        g = fractions.gcd(g,l[j])
    g_list.append(g)

    g = fractions.gcd(l[0],l[2])
    for j in range(3,N):
        g = fractions.gcd(g,l[j])
    g_list.append(g)

    for i in range(N):
        g = fractions.gcd(l[0],l[1])
        for j in range(N):
            if j != i:
                g = fractions.gcd(g,l[j])
        g_list.append(g)

print(max(g_list))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
n = int(input())
A = list(int(i) for i in (input().split()))

ans = 0
tmp = 0
for i in range(n):
  list = copy.copy(A)
  list.remove(A[i])
  tmp = gcd_list(list)
  if(ans < tmp):
    ans = tmp

print(ans)