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 functools
import fractions

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

g = []
if N == 2:
  print(max(A))
else:
  for i in range(len(A)):
    g.append(functools.reduce(fractions.gcd, A[:i] + A[i+1:]))
  print(max(g))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
A = sorted(A,reverse=True)

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

def gcd_list(list):
    a = list.pop()
    for b in list:
        a = gcd(a,b)
    return a

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

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd as gc
from functools import reduce
 
def gcd(x):
    return reduce(gc,x)
 
N=int(input())
A=list(map(int,input().split()))
B=A[:]
maxi=gcd(A)
for i in range(N):
    for j in range(gcd(A),max(A)+1):
        B[i]=j
        #print("{} {}".format(B,gcd(B)))
        if(maxi<gcd(B)):
            maxi=gcd(B)
    B=A
    
print(maxi)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

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

l = [0 for _ in range(n)]
r = [0 for _ in range(n)]
ans = []

for i in range(n):
    l[i] = reduce(gcd,[0]+A[:i])
    r[i] = reduce(gcd,A[i+1:]+[0])
    
for i in range(n):
    ans.append(gcd(l[i],r[i]))
    
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions

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

g = []
if len(A) == 2:
  print(max(A))
else:
  for i in range(len(A)):
    g.append(functools.reduce(fractions.gcd, A[:i] + A[i+1:]))
  print(max(g))

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    gcds = []
    for i in range(N):
        A_ = copy(A)
        A_.pop(i)
        gcds.append(gcd(*A_))
    print(max(gcds))


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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

n = int(raw_input())
a = raw_input().split()

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:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
#        for j in del_list:
#            del chk[j]
        cnt += 1

#    answer = max(chk.keys())
    answer = 0
    for i in chk:
        if int(chk[i]) >= n - 1 and i > answer:
            answer = i
    print answer

Submission status: Time Limit Exceeded

Solution code:
def yuk(a, b):
    ans = int(a / b)
    amari = a % b
    if amari != 0:
        ans = yuk(b, amari)
    else:
        ans = b
    return ans

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

ansLst = []
for i in range(len(aLst)):
    ans = 0
    for j in range(len(aLst)):
        if i != j:
            ans = yuk(ans, aLst[j])
    ansLst.append(ans)
ansLst.sort(reverse=True)
print(ansLst[0])

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions

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

num = 1

if len(A) == 2:
  print(max(A))
else:
  for i in range(len(A)):
    g = functools.reduce(fractions.gcd, A[:i] + A[i+1:])
    num = g if g > num else num
  print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
a = raw_input().split()

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:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

    answer = max(chk.keys())
#    answer = 0
#    for i in chk:
#        if int(chk[i]) >= n - 1 and i > answer:
#            answer = i
    print answer

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
gcd_list=[]
reverse_gcd_list = []
num = 0
gcd_num = 0

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

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

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

for i in range(N-2):
    num = max(gcd(gcd_list[i], reverse_gcd_list[i+2]), num)

print(max(num, gcd_list[-2], reverse_gcd_list[1]))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(raw_input())
a = raw_input().split()

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:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

#    answer = max(chk.keys())
    answer = 0
    for i in chk:
        if int(chk[i]) >= n - 1 and i > answer:
            answer = i
    print answer

Submission status: Time Limit Exceeded

Solution code:
import copy

def gcd_l(list):
    g = list[0]
    for l in list:
        g = gcd(g, l)
    return g

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

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

_max = 0
for i in range(N):
    temp = copy.deepcopy(A)
    temp.remove(A[i])
    _max = max(_max, gcd_l(temp))
    
print(_max)

Submission status: Time Limit Exceeded

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

kosuu = int(input())
nums = list(map(int,input().split()))

i=0
saidai = []
while i < len(nums):
    x,l=0,0
    while x < i:
        l = gcd(l,nums[x])
        x += 1
    y,r = i+1,0
    while y < len(nums):
        r = gcd(r,nums[y])
        y +=1
    i +=1
    saidai.append(gcd(l,r))

print(max(saidai))

Submission status: Time Limit Exceeded

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

num = 1

if len(A) == 2:
  print(max(A))
else:
  for i in range(len(A)):
    B = A[ : i] + A[i + 1:]
    g = functools.reduce(fractions.gcd, B)
    if (g > num):
      num = g
  print(num)

Submission status: Time Limit Exceeded

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

l = [0]*N
r = [0]*N

# 左から右
for i in range(N-1):
    if i == 0:
        a = al[i]
        l[i+1] = a
    if i >= 1:
        a = gcd(a, al[i])
        l[i+1] = a

# 右から左
for i in range(N-1):
    if i == 0:
        a = al[::-1][i]
        r[-i-2] = a
    if i >= 1:
        a = gcd(a, al[::-1][i])
        r[-i-2] = a

# 最大公約数リスト
ansl = [0]*N
for i in range(N):
    if i == 0:
        ansl[i] = r[i]
    elif i == N-1:
        ansl[i] = l[i]
    else:
        ansl[i] = gcd(r[i], l[i])
print(max(ansl))

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)

def _gcd(a,b):
  if a is None and b is None:
    return None
  if a is None:
    return b
  if b is None:
    return a
  else:
    return gcd(a,b)

class SegTree():
  import math
  def __init__(self, func, A, identity=1):
    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=None)

answer = 0
for i in range(len(A)):
  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:
# -*- coding: utf-8 -*-
from functools import reduce

n = int(input()) 
alist = list(map(int, input().split()))
 
def gcd2(a,b):
  while b!=0:
    a,b=b,a%b
  return a

def gcd(*numbers):
    return reduce(gcd2, numbers)

if len(alist) == 2:
  ans = max(alist)
else:
  ansList = []
  for i in range(len(alist)):
    srList = alist[:]
    del srList[i]
    gcdval = gcd(*srList)
    ansList.append(gcdval)
  ans = max(ansList)
print("{}".format(ans))

Submission status: Time Limit Exceeded

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

kosuu = int(input())
nums = list(map(int,input().split()))

i=0
max = 0
while i < len(nums):
    x,l=0,0
    while x < i:
        l = gcd(l,nums[x])
        x += 1
    y,r = i+1,0
    while y < len(nums):
        r = gcd(r,nums[y])
        y +=1
    i +=1
    tmp = gcd(l,r)
    if max < tmp:
        max = tmp
print(max)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy


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


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

ans = 1
for i in range(N):
    a = copy.copy(A)
    a.pop(i)
    g = gcd_list(a)
    if g > ans:
        ans = g
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import time
from functools import reduce

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

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

def l(i):
    l = numbers[0:i]
    if len(l) == 0:
        return 0
    return reduce(gcd, l)
def r(i):
    l = numbers[i+1:]
    if len(l) == 0:
        return 0
    return reduce(gcd, l)

m = [gcd(l(i), r(i)) for i in range(n)]

print(max(m))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

ans = []
for i in range(len(A)):
    tmp = A[i]
    L = reduce(gcd, [0] + A[:i])
    R = reduce(gcd, A[i+1:] + [0])
    ans.append(gcd(L,R))
        
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
import functools
import fractions
l1=[]
for i in range(n):
  a=l[0]
  del l[0]
  s = functools.reduce(fractions.gcd, l)
  l1.append(s)
  l.append(a)
print(max(l1))

Submission status: Time Limit Exceeded

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

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 range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

    answer = max(chk.keys())
    print answer

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):
  before = A[0:i]
  after = A[i+1:N]
  gcd_before = 0
  for j in range(len(before)):
    gcd_before = gcd(gcd_before,before[j])

  gcd_after = 0
  for k in range(len(after)):
    gcd_after = gcd(gcd_after,after[k])

  valid_number = gcd(gcd_before,gcd_after)
  ans = max(ans,valid_number)

print(ans)

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:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    return divisors

N = int(input())
aList = [int(x) for x in input().split()]
memo = {}
for a in aList:
    cache = list(set(make_divisors(a)))
    for x in cache:
        if x in memo:
            memo[x] += 1
        else:
            memo[x] = 1
sd = sorted(memo.items(), key=lambda x:(x[1], x[0]), reverse=True)
result = 1
for x, y in sd:
    if y > N-2 and x > result:
        result = x
print(result)

Submission status: Time Limit Exceeded

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

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 range(2):
        make_divisors(a[i])
    cnt = 1
    for i in reversed(range(2,len(a))):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

    answer = max(chk.keys())
    print answer

Submission status: Time Limit Exceeded

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


def gcd(vs):
    vs = list(set(vs))
    while True:
        m = min(vs)
        vs_ = []
        for i, v in enumerate(vs):
            if v % m != 0:
                vs_.append(v % m)
        vs_ = vs_ + [m]
        vs = vs_
        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:
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%y==0:
        return y
    else:
        while x%y!=0:
            x,y = y,x%y
        return y

res = []
tmp = []
for i in range(n):
    if a_list[i] in tmp:
        continue
    if a_list[:i]==[]:
        res.append(functools.reduce(gcd, a_list[i+1:]))
    elif a_list[i+1:]==[]:
        res.append(functools.reduce(gcd, a_list[:i]))
    else:
        l_gcd = functools.reduce(gcd, a_list[:i])
        r_gcd = functools.reduce(gcd, a_list[i+1:])
        res.append(gcd(l_gcd,r_gcd))
    tmp.append(a_list[i])
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import fractions
from functools import reduce

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

gcd_list = []
for i in range(N):
    removed = A[0:i] + A[i + 1:N]
    gcd_list.append(reduce(fractions.gcd, removed))
print(max(gcd_list))

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%y==0:
        return y
    else:
        while x%y!=0:
            x,y = y,x%y
        return y

res = []
tmp = []
for i in range(n):
    if a_list[:i]==[]:
        res.append(functools.reduce(gcd, a_list[i+1:]))
    elif a_list[i+1:]==[]:
        res.append(functools.reduce(gcd, a_list[:i]))
    else:
        l_gcd = functools.reduce(gcd, a_list[:i])
        r_gcd = functools.reduce(gcd, a_list[i+1:])
        res.append(gcd(l_gcd,r_gcd))
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

import copy
import math
#from functools import reduce
import fractions

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

def gcd_list(num):

    #print("n", num)
    a = fractions.gcd(num[0], num[1])
    for i in range(len(num)):
        #print("i", i)
        a = fractions.gcd(a, num[i])
        #print(a)
    return a


# input
l = list(map(int, input().split()))
N = l[0]

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


best_result = gcd_list(A)

for brank_iter in range(N):
    #print("iter", brank_iter)
    a = copy.copy(A)
    a.pop(brank_iter)
    if len(a) >= 2:
        result = gcd_list(a)
    else:
        result = a[0]

    # Renew
    if best_result <= result:
        best_result = result

print(best_result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
def gcm(a, b):
  if a < b:
    a = a+b
    b=a-b
    a = a-b
  if b == 0:
    return a
  else:
    r = a%b
    return b if r == 0 else gcm(b, r)
ans = 0
for i in range(N):
  g = 0
  for j in range(i):
    g = gcm(g, A[j])
  for j in range(i+1,N):
    g = gcm(g, A[j])
  ans = max(g, ans)
print(ans)

Submission status: Time Limit Exceeded

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

b = [0] * n

def gcd(x, y):
  while y > 0:
    x, y = y, x % y
  return x

for i in range(n):
 c = 0
 for j in range(n):
  if i != j:
   c = gcd(c,a[j])
  if c < max(b):
   break
 b[i] = c

print max(b)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy

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

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

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

maxi = 0

# おなじか？
if len(list(set(v))) == 1:
    maxi = (v[0])
elif len(v) == 2:
    maxi = (max(v))
# 書き換え 1
else:
    for i in range(n):
        listv = []
        listv = copy.copy(v)
        listv[i] = min(v) 
        listv[i] = max(listv)
        if gcd_list(listv)> maxi:
            maxi = gcd_list(listv)
#最大公約数


print(maxi)

# 書き換え 2

Submission status: Time Limit Exceeded

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


def gcd(vs):
    vs = list(set(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:
N = int(input())
a =list(map(int, input().split()))

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

from functools import reduce
max_val = reduce(gcd, a[1:])
for i in range(1, N):
    tmp = a[i]
    a[i] = a[0]
    val = reduce(gcd, a)
    if max_val < val:
        max_val = val
        
    a[i] = tmp
print(max_val)

Submission status: Time Limit Exceeded

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

b = [0] * n

def gcd(x, y):
  while y > 0:
    x, y = y, x % y
  return x

for i in range(n):
 c = 0
 for j in range(n):
  if i != j:
   c = gcd(c,a[j])
 b[i] = c

print max(b)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(raw_input())
a = raw_input().split()

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 range(2):
        make_divisors(a[i])
    cnt = 1
    for i in reversed(range(2,len(a))):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

    answer = 0
    for i in chk:
        if int(chk[i]) >= n - 1 and i > answer:
            answer = i
    print answer

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
allyp=[]
for i in range(len(A)):
    divesor=[]
    a=A[i]**0.5
    for j in range(1,int(a)+1):
        if A[i]%j==0:
            divesor.append(j)
            divesor.append(A[i]/j)
    A[i]=divesor
    allyp+=A[i]
allyp=list(set(allyp))
allyp.sort(reverse=True)
for i in allyp:
    flag=1
    for j in range(len(A)):
        if i not in A[j]:
            flag-=1
    if flag >=0:
        print(int(i))
        break

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy

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

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

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

maxi = 0

# おなじか？
if len(list(set(v))) == 1:
    maxi = (v[0])
elif len(v) == 2:
    maxi = (max(v))
# 書き換え 1
else:
    for i in range(n):
        listv = []
        listv = copy.copy(v)
        listv[i] = min(v) 
        listv[i] = max(listv)
        listval = listv
        temp = gcd_list(listval)
        if temp> maxi:
            maxi = temp
#最大公約数


print(maxi)

# 書き換え 2

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import functools

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

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

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 fractions
n = int(input())
lis = list(map(int,input().split()))
lis.sort()
if n == 2:
  print(max(lis))
  exit()
li = [lis[0]]
lii = [lis[-1]]
for i in range(1,n):
  li.append(fractions.gcd(li[i-1],lis[i]))
  lii.insert(0,fractions.gcd(lii[-i],lis[-(i+1)]))
ans = max(lii[1],li[-2])
for i in range(n-2):
  ans = max(ans,fractions.gcd(li[i],lii[i+2]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(raw_input())
a = raw_input().split()

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 range(2):
        make_divisors(a[i])
    cnt = 1
    for i in reversed(range(2,len(a))):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

    answer = 0
    for i in chk:
        if int(chk[i]) >= n - 1 and i > answer:
            answer = i
    print answer

Submission status: Time Limit Exceeded

Solution code:
import functools
import operator

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

result = 0
for i, _ in enumerate(A):
    factors = set()
    for j, a in enumerate(A):
        if i == j:
            continue
        factors_a = set()
        for n in range(1, a+1):
            if a%n == 0:
                factors_a.add(n)
                a /= n
        if len(factors) == 0:
            factors = factors_a
        else:
            factors &= factors_a
    if len(factors) > 0:
        result = max(result, functools.reduce(operator.mul, factors))

print(result)

Submission status: Time Limit Exceeded

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

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

valueList = []
maxList = []

g = a[0]
valueList.append(g)
g = a[1]
for k in range(1, n):
  g = gcd(g, a[k])
maxList.append(g)
  

for i in range(1,n):
  g = valueList[i-1]
  valueList.append(gcd(g, a[i]))
  for k in range(i+1, n):
    g = gcd(g, a[k])
  maxList.append(g)
    
print(max(maxList))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
maxi=10**9
allyp=[]
for i in range(len(A)):
    divesor=[]
    a=A[i]**0.5
    for j in range(1,int(a)+1):
        if A[i]%j==0:
            divesor.append(j)
            divesor.append(A[i]/j)
    A[i]=divesor
    allyp+=A[i]
    if maxi>max(A[i]):
        maxi=max(A[i])
allyp=list(set(allyp))
allyp.sort(reverse=True)
for i in allyp:
    flag=1
    for j in range(len(A)):
        if i not in A[j]:
            flag-=1
    if flag >=0:
        print(int(i))
        break

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,ans,-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:
import copy
import functools
import fractions

N = input()
str_A = input()

str_A = str_A.split()

A = []

for i in str_A:
    A.append(int(i))

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

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

def gcd_base(x, y):
    if x >= y:
        while y != 0:
            x, y = y, x % y
        return x
    else:
        while x != 0:
            y, x = x, y % x
        return y

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

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

for i in range(N):
    a = A.pop(0)
    GCD.append(gcd(A))
    A.append(a)

print(max(GCD))