Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
7 6 8

Sample Output 1

2

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

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Time Limit Exceeded

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

def gcd(nums):
    gcd = 0

    for i in range(max(nums), 0, -1):
#        print('\ni:{}'.format(i))

        for j in range(len(nums)):
#            print('nums[j]:{}'.format(nums[j]))
            if (nums[j] % i != 0):
                break
        else:
#            print('Can divide! set gcd!')
            gcd = i
#            print('Now gcd: {}'.format(gcd))
            break
        # print('Cannot divide... go next.')
        # continue

    return gcd

gcdOrg = gcd(nums)
gcdMax = gcdOrg

for i in range(n):
    tempList = list(nums)
    del tempList[i]

    #temp = gcd(filter(index == i, nums))
    temp = gcd(tempList)

    if temp > gcdMax:
        gcdMax = temp

#print(gcdOrg)
print(gcdMax)

Submission status: Time Limit Exceeded

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

def gcd(nums):
    maxDiv = 0

    for i in range(1, max(nums)):
#        print('\ni:{}'.format(i))

        for j in range(len(nums)):
#            print('nums[j]:{}'.format(nums[j]))
            if (nums[j] % i != 0):
                break
        else:
#            print('Can divide! increment gcd..')
            maxDiv = i
#            print('Now gcd: {}'.format(maxDiv))
        continue

    return maxDiv

gcdOrg = gcd(nums)
gcdMax = gcdOrg

for i in range(n):
    tempList = list(nums)
    del tempList[i]

    #temp = gcd(filter(index == i, nums))
    temp = gcd(tempList)

    if temp > gcdMax:
        gcdMax = temp

#print(gcdOrg)
print(gcdMax)

Submission status: Time Limit Exceeded

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

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

def gcd(x, y):
    if x == 0:
        return y
    if y == 0:
        return x
        
    if y > x:
        temp = y
        y = x
        x = temp
    amari = x % y
    if amari == 0:
        return y
    else:
        return gcd(y, amari)
        
L = [0]
for i in range(N):
    l = L[-1]
    L.append(gcd(l, A[i]))
    
R = [0]
for i in reversed(range(N)):
    r = R[0]
    R.insert(0, gcd(r, A[i]))

M = [gcd(L[i], R[i+1]) for i in range(N)] # i=1,...,N

print(max(M))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache, reduce
from sys import stdin


@lru_cache(maxsize=None)
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def mygcd(lst):
    result = lst[0]
    for x in lst[1:]:
        result = gcd(result, x)
    return result


def main():
    _ = int(stdin.readline().rstrip())
    As = [int(x) for x in stdin.readline().rstrip().split()]
    max_ = 0
    for a in As:
        tmp = As[:]
        tmp.remove(a)
        max_ = max(max_, reduce(gcd, tmp))
    print(max_)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
from sys import stdin


@lru_cache(maxsize=None)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def mygcd(lst):
    result = lst[0]
    for x in lst[1:]:
        result = gcd(result, x)
    return result


def main():
    _ = int(stdin.readline().rstrip())
    As = [int(x) for x in stdin.readline().rstrip().split()]
    max_ = 0
    for a in As:
        tmp = As[:]
        tmp.remove(a)
        max_ = max(max_, mygcd(tmp))
    print(max_)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

n=input()
original=[int(i) for i in input().split()]
targets=original.copy()
max_gcd = 0
if len(targets) < 3:
    max_gcd = max(targets)
else:
    for i in set(original):
        targets.remove(i)
        gcd = reduce(fractions.gcd, targets)
        targets = original.copy()
        if gcd > max_gcd:
            max_gcd = gcd
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

n=input()
original=[int(i) for i in input().split()]
targets=original.copy()
max_gcd = 0
if len(targets) < 3:
    max_gcd = max(targets)
else:
    for i in original:
        targets.remove(i)
        gcd = reduce(fractions.gcd, targets)
        targets = original.copy()
        if gcd > max_gcd:
            max_gcd = gcd
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


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


def mygcd(lst):
    result = lst[0]
    for x in lst[1:]:
        result = gcd(result, x)
    return result


def main():
    _ = int(stdin.readline().rstrip())
    As = [int(x) for x in stdin.readline().rstrip().split()]
    max_ = 0
    for a in As:
        tmp = As[:]
        tmp.remove(a)
        max_ = max(max_, mygcd(tmp))
    print(max_)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import functools


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


def sub():
    N, As = [input() for _ in range(2)]
    N = int(N)
    As = [int(e) for e in As.split(" ")]
    As = sorted(As, reverse=True)

    memG = 0
    for i in range(N):
        tmp = As[:i] + As[i + 1 :]
        g = functools.reduce(gcd, tmp)
        if g > memG:
            memG = g
    print("{}".format(memG))


sub()

Submission status: Time Limit Exceeded

Solution code:


from functools import reduce
import fractions

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



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

    print(algorithm(N, A))


def algorithm(N, A):
    gcdList = [0] * N
    for i in range(N):
        copy_A = A.copy()
        copy_A.pop(i)
        gcdList[i] = gcd_list(copy_A)

    return max(gcdList)




problem()

Submission status: Time Limit Exceeded

Solution code:
#-*- coding:utf-8 -*-
import copy
import fractions
from functools import reduce


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

gcd_list =[]

for i, a in enumerate(A):
    A_except_a = copy.deepcopy(A)
    A_except_a.pop(i)
    
    gcd = reduce(fractions.gcd, A_except_a)
    gcd_list.append(gcd)

print(max(gcd_list))

Submission status: Time Limit Exceeded

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

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

# if N == 2:
#     print(max(A[0], A[1]))
#     return 0

Llist = [0]
Rlist = [0]

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

ans = 0
for i in range(N):
    t = gcd(Llist[i], Rlist[i+1])
    if ans < t:
        ans = t

print(ans)

Submission status: Time Limit Exceeded

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

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

# if N == 2:
#     print(max(A[0], A[1]))
#     return 0

Llist = [0]
Rlist = [0]

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

ans = []
for i in range(N):
    ans.append(gcd(Llist[i], Rlist[i+1]))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
def gcd(a,b):
    if a<b:
        a,b = b,a
    while b:
        a,b = b,a%b
    return a

N = int(input())
A = list(map(int,input().split()))
gcd_max = 0
for i in range(N):
    B = A.copy()
    B[i:i+1] = []
    gcd_max = max(gcd_max,reduce(gcd,B))
print(gcd_max)

Submission status: Time Limit Exceeded

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

import fractions
def gcd(list):
    gcd = list[0]
    for a in list:
        gcd = fractions.gcd(gcd, a)
    return gcd

max_gcd = 0
for i in range(n):
    Ai = A.pop(i)
    max_gcd = max(max_gcd, gcd(A))
    A.insert(i, Ai)

print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


def check(v):
    def get_gcd(l):
        if len(l) == 1:
            return l[0]
        elif len(l) == 2:
            return gcd(l[0], l[1])

        return gcd(get_gcd(l[:int(len(l)/2)]), get_gcd(l[int(len(l)/2):]))

    result = 0

    for i in range(len(v)):
        w = v.copy()
        del w[i]
        tmp_result = get_gcd(w)
        if result < tmp_result:
            result = tmp_result
    return result

n = input()

v = input()
v = v.split(' ')
v = [int(a) for a in v]

print(check(v))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
import itertools, fractions
from functools import reduce

max_gcd = 0
for ii in itertools.combinations(A, len(A)-1):
    if max_gcd < reduce(fractions.gcd, list(ii)):
        max_gcd = reduce(fractions.gcd, list(ii))
    
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import itertools

from fractions import gcd
from functools import reduce

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


N = int(input())

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

L = list(itertools.combinations(A,N-1))

max = 0

for l in L:
    gl = gcd_list(l)
    if max < gl:
        max = gl

print(max)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
import numpy as np
import copy as copy
N=int(input())
A_i=list(map(int,input().split()))

def GCD(N,A_i):
  GCD=[]
  for i in range(N):
    A=A_i.copy()
    A[i]=A[i-1]
    ans=A[0]
    for j in range(1,N):
      ans=gcd(ans,A[j])
    GCD.append(ans)
  return max(GCD)

print("{}".format(GCD(N,A_i)))

Submission status: Time Limit Exceeded

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

# print(n)
# print(aList)

# # 最大公約数を求めてみる
# def getGCD(a,b):
# 	#print(max(a, b))

# 	gcdVal = 1
# 	for i in range(1, max(a, b) + 1) :
# 		if i > a or i > b :
# 			break

# 		if a%i == 0 and b%i == 0 :
# 			gcdVal = i
# 	return gcdVal


# 最大公約数を求めてみる
def getGCD_List(numList):
	gcdVal = 1
	for i in range(1, max(numList) + 1) :
		if i > min(numList) :
			# 最小値を超えたら終わり
			break


		calcList = list(map(lambda x: x % i, numList))

		filterdList = list(filter(lambda x: x == 0, calcList))

		if len(filterdList) == len(numList):
			gcdVal = i
		
	return gcdVal


# 1つずつ要素を削りながら最大公約数を検索
maxGCD = 0
for i in range(n):
	tempList = aList.copy()
	# print(i)
	# #print(aList)
	# print(tempList.pop(i))
	# print(tempList)
	# print("---")
	# print(gcd_list(tempList))

	tempList.pop(i)
	gcdNum = getGCD_List(tempList)
	if gcdNum > maxGCD:
		maxGCD = gcdNum

print(maxGCD)

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

ans = 0
lst_l = [lst[0]]
lst_r = [lst[n-1]]

for i in range(n-1):
  lst_l.append(fractions.gcd(lst_l[-1], lst[i + 1]))
  lst_r.insert(0, fractions.gcd(lst_r[0], lst[n-2-i]))

for i in range(n-2):
  x = fractions.gcd(lst_l[i], lst_r[i+2])
  if ans < x:
    ans = x
print(max(ans,lst_r[1],lst_l[-2]))

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

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

ans = []
lst_l = [lst[0]]
lst_r = [lst[n-1]]
for i in range(n-1):
  lst_l.append(fractions.gcd(lst_l[-1], lst[i + 1]))
  lst_r.insert(0, fractions.gcd(lst_r[0], lst[n-2-i]))

ans.append(lst_r[1])
for i in range(n-2):
  ans.append(fractions.gcd(lst_l[i], lst_r[i+2]))
ans.append(lst_l[-2])

print(max(ans))

Submission status: Time Limit Exceeded

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

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)
def gcd_array(a):#aは配列
  return reduce(gcd, a)

ans = []
lst_l = [lst[0]]
lst_r = [lst[n-1]]
for i in range(n-1):
  lst_l.append(gcd(lst_l[-1], lst[i + 1]))
  lst_r.insert(0, gcd(lst_r[0], lst[n-2-i]))

ans.append(lst_r[1])
for i in range(n-2):
  ans.append(gcd(lst_l[i], lst_r[i+2]))
ans.append(lst_l[-2])

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy
from functools import reduce

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

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)
def gcd_array(a):#aは配列
  return reduce(gcd, a)

ans = 0
lst_t = []
for i in range(n):
  lst_t = copy.deepcopy(lst)
  del lst_t[i]
  x = gcd_array(lst_t)
  if ans < x:
    ans = x
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
from functools import reduce

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

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)
def gcd_array(a):#aは配列
  return reduce(gcd, a)

ans = []
lst_t = []
for i in range(n):
  lst_t = copy.deepcopy(lst)
  del lst_t[i]
  x = gcd_array(lst_t)
  ans.append(x)
  
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)
def gcd_array(a):#aは配列
  ans = a[0]
  for i in range(len(a)-1):
    ans = gcd(ans, a[i+1])
  return ans

ans = []
lst_t = []
for i in range(n):
  lst_t = copy.deepcopy(lst)
  del lst_t[i]
  x = gcd_array(lst_t)
  ans.append(x)
  
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy
import heapq

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

def cal(array):
  while(len(array) > 1):
    a = array.pop(0)
    b = array.pop(0)
    array.insert(0, gcd(a, b))
  return array[0]

def bSort(a):
  for i in range(len(a)):
    for j in range(len(a)-1, i, -1):
      if a[j] < a[j-1]:
        a[j], a[j-1] = a[j-1], a[j]

  return a

n = int(input())
a = list(map(lambda x: int(x), input().split()))
a = bSort(a)
answer_list = []

for i in range(int(n)):
  copy_a = copy.copy(a)
  copy_pop_a = copy_a.pop(i)
  heapq.heappush(answer_list, -(cal(copy_a)))

answer = -heapq.heappop(answer_list)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def cal(array):
  while(len(array) > 1):
    a = array.pop(0)
    b = array.pop(0)
    array.insert(0, gcd(a, b))
  return array[0]

def bSort(a):
  for i in range(len(a)):
    for j in range(len(a)-1, i, -1):
      if a[j] < a[j-1]:
        a[j], a[j-1] = a[j-1], a[j]

  return a

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


answer_list = []

for i in range(int(n)):
  copy_a = copy.copy(a)
  copy_pop_a = copy_a.pop(i)
  answer_list.insert(0, cal(copy_a))

answer = max(answer_list)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def cal(array):
  while(len(array) > 1):
    a = int(array.pop(0))
    b = int(array.pop(0))
    array.append(gcd(a, b))
  return array[0]

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

answer_list = []

for i in range(int(n)):
  copy_a = copy.copy(a)
  copy_pop_a = copy_a.pop(i)
  answer_list.insert(0, cal(copy_a))

answer = max(answer_list)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def cal(array):
  while(len(array) > 1):
    a = int(array.pop(0))
    b = int(array.pop(0))
    array.append(gcd(a, b))
  return array[0]

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

answer_list = []

for i in range(int(n)):
  copy_a = copy.copy(a)
  copy_pop_a = copy_a.pop(i)
  answer_list.append(cal(copy_a))

answer = max(answer_list)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import fractions
def gcd_list(list1):
    ans=list1[0]
    for i in range(len(list1)):
        ans=fractions.gcd(ans,list1[i])
    return ans
N=int(input())
A=list(map(int,input().split()))
n=0
for i in range(N):
    b=A[i]
    A.remove(A[i])
    m=gcd_list(A)
    n=max(n,m)
    A.insert(i,b)
print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()

import fractions
from functools import reduce
from functools import lru_cache

@lru_cache(maxsize=None)
def calc_gcd(a, b):
    return fractions.gcd(a, b)

def gcd_list(numbers):
    if len(numbers) == 0: return None
    elif len(numbers) == 1: return numbers[0]
    else: return reduce(calc_gcd, numbers)

left_gcd = [gcd_list(A_li[:i]) for i in range(len(A_li))]
right_gcd = [gcd_list(list(reversed(A_li[i+1:]))) for i in range(len(A_li))]

print(max([right_gcd[0]]+[calc_gcd(left_gcd[i], right_gcd[i]) for i in range(1,len(A_li)-1)]+[left_gcd[N-1]]))

Submission status: Time Limit Exceeded

Solution code:
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()

import fractions
from functools import reduce
from functools import lru_cache

@lru_cache(maxsize=None)
def calc_gcd(a, b):
    return fractions.gcd(a, b)

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

print( max([gcd_list(A_li[:i]+A_li[i+1:]) for i in range(len(A_li))]) )

Submission status: Time Limit Exceeded

Solution code:
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()

import fractions
from functools import reduce

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

print( max([gcd_list(A_li[:i]+A_li[i+1:]) for i in range(len(A_li))]) )

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from copy import deepcopy


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


input()
n = list(map(int, input().split()))
if len(n) == 2:
    print(max(n))
else:
    g = [gcd(i, j) for i, j in zip(n[1:], n[:-1])]
    maxs = []
    idx = [i for i, x in enumerate(g) if x == min(g)]
    for i in idx:
        g1 = deepcopy(n)
        g1.pop(i)
        g2 = deepcopy(n)
        g2.pop(i + 1)
        r1 = reduce(gcd, g1)
        r2 = reduce(gcd, g2)
        maxs.append(max(r1, r2))
    print(max(maxs))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
A = list(map(int, input().split()))

def gcd_list(l):
    if len(l) == 0:
        l.append(0)
    res = l[0]
    for i in range(len(l)):
        res = gcd(res, l[i])
    return res

m = []
left = 0
for i in range(n):
    if i == 0:
        left = 0
    else:
        left = gcd(left, A[i-1])
        
    right = gcd_list(A[i+1:])
    m.append(gcd(left, right))

print(max(m))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
A = list(map(int, input().split()))

def gcd_list(l):
    if len(l) == 0:
        l.append(0)
    res = l[0]
    for i in range(len(l)):
        res = gcd(res, l[i])
    return res

m = []
for i in range(n):
    left = gcd_list(A[:i])
    if i == n-1:
        right = 0
    else:
        right = gcd_list(A[i+1:])
    m.append(gcd(left, right))

print(max(m))

Submission status: Time Limit Exceeded

Solution code:
import time
import sys
import fractions
n=int(input())
k=input().split()

left_gcd=0
right_gcd=0
all_answer=0

input_list= [int(i) for i in k]
for i in range(n):

  for j in range(i):
    left_gcd=fractions.gcd(left_gcd,input_list[j])
  for m in range(i+1,n):
    right_gcd=fractions.gcd(right_gcd,input_list[m])

  answer=fractions.gcd(left_gcd,right_gcd)
  all_answer=max(all_answer,answer)
  left_gcd=0
  right_gcd=0

print(all_answer)

Submission status: Time Limit Exceeded

Solution code:
import fractions
n=int(input())
k=input().split()
j=0
m=0
left_gcd=0
right_gcd=0
all_answer=0

input_list= [int(i) for i in k]
i=0
for i in range(n):
  m = i+1
  for j in range(i):
    left_gcd=fractions.gcd(left_gcd,input_list[j])
  for m in range(i+1,n):
    right_gcd=fractions.gcd(right_gcd,input_list[m])

  answer=fractions.gcd(left_gcd,right_gcd)
  all_answer=max(all_answer,answer)
  j=0
  m=0
  left_gcd=0
  right_gcd=0

print(all_answer)

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
T = list(map(int,input().split()))
T.sort()
ANSL = []
for i in range(len(T)):
    S = copy.copy(T)
    S.pop(i)
    ANS = 0
    while True:
        S.sort()
        LIS = []
        if S[0] == 1:
            ANS += 1
            break
        elif len(S) == 1:
            ANS += S[0]
            break
        else:
            LIS.append(S[0])
            for k in range(len(S)-1):
                if (S[k+1] % S[k] == 0) or (S[k+1] % LIS[0] == 0):
                    continue
                else:
                    LIS.append(S[k+1] % S[k])
                    LIS.sort()
        S = LIS
    ANSL.append(ANS)
print(max(ANSL))

Submission status: Time Limit Exceeded

Solution code:
def gcd(a,b):
    if b > a:
        a, b = b, a
    while b:
        a, b = b, a%b
    return a
N = int(input())
T = list(map(int,input().split()))
T.sort()
ANSLF = []
for i in range(len(T)):
    S = []
    for j in range(len(T)):
        S.append(T[j])
    S.pop(i)
    ANSL = [S[0]]
    for k in range(len(S)-1):
        ANSL.append(gcd(ANSL[k],S[k+1]))
    ANSLF.append(min(ANSL))
print(max(ANSLF))

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions

N = int(input())
T = list(map(int,input().split()))
T.sort()
ANSL = []
for i in range(len(T)):
    S = []
    for j in range(len(T)):
        S.append(T[j])
    S.pop(i)
    ANS = functools.reduce(fractions.gcd, S)
    ANSL.append(ANS)
print(max(ANSL))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
T = list(map(int,input().split()))
T.sort()
ANSL = []
for i in range(len(T)):
    S = []
    for j in range(len(T)):
        S.append(T[j])
    S.pop(i)
    ANS = 0
    while True:
        S.sort()
        LIS = []
        if S[0] == 1:
            ANS += 1
            break
        elif len(S) == 1:
            ANS += S[0]
            break
        else:
            LIS.append(S[0])
            for k in range(len(S)-1):
                if (S[k+1] % S[k] == 0) or (S[k+1] % LIS[0] == 0):
                    continue
                else:
                    LIS.append(S[k+1] % S[k])
                    LIS.sort()
        S = LIS
    ANSL.append(ANS)
print(max(ANSL))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
T = list(map(int,input().split()))
T.sort()
ANSL = []
for i in range(len(T)):
    S = []
    for j in range(len(T)):
        S.append(T[j])
    S.pop(i)
    ANS = 0
    while True:
        S.sort()
        LIS = []
        if S[0] == 1:
            ANS += 1
            break
        elif len(S) == 1:
            ANS += S[0]
            break
        else:
            LIS.append(S[0])
            for k in range(len(S)-1):
                if S[k+1] % S[k] == 0:
                    continue
                else:
                    LIS.append(S[k+1] % S[k])
        S = LIS
    ANSL.append(ANS)
print(max(ANSL))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd


def checkN(num):
    try:
        if(2 <= num <= 10**5):
            return True
        else:
            return False

    except TypeError:
        return False


def checkAi(numList, N):
    try:
        isValid = True
        if(len(numList) != N):
            return False

        for num in numList:
            if(not 1 <= num <= 10**9):
                isValid = False
        return isValid

    except TypeError:
        return False


def getValidN():
    n = 0
    while True:
        n = int(input())
        if(checkN(n)):
            break
        print("2以上、10^5以下の数字を入力してください")
    return n


def getValidA(n):
    numList = []
    while True:
        strList = input()
        numList = [int(num) for num in strList.split()]
        if(checkAi(numList, n)):
            break
        print("1以上、10^9以下の数の並びを、Nと一緒の数だけスペースで区切って入力してください")
    return numList


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


if __name__ == "__main__":
    n = getValidN()
    numList = getValidA(n)

    ans = 0
    for i in range(n):
        li = numList[:i] + numList[i + 1:]
        tmp = gcd_list(li)
        if tmp > ans:
            ans = tmp

    print(str(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list_A = list(map(int, input().split()))
ansLIST = []
def gcd_1(x, y):
    if y == 0:
        return x
    else:
        return gcd_1(y, x % y)
def gcd(list):
    Gcd = list[0]
    for i in range(len(list)):
        Gcd = gcd_1(list[i],Gcd)
    return Gcd
for i in range(N):
    listcurrent = list_A[:i]
    listcurrent.extend(list_A[i+1:])
    ansLIST.append(gcd(listcurrent))
print(max(ansLIST))

Submission status: Time Limit Exceeded

Solution code:
import fractions
n=int(input())
k=input().split()
j=0
m=0
left_gcd=0
right_gcd=0
all_answer=0

answer_list=[]
input_list= [int(i) for i in k]
i=0
for i in range(n):
  m = i+1
  while j<i:
    left_gcd=fractions.gcd(left_gcd,input_list[j])
    j=j+1
  while (m < n):
    right_gcd=fractions.gcd(right_gcd,input_list[m])
    m=m+1

  answer=fractions.gcd(left_gcd,right_gcd)
  all_answer=max(all_answer,answer)
  j=0
  m=0
  left_gcd=0
  right_gcd=0

print(all_answer)

Submission status: Time Limit Exceeded

Solution code:
import fractions
n=int(input())
k=input().split()
i=0
j=0
m=0
left_gcd=0
right_gcd=0

answer_list=[]
input_list= [int(i) for i in k]
i=0
for i in range(n):
  m = i+1
  while j<i:
    left_gcd=fractions.gcd(left_gcd,input_list[j])
    j=j+1
  while (m < n) :
    right_gcd=fractions.gcd(right_gcd,input_list[m])
    m=m+1

  answer=fractions.gcd(left_gcd,right_gcd)
  answer_list.append(answer)
  j=0
  m=0
  left_gcd=0
  right_gcd=0

print(max(answer_list))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
    tmp_arr = A[:i] + A[i+1:]
    ans_tmp = reduce(gcd,tmp_arr)
    if ans < ans_tmp:
        ans = ans_tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(x, y):
    xx = 0; yy = 0
    if x >= y:
        xx = x; yy = y
    else:
        xx = y; yy = x
    while yy > 0:
        xx, yy = yy, xx%yy
    return xx


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    ans = 1
    for i in range(n):
        tmp = -1
        for j in range(n):
            if j == i:
                continue
            if tmp == -1:
                tmp = a[j]
            tmp = gcd(tmp, a[j])
        ans = max(ans, tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from itertools import accumulate
N=int(input())
A=[int(i) for i in input().split()]
GCD=[]
if len(A)==2:
    print(max(A))
else:
    for i in range(N):
        B=A.copy()
        B.remove(B[i])
        C=list(accumulate(B,gcd))
        GCD.append(C[-1])
    print(max(GCD))

Submission status: Time Limit Exceeded

Solution code:
def gcd(x,y):
    while y!=0:
        x,y=y,x%y
    else:
        return x

N=int(input())
A=[int(i) for i in input().split()]
GCD=[]
if len(A)==2:
    print(max(A))
else:
    for i in range(N):
        B=A.copy()
        B.remove(B[i])
    
        a=gcd(B[0],B[1])
        for k in range(N-3):
            a=gcd(a,B[k+2])
        GCD.append(a)
    print(max(GCD))

Submission status: Time Limit Exceeded

Solution code:
def GCD(a, b):
    """ Euclidean algorithm"""
    if a < b: (a, b) = (b, a)
    if b == 0: return a
    m = a % b
    while m != 0:
        a = b
        b = m
        m = a % b
    return b

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

if N < 2 and 1e5 < N: raise()
for v in A:
    if v < 1 and v < 1e9: raise()

l_list = [0]
r_list = [0]
j = -1
for i in range(0, N):
    l_list.append(    GCD(l_list[i],  A[i]) )
    r_list.insert( 0, GCD(r_list[j],  A[j]) )
    j -= 1
n = []
for i in range(0, N):
    n.append(GCD(l_list[i], r_list[i+1]))
print(max(n))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
import copy
from functools import reduce

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

def plus(a,b):
    return a+b

def gcd2(n,a):
    array = []
    for i in range(n):
        change_list = a.copy()
        del change_list[i]
        array.append((reduce(gcd,change_list)))
    return max(array)


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

print(gcd2(n,a))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

N = int(input())
A = list(map(int,input().split()))
g_max = 0
for i in range(N):
    B = A.copy()
    B[i:i+1] = []
    g = reduce(fractions.gcd,B)
    if g_max < g:
        g_max = g
print(g_max)

Submission status: Time Limit Exceeded

Solution code:
def GCD(a, b):
    """ Euclidean algorithm"""
    if a < b: (a, b) = (b, a)
    if b == 0: return a
    m = a % b
    while m != 0:
        a = b
        b = m
        m = a % b
    return b

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

if N < 2 and 1e5 < N: raise()
for v in A:
    if v < 1 and v < 1e9: raise()

result = []
for skip in range(0, N):
    l = 0
    r = 0
    for i in range(0, skip):
        l = GCD(l, A[i])
    for i in range(skip+1, N):
        r = GCD(r, A[i])
    n = GCD(l, r)
#   print(l,r,n)
    result.append(n)
print(max(result))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
from functools import reduce

def gcd(n,a):
    array = []
    for i in range(n):
        change_list = copy.copy(a)
        del change_list[i]
        array.append((reduce(fractions.gcd,change_list)))
    return max(array)


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

print(gcd(n,a))

Submission status: Time Limit Exceeded

Solution code:
def GCD(a, b):
    """ Euclidean algorithm"""
    if a < b: (a, b) = (b, a)
    if b == 0: return a
    m = a % b
    while m != 0:
        a = b
        b = m
        m = a % b
    return b

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

if N < 2 and 1e5 < N: raise()
for v in A:
    if v < 1 and v < 1e9: raise()

result = []
for skip in range(N):
    l = 0
    r = 0
    for v in range(0, skip):
        l = GCD(l, A[v])
    for v in range(N-1, skip, -1):
        r = GCD(r, A[v])
    result.append(GCD(l, r))

print(max(result))