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

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

r = {}

for j in a:
    for i in range(1, int(j**0.5)+1):
        if j % i == 0:
            if i in r.keys():
                r[i] += 1
            else:
                r[i] = 1
            if i != j // i:
                if j//i in r.keys():
                    r[j//i] += 1
                else:
                    r[j//i] = 1
m = n - 1
r_a = sorted([i[0] for i in r.items() if i[1] >= m], reverse=True)
print(r_a[0])

Submission status: Time Limit Exceeded

Solution code:
import fractions
N=int(input())
A=list(map(int,input().split()))
t=0
ans=[0]*N
while t<N:
    u=0
    if t!=0:
        ans[t]=A[0]
        while u<N-1:
            if (u+1)!=t:
                ans[t]=fractions.gcd(ans[t],A[u+1])
            u=u+1
    else:
        ans[t]=A[1]
        y=1
        while y<N-1:
            ans[t]=fractions.gcd(ans[t],A[y+1])
            y=y+1
    t=t+1
print(max(ans))

Submission status: Time Limit Exceeded

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

def factorize(x):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    d = 2
    while x > 1:
        c = 0
        while x % d == 0:
            c += 1
            x //= d
        f[d] = c
        d += 1
        if d * d > x > d:
            d = x
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    # l = [min(ls[:i]) for i in range(len(ls))]
    l = [float('inf')] * len(ls)
    for i in range(1, len(ls)):
        l[i] = min(l[i - 1], ls[i - 1])
    # r = [min(ls[i + 1:]) for i in range(len(ls))]
    r = [float('inf')] * len(ls)
    for i in range(len(ls) - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p

# 各A_iの素因数
F = [factorize(a) for a in A]

# 素因数の集合 (2要素に出現しない素因数は無視)
D =  set(F[0].keys())
D.update(F[1].keys())

# Dの要素 d → dが各A_iに含まれる個数のリスト
X = {d: [f[d] if d in f else 0 for f in F] for d in D}

# Dの要素 d → A_iを削除したときのdの個数の最小値のリスト
X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D if X[d][i] > 0) for i in range(N)]
print(max(S))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

from functools import reduce
def gcd_list(n):
    return reduce(gcd, n)

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

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

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
from functools import reduce
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

n=int(input())
v=[int(x) for x in input().split()]
c=0
for i in range(n):
    d=v[i]
    v[i]=0
    l=gcd_list(v)
    if l>int(c):
        c=l
    v[i]=d
print(int(c))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools


n = int(input())
a = list(map(int, input().split()))
nl = set(range(n))
gcd_list = [0]
for i in nl:
    tmp, a[i] = a[i], 0
    gcd_list.append(functools.reduce(fractions.gcd, a))
    a[i] = tmp
ans = max(gcd_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

divisor = []
for i in range(N):
    for j in range(1,(int(A[i])+1)):
        if A[i] % j == 0:
            divisor.append(j)

c = collections.Counter(divisor)
count_divisor = c.most_common()

max = 1
for i in range(len(count_divisor)):
    if count_divisor[i][1] >= N-1:
        if count_divisor[i][0] >= max:
            max = count_divisor[i][0]
    else:
        break
print(max)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
inpl = lambda: list(map(int,input().split()))

def factorize(n):
    i = 2
    primes = []
    powers = []
    while i * i <= n:
        if n % i == 0:
            k = 0
            while n % i == 0:
                n //= i
                k += 1
            primes.append(i)
            powers.append(k)
        i += 1
    if n > 1:
        primes.append(n)
        powers.append(1)
    return primes, powers

N = int(input())
A = inpl()
infty = 100
lowest_powers = defaultdict(lambda: [infty,infty])
bottle_necks = defaultdict(lambda: -1)
all_factors = set()
for n in range(N):
    primes, powers = factorize(A[n])
    prev_primes = list(bottle_necks.keys())
    done = defaultdict(int)
    for i in range(len(primes)):
        done[primes[i]] = 1
        if not primes[i] in all_factors:
            all_factors.add(primes[i])
            if n == 1:
                lowest_powers[primes[i]] = [0,powers[i]]
                bottle_necks[primes[i]] = 0
            elif n == 0:
                lowest_powers[primes[i]] = [powers[i],infty]
                bottle_necks[primes[i]] = 0
        lowest = lowest_powers[primes[i]]
        if powers[i] == lowest[0]:
            lowest[1] = lowest[0]
            bottle_necks[primes[i]] = -1
        elif powers[i] < lowest[0]:
            lowest[1] = lowest[0]
            lowest[0] = powers[i]
            bottle_necks[primes[i]] = n
        elif powers[i] < lowest[1]:
            lowest[1] = powers[i]
    for p in all_factors:
        if done[p] == 0:
            lowest = lowest_powers[p]
            if lowest[0] == 0:
                lowest[1] = 0
                bottle_necks[p] = -1
            else:
                lowest[1] = lowest[0]
                lowest[0] = 0
                bottle_necks[p] = n

bnl = defaultdict(lambda: [])
for p,n in bottle_necks.items():
    if n >= 0:
        lowest = lowest_powers[p]
        bnl[n].append([p,lowest[1]-lowest[0]])
best_n = -1
largest_f = 1
for n in range(N):
    f = 1
    for pp in bnl[n]:
        f *= pp[0]**pp[1]
    if f > largest_f:
        largest_f = f
        best_n = n

ans = 1
bn = bnl[best_n]
pn = [ s[0] for s in bn]
for p in all_factors:
    if p in pn:
        ans *= p**lowest_powers[p][1]
    else:
        ans *= p**lowest_powers[p][0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import fractions
import copy
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
  b=copy.deepcopy(a)
  del b[i]
  ans=max(ans, reduce(fractions.gcd,b))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = [int(i) for i in input().split()]
def calc(a, b):
    r = a % b
    while(r != 0):
        a = b
        b = r
        r = a % b
    return b
answer = 0

for i in range(len(As)):
    As_copy = As.copy()
    res = As_copy.pop(i)
    if i == N-1:
        i = 0
    _ = As_copy.pop(i)
    for j in As_copy:
        if i == j:
            continue
        temp = calc(res, j)
        res = temp
    if res > answer:
        answer = res
print(answer)

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]
#     print(B)
    gdc = gcd(*B)
    if max < gdc:
        max = gdc
            
print(max)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
# a = 
# b = 10
# gcd_list = []
# for i in range(n-1):
#     gcd_list.appned(gcd(a[i],a[i+1]))
# divisors_ll = []
# def gcd(n, a):

#     a_list = []
#     b_list = []
#     for i in range(1,a+1):
#         a_list.append(int(a//i))
#     for i in range(1,b+1):
#         b_list.append(int(b//i))
#     set_list = list(set(a_list) & set(b_list))
#     return max(set_list))
gcd_l = []
divisors_ll = []
for i in range(len(a)):
    divisors_ll.append([int(a[i]//j) for j in range(1,a[i]+1) if a[i]%j == 0])
for i in range(len(a)):
    divisors_ll_copy = divisors_ll.copy()
    divisors_ll_copy.pop(i)
    divisors_ll_copy
    tmp_l = [flatten for inner in divisors_ll_copy for flatten in inner]
    c = collections.Counter(tmp_l)
    tmp = []
    for v, count in c.most_common():
        if count == (len(a)-1):
            tmp.append(v)
    gcd_l.append(max(tmp))
print(max(gcd_l))

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


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import fractions
N = int(input())
numbers = np.array(list(map(int, input().split())))
# numbers = np.array([7, 6, 8])
# numbers = np.array([12, 15, 18])
# numbers = np.array([1000000000, 1000000000])
# N = len(numbers)
gcd = np.frompyfunc(fractions.gcd, 2, 1)


def calculate_maximum_sub_gcd(numbers):
    sub_gcds = np.array([sub_gcd(numbers[:i], numbers[i+1:]) for i in range(N)])
    return np.max(sub_gcds)


def sub_gcd(left, right):
    if not left.size > 0:
        return np.ufunc.reduce(gcd, right)
    if not right.size > 0:
        return np.ufunc.reduce(gcd, left)
    else:
        subleft = np.ufunc.reduce(gcd, left)
        subright = np.ufunc.reduce(gcd, right)
        return gcd(subleft, subright)


print(calculate_maximum_sub_gcd(numbers))

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:]
    tmp = lstA[i]
    if i < N-1:
        lstA[i] = lstA[i+1]
        # print(lstA)
        ans = max(ans, gcd4args(lstA))
    else:
        lstA[i] = lstA[i-1]
        # print(lstA)
        ans = max(ans, gcd4args(lstA))
    lstA[i] = tmp
    # print(lstA)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

for i in range(n):
    if i == 0:
        b = a[1:]
    elif i == n-1:
        b = a[:n-1]
    else:
        tmp1 = a[:i]
        tmp2 = a[i+1:]
        b = []
        b.extend(tmp1)
        b.extend(tmp2)
    current = b[0]
    for j in range(n-1):
        current = fractions.gcd(current, b[j])
    if max < current:
        max = current
print(max)

Submission status: Time Limit Exceeded

Solution code:
import sys
import io
import math
import re

datalist = input().split()
num_summit = int(datalist[0])

array_1 = []
array_1 = input().split()
array_1.sort(reverse=True)
flag = 0
a = 0
b = 0
swap_data = 0
c = 0
sum_data = 0

for num1 in range(int(array_1[0])+1):
    flag = 0
    for num2 in range(num_summit):
        if int(array_1[num2]) % (num1+1) != 0:
          flag += 1
    if flag < 2:
        if num1 + 1 > c:
            c = num1 +1
print(c)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def gcd(s):
    list=[]
    for j in range(len(s)):
        a=0
        b=0
        temp=10000000000000000000
        v=[]
        if len(s)>2:
            for i in range(len(s)):
                if i!=j:
                    x=s[i]
                    v.append(x)
            for i in range(len(v)):
                if i==0:
                    a=v[i]
                    b=v[i+1]
                else:
                    b=v[i]
                if a<b:
                    temp=a
                    a=b
                    b=temp
                while b!=0:
                    temp=a%b
                    a=b
                    b=temp
            list.append(a)
        else:
            list.append(max(s))
    return(max(list))


n=int(input())

v=[]
line=input().split()

for i in range(n):
    v.append(int(line[i]))

print(gcd(v))

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)


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(reverse=True)
    return divisors

n = int(input())
a = list(map(int, input().split()))
a_div = [make_divisors(a[i]) for i in range(n)]

flag=False

for i in range(100000):
    out = 0
    tmp = a_div[0][i]
    for j in range(n):
        if tmp not in a_div[j]:
            out += 1
    if out <= 1:
        break

a_tmp = a[:]
del a_tmp[0]
temp = gcd_list(a_tmp)

print(max(temp,tmp))

Submission status: Time Limit Exceeded

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

n=100

from fractions import gcd
from functools import reduce
gs = [reduce(gcd, A[100*i:100*(i+1)]) for i in range((N-1)//100 + 1)]

answer = 0

for i in range(N):
  k = i%100
  i = (i//100)
  
  g = reduce(gcd, A[i*100:k] + A[k+1:(i+1)*100] + gs[0:i] + gs[i+1:])

  answer = max(answer, g)

print(answer)

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)


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(reverse=True)
    return divisors

n = int(input())
a = list(map(int, input().split()))
a_div = [make_divisors(a[i]) for i in range(n)]

flag=False

for i in range(10000):
    out = 0
    tmp = a_div[0][i]
    for j in range(n):
        if tmp not in a_div[j]:
            out += 1
    if out <= 1:
        break

a_tmp = a[:]
del a_tmp[0]
temp = gcd_list(a_tmp)

print(max(temp,tmp))

Submission status: Time Limit Exceeded

Solution code:
import sys
def gcd(num_list,a,N):
  try:
    return max([ i for i in range(min(num_list),a,-1) if len([0 for o in num_list if o%i == 0]) == N or len([0 for o in num_list if o%i == 0]) == N-1])
  except ValueError:
    return 1
  
def gcd2(num_list,a,N):
  try:
    return max([ i for i in range(min(num_list),a,-1) if len([0 for o in num_list if o%i == 0]) == N-1])
  except ValueError:
    return 1

N = int(sys.stdin.readline())
A = list(map(int,sys.stdin.readline().split()))
ans1 = gcd(A,0,N)
temp = min(A)
A.remove(min(A))
ans2 = gcd2(A,temp,N)
print(max(ans1,ans2))

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())
numbers=list(map(int,input().split()))
GCD_candidate=list()
for i in range(0,N):
    trap=numbers[i]
    numbers[i]=1
    BALOON=max(numbers)
    numbers[i]=BALOON
    GCD=gcd_list(numbers)
    GCD_candidate.append(GCD)
    numbers[i]=trap

MAX=max(GCD_candidate)
print(MAX)

Submission status: Time Limit Exceeded

Solution code:
import copy


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

n = int(input())
a = list(map(int,input().split())) #input from array of integer like "1 2 3"
dic = {}
ans = []
max = 0

for i in range(n):
  l = copy.deepcopy(a)
  l.pop(i)
  res = l[0]
  for c in l[1::]:
    xx = (res, c)
    if xx in dic:
      res = dic[xx]
    else:
      res = gcd(res, c)
      dic2 = {xx:res}
      dic.update(dic2)
  ans.append(res)
  if max < res:
    max = res

print(max)

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
N = int(input())
al = [int(a) for a in input().split()]

def gcd(a, b):
    while b > 0:
        a, b = b, a%b
    return a
  
ans_l = [0]*N

for i in range(N):
    tmp2 = copy(al)
    del tmp2[i]
    a = tmp2[0]
    for j in range(N-1):
        a = gcd(a, tmp2[j])
    ans_l[i] = a

print(max(ans_l))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	N = int(input())
	A = input().split()
	A = [int(s) for s in A]

	base = -1
	for i, val in enumerate(A):
		if i == 0:
			base == val
			continue
		base = gcd(val, base)

	ans = base
	for i, val in enumerate(A):
		tmp = -1
		if ans >= val or gcd(val, tmp) == ans:
			break
		for j, vol in enumerate(A):
			if i == j:
				continue
 
			if tmp == -1:
				tmp = vol
				continue
 
			tmp = gcd(vol, tmp)
			if tmp <= ans:
				break
 
		if tmp > ans:
			ans = tmp
 
	print(ans)
 
 
 
def gcd(a, b):
	if a > b:
		a, b = b, a
		
	while a > 0:
		a, b = b % a, a
	return b
	
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import fractions
N = int(input())
numbers = np.array(list(map(int, input().split())))
# numbers = np.array([7, 6, 8])
# numbers = np.array([12, 15, 18])
# numbers = np.array([1000000000, 1000000000])
# N = len(numbers)


def calculate_maximum_sub_gcd(numbers):
    sub_gcds = np.array([sub_gcd(numbers[:i], numbers[i+1:]) for i in range(N)])
    return np.max(sub_gcds)


def sub_gcd(left, right):
    gcd = np.frompyfunc(fractions.gcd, 2, 1)
    if not left.size > 0:
        return np.ufunc.reduce(gcd, right)
    if not right.size > 0:
        return np.ufunc.reduce(gcd, left)
    else:
        subleft = np.ufunc.reduce(gcd, left)
        subright = np.ufunc.reduce(gcd, right)
        return gcd(subleft, subright)


print(calculate_maximum_sub_gcd(numbers))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
gcd_list = []
min_element = min(A)
max_element = max(A)

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

for i in range(n):
    y = A[i]
    for j in range(min_element, max_element + 1):
        A[i] = j
        x = A[0]
        for k in range(1, n):
            x = gcd(x, A[k])
            if x == 1:
                break
        gcd_list.append(x)
    A[i] = y
print(max(gcd_list))

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 == 1:
      break
    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:
def GCD(a, b):
    while a % b != 0:
        r = a % b
        a, b = b, r
    return b

ans = 0
N = int(input())
A = tuple(map(int,input().split()))
for S in range(len(A)):
    Af = 0
    for i in range(len(A)):
        if S != i:
            Af = GCD(Af,A[i])
    ans = max(ans,Af)

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

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

Submission status: Time Limit Exceeded

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

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

ans = 0
pre_a = -1
pre_gcd = a[0]

if n > 1000:
    for i in range(n):
        if i >= 1:
            pre_gcd = functools.reduce(gcd, [pre_gcd, a[i-1]])
        values = a[i+1:]
        values.append(pre_gcd)

        res = functools.reduce(gcd, values)
        ans = max(ans, res)
        pre_a = a[i]
else:
    for i in range(n):
        if pre_a != a[i]:
            values = a[:i] + a[i+1:]
            res = functools.reduce(gcd, values)
            ans = max(ans, res)
        pre_a = a[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

gcd_left = []
gcd_left.append(A[0])
gcd_right = []
gcd_right.append(A[N-1])
for i in range(N-1):
    gcd_left.append(fractions.gcd(gcd_left[i],A[i+1]))
    gcd_right.insert(0,fractions.gcd(gcd_right[0],A[N-2-i]))

min_gcd = 1
tmp_left = 0
tmp_right = 0
if min_gcd < gcd_right[1]:
    min_gcd = gcd_right[1]
if min_gcd < gcd_left[N-2]:
    min_gcd = gcd_left[N-2]
for i in range(0,N-2):
    tmp_left = gcd_left[i]
    tmp_right = gcd_right[i+2]
    tmp = fractions.gcd(tmp_left,tmp_right)
    if tmp > min_gcd:
        min_gcd = tmp
        if tmp_left == 1 or tmp_right == 1:
            break;
print(min_gcd)

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)


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(reverse=True)
    return divisors

n = int(input())
a = list(map(int, input().split()))
a_div = [make_divisors(a[i]) for i in range(n)]

flag=False

for i in range(1000000):
    out = 0
    tmp = a_div[0][i]
    for j in range(n):
        if tmp not in a_div[j]:
            out += 1
    if out <= 1:
        break

a_tmp = a[:]
del a_tmp[0]
temp = gcd_list(a_tmp)

print(max(temp,tmp))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

n = int(input())
list_a = list(map(int, input().split()))
list_a.sort()
result = reduce(gcd, list_a)

if result != min(list_a):
    for i in range(n):
        copied = list_a.copy()
        del copied[i]
        res = reduce(gcd, copied) if len(copied) > 1 else copied[0]
        if res > result:
            result = res
            if i != 1:
                break

print(result)

Submission status: Time Limit Exceeded

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

B = {}
C = []
for i in range(N):
    if i in B and B[str(i)] >= 2:
        continue
    if i not in B:
        B[str(i)] = 1
    else:
        B[i] += 1
    C.append(A[i])

if len(B.keys()) == 2:
    print(max(C[0], C[1]))
    sys.exit()

N = len(C)
A = C

if N == 2:
    print(max(A[0], A[1]))
else:
    m = min(A)
    ans = 1
    for i in range(m, 1, -1):
        count = 0
        for n in range(N):
            if A[n] % i != 0:
                count += 1
                if count > 1:
                    break
        if count <= 1:
            ans = i
            break

    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(sa[1], 0, -1):
  cnt = 0
  for j in range(n):
    if a[j] % i == 0:
      cnt += 1
  if cnt >= n - 1:
    print(i)
    break

Submission status: Time Limit Exceeded

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

from fractions import gcd
from functools import reduce
gs = [reduce(gcd, A[300*i:300*(i+1)]) for i in range((N-1)//300 + 1)]

answer = 0

for i in range(N):
  k = i%300
  i = (i//300)
  
  g = reduce(gcd, A[i*300:k] + A[k+1:(i+1)*300] + gs[0:i] + gs[i+1:])

  answer = max(answer, g)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from itertools import combinations
from functools import reduce

N = int(input())
A = map(int, input().split())
ans = 0
for a in combinations(A,N-1):
    tmp = reduce(gcd,a)
    ans = tmp if ans < tmp else ans

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
mi_A=min(A)
count=0
bad=0
for j in range(N-1,-1,-1):
  if A[j]%mi_A==0:
    count=count+1
  elif bad==1:
    break
  else:
    bad=bad+1
if count>=N-1:
  ans=mi_A 
else:
  for i in range(mi_A//2+1,0,-1):
    if mi_A%i==0:
      count=0
      bad=0
      for j in range(N-1,-1,-1):
        if A[j]%i==0:
          count=count+1
        elif bad==1:
          break
        else:
          bad=bad+1
      if count>=N-1:
        ans=i
        break
A.remove(min(A))
if len(A)==N-1:
  mi_A=min(A)
  count=0
  bad=0
  for j in range(0,N-1):
    if A[j]%mi_A==0:
      count=count+1
    else:
      break
  if count>=N-1:
    ans=mi_A    
  else:
    for i in range(mi_A//2+1,0,-1):
      if mi_A%i==0:   
        count=0
        bad=0
        for j in range(0,N-1):        
          if A[j]%i==0:
            count=count+1         
          else:
            break
        if (count>=N-1) and (i>ans):
          ans=i
          break
print(ans)

Submission status: Time Limit Exceeded

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

def factorize(x):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    d = 2
    while x > 1:
        while x % d == 0:
            if d not in f:
                f[d] = 0
            f[d] += 1
            x //= d
        if d * d > x > d:
            f[x] = 1
            break
        d += 1
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    # l = [min(ls[:i]) for i in range(len(ls))]
    l = [float('inf')] * len(ls)
    for i in range(1, len(ls)):
        l[i] = min(l[i - 1], ls[i - 1])
    # r = [min(ls[i + 1:]) for i in range(len(ls))]
    r = [float('inf')] * len(ls)
    for i in range(len(ls) - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p

# 各A_iの素因数
F = [factorize(a) for a in A]

# 素因数の集合 (2要素に出現しない素因数は無視)
D =  set(F[0].keys())
D.update(F[1].keys())

# Dの要素 d → dが各A_iに含まれる個数のリスト
X = {d: [f[d] if d in f else 0 for f in F] for d in D}

# Dの要素 d → A_iを削除したときのdの個数の最小値のリスト
X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D if X[d][i] > 0) for i in range(N)]
print(max(S))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math
import fractions
input=[]
for i in range(2):
    input.append(stdin.readline().rstrip().split())

N=int(input[0][0])
A=[]
for i in input[1]:
    A.append(int(i))

#print(N,A)

"""
x=0
for i in range(N):
    if i <= 1:
        x=math.gcd(A[0],A[1])
    else:
        x=math.gcd(x,A[i])
print(x)
"""
x=0
output=0
A.sort(reverse=True)
for i in range(N):
    copy_A=A.copy()
    copy_A.pop(i)
    if N==2:
        if A[0]<A[1]:
            output=A[1]
        else:
            output=A[0]
        break

    else:
        for i in range(N-1):
            if i <= 1:
                x=fractions.gcd(copy_A[0],copy_A[1])
            else:
                x=fractions.gcd(x,copy_A[i])
                if (x==1) or (x<=output):
                    break

        if output<x:
            output=x

print(output)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
import collections

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

r = []

for j in a:
    for i in range(1, int(j**0.5)+1):
        if j % i == 0:
            r.append(i)
            if i != j // i:
                r.append(j // i)

c = collections.Counter(r)
m = c.most_common()
mc = m[0][1] - 1
c_a = [i[0] for i in m if i[1] >= mc]
print(sorted(c_a)[-1])

Submission status: Time Limit Exceeded

Solution code:
import copy
import functools
import fractions

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

origin_list = copy.deepcopy(A)
max_gcd = 0
kesusoezi = 0

for i in range(len(A)):
    A.pop(i)
    gcd = functools.reduce(fractions.gcd, A)
    A = copy.deepcopy(origin_list)
    if max_gcd < gcd:
        kesusoezi = i
        max_gcd = gcd

A = copy.deepcopy(origin_list)

kotae = functools.reduce(fractions.gcd, A)

A[kesusoezi] = max_gcd

test = functools.reduce(fractions.gcd, A)

print(test)

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 = 1
for i in range(n):
    j = 0
    if i == 0:
        tmp = a[1]
    else:
        tmp = a[0]
    while j != n:
        if j != i:
            tmp = gcd(tmp, a[j])
        if tmp <= ans:
            break
        j += 1
    if tmp > ans:
        ans = tmp
    if (i!=0) and ans >= a[0]:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
N=int(input())
A=[int(i) for i in input().split()] 
#max_gcd=functools.reduce(fractions.gcd, A)
max_gcd = 1
for i in range(N):
	tmp=A.copy()
	tmp.pop(i)
	gcd=functools.reduce(fractions.gcd, tmp)
	if max_gcd<gcd:
		max_gcd=gcd
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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

A = np.array(A)

def gcd(p,q):
	if q==0:
		return p
	else:
		return gcd(q, p%q)

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

current_max_gcd = 0
max_val = A.max()
for i in range(0,N):
	B = np.copy(A)
	for j in range(max_val):
		B[i] = j
		max_gcd = calc_gcd(B)
		if max_gcd > current_max_gcd:
			current_max_gcd = max_gcd
print(current_max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from itertools import combinations
from functools import reduce

N = int(input())
A = map(int, input().split())
ans = 0
for a in combinations(A,N-1):
    tmp = reduce(gcd,a)
    ans = tmp if ans < tmp else ans

print(ans)

Submission status: Time Limit Exceeded

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

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

if len(a)==2:
    print(max(a))
else:
    for i in range(n-1):
        cnt.append(gcd(a[i],a[i+1]))
    cnt.append(gcd(a[0],a[n-1]))
    cnt2 = cnt
    while 1:
        tmp = max(cnt2)
        g = sum(x%tmp==0 for x in cnt)
        if g>=len(cnt)-2:
            print(tmp)
            break
        else:
            cnt2 = [i for i in cnt2 if i != 0]