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:
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

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))

    numdct = {}
    for i in range(n):
        for div in make_divisors(a[i]):
            if(not div in numdct):
                numdct[div] = 0
            numdct[div] += 1
    
    numlist = sorted(numdct.items(), key=lambda x: -x[1])

    maxnum = 0
    for item in numlist:
        if(item[1] >= n - 1):
            if(maxnum <= item[0]):
                maxnum = item[0]
        else:
            break

    print(maxnum)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

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

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

if N==2:
    print(max(A))
    exit()
else:
    max=0
    for i in range(N):
        x = copy.deepcopy(A)
        del x[i]
        element = gcd(x[0], x[1])
        if N==2:
            c = element
        else:
            for i in range(2,N-1):
                element=gcd(element,x[i])
            c = element
        if c >= max:
            max = c
print(max)

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 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:
# coding: utf-8

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

appear_D = {}


def sosuu(n):
    output = []
    for i in range(1, int(pow(n, 0.5))+1):
        if n % i == 0:
            output.append(i)
            if i != n // i:
                output.append(n // i)

    return output


ans_cantdidate = []
sosuus = sosuu(a[0]) + sosuu(a[1])
for num in sosuus:
    not_common = 0
    for i in range(n):
        if a[i] % num > 0:
            not_common += 1
        if not_common > 1:
            break
    if not_common < 2:
        ans_cantdidate.append(num)

print(max(ans_cantdidate))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2000)

def uclid_gcm(a, b):
  if a < b:
    a += b
    b = a-b
    a=a-b
  if b == 0:
    return a
  r = a%b
  return b if r == 0 else uclid_gcm(b, r)

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:
from functools import reduce

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

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

max = 0
n = int(input())
li = list(map(int, input().split()))

for i in range(n):
  x = li.copy()
  del x[i]
  g = gcd_list(x)
  if max < g:
    max = g

print(max)

Submission status: Time Limit Exceeded

Solution code:
import fractions

n = int(input())
a = list(map(int, input().split()))
t = []
a.sort()

for i in range(int(n/2) + 1):
    x = 0
    for j in range(n):
        if j != i:
            x = fractions.gcd(x, a[j])
    t.append(x)

print(max(t))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import fractions
 
 
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
 
res = []
 
for i in range(N):
    if i == 0:
        gcd_num = A[1]
    else:
        gcd_num = A[0]
    for j in range(N):
        if j == i:
            continue
        gcd_num = fractions.gcd(gcd_num, A[j])
    res.append(gcd_num)
        
 
 
print(max(res))

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()]
ans=gcd_list(a)
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 functools
import fractions
import copy

N = int(input())
A_list = list(map(int, input().split()))
vlaues = []
values_list = []

for i in range(N):
    values = copy.copy(A_list)
    del values[i]
    gcd = functools.reduce(fractions.gcd, values)
    values_list.append(gcd)

print(max(values_list))

Submission status: Time Limit Exceeded

Solution code:
import fractions


def solve(N, A):
    max_gcd = 0
    for i in range(N):
        lst = A[0:i]
        lst.extend(A[i + 1: N])
        gcd = lst[0]
        for j in range(1, len(lst)):
            gcd = fractions.gcd(gcd, lst[j])
        if gcd > max_gcd:
            max_gcd = gcd
    return max_gcd


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

Submission status: Time Limit Exceeded

Solution code:
import fractions as f
N = int(input())
As = list(map(int, input().split()))
ans = 0

for i in range(N):
    new_As = As.copy()
    new_As.pop(i)
    a = new_As[0]
    for j in range(1, N-1):
        b = new_As[j]
        a = f.gcd(a, b)
        if a == 1 or a < ans:
            break
    ans = max(ans, a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

ans = 0
if n == 2:
    ans = max(aList)
else:
    for i in range(0,n):
        tmp = aList.pop(0)
        r = gcd_numbers(aList)
        if ans < r:
            ans = r
        aList.append(tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

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

div = defaultdict(int)
for a in A:
    divisor = make_divisors(a)
    for d in divisor:
        div[d] += 1

for num, a in sorted(div.items(), reverse=True):
    if a > N - 2:
        print(num)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import copy

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):
  tmp = list[0]
  for i in range(len(list)):
    tmp = gcd(tmp, list[i])
  return tmp

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

b = copy.copy(a)
c = []

for i in range(n):
  del b[i]
  c.append(gcd_(b))
  b = copy.copy(a)
  
print(max(c))

Submission status: Time Limit Exceeded

Solution code:

from functools import reduce

def gcd(a,b):
  x = max([a,b])
  y = min([a,b])
  while y:
    x, y = y, x % y
  return x

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

n = int(input())

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


ans = 0
for i in range(n):
      tmp_list = a[:i] + (a[i+1:])
      #print(tmp_list)
      tmp = gcd_list(tmp_list)
      if ans < tmp:
            ans = tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions
from functools import reduce

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

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


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

g = []
for i in range(n):
    temp = copy.copy(a)
    del temp[i]
    g.append(gcd(*temp))

print(max(g))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

from fractions import gcd

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap



@mt
def slv(N, A):
  
    ans = 0
    for i in range(N):
        cand = A[0] if i != 0 else A[1]
        for j, a in enumerate(A):
            if i == j:
                continue
            cand = gcd(cand, a)
            if cand <= ans:
                break
        ans = max(ans, cand)
        
    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))

    # N  = 10**5
    # A = [10**9] * N
    # print(slv(N, A))


if __name__ == '__main__':
    main()

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.sort()
tmp_a = copy.deepcopy(a)
tmp_a.pop(0)
result = gcd_list(tmp_a)

for i in range(1, n):
    tmp_a = copy.deepcopy(a)
    tmp_a.pop(i)
    tmp = gcd_list(tmp_a)
    if(result < tmp):
        result = tmp
        break

print(result)

Submission status: Time Limit Exceeded

Solution code:
memo = {}

def gcd(a, b):
    if (a, b) in memo.keys():
        return memo[(a, b)]
    if b == 0:
        return a
    return gcd(b, a % b)

n = input()
a_list = map(int, raw_input().split())
max_g = 0
for exclude in xrange(n):
    g = a_list[0] if exclude > 0 else a_list[1]
    for i in xrange(1, n):
        if i == exclude:
            continue
        g_new = gcd(g, a_list[i])
        memo[(g, a_list[i])] = g_new
        g = g_new
        if g <= max_g:
            break
    if g > max_g:
        max_g = g
print(max_g)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

max_gcd = 0
for i in range(len(li)):
    if li[i] <= max_gcd:
        continue
    tmp = li[i]
    del li[i]
    tmp_gcd = gcd(*li)
    if max_gcd < tmp_gcd:
        max_gcd = tmp_gcd
    li.insert(0,tmp)

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
import fractions

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


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


def main():
    n = II()
    A = LI()
    A.sort()
    A.reverse()

    pre = [0] * n
    pre[0] = A[0]
    for i in range(1, n):
        pre[i] = fractions.gcd(pre[i - 1], A[i])
    res = pre[-1]
    for i in range(n):
        t = pre[i]
        for j in range(i + 2, n):
            t = fractions.gcd(t, A[j])
        res = max(res, t)

    return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

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

max = 0
n = int(input())
li = list(map(int, input().split()))

for i in range(n):
  x = li.copy()
  x.pop(i)
  g = gcd_list(x)
  if max < g:
    max = g

print(max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
gcds=[]

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

for i in range(len(l)):
    omitlist=list(l)
    omitlist.pop(i)
    n=len(omitlist)

    if n==1:
        gcds.append(omitlist[0])
    else:
        element = gcd(omitlist[0], omitlist[1])
        if n==2:
            gcds.append(element)
        else:
            for i in range(2,n):
                element=gcd(element,omitlist[i])
            gcds.append(element)

print(max(gcds))

Submission status: Time Limit Exceeded

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

from functools import reduce

def g():
  n = int(input())
  vs = list(map(int, input().split()))
  
  mr = 0
  for i in range(len(vs)):
    r = reduce(gcd, vs[:i]+vs[i+1:])
    mr = max(mr, r)
  return mr
  
print(g())

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import copy

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

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

def gcd_list(*numbers):
    return reduce(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:
from functools import reduce
import numpy as np

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

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:
import fractions
from functools import reduce

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

n = int(input())
a = list(map(int, input().split()))
b = list()
c = list()
for i in range(n):
    b = a[::]
    del b[i]
    c.append(gcd_list(b))
print(max(c))

Submission status: Time Limit Exceeded

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

wari_num = []
for i in range(1, max(a)+1):
    if max(a) % i == 0:
        wari_num.append(i)

one = 0
for i in wari_num[::-1]:
    for j in a:
        if j % i != 0:
            one += 1
            if one == 2:
                one = 0
                break

    if one == 1:
        print(i)
        exit()

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
def gcd(aa, bb):
	while bb:
		aa, bb = bb, aa % bb
	return aa

n = int(input())

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

ans = 0
for i in range(n):
    flag = False
    j = 0
    if i == 0:
        tmp = a[1]
    else:
        tmp = a[0]
    while not flag and j != n:
        if j != i:
            tmp = gcd(tmp, a[j])
        if tmp == 1:
            flag = True
        j += 1
    if tmp > ans:
        ans = tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

def func(i):
    res = []
    for j,a in enumerate(A):
        if i == j:
            continue
        res.append(a)
    return res

ans = 0

for i in range(N):
    res = gcd_list(func(i))
    if res > ans:
        ans = res

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

appear_D = {}


def sosuu(n):
    output = []
    for i in range(1, int(pow(n, 0.5))+1):
        if n % i == 0:
            output.append(i)
            if i != n // i:
                output.append(n // i)

    return output


ans_cantdidate = []
sosuus = sosuu(a[0]) + sosuu(a[1])
for num in sosuus:
    n_appear = 0
    for i in range(n):
        if a[i] % num == 0:
            n_appear += 1
    if n_appear >= n-1:
        ans_cantdidate.append(num)

print(max(ans_cantdidate))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

n = int(input())
a = list(map(int, input().split()))
y = []
for i in range(n):
    if i == 0:
        x = a[1:]
    elif i == n - 1:
        x = a[:-1]
    else:
        x = a[:i] + a[i + 1:]
    y.append(gcd_list(x))
print(max(y))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
import fractions
from functools import reduce
from itertools import combinations
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
combi = combinations(A,N-1)
ls = []
for t in combi:
    #print(t,gcd_list(t))
    ls.append(gcd_list(t))
print(max(ls))

Submission status: Time Limit Exceeded

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

num = 1

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

for i in range(N):
    B = A[:]
    B.pop(i)
    gcd1 = int(B[0])

    for j in range(1, N-1):
        gcd1 = gcd(gcd1, B[j])

    if num < gcd1:
        num = gcd1

print(num)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())

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


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


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


def gcd_list(li):
    g = gcd(li[0], li[1])
    for i in range(2, n):
        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:
 
 
def gcd(a, b):
	if a < b:
		a, b = b, a
	if b == 0:
		return a
	c = a % b
	return gcd(b, c)
 


n = int(input())
a = list(map(int,input().split()))
gcds = []
for i in range(n):
	listgcd = 0
	b = []
	for k in range(n):
		if (i != k):
			b.append(a[k])
	tmp = b[0]
	for bi in b:
		tmp = gcd(tmp,bi)
		
	gcds.append(tmp)
 
print (max(gcds))

Submission status: Time Limit Exceeded

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

from fractions import gcd
from functools import reduce

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

ans=[]
for i in range(N):
    B=A.copy()
    del B[i]
    ans.append(gcd_list(B))
    
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
from functools import reduce


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

def gcd_group(A):
    return reduce(gcd, A)


def solve(N: int, A: "List[int]"):
    ret = 0
    for i in range(N):
        temp = gcd_group(A[:i] + A[i+1:])
        if temp > ret:
            ret = temp
    print(ret)
    return


# Generated by 1.1.4 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:
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):
    if check(data, i):
        ans = i
 
print(ans)

Submission status: Time Limit Exceeded

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

def list_gcd(v_list):
    for i in range(len(v_list)):
        v_list[0] = gcd(v_list[0],v_list[i])
    return v_list[0]

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

max = 0
for i in range(n):
    tmp = v_list[:i]
    tmp.extend(v_list[i+1:])
    tmp_v = list_gcd(tmp)
    if tmp_v > max:
        max = tmp_v

print(max)

Submission status: Time Limit Exceeded

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

def gcd_list(numbers):
    gcdv = numbers[0]
    for n in range(1, len(numbers)):
        gcdv = gcd(gcdv, numbers[n])
    return gcdv

def main():
    n = int(input())
    a = [int(a) for a in input().split()]

    maxv = 0
    for nn in range(n):
        gcdv = gcd_list(a[:nn]+a[nn+1:])
        if gcdv > maxv:
            maxv = gcdv
    print(maxv)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

N = int(input())
A = [i for i in map(int,input().split())]
g = []
for i in range(N):
    ans = 0
    for j in range(N):
        if ans == 0 and i != j:
            ans = A[j]
            continue
        if i != j:
            ans = gcd(A[j],ans)
    g.append(ans)
print(max(g))

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()))

ans = 1
for i in range(N):
    x = A[:i] + A[i+1:]
    tmp = gcd(*x)
    ans = max(tmp,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# a>b
dic = {}

def gcd(a, b):
    key = (a, b)
    if key in dic:
        return dic[key]
    ans = 0
    if b == 0:
        ans = a
    else:
        ans = gcd(b, a%b)
    dic[key] = ans
    return ans

N = int(input())
As = [int(s) for s in input().split()]
ans = 0

for i in range(N):
    curr = None
    for j in range(N):
        if i==j:
            continue
        curr = As[j] if curr is None else gcd(max(curr, As[j]), min(curr, As[j]))
    ans = max(curr, ans)
    
print(ans)

Submission status: Time Limit Exceeded

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

MAX_A = 10**9

num = 1

for i in range(max(A), 1, -1):
    c = 0
    for j in range(N):
        if A[j] % i == 0:
            c += 1
    if c == N:
        num = i
        break
    elif c == N-1:
        num = i
        break


print(num)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

ans = 1
for i in range(N):
    pop_num = A.pop(i)
    ans = max(ans, reduce(fractions.gcd, A))
    A.insert(i, pop_num)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
n = int(input())
a = list(map(int, input().split()))
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
max_gcd = 0
for i in range(len(a)):
    b = a.copy()
    b.pop(i)
    if gcd_list(b) > max_gcd:
        max_gcd = gcd_list(b)
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

max_gcd = 0
for i in range(len(li)):
    tmp = li[i]
    del li[i]
    tmp_gcd = gcd(*li)
    if max_gcd < tmp_gcd:
        max_gcd = tmp_gcd
    li.insert(0,tmp)

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
print(max(reduce(gcd, (a[i] for i in range(n) if i != j)) for j in range(n)))

Submission status: Time Limit Exceeded

Solution code:
import functools
#import math

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

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

gcd_max = 0
for i in range(0, N):
    gcd_part = functools.reduce(gcd, A[:i]+A[i+1:])
    if gcd_max < gcd_part:
        gcd_max = gcd_part
    
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n = int( input())
a = list(map(int, input().split()))

def m_ds(n):
    ds = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            ds.append(i)
            if i != n // i:
                ds.append(n//i)

    return ds

h={}
for i in range(0,n):
  l = m_ds(a[i])
  for j in range(0,len(l)):
    if l[j] in h:
      h[l[j]]=h[l[j]]+1
    else:
      h[l[j]]=1
k=list(h.keys())
k.sort(reverse=True)
for v in k:
  if h[v]>=(n-1):
    print(v)
    break

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

ans = 1
for i in range(N):
    cand = gcd_list(A[:i] + A[i+1:])
    if cand > ans:
        ans = cand
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

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

div = defaultdict(list)
for i in range(len(A)):
    divisor = make_divisors(A[i])
    for d in divisor:
        div[d].append(i)

for num, a in sorted(div.items(), reverse=True):
    if len(a) > N - 2:
        print(num)
        exit()

Submission status: Time Limit Exceeded

Solution code:

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

    old = None
    for i in nums:
        if old is None:
            old = i
            continue

        old = gcd_internal(old, i)

    return old


def resolv(nums, cnt):
    result = []
    for i in range(cnt):
        result.append(gcd(nums[0:i] + nums[i+1:cnt]))
    return result


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

print(max(resolv(A, N)))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import fractions
 
 
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
 
res = []
 
 
for i in range(N):
    B = A[:]
    del B[i]
    gcd_num = B[0]
    for j in range(1, N-1):
        gcd_num = fractions.gcd(gcd_num, B[j])
    res.append(gcd_num)
        
 
 
print(max(res))

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)

ans = 0
for i in range(n):
    c = a.pop(0)
    b = gcd(a)
    if b > ans:
        ans = b
    a.append(c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
end = sorted(a)[1]

for i in range(end+1, 0, -1):
  x = 0
  for j in a:
    if j % i != 0:
      x += 1
  if x <= 1:
    print(i)
    break

Submission status: Time Limit Exceeded

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

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

m = 0

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

for i in range(n):
    c = copy(a)
    c.pop(i)
    s = gcd_list(c)
    if  s > m:
        m = s
    
print(m)

Submission status: Time Limit Exceeded

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

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

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

result = 0

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

print(result)

Submission status: Time Limit Exceeded

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

ans = 0
valid = []

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


for i in range(N):
  valid = A[:]
  valid.pop(i-1)
  valid_number = valid[0]
  for j in range(len(valid)):
    valid_number = gcd(valid_number,valid[j])
  ans = max(ans,valid_number)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools
from functools import reduce

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

n = int(input())
tup = tuple(map(int,input().split()))
max = 0
for i in tuple(itertools.combinations(tup,n-1)):
    l = gcd(*i)
    if max<l:
        max = l
        num = i
print(max)