Problem:


Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Time Limit Exceeded

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

import fractions

C = []

for i in range(N):
	B = A.copy()
	B.pop(i)
	ans = B[0]
	for j in range(1, N-1):
		ans = fractions.gcd(ans, B[j])
	C.append(ans)


X = max(C)

print(X)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce 
from fractions import gcd
n = int(input())
*v, = map(int,input().split())
#print(v)

mn = 0
for i in range(n):
  c = v.copy()
  #print('c',c)
  c.pop(i)
  #print('c2',c)
  ans = reduce(gcd,c)
  #print('v',v)
  #print('ans',ans)
  if ans > mn:
    mn = ans
    
print(mn)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
from fractions import gcd
from functools import reduce
from collections import defaultdict
import sys
N = int(input())
A = list(set([int(i) for i in input().split()]))
 
def gcd_(*numbers):
    return reduce(gcd, numbers)

def prime(n):
    # 素数列挙
    # nまでの素数をリストで返す(nを含む)
    primes = set(range(2, n+1))
    for i in range(2, int(n**0.5+1)):
        primes.difference_update(range(i*2, n+1, i))
    primes=list(primes)
    return primes

num = gcd_(*A)
dic = defaultdict(int)
B = []
for x in A:
    B.append(x//num)
ls = prime(max(B))
for x in ls[::-1]:
    a = 0
    for y in B:
        if y%x == 0:
            a += 1
            if a == len(B)-1:
                print(num*x)
                sys.exit()
print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
def u(a, b):
    if b == 0:
        return a
    return u(b, a % b)

if len(A) <= 2:
    res = max(A)
else:
    res = 0
    for i in range(len(A)):
        tmp = list(A)
        if tmp.index(tmp[i]) < i:
            continue
        tmp.pop(i)
        for k in range(len(tmp)-1):
            tmp[k+1] = u(tmp[k], tmp[k+1])
        res = max(res, tmp[len(tmp)-1])
print(res)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce


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


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


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

max_ = 0
for i in range(len(A)):
    l = A[:]
    del l[i]
    num = gcd_list(l)
    if max_ < num:
        max_ = num

print(max_)

Submission status: Time Limit Exceeded

Solution code:
import functools
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):
    if len(numbers) == 1:
        return numbers[0]
    else:
        return functools.reduce(gcd, numbers)
ans = []
B = []
for i in range(len(A)):
    B = copy.copy(A)
    B.pop(i)
    ans.append(gcd_list(B))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import re
import bisect
from datetime import datetime as dt
import fractions
from functools import reduce


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


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


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

if n == 2:
    print max(a[0], a[1])
    exit()

M = 0
max_idx = 0
for i in range(n):
    if M >= a[i]:
        continue
    tmp_arr = a[:i] + a[i+1:]
    M = max(gcd_list(tmp_arr), M)
print M

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_mul(*numbers):
    return reduce(gcd, numbers)

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

l = set()
for i in range(n):
    a_reduced = a[:i] + a[i+1:]
    l.add(gcd_mul(*a_reduced))

print(max(l))

Submission status: Time Limit Exceeded

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

def gcd(a, b):
  if (min(a, b) == 0):
    return max(a, b)
  elif (a > b):
    return gcd(b, a % b)
  else:
    return gcd(a, b % a)

gcd_forward = []
gcd_all = numbers[0]
for i in range(0, N - 1):
  if (gcd_all > 1):
    gcd_all = gcd(gcd_all, numbers[i])
  gcd_forward.append(gcd_all)

gcd_backward = []
gcd_all = numbers[-1]
for i in range(N - 1, 0, -1):
  if (gcd_all > 1):
    gcd_all = gcd(gcd_all, numbers[i])
  gcd_backward.insert(0, gcd_all)

gcd_forward.insert(0, gcd_backward[0])
gcd_backward.append(gcd_forward[-1])

gcd_max = 1
for i in range(N):
  if (gcd_forward[i] > 1 and gcd_backward[i] > 1):
    gcd_max = max(gcd_max, gcd(gcd_forward[i], gcd_backward[i]))
  
print(gcd_max)

Submission status: Time Limit Exceeded

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

d = {}
def divisor(n):
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            if i not in d:
                d[i] = 1
            else:
                d[i] += 1

            rev = n // i
            if i != rev:
                if rev not in d:
                    d[rev] = 1
                else:
                    d[rev] += 1

for num in A:
    divisor(num)

ans = 1
for key, val in d.items():
    if val >= N - 1:
        ans = max(ans, key)

print(ans)

Submission status: Time Limit Exceeded

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

n = int(input())

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

ans = 0

for i in range(n):
    # print('i=',i)
    f = 0

    for j in range(n):
        # print('j=',j)

        if j!= i:
            if f == 0:
                f = a[j]
            else:
                # print(f,a[j])
                f=gcd(a[j],f)
    ans = max(ans,f)

print(ans)

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()]
a.sort()
for i in range(len(a)//2+1):
    b=a[:]
    b[i]=max(a)
    g=gcd_list(b)
    if g>ans:
        ans=g
print(ans)

Submission status: Time Limit Exceeded

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

def gcd_l(a):
  s = a[0]
  for i in range(len(a)):
    s = gcd(s,a[i])
  return s

def gcd_list(a,b):
  if b == 0:
    s = a[1]
  else:
    s = a[0]
  for i in range(len(a)):
    if i == b:
      pass
    else:
      s = gcd(s,a[i])
  return s

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

s = gcd_l(a)
for i in range(n):
  l = gcd_list(a,i)
  if s < l:
    s = l
print(s)

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions

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

for i in range(N):
    B = A.copy()
    del B[i]
    gcd[i] = functools.reduce(fractions.gcd, B)

print(max(gcd))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

from collections import defaultdict

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

memo = defaultdict(int)
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
    
for a in A:
    divisors = make_divisors(a)
    for divisor in divisors:
        memo[divisor] += 1

max_divisor = 1        
for k,v in memo.items():
    if v >= N - 1:
        max_divisor = max(max_divisor, k)
        
print(max_divisor)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import sys

sys.setrecursionlimit(100000)

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

N = int(input())
A = list(map(int,input().split()))
M = 0
gcd_i = [0]*N
gcd_i[0] = A[0] 
for i in range(N):
  tmp = 0
  if i == 0:
    tmp = gcd(A[i+1:])
  elif i == N - 1:
    tmp = gcd(A[:i])
  else:
    tmp = gcd( A[0:i] + A[i+1:])
  M = max(M,tmp)  
print(M)

Submission status: Time Limit Exceeded

Solution code:
def gcd(num_list,a,N):
  for i in range(min(num_list),a,-1):
    D = [o%i for o in num_list].count(0)
    if D == N:
      return i
    elif D == N-1:
      return i
  return 1

def gcd2(num_list,a):
  for i in range(min(num_list),a,-1):
    if sum([o%i for o in num_list]) == 0:
      return i
  return 1

N = int(input())
A = list(map(int,input().split()))
ans1 = gcd(A,1,N)
temp = min(A)
A.remove(min(A))
ans2 = gcd2(A,temp)
print(max(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array([int(x) for x in input().split(' ')])
A.sort()

if N > 2:
    x = 0
    for n, m in [tuple(A[0:2]), tuple(A[1:3]), (A[0], A[2])]: 
        while n != 0:
            l = n
            n = m % n
            m = l
        x = max(m, x)
else:
    x = A[-1]

while True:
    tmp = A % x
    if sum(tmp != 0) < 2:
        break
    x -= 1

print(x)

Submission status: Time Limit Exceeded

Solution code:
import functools

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

gcd_l = lambda *x, f=(lambda f, x, y: f(f, y, x % y) if y else x): f(f, *x)

max = 0
for i in range(len(li)):
    n_li = li[0:i] + li[i+1:]
    gcd = functools.reduce(gcd_l, n_li)
    if max < gcd:
        max = gcd
print(max)

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
# ans = 1
ans = reduce(fractions.gcd, A[1:])
for i in range(1, N-1):
    left_gcd = reduce(fractions.gcd, A[:i])
    right_gcd = reduce(fractions.gcd, A[i+1:])
    ans = max(ans, fractions.gcd(left_gcd, right_gcd))
left_gcd = reduce(fractions.gcd, A[:-1])
ans = max(ans, left_gcd)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce


def main():

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

    maxd = 1
    for n in range(N):
        l = A[:n] + A[n+1:]
        maxd = max(maxd, gcd(*l))

    print(maxd)    


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


main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
def gcd(m,n):
    if n==0:
        return m
    return gcd(n,m%n)

N=int(input())
As=map(int,input().split())
ans=0
for c in combinations(As, N-1):
    tmp=list(c)[0]
    for a in c:
        tmp=gcd(tmp,a)
    if tmp>ans:
        ans=tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
As = [[0]*N for _ in range(N-1)]
As[0] = A

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


for i in range(1,N-1):
    for j in range(N):
        As[i][j] = gcd(As[i-1][j-1],As[i-1][j])

print(max(As[-1]))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
N = int(input())
Alist = list(map(int, input().split()))
def gcd_list(numbers):
  return reduce(gcd, numbers)
print(max([gcd_list(Alist[:i]+Alist[i+1:])for i in range(N)]) if len(Alist)>2 else max(Alist))

Submission status: Time Limit Exceeded

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

def gcd(a,b):
	while b:
		a, b = b, a % b
	return a
if n == 2:
	print(max(l[0],l[1]))
elif n == 3:
	print(max(gcd(l[0],l[1]),gcd(l[0],l[2]),gcd(l[2],l[1])))
else:
	for i in range(n):
		tmp = copy.copy(l)
		tmp.pop(i)
		x = gcd(tmp[0],tmp[1])
		for j in list(range(2, n-1)):
			x = gcd(x,tmp[j])
		b.append(x)
	print(max(b))

Submission status: Time Limit Exceeded

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

N = int(input())
nums = [int(i) for i in input().split()]
nums2 = copy.copy(nums)
ans = []

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


for i in range(N):
   nums.pop(i)
   ans.append(gcds(nums))
   nums = copy.copy(nums2)

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
import math
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


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


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


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


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

n = INT()

a = LIST()

q = GCD(a)

for i in range(n):
    lis = a.copy()
    lis.pop(i)
    if GCD(lis) > q:
        q = GCD(lis)

print(q)

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]
  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:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

left_gcd = [0]
right_gcd = [0]

for i in range(n):
    left_gcd.append(gcd(left_gcd[-1], a[i]))
    right_gcd.insert(0, gcd(right_gcd[0], a[(n - 1) - i]))

max_gcd = 0

for i in range(n):
    get_gcd = gcd(left_gcd[i], right_gcd[i + 1])
    if get_gcd > max_gcd:
        max_gcd = get_gcd

print(max_gcd)

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 = int(input())
a_list = list(map(int, input().split()))
max_g = 0
for exclude in range(n):
    g = a_list[0] if exclude > 0 else a_list[1]
    for i, a in enumerate(a_list):
        if i == 0 or i == exclude:
            continue
        g_new = gcd(g, a)
        if (g, a) not in memo:
            memo[(g, a)] = 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:
# coding: utf-8
# Your code here!

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

temp_arr=[]
for temp in nums:
    temp_arr.append(make_divisors(temp))
    

temp_arr2 = sum(temp_arr,[])
temp_arr2.sort(reverse=True)
for temp in temp_arr2:
    suuzi = temp_arr2.count(temp)
    if suuzi == kosuu -1 or suuzi == kosuu:
        print(temp)
        break

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


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

divisor_num = {}
for number in numbers:
    divisors = make_divisors(number)

    for divisor in divisors:
        if divisor in divisor_num:
            divisor_num[divisor] += 1
        else:
            divisor_num[divisor] = 1

res = 1
for k, v in divisor_num.items():
    if v == n or v == n-1:
        res = max(res, k)

print(res)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a%b)
N = int(input())
A = [int(i) for i in input().split()]
ans = 0
for n in range(N):
  c = A[0]
  del A[0]
  agcd = A[0]
  for j in A[1:]:
    agcd = gcd(agcd, j)
  if ans < agcd:
    ans = agcd
  A.append(c)
print(ans)

Submission status: Time Limit Exceeded

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

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

element = 0

m = 1
if N == 2:
    print(max(A[0],A[1]))
else:
    for j in range(N):
        count = 2
        if j == 0:
            element = gcd(A[1],A[2])
            count = 3
        elif j == 1:
            element = gcd(A[0],A[2])
            count = 3
        else:
            element = gcd(A[0],A[1])

        for i in range(count,N):
            if j == i:
                continue
            element = gcd(element,A[i])
        m = max(m,element)
    print(m)

Submission status: Time Limit Exceeded

Solution code:
n,*a=map(int,open(0).read().split())
s=[]
g=[]
for c in a:
  b=[]
  for i in range(1,int(c**.5)+1):
    if c%i==0:
      b+=[i,c//i]
  s.append(sorted(b)[::-1])
  g+=sorted(b)[::-1]
for c in sorted(list(set(g)))[::-1]:
  if g.count(c)>=len(a)-1:
    print(c)
    exit()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def calc(a, b):
  if a == 1 or b == 1:
    return 1

  while a != 0 and b != 0:
    if a < b:
      b %= a
    else:
      a %= b

  return a if a != 0 else b


def main():
  input()
  a = list(map(int, input().split()))
  p = [a[0], a[1], calc(a[0], a[1])]

  for v in a[2:]:
    for j in range(len(p) - 1):
      p[j] = calc(p[j], v)

    p.append(calc(p[-1], v))

  print(max(p), flush=True)


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def gcd(a):
    b = sorted(a)
    c = [0 for i in range(len(b))]
    for i in range(len(b)):
        if b[i]%b[0]==0:
            c[i]=b[0]
        else:
            c[i]=b[i]%b[0]
    d=sorted(c)
    if d[0]==d[-1]:
        return d[0]
    else:
        return gcd(c)
n = int(input())
a = list(map(int,input().split()))
e = [0 for i in range(n)]
for i in range(n):
    e[i] = gcd(a[:i]+a[i+1:])
print(max(e))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
divsors=[]
for i in range(n):
  for j in range(1,int(A[i]**(1/2)+1)):
    if A[i]%j==0:
      divsors.append(j)
      if j!=A[i]//j:
        divsors.append(A[i]//j)
set_divsors=sorted(list(set(divsors)))[::-1]
divsors=sorted(divsors)[::-1]
count=[]
for i in range(len(set_divsors)):
  count.append(divsors.count(set_divsors[i]))
for i in range(len(count)):
  if count[i]==n-1 or count[i]==n:
    print(set_divsors[i])
    break

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

n=int(input())
array = list(map(int, input().split())) 
array.sort(reverse=True)


max=-100

for i in range(n):
    copy_array=array.copy()
    if(max>array[i]):
        break
    copy_array.pop(i)
    tmp_gcd=gcd(*copy_array)
    # print(copy_array)
    if(max<tmp_gcd):
        max=tmp_gcd
    
print(max)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def gcd(a,b):
    num = []
    num.append(max(a,b))
    num.append(min(a,b))
    i = 0
    while True:
        num.append(num[i]%num[i+1])
        if num[i+2] == 0:
            break
        else: 
            i += 1
    return num[i+1]

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

C = []
for k in range(N):
    B = copy.copy(A)
    del B[k]
    C.append(gcd_list(B))

print(max(C))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

result = 0

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

n = int(input())
A = list(map(int, input().split()))
for i in range(n):
    result = max(result, reduce(gcd, A[:i]+A[i+1:]))

print(result)

Submission status: Time Limit Exceeded

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


if N == 2:
    print(max(A[0], A[1]))
else:
    m = min(A)

    ans = -1
    for i in range(1, m+1):
        count = 0
        for n in range(N):
            if A[n] % i != 0:
                count += 1
        if count <= 1 and i > ans:
            ans = i

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import re
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 = max(r.values()) - 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:
def make_divisors(arg):
    divisors = set()
    for i in range(1, int(arg**0.5)+1):
        if arg % i == 0:
            divisors.add(i)
            divisors.add(arg//i)

    return divisors


n = int(input())
a = list(map(int, input().split()))
a.sort()
a0y = make_divisors(a[0])
a1y = make_divisors(a[1])
common = a0y & a1y
one = a0y ^ a1y
for e in a[2:]:
    tmp = make_divisors(e)
    common = tmp & common
    one = (one & tmp) | (common - tmp)

print(max(common.union(one)))

Submission status: Time Limit Exceeded

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

L = len(A)
from functools import reduce
from fractions import gcd
def lcm(a,b):
  return a*b//gcd(a,b)

g = reduce(gcd, A)
l = reduce(lcm, A)

class SegTree():
  import math
  def __init__(self, func, A, identity=1e9):
    self._func = func
    self._N = 1
    while self._N < len(A):
      self._N *= 2
       # 長さ 7 なら N=3 (2^3=8)
    self._identity = identity # maxなら-1e9, sumなら0, productなら1

    self._tree = [self._identity] * (2*self._N -1) 
    self._tree[self._N-1:self._N-1+ len(A)] = A #最下段に元の配列を
    for i in range(self._N-2, -1, -1):
      self._update_single_node(i)
    ########
    # N=3: #
    # 0000 #
    # 1122 #
    # 3456 #
    ########
    self._node_range = [(i-self._N+1,i-self._N+2) for i in range(2*self._N-1)] #right_exclusive
    for i in range(self._N-2, -1, -1):
      self._node_range[i] = (self._node_range[2*i+1][0], self._node_range[2*i+2][1])

  def _update_single_node(self,i):
    self._tree[i] = self._func(self._tree[2*i+1], self._tree[2*i+2])

  def point_update(self,i,x):
    # A[i] = x (addではないので注意)
    j = self._N-1+i
    self._tree[j] = x
    while j>0:
      j = (j-1)//2
      self._update_single_node(j)

  def range_query(self, l, r, on=0):
    # reduce(func, A[l:r])
    # onはノード[on]上と重なる部分の，ということ
    onl, onr = self._node_range[on]
    
    # ノード[on]上はすべて覆う
    if l<=onl and onr<=r:
      return self._tree[on]
    
    # 重なりなし
    elif onr<=l or r<=onl:
      return self._identity
    
    else:
      return self._func(self.range_query(l, r, on=2*on+1),
                        self.range_query(l, r, on=2*on+2))

st = SegTree(gcd, A, identity=l)

answer = 0
for i in range(len(A)):
  st
  st.point_update(i, A[i-1])
  answer = max(answer, st._tree[0])
  st.point_update(i, A[i])

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
import fractions
from functools import reduce

N = int(input())
A = np.array(input().split()).astype(int)

def gcd(numbers):
    return reduce(fractions.gcd, numbers)
rt = 1
for i in range(N):
    ind = np.ones(N, dtype=bool)
    ind[i]=False
    A_tmp = A[ind]
    f = gcd(A_tmp)
    if rt < f:
        rt = f
print(rt)

Submission status: Time Limit Exceeded

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

def gcd(a, b):
  if (min(a, b) == 0):
    return max(a, b)
  elif (a > b):
    return gcd(b, a % b)
  else:
    return gcd(a, b % a)

gcd_forward = []
gcd_all = numbers[0]
for i in range(0, N - 1):
  gcd_all = gcd(gcd_all, numbers[i])
  gcd_forward.append(gcd_all)

gcd_backward = []
gcd_all = numbers[-1]
for i in range(N - 1, 0, -1):
  gcd_all = gcd(gcd_all, numbers[i])
  gcd_backward.insert(0, gcd_all)

gcd_forward.insert(0, gcd_backward[0])
gcd_backward.append(gcd_forward[-1])

gcd_max = 1
for i in range(N):
  gcd_max = max(gcd_max, gcd(gcd_forward[i], gcd_backward[i]))
  
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = list(map(int,input().split()))
As.sort(reverse=True)
first_d = As[0]
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 N == 2:
    print(max(As))
else:
    first = make_divisors(As[0])
    second = make_divisors(As[1])
    first_second = list(set(first) & set(second))
    third  = make_divisors(As[2])
    
    first_third = list(set(first) & set(third))

    second_third = list(set(third) & set(second))
    div_list = list((set(first) & set(second)) | (set(first) & set(third)) | (set(third) & set(second)))
    div_list.sort(reverse=True)
    
    for d in range(len(div_list)):
        cnt = 0
        for i in range(N):
            if As[i] % (div_list[d]) == 0:
                cnt += 1
        if cnt >= N-1:
            ans = div_list[d]
            break
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
 
N = map(int, raw_input().strip().split(" "))
M = map(int, raw_input().strip().split(" "))
max = 0

def gcd (a,b):
    if (b == 0):
        return a
    else:
         return gcd (b, a % b)
for i in range(N[0]):
    A = copy.copy(M)
    A.pop(i)
    res = A[0]
    for c in A[1::]:
        res = gcd(res , c)
    if max <= res:
        max = res
print max

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)

kys = []

for i in range(N):
    B = A.copy()
    B.pop(i)
    kys.append(gcd(B))

print(max(kys))

Submission status: Time Limit Exceeded

Solution code:
gcd_dict = {(1,1):1}
def gcd(a, b):
  if (a,b) in gcd_dict.keys():
    return gcd_dict[(a,b)]
  else:
    a_,b_ = a,b
    while b:
      a, b = b, a % b
    gcd_dict[(a_,b_)]=a
    return a
from functools import reduce
import copy


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

N = int(input())
A = list(map(int,input().split()))
ll=[]
for i in range(N):
    l = A.pop(i)
    ll.append(copy.copy(A))
    A.insert(i,l)

print(max([gcd_list(lll) for  lll in ll]))

Submission status: Time Limit Exceeded

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

import fractions
from functools import reduce

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


ans=0
GCD_target=gcd_list(A)
#print("GCD_target = {}".format(GCD_target))

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools

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

gcd_max = 0
if N > 2:
  A_list = list(itertools.combinations(A_,N-1))
  for a in A_list:
    tmp = fractions.gcd(a[0],a[1])
    for i in range(2,N-1):
      tmp = fractions.gcd(tmp,a[i])
    if tmp > gcd_max:
      gcd_max = tmp
else:
  gcd_max = max(A_)
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
from fractions import gcd
from functools import reduce
from collections import defaultdict
N = int(input())
A = list(set([int(i) for i in input().split()]))
 
def gcd_(*numbers):
    return reduce(gcd, numbers)

def prime(n):
    # 素数列挙
    # nまでの素数をリストで返す(nを含む)
    primes = set(range(2, n+1))
    for i in range(2, int(n**0.5+1)):
        primes.difference_update(range(i*2, n+1, i))
    primes=list(primes)
    return primes

num = gcd_(*A)
dic = defaultdict(int)
B = []
for x in A:
    B.append(x//num)
ls = prime(max(B))
for x in ls:
    for y in B:
        if y%x == 0:
            dic[x] += 1
ans = 1
for x, y in dic.items():
    if y == len(B)-1:
        ans = max(ans, x)
print(num*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()))

ans = 0

if n <= 10**3:
    for i in range(n):
        ans = max(gcd_list(a[:i]+a[i+1:]), ans)

    print(ans)
    exit()
else:
    n1 = n//10**3
    n2 = n%10**3

    g = []

    for i in range(n1):
        g.append(gcd_list(a[i*(10**3):(i+1)*(10**3)]))

    if n2 >0:
        g.append(gcd_list(a[n1*(10**3):]))

    for i in range(n):
        memo = g[i//(10**3)]
        g[i//(10**3)] = gcd_list(a[(i//10**3)*(10**3):i] + a[i+1:(i//10**3+1)*(10**3)])
        ans = max(gcd_list(g), ans)
        g[i // (10 ** 3)] = memo
    print(ans)