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:
N = int(input())
A = list(map(int, input().split()))

from functools import reduce
import fractions

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

max_kys = 0

for i in range(N):
    B = A.copy()
    del B[i]
    max_kys = max(max_kys, gcd(B))

print(max_kys)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
from functools import reduce

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

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

# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

ka = 0
k = 0

for i in range(n):
    ac = copy.deepcopy(a)
    ac.pop(i)
    ka = gcd_list(ac)
    if ka > k:
        k = ka

print(k)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N =int(raw_input())
A =map(int ,raw_input().split())

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
def gcd2(A,S):
    r = gcd(A[0],A[1])
    for i in range(len(A)-2):
        while(r>S):
            r = gcd(A[i+2],r)
    return r
S = 0
if len(A) ==2:
    print int(max(A))
else:
    for i in range(N):
        k = A.pop(i)
        S =max(S,gcd2(A,S))
        A.insert(i,k)
    print int(S)

Submission status: Time Limit Exceeded

Solution code:
import math
import re

n = int(raw_input())
a = raw_input().split()
#s = list(raw_input())

cnt = 0
for i in a:
    a[cnt] = int(i)
    cnt += 1

chk = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            #divisors.append(i)
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                #divisors.append(n//i)
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    # divisors.sort()
    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in a:
        make_divisors(i)
    answer = 0
    for i in chk:
        if int(chk[i]) >= n - 1:
            answer = i
    print answer

Submission status: Time Limit Exceeded

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

from collections import defaultdict
import copy

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
                
    # divisors.sort()
    return divisors
    
max_divisor = 0
for a in A[:2]:
    divisors = make_divisors(a)
    
    for divisor in divisors:
        divisable = 0
        for a in A:
            if a % divisor == 0:
                divisable += 1
        
        if divisable >= N - 1:
            max_divisor = max(max_divisor, divisor)
            
            
print(max_divisor)

Submission status: Time Limit Exceeded

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

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(map(int, input().split()))

b = 1
for i in range(n):
    #a_tmp = []
    a_tmp = copy.copy(a)
    a_tmp.pop(i)
    b_tmp = gcd(*a_tmp)
    if b < b_tmp:
        b = b_tmp
print(b)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

def gcd(m, n):
    # 入力を m, n (m ≧ n) とする。
    tmp = max(m,n)
    n = min(m,n)
    m = tmp
    # n = 0 なら、 m を出力してアルゴリズムを終了する。
    if n == 0:
        return m
    # m を n で割った余りを新たに n とし、更に 元のnを新たにm とし 2. に戻る。
    return gcd(n, m % n)

def gcda(arr, left, right):
    return reduce((lambda a,b:gcd(a,b)), arr[left:right+1], gcd(arr[left], arr[right]))

n = int(input())
nums = [ int(s) for s in input().split(" ")]

if n == 2:
    print(max(nums))
    exit()

allgcd = gcda(nums, 0, n-1)

maxgcd = 0
for jogai in range(n):
    if(jogai == 0):
        maxgcd = max(maxgcd, gcda(nums, 1, n-1))
    elif(jogai == n-1):
        maxgcd = max(maxgcd, gcda(nums, 0, n-2))
    else:
        maxgcd = max(maxgcd,  gcd(gcda(nums, 0, jogai-1), gcda(nums, jogai+1, n-1)))

print(maxgcd)

Submission status: Time Limit Exceeded

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

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

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

if n < 6000:
  for i in range(n):
    b = copy.copy(a)
    b.pop(i)
    t = gcd(b)
    if ans < t:
      ans = t
else:
  for _ in range(200):
    t = gcd(random.sample(a, 50))
    if ans < t:
      ans = t

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
    c = a % b
    return gcd(b, c)


def list_gcd(a):
    a_gcd = a[0]
    for i in range(n-1):
        a_gcd = gcd(a_gcd, a[i+1])
    return a_gcd


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

for i in range((n+1)//2):
    b = a[:]
    b[i] = b[i-1]
    ans = max(ans, list_gcd(b))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()

def primes(N):
    sieve = [1]*(N+1)
    sieve[:2] = [0, 0]
    P = []
    for i in range(2, N+1):
        if sieve[i]:
            for j in range(2*i, N, i):
                sieve[j] = 0
            P.append(i)
    return P

P = primes(32000)
D = defaultdict(list)

for a in A:
    for p in P:
        cnt = 0
        while a%p==0:
            cnt += 1
            a //= p
        if cnt:
            D[p].append(cnt)
    if a > 1:
        D[a].append(1)

D = {k:[0]*(len(v) == N-1) + sorted(v) for k, v in D.items() if len(v) >= N-1}

ori = 1
for k, v in D.items():
    ori *= k**v[0]

ans = max(ori, 1)
for a in A:
    tmp = 1 * ori
    for p in D.keys():
        cnt = 0
        while a%p==0:
            cnt += 1
            a //= p
        if cnt == D[p][0]:
            tmp *= p**(D[p][1] - D[p][0])
    ans = max(ans, tmp)
print(ans)

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 = 1
for j in range(2,max(aa)+1):
    count = 0
    for i in range(n):
        if aa[i]%j==0: count +=1
    if count >= n-1:
        ans = max(ans,j)
        # print(ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# # 整数の入力
# a = int(input())
# # スペース区切りの整数の入力
# b, c = map(int, input().split())
# # 文字列の入力
# s = input()
# # 出力
# print("{} {}".format(a+b+c, s))
import numpy as np
def GCD(a,b):
    if a<b:
        tmp = a
        a = b
        b = tmp
    while True:
        r = a%b
        if r == 0:
            break
        a = b
        b = r
    return b

def split_GCD(list):
    if len(list) == 1:
        return list[0]
    elif len(list) == 2:
        return GCD(list[0],list[1])
    else:
        list_a = list[:len(list)//2]
        list_b = list[len(list)//2:]
        a = split_GCD(list_a)
        b = split_GCD(list_b)
        return(GCD(a,b))

N = int(input())
A = list(map(int,input().split()))	#1 2 3 4 5 ... n  li=[1,2,3,4,5,...,n]
r_li = []
for i in range(N):
    tmp_li = A.copy()
    tmp_li.pop(i)
    r_li.append(split_GCD(tmp_li))
print(max(r_li))

Submission status: Time Limit Exceeded

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

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

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

if n < 5000:
  for i in range(n):
    b = copy.copy(a)
    b.pop(i)
    t = gcd(b)
    if ans < t:
      ans = t
else:
  for _ in range(200):
    t = gcd(random.sample(a, 50))
    if ans < t:
      ans = t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
integerList = list(map(int, input().split()))

table = []
for s in integerList:
    miniTable = []
    for i in range(2,int(s**0.5)+1):
         if s % i == 0:
             miniTable.append(i)
             if i != s//i:
                 miniTable.append(s//i)
    miniTable.append(s)
    table.append(miniTable)
# print(table)

items = []
for v in table:
    for item in v:
        items.append(item)
#
# print(items)
kouyakusuList = []
for item in items:

    if items.count(item) >= N-1:
        kouyakusuList.append(item)
        # print(item)

print(max(kouyakusuList))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
from functools import reduce, lru_cache

@lru_cache(maxsize=10000)
def gcd(a, b):
    return reduce(fractions.gcd, [a, b])

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

ans = 1

for i in range(n):
    tmp_list = copy.copy(a)
    tmp_list.pop(i)
    tmp = tmp_list[0]
    for i in range(len(tmp_list)):
        tmp = gcd(tmp, tmp_list[i])
    if ans < tmp:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def getDivisor(x):
    ans = []
    for d in range(1, int(x**0.5)+1):
        if x % d == 0:
            ans.append(d)
            if x//d != d:
                ans.append(x//d)
    return sorted(ans)

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

divisors = getDivisor(As[0]) + getDivisor(As[1])
cnt = Counter(divisors)

for i, A in enumerate(As[2:], 2):
    for d, num in list(cnt.items()):
        if A%d == 0:
            cnt[d] += 1

    for d, num in list(cnt.items()):
        if num < i:
            del cnt[d]

print(max(cnt.keys()))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
N=int(input())
A=list(map(int, input().split()))
Z=0
ans=0
for i in range(N):
    if i==0:
        B=A.copy()
        B[0]=B[1]
        ans=functools.reduce(fractions.gcd, B)


    else :
        B=A.copy()
        B[i]=B[0]
        Z=B[0]
        Z =functools.reduce(fractions.gcd, B)


    if ans >Z:
        ans=ans
    else:
        ans=Z

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()

def primes(N):
    sieve = [1]*(N+1)
    sieve[:2] = [0, 0]
    P = []
    for i in range(2, N+1):
        if sieve[i]:
            for j in range(2*i, N, i):
                sieve[j] = 0
            P.append(i)
    return P

P = primes(32000)
D = defaultdict(list)

for a in A:
    for p in P:
        cnt = 0
        while a%p==0:
            cnt += 1
            a //= p
        if cnt:
            D[p].append(cnt)
    if a > 1:
        D[a].append(1)

D = {k:[0]*(len(v) == N-1) + sorted(v) for k, v in D.items() if len(v) >= N-1}

ori = 1
for k, v in D.items():
    ori *= k**v[0]

ans = max(ori, 1)
for a in A:
    tmp = 1 * ori
    for p in D.keys():
        cnt = 0
        while a%p==0:
            cnt += 1
            a //= p
        if cnt == D[p][0]:
            tmp *= p**(D[p][1] - D[p][0])
    ans = max(ans, tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
def find_gcd(list):
    x = reduce(gcd, list)
    return x

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

maxgcd = 0
for i in range(n):
  if i == 0:
    a2 = a[1:]
  elif i == n:
    a2 = a[:n-1]
  else:
  	a2 = a[:i] + a[i+1:]
    
  maxgcd = max(maxgcd, find_gcd(a2))
  
print(maxgcd)

Submission status: Time Limit Exceeded

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

from fractions import gcd
from functools import reduce
import functools

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

for i in range(N):
  if i < N:
    values = A[i+1:] + A[:i] 
  else:
    values = A[:-1]
  
  g = lambda a,b:a if b==0 else g(b,a%b)
  gcd = functools.reduce(lambda x,y:g(x,y),values)
  max_gcd = max(max_gcd,gcd)

  
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = [int(n) for n in input().split()]
enh = [1 for _ in range(N)]
maxn = 1

dev = 2
while True:
    cnt = [0 for _ in range(N)]

    for i in range(N):
        while A[i] % dev == 0:
            A[i] /= dev
            cnt[i] += 1

    arg = np.argsort(np.array(cnt))
    maxn *= dev ** cnt[arg[0]]
    enh[arg[0]] *= dev ** (cnt[arg[1]] - cnt[arg[0]])

    if dev == 2:
        dev = 3
    else:
        dev += 2

    if sum(A) == N:
        break

print(maxn * max(enh))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
import copy
def find_gcd(list):
    x = reduce(gcd, list)
    return x


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

ans=0
for i in range(n):
    b=copy.copy(a)
    del b[i]
    g=find_gcd(b)
    ans=max(ans,g)
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(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:
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 = (input()).split()
A = [int(a) for a in A]
max=0

for i in range(N):
    B = A.copy()
    del B[i]
    for j in range(N-1):
        if max < gcd(*B):
            max = gcd(*B)
            
print(max)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from fractions import gcd
from functools import reduce
def gcd_list(numbers):
    return reduce(gcd, numbers)
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
set_A = set(A)
ans=0

for i in range(N):
    for j in set_A:
        tmp = gcd_list([j]+A[0:i]+A[i+1:])
        if tmp > ans:
            ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
max_true = 0
N = int(input())
A = list(map(int,input().split()))

def gcd(big,small):
  while big % small:
    big, small = small, big % small
  return small

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

for i in range(N):
  a = A.copy()
  a.pop(i)
  m = gcd_list(a)
  if m > max_true:
    max_true = m
print(max_true)

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()))
gcd = []

for i in range(N):
  a = A[i]
  A[i] = 0
  gcd.append(gcd_list(A))
  A[i] = a
print(max(gcd))

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
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())
numlist = [ int(d) for d in input().split(" ")]
buflist = list()
maxdivlist = list()

maxdivlist.append(gcd_list(numlist))

for i in range(n):
	buflist = copy.copy(numlist)
	buflist.pop(i)
	maxdivlist.append(gcd_list(buflist))
print(max(maxdivlist))

Submission status: Time Limit Exceeded

Solution code:
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m


N = int(input())

A = list(map(int, input().split()))
max_A = max(A)
gcd_list = []
for i in range(N):
    tmp = A[i]
    A[i] = max_A
    hoge = gcd(A[0], A[1])
    for j in range(2, N):
        hoge = gcd(A[j], hoge)
    gcd_list.append(hoge)
    A[i] = tmp

print(max(gcd_list))
# 最大の値と変えてもいい

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools

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

al = list(set(list(itertools.combinations(l, n-1))))

ansl = []

for j in range(len(al)):
    a = list(al[j])
    ans = a[0]
    for i in range(1, n-1):
        ans = fractions.gcd(ans, a[i])
    ansl.append(ans)
    
print(max(ansl))

Submission status: Time Limit Exceeded

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

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

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

if n < 10000:
  for i in range(n):
    b = copy.copy(a)
    b.pop(i)
    t = gcd(b)
    if ans < t:
      ans = t
else:
  for _ in range(200):
    t = gcd(random.sample(a, 50))
    if ans < t:
      ans = t

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 % m for v in vs if v % m != 0] + [m]
        if len(vs) == 1:
            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:
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
count = 0
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:
#!/snap/bin/pypy3

def main():
    N = int(input())
    nums = list(map(int, input().split()))
    sorted_num = sorted(nums)
    res = 1
    i = sorted_num[1]
    while True:
        nums_div_i = [num % i for num in nums]
        # print(i, nums_div_i)
        _len = len(tuple(filter(lambda x: x > 0, nums_div_i)))
        if _len == 1 or _len == 0:
            break

        i -= 1

    print(i)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(2*10**9)

def main():
    N = int(input())
    A = sorted(input().split(" "))
    ans = [0]*N
    for i in range(N):
        tmp = A[i]
        if (i == N-1):
            A[i] = A[N-2]
        else :
            A[i] = A[N-1]
        ans.append(recursive_gcd(N,A))
        A[i] = tmp
    print(max(ans))


def recursive_gcd(n,array):
    if (n <= 2):
        return gcd_euclid(int(array[0]),int(array[1]))
    else:
        return gcd_euclid(recursive_gcd(n-1,array),int(array[n-1]))

# http://d.hatena.ne.jp/sh-2/20100320/1269039543 から引用
def gcd_euclid(u, v) :
  while (0 != v) :
    r = u % v
    u = v
    v = r
  return u
#引用ここまで

if __name__ == "__main__":
    main()

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())
ls = [int(i) for i in input().split()]

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

ans = gcd_list(ls)

ls = [i // ans for i in ls]

tempx = 1


for i in range(N):
	temp = ls[:]
	temp.pop(i)
	a = gcd(temp[0], temp[1])
	if a <= tempx:
		continue
	for j in range(2, N - 1):
		a = gcd(a, temp[j])
		if a <= tempx:
			break
	if a > tempx:
		tempx = a
print(ans * tempx)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools

N=int(input())
A=list(map(int,input().split()))
gcdA=functools.reduce(fractions.gcd,A)
ans=gcdA
B=A
for i in range(N):
    num=B[i]
    B.pop(i)
    gcdB=functools.reduce(fractions.gcd,B) 
    gcd=max(gcdA,gcdB)
    if gcd>ans:
        ans=gcd
        B.insert(i,num)
    else:
        B.insert(i,num)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
import fractions
from functools import reduce
 
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
# 整数の入力
a = int(input())
# 数値のリスト化
xList = [int(i) for i in input().split()]
keisanList = deepcopy(xList)
saidai = max(xList)
saidaiKoyakuSu = 0
koyakuSu = 0
listIndex = 0
 
for i in range(a):
    keisanList = deepcopy(xList)
    keisanList[i] = saidai
    koyakuSu = gcd_list(keisanList)
    if(saidaiKoyakuSu < koyakuSu):
        saidaiKoyakuSu = koyakuSu
print(saidaiKoyakuSu)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
N = input()
A = map(int, raw_input().split())
B = []
for i in range(N):
	t = 0
	for j in range(N):
		if(i == j):
			continue
		else:
			if(t == 0):
				t = A[j]
			else:
				t = gcd(t,A[j])
		if(t == 1):
			break
	B.append(t)
print(max(B))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools

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

answer = 0
for i in range(N):
  tmp = A[:]
  tmp.pop(i)
  m = functools.reduce(fractions.gcd, tmp)
  if answer < m:
    answer = m
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N =int(raw_input())
A =map(int ,raw_input().split())

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
def gcd2(A,S):
    r = gcd(A[0],A[1])
    for i in range(len(A)-2):
        r = gcd(A[i+2],r)
        if r<=S:
            break
    return r
S = 0
if len(A) ==2:
    print int(max(A))
else:
    for i in range(N):
        k = A.pop(i)
        S =max(S,gcd2(A,S))
        A.insert(i,k)
    print int(S)

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

a = sorted(a)
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 <= ans:
            flag = True
        j += 1
    if tmp > ans:
        ans = tmp
    if ans >= a[0]:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for x in input().split():
    a.append(int(x))

ans = []

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

def gcd_list(numbers):
    element = gcd(numbers[0], numbers[1])
    if len(numbers)==2:
        return element
    else:
        for i in range(2, len(numbers)):
            element=gcd(element, numbers[i])
        return element

if n == 2:
    print(max(a))
else:
    for i in range(n):
        c = a.copy()
        c.remove(a[i])
        ans.append(gcd_list(c))

    print(max(ans))

Submission status: Time Limit Exceeded

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

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

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

for i in range(n):
  b = copy.copy(a)
  b.pop(i)
  t = gcd(b)
  if ans < t:
    ans = t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc125/tasks/abc125_c

import itertools
from collections import Counter
from collections import defaultdict
import bisect

memo = {}


def gcd(a, b):
    if a in memo:
        if b in memo[a]:
            return memo[a][b]
    ans = gcd_core(a, b)
    if a in memo:
        memo[a][b] = ans
    else:
        memo[a] = {}
        memo[a][b] = ans
    return ans


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


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

    answers = []
    # half
    m = N // 2
    ans1 = 0
    for i in range(m):
        ans1 = gcd(A[i], ans1)

    ans2 = 0
    for i in range(m, N):
        ans2 = gcd(A[i], ans2)

    for i in range(m, N):
        ans = ans1
        for j in range(m, N):
            if i == j:
                continue
            ans = gcd(A[j], ans)
        answers.append(ans)

    for i in range(m):
        ans = ans2
        for j in range(m):
            if i == j:
                continue
            ans = gcd(A[j], ans)
        answers.append(ans)

    print(max(answers))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import functools
n = int(input())
a_list = list(map(int,input().split()))

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

res = []
tmp = []
for _ in range(n):
    e = a_list.pop(0)
    if e in tmp:
        continue
    res.append(functools.reduce(gcd, a_list))
    a_list.append(e)
    tmp.append(e)

print(max(res))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

number_of_numbers = int(input())
number_list = list(map(int, input().split()))

number_list.sort()
number_list.reverse()
# i = 1~number_list[0]の値をとる
i = number_list[0]

while i > 0:
    number_of_disable = 0 #割り切れなかった値の数
    
    for number in number_list:
        if number%i != 0:
            number_of_disable += 1
        
        if number_of_disable > 1:
            break
    if number_of_disable <= 1:
        print(i)
        break
    i -= 1

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions
import functools


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


n = int(input())
a = list(map(int, input().split()))
nl = set(range(n))
gcd_list = [0]
for i in nl:
    tmp = copy.deepcopy(a)
    tmp.pop(i)
    tmp2 = gcd(*tmp)
    gcd_list.append(tmp2)
ans = max(gcd_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()])
A.sort()
for i in range(A[1], -1, -1):
    if len(A[np.where(A % i == 0)]) >= len(A)-1:
        res = i
        break

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
def plane_gcd(a,b):
	alp=max(a,b)
	bet=min(a,b)
	while(1):
		if alp%bet==0:
			return bet
		else:
			r=alp%bet
			alp=bet
			bet=r

def list_gcd(li):
	ans=plane_gcd(li[0],li[1])
	for i in range(2,len(li)):
		tmp=plane_gcd(li[i],ans)
		if tmp<ans:
			ans=tmp
	return ans

n=int(input())
a=list(map(int,input().split()))
if n==2:
	print(max(a))
	sys.exit()
data=[]
for i in range(n):
	if i==0:
		data.append(list_gcd(a[1:n]))
	elif i==n-1:
		data.append(list_gcd(a[0:n-1]))
	else:
		data.append(list_gcd(a[0:i]+a[i+1:n]))

data.sort(reverse=True)
if data[0]!=data[1]:
	print(data[0])
else:
	print(data[1])

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
  else:
    # N = 3
    # A = [7,6,8]
    # N = 3
    # A = [12,15,18]
    N = 2
    A = [1000000000,1000000000]

  x = 0
  for i in range(N):
    tmp = functools.reduce(fractions.gcd, A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

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

max_gcd = gcd_list(a)

for i in range(n):
    b = [a[j] if i != j else 0 for j in range(n)]
    gcd_b = gcd_list(b)
    if gcd_b > max_gcd:
        max_gcd = gcd_b

print(max_gcd)

Submission status: Time Limit Exceeded

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
ans=[]

b=a[1:]
ans.append(gcd_list(b))

for i in range(1,n):
    b=a[:i]+a[i+1:]
    ans.append(gcd_list(b))

ans.sort()
print(ans[n-1])

Submission status: Time Limit Exceeded

Solution code:
import fractions

N=int(input())
A=list(map(int, input().split()))
Z=0
for i in range(N):
    if i==0:
        ans=A[1]
        for j in range(2,N):
            ans=fractions.gcd(ans,A[j])


    else :
        B=A.copy()
        B[i]=B[0]
        Z=B[0]
        for k in range(1,N):
            Z =fractions.gcd(Z,B[k])


    if ans >Z:
        ans=ans
    else:
        ans=Z

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations, product

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    A = il()
    ans = 0
    for n in combinations(A, N - 1):
        ans = max(ans, reduce(gcd, n))
    return ans


if __name__ == "__main__":
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
maxgcd = 1
gcd = 0
x = 0
y = 0
r = 0
for j in range(0,n):
    gcd = int(a[1])
    if r != 0:
        gcd = int(a[0])
    r = 1
    for i in range(0,n):
        if i != j:
            x = int(a[i])
            y = gcd
            if (x < y):
                e = x
                x = y
                y = e
            f = True
            while f:
                while x >= 0:
                    x = x-y
                x = x + y
                if x == 0:
                    f = False
                else:
                    e = x
                    x = y
                    y = e
            gcd = y
    if gcd > maxgcd:
        maxgcd = gcd
print(maxgcd)

Submission status: Time Limit Exceeded

Solution code:
def gcd(m,n):
  x=max(m,n)
  y=min(m,n)
  if x==0:
    return 0
  while y!=0:
    x,y=y,x%y
  return x
 
n = int(input())
a = list(map(int, input().split()))
b = [0]
 
for i in range(n):
  a[i],a[n-1]=a[n-1],a[i]
  temp = a[0]
  for j in range(n-1):
    temp=gcd(temp,a[j])
    if temp < max(b):
      break
  b.append(temp)
  a[i],a[n-1]=a[n-1],a[i]
  
print(max(b))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd


def gcd4args(args):
    return reduce(gcd, args)


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

ans = 0
for i in range(N):
    lstTmp = lstA[0:i] + lstA[i+1:]
    ans = max(ans, gcd4args(lstTmp))
print(ans)