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 importlib
import_module = lambda name:importlib.import_module(name)

input = import_module('sys').stdin.readline
input_int = lambda:int(input())
input_ints = lambda:map(int,input().split())
input_ints_list = lambda:list(input_ints())
input_str = lambda:input()
input_strs = lambda:input().split()
input_lines = lambda n,f:[f() for _ in range(n)]

gcd_base = lambda a,b:import_module('fractions').gcd(a,b) # 最大公約数（2値)
gcd = lambda a:import_module('functools').reduce(gcd_base,a) # 最大公約数（リスト）
lcm_base = lambda a,b:(a*b)//import_module('fractions').gcd(a,b) # 最小公倍数(2値)
lcm = lambda a:import_module('functools').reduce(lcm_base,a,1) # 最小公倍数(リスト)

permutations = lambda a,n:import_module('itertools').permutations(a,n) # 順列
combinations = lambda a,n:import_module('itertools').combinations(a,n) # 組み合わせ
product = lambda a,b:import_module('itertools').product(a,b) # 二つのリストの直積

init_array_1dim = lambda v,n:[v]*n
init_array_2dim = lambda v,n,m:[init_array_1dim for _ in range(m)]

# 四捨五入はround
ceil = lambda a:import_module('math').ceil(a) # 切り上げ
floor = lambda a:import_module('math').floor(a) # 切り捨て

# キュー
convert_list_to_queue = lambda a:import_module('collections').deque(a)
pop_q = lambda a:a.popleft()
push_q = lambda a,e:a.appendleft(e)
push_q_list = lambda a,l:a.extendleft(l) 

# プライオリティキュー
def init_head(a):import_module('heapq').heapify(a)
def heap_push(a,v):import_module('heapq').heappush(a,v)
heap_pop = lambda a:import_module('heapq').heappop(a) # 最小値を取り出す

# 二分探索
bisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)
bisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)
insert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))
insert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))

# 累積和、累積積、累積GCD
cumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) # 累積和
cumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) # 累積積
cumgcd = lambda a:import_module('numpy').frompyfunc(gcd_base, 2, 1).accumulate(a, dtype=import_module('numpy').object).astype(import_module('numpy').int) # 累積GCD

class UnionFind(object):
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

# グラフで全部到達できる経路が何個あるか
def dfs_graph(v,n,graph,visited):
    if all(visited):return 1 # 全部到達できたら1を返す
    ans = 0
    for i in range(n):
        if not graph[v][i]:continue # 結ぶ線がないときはスキップ
        if visited[i]:continue # 到達済みだったらスキップ
        visited[i] = True
        ans += dfs_graph(i,n,graph,visited)
        visited[i] = False
    return ans

# ワーシャルフロイド（グラフの要素数、グラフ）
def warshall_floyd(n,graph):
    d = init_array_2dim(float("inf"),n,n)
    # 初期化
    for i in range(n):
        d[i][i] = 0
        for j in range(n):
            if graph[i][j]:d[i][j] = 1
    # 距離算出
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

# グラフを初期化（グラフの要素数、グラフの線の両端、有向無向）
def init_graph(n,a,directed=False):
    visited = init_array_1dim(False,n)
    graph = init_array_2dim(False,n,n)
    for e in a:
        graph[e[0]-1][e[1]-1] = True
        if not directed:graph[e[1]-1][e[0]-1] = True # 無向グラフの場合
    return visited,graph

def two_pointers(s,threshold,ope='sum'):
    l,r = 0,0
    base = 0 
    if ope == 'sum':base = 0
    if ope == 'prd':base = 1
    diff = 0
    while r < len(s):
        val = 0
        if ope == 'sum':val = base+s[r]
        if ope == 'prd':val = base*s[r]
        if val <= threshold:
            if ope == 'sum':base += s[r]
            if ope == 'prd':base *= s[r]
            diff = max(diff, r-l+1)
            r += 1
        elif l == r:
            r += 1
            l += 1
        else:
            if ope == 'sum':base -= s[l]
            if ope == 'prd':base //= s[l]
            l += 1
    return diff

def solution():
    import numpy as np
    N = input_int()
    A = np.array(input_ints_list())

    gcds = [gcd(np.delete(A,i).tolist()) for i in range(len(A))]
    
    print(max(gcds))
	
if __name__ == '__main__':
    solution()

Submission status: Time Limit Exceeded

Solution code:
import importlib
import_module = lambda name:importlib.import_module(name)

input = import_module('sys').stdin.readline
input_int = lambda:int(input())
input_ints = lambda:map(int,input().split())
input_ints_list = lambda:list(input_ints())
input_str = lambda:input()
input_strs = lambda:input().split()
input_lines = lambda n,f:[f() for _ in range(n)]

gcd_base = lambda a,b:import_module('fractions').gcd(a,b) # 最大公約数（2値)
gcd = lambda a:import_module('functools').reduce(gcd_base,a) # 最大公約数（リスト）
lcm_base = lambda a,b:(a*b)//import_module('fractions').gcd(a,b) # 最小公倍数(2値)
lcm = lambda a:import_module('functools').reduce(lcm_base,a,1) # 最小公倍数(リスト)

permutations = lambda a,n:import_module('itertools').permutations(a,n) # 順列
combinations = lambda a,n:import_module('itertools').combinations(a,n) # 組み合わせ
product = lambda a,b:import_module('itertools').product(a,b) # 二つのリストの直積

init_array_1dim = lambda v,n:[v]*n
init_array_2dim = lambda v,n,m:[init_array_1dim for _ in range(m)]

# 四捨五入はround
ceil = lambda a:import_module('math').ceil(a) # 切り上げ
floor = lambda a:import_module('math').floor(a) # 切り捨て

# キュー
convert_list_to_queue = lambda a:import_module('collections').deque(a)
pop_q = lambda a:a.popleft()
push_q = lambda a,e:a.appendleft(e)
push_q_list = lambda a,l:a.extendleft(l) 

# プライオリティキュー
def init_head(a):import_module('heapq').heapify(a)
def heap_push(a,v):import_module('heapq').heappush(a,v)
heap_pop = lambda a:import_module('heapq').heappop(a) # 最小値を取り出す

# 二分探索
bisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)
bisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)
insert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))
insert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))

# 累積和、累積積、累積GCD
cumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) # 累積和
cumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) # 累積積
cumgcd = lambda a:import_module('numpy').frompyfunc(gcd_base, 2, 1).accumulate(a, dtype=import_module('numpy').object).astype(import_module('numpy').int) # 累積GCD

class UnionFind(object):
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

# グラフで全部到達できる経路が何個あるか
def dfs_graph(v,n,graph,visited):
    if all(visited):return 1 # 全部到達できたら1を返す
    ans = 0
    for i in range(n):
        if not graph[v][i]:continue # 結ぶ線がないときはスキップ
        if visited[i]:continue # 到達済みだったらスキップ
        visited[i] = True
        ans += dfs_graph(i,n,graph,visited)
        visited[i] = False
    return ans

# ワーシャルフロイド（グラフの要素数、グラフ）
def warshall_floyd(n,graph):
    d = init_array_2dim(float("inf"),n,n)
    # 初期化
    for i in range(n):
        d[i][i] = 0
        for j in range(n):
            if graph[i][j]:d[i][j] = 1
    # 距離算出
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

# グラフを初期化（グラフの要素数、グラフの線の両端、有向無向）
def init_graph(n,a,directed=False):
    visited = init_array_1dim(False,n)
    graph = init_array_2dim(False,n,n)
    for e in a:
        graph[e[0]-1][e[1]-1] = True
        if not directed:graph[e[1]-1][e[0]-1] = True # 無向グラフの場合
    return visited,graph

def two_pointers(s,threshold,ope='sum'):
    l,r = 0,0
    base = 0 
    if ope == 'sum':base = 0
    if ope == 'prd':base = 1
    diff = 0
    while r < len(s):
        val = 0
        if ope == 'sum':val = base+s[r]
        if ope == 'prd':val = base*s[r]
        if val <= threshold:
            if ope == 'sum':base += s[r]
            if ope == 'prd':base *= s[r]
            diff = max(diff, r-l+1)
            r += 1
        elif l == r:
            r += 1
            l += 1
        else:
            if ope == 'sum':base -= s[l]
            if ope == 'prd':base //= s[l]
            l += 1
    return diff

def solution():
    import numpy as np
    N = input_int()
    A = np.array(input_ints_list())
    A[::-1].sort()

    gcds = [gcd(np.delete(A,i).tolist()) for i in range(len(A))]
    
    print(max(gcds))
	
if __name__ == '__main__':
    solution()

Submission status: Time Limit Exceeded

Solution code:
import importlib
import_module = lambda name:importlib.import_module(name)

input = import_module('sys').stdin.readline
input_int = lambda:int(input())
input_ints = lambda:map(int,input().split())
input_ints_list = lambda:list(input_ints())
input_str = lambda:input()
input_strs = lambda:input().split()
input_lines = lambda n,f:[f() for _ in range(n)]

gcd_base = lambda a,b:import_module('fractions').gcd(a,b) # 最大公約数（2値)
gcd = lambda a:import_module('functools').reduce(gcd_base,a) # 最大公約数（リスト）
lcm_base = lambda a,b:(a*b)//import_module('fractions').gcd(a,b) # 最小公倍数(2値)
lcm = lambda a:import_module('functools').reduce(lcm_base,a,1) # 最小公倍数(リスト)

permutations = lambda a,n:import_module('itertools').permutations(a,n) # 順列
combinations = lambda a,n:import_module('itertools').combinations(a,n) # 組み合わせ
product = lambda a,b:import_module('itertools').product(a,b) # 二つのリストの直積

init_array_1dim = lambda v,n:[v]*n
init_array_2dim = lambda v,n,m:[init_array_1dim for _ in range(m)]

# 四捨五入はround
ceil = lambda a:import_module('math').ceil(a) # 切り上げ
floor = lambda a:import_module('math').floor(a) # 切り捨て

# キュー
convert_list_to_queue = lambda a:import_module('collections').deque(a)
pop_q = lambda a:a.popleft()
push_q = lambda a,e:a.appendleft(e)
push_q_list = lambda a,l:a.extendleft(l) 

# プライオリティキュー
def init_head(a):import_module('heapq').heapify(a)
def heap_push(a,v):import_module('heapq').heappush(a,v)
heap_pop = lambda a:import_module('heapq').heappop(a) # 最小値を取り出す

# 二分探索
bisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)
bisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)
insert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))
insert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))

# 累積和、累積積、累積GCD
cumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) # 累積和
cumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) # 累積積
cumgcd = lambda a:import_module('numpy').frompyfunc(gcd_base, 2, 1).accumulate(a, dtype=import_module('numpy').object).astype(import_module('numpy').int) # 累積GCD

class UnionFind(object):
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

# グラフで全部到達できる経路が何個あるか
def dfs_graph(v,n,graph,visited):
    if all(visited):return 1 # 全部到達できたら1を返す
    ans = 0
    for i in range(n):
        if not graph[v][i]:continue # 結ぶ線がないときはスキップ
        if visited[i]:continue # 到達済みだったらスキップ
        visited[i] = True
        ans += dfs_graph(i,n,graph,visited)
        visited[i] = False
    return ans

# ワーシャルフロイド（グラフの要素数、グラフ）
def warshall_floyd(n,graph):
    d = init_array_2dim(float("inf"),n,n)
    # 初期化
    for i in range(n):
        d[i][i] = 0
        for j in range(n):
            if graph[i][j]:d[i][j] = 1
    # 距離算出
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

# グラフを初期化（グラフの要素数、グラフの線の両端、有向無向）
def init_graph(n,a,directed=False):
    visited = init_array_1dim(False,n)
    graph = init_array_2dim(False,n,n)
    for e in a:
        graph[e[0]-1][e[1]-1] = True
        if not directed:graph[e[1]-1][e[0]-1] = True # 無向グラフの場合
    return visited,graph

def two_pointers(s,threshold,ope='sum'):
    l,r = 0,0
    base = 0 
    if ope == 'sum':base = 0
    if ope == 'prd':base = 1
    diff = 0
    while r < len(s):
        val = 0
        if ope == 'sum':val = base+s[r]
        if ope == 'prd':val = base*s[r]
        if val <= threshold:
            if ope == 'sum':base += s[r]
            if ope == 'prd':base *= s[r]
            diff = max(diff, r-l+1)
            r += 1
        elif l == r:
            r += 1
            l += 1
        else:
            if ope == 'sum':base -= s[l]
            if ope == 'prd':base //= s[l]
            l += 1
    return diff

def solution():
    N = input_int()
    A = input_ints_list()

    gcd_notas = []
    for i in range(len(A)):
        nota = [A[j] for j in range(len(A)) if j != i]
        gcd_nota = gcd(nota)
        gcd_notas.append(gcd_nota)
    
    print(max(gcd_notas))
	
if __name__ == '__main__':
    solution()

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    b=list(a)
    del b[i]
    gc=b[0]
    for j in range(n-1):
        gc=gcd(gc,b[j])
        if gc<ggcd:
            break
    ggcd=max(gc,ggcd)

print(ggcd)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    b=list(a)
    del b[i]
    gc=b[0]
    for j in range(n-1):
        gc=gcd(gc,b[j])
    ggcd=max(gc,ggcd)

print(ggcd)

Submission status: Time Limit Exceeded

Solution code:
import fractions as f
N = int(input())
A = [int(j) for j in input().split()]
gcdmx = 0
for j in range(0,N):
    gcdloc = 0
    C = A[:]
    del(C[j])
    for k in range(0,N-1):
        gcdloc = f.gcd(gcdloc,C[k])
    gcdmx = max(gcdloc,gcdmx)
print(gcdmx)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

target = []
for i in range(N):
  new = A.copy()
  new.pop(i)
  target.append(new)
  
ans = [0]
for num in target:
  ans.append(gcd_list(num))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def reduce_gcd(A):
    from fractions import gcd as GCD
    gcd = A[0]
    for i in A:
        gcd = GCD(gcd, i)
    return gcd
 
N = int(input())
A = list(map(int, input().split(" ")))
 
MAX = 0;
for i in range(N):
    a = A.pop()
 
    gcd = reduce_gcd(A)
 
    if MAX < gcd:
        MAX = gcd
 
    A.insert(0, a)
 
print(MAX)

Submission status: Time Limit Exceeded

Solution code:
def reduce_gcd(A):
    from fractions import gcd as GCD
    gcd = A[0]
    for i in A:
        gcd = GCD(gcd, i)
    return gcd

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

MAX = 0;
for i in range(N):
    a = A.pop()

    gcd = reduce_gcd(A)

    if MAX < gcd:
        MAX = gcd

    A.insert(0, a)

print(MAX)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

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


MAX = 0;
for i in range(N):
    a = A.pop()

    kkgcd = reduce(gcd, A)

    if MAX < kkgcd:
        MAX = kkgcd

    A.insert(0, a)

print(MAX)

Submission status: Time Limit Exceeded

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

# print(A)

if N == 2:
    print(max(A))
    sys.exit()

GCD = [0] * N

for i in range(N):
    B = copy.copy(A)
    del B[i]
    b = B[0]
    for j in range(1, N-1):
        b = fr.gcd(b, B[j])
    GCD[i] = b

print(max(GCD))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 0
lgcd = [li[0]]
rgcd = [li[-1]]

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

for i in range(n-1):
    lgcd.append(gcd(lgcd[-1],li[i+1]))
    rgcd.insert(0,gcd(rgcd[0],li[-(i+2)]))

gcd_list = [gcd(lgcd[i],rgcd[i+2]) for i in range(n-2)]

gcd_list.append(lgcd[-2])
gcd_list.append(rgcd[1])

print(max(gcd_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
num = input().rstrip().split(" ")
Ai = []
M =[]


def gcd_list(numbers):
  def gcd(a,b):
    while b!=0:
      a,b=b,a%b
    return a
  
  ans = numbers[0]
  for i in range(1, N-1):
    ans = gcd(ans, numbers[i])
  return ans

for i in range(N):
  Ai.append(int(num[i]))
for i in range(N):
  a = Ai.pop(i)
  M.append(gcd_list(Ai))
  Ai.insert(i,a)

print(max(M))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 0
lgcd = [li[0]]
rgcd = [li[-1]]

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

for i in range(n-1):
    lgcd.append(gcd(lgcd[-1],li[i+1]))
    rgcd.insert(0,gcd(rgcd[0],li[-(i+2)]))

gcd_list = [lgcd[-2],rgcd[1]]

for i in range(n-2):
    gcd_list.append(gcd(lgcd[i],rgcd[i+2]))

print(max(gcd_list))

Submission status: Time Limit Exceeded

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


def calc_gcd(nums):
    return reduce(gcd, nums)


def main(input_1, input_2):
    nums_list = list(map(list, list(combinations(input_2, len(input_2)-1))))

    gcd_list = []
    for nums in nums_list:
        calced = calc_gcd(nums)
        gcd_list.append(calced)

    return max(gcd_list)


if __name__ == "__main__":
    input_1 = input()
    input_2 = list(map(int, input().split()))
    print(main(input_1, input_2))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

Submission status: Time Limit Exceeded

Solution code:
import copy

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

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

if n>2:
    memo = [1]

    for k in range(n):
        t = copy.copy(s_all)
        t.pop(k)

        c = gcd(t[0], t[1])

        for i in t:
            c = gcd(c,i)
            if c<max(memo):
                break

        memo.append(c)

    print(max(memo))

if n==2:
    print(max(s_all))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

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

if n>2:
    memo = list([])

    for k in range(n):
        t = copy.copy(s_all)
        t.pop(k)

        c = gcd(t[0], t[1])

        for i in t:
            c = gcd(c,i)

        memo.append(c)

    print(max(memo))

if n==2:
    print(max(s_all))

Submission status: Time Limit Exceeded

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

# set_a = set(a)
set_a = a
set_a = list(set_a)
len_set_a = len(set_a)
if len_set_a ==1:
    print(set_a[0])
else:
    set_a.append(0)
    set_a.insert(0, 0)
    cd = []
    for i, ai in enumerate(set_a[:len(set_a)-1]):
        common_divisor = 0
        for li in set_a[:i]:
            common_divisor = fractions.gcd(common_divisor,li)
        for ri in set_a[i+1:]:
            common_divisor = fractions.gcd(common_divisor, ri)
        cd.append(common_divisor)
    print(max(cd))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

max_gcd = 0
for i in range(n):
  bbb = aaa[0:i]
  bbb.extend(aaa[i+1:])
  candidate = gcd(*bbb)
  if max_gcd < candidate:
    max_gcd = candidate

print(max_gcd)

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())
aaa = [int(s) for s in input().split()]
max_gcd = 0

for i in range(n):
    bbb = aaa[0:i]
    bbb.extend(aaa[i+1:])
    if max_gcd < gcd_list(bbb):
        max_gcd = gcd_list(bbb)

print(max_gcd)

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

M_list = []
for i in range(n):
    alist = Alist[:i] + Alist[i+1:]
    M_list.append(gcd_L(alist))

print(max(M_list))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import operator
import sys

sys.setrecursionlimit(10000)
INF = float('inf')


class SegmentTree:
    # http://tsutaj.hatenablog.com/entry/2017/03/29/204841
    def __init__(self, size, fn=operator.add, default=0):
        """
        :param int size:
        :param callable fn: 区間に適用する関数。引数を 2 つ取る。min, max, operator.xor など
        """
        # size 以上である最小の 2 冪
        n = 1
        while n < size:
            n *= 2
        self._size = n
        self._tree = [default for _ in range(self._size * 2 - 1)]
        self._fn = fn

    def set(self, i, value):
        """
        i 番目に value を設定
        :param int i:
        :param int value:
        :return:
        """
        x = self._size - 1 + i
        self._tree[x] = value

        while x > 0:
            x = (x - 1) // 2
            self._tree[x] = self._fn(self._tree[x * 2 + 1], self._tree[x * 2 + 2])

    def add(self, i, value):
        """
        もとの i 番目と value に fn を適用したものを i 番目に設定
        :param int i:
        :param int value:
        :return:
        """
        x = self._size - 1 + i
        self.set(i, self._fn(self._tree[x], value))

    def get(self, from_i, to_i, k=0, L=None, r=None):
        """
        [from_i, to_i) に fn を適用した結果を返す
        :param int from_i:
        :param int to_i:
        :param int k: self._tree[k] が、[L, r) に fn を適用した結果を持つ
        :param int L:
        :param int r:
        :return:
        """
        L = 0 if L is None else L
        r = self._size if r is None else r

        if from_i <= L and r <= to_i:
            return self._tree[k]

        if to_i <= L or r <= from_i:
            return None

        ret_L = self.get(from_i, to_i, k * 2 + 1, L, (L + r) // 2)
        ret_r = self.get(from_i, to_i, k * 2 + 2, (L + r) // 2, r)
        if ret_L is None:
            return ret_r
        if ret_r is None:
            return ret_L
        return self._fn(ret_L, ret_r)

    def __len__(self):
        return self._size


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

st = SegmentTree(size=N, fn=fractions.gcd, default=0)
for i, a in enumerate(A):
    st.set(i, a)

ans = 0
for i in range(N):
    ans = max(ans, fractions.gcd(st.get(0, i) or 0, st.get(i + 1, N) or 0))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions

number_of_integers = int(input())
integers = list(map(int, input().split()))

holed_integers = []
gcds = [0] * number_of_integers
for i in range(number_of_integers):
    holed_integers = integers[:i] + integers[i+1:]
    for hole_integer in holed_integers:
        gcds[i] = fractions.gcd(gcds[i], hole_integer)

print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache, reduce

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

@lru_cache(maxsize=100000)
def gcd(a, b):
    a , b = (a, b) if a > b else (b, a)
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
        pass
    return b

ans = max([ reduce(gcd, ( As[j] for j in range(N) if j != i )) for i in range(N) ])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

A=np.array(A)





for i in range(sorted(A)[1],1,-1):
	if(np.sum(A%i==0)>=N-1):
		print(i)
		exit()
print(1)

Submission status: Time Limit Exceeded

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

def gcd_arr(arr, N):
    if N == 2:
        return gcd(arr[0], arr[1])
    elif N == 1:
        return arr[0]
    else:
        mid = N // 2
        return gcd(gcd_arr(arr[:mid], mid), gcd_arr(arr[mid:], N-mid))

def gcd_arr_with_rm(arr, N, remove = 0):
    return gcd_arr(arr[0:remove] + arr[(remove+1):], N - 1)

def solve():
    N = int(raw_input())
    arr = [int(_) for _ in raw_input().split()]
    max_gcd = 1
    for j in range(N):
        tmp = gcd_arr_with_rm(arr, N, remove = j)
        if tmp > max_gcd:
            max_gcd = tmp
    print str(max_gcd)

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
N = int(input())
A = [int(i) for i in input().split()]

Answer = 0

for i in range(N):
  B = list(A)
  del B[i]
  G = gcd_list(B)
  if(Answer < G):
    Answer = G

print(Answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
 
from functools import reduce
def gcd(x,y=0):
    while y:
        x,y = y, x % y
    return x
 
ans = max(reduce(gcd,a[1:n]),reduce(gcd,a[0:n-1]))
if n>2:
    for i in range(1,n - 1):
        ans = max(ans,gcd(reduce(gcd,a[0:i]),reduce(gcd,a[i + 1:n])))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import fractions
import copy

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

n = int(input())

a = input()

ans = []
for i in range(n):
  As = [int(a.split()[i]) for i in range(n)]
  As.pop(i)
  gcd = GCD(As)
  ans.append(gcd)
#  if gcd > ans:
#    ans = gcd

ans.sort()

print(ans[-1])

Submission status: Time Limit Exceeded

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

N = int(input())
yaku = [0]*N
X = input().split()
X = list(map(int, X))


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


for i in range(N):
    Xwork = copy.copy(X)
    del(Xwork[i])
    yaku[i] = gcd_list(Xwork)

print(max(yaku))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import fractions
import copy

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

n = int(input())

a = input()

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

ans = 0
for i in range(n):
  a_copy = copy.deepcopy(As)
  a_copy.pop(i)
  gcd = GCD(a_copy)
  if gcd > ans:
    ans = gcd

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(*A):
    A = set(A)
    tmp = min(A)
    B = {ai % tmp for ai in A if ai % tmp > 0}
    B.add(tmp)
    for _ in range(100000000000000):
        A = B
        tmp = min(A)
        B = {ai % tmp for ai in A if ai % tmp > 0}
        if len(B) == 0: break 
        B.add(tmp)
    return tmp    


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

print(max(gcd(*(A[j] for j in range(N) if i != j)) for i in range(N)))

Submission status: Time Limit Exceeded

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

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

gcd_list = []
for skip in range(N):
    ans = max(A)
    for i in range(N):
        if i == skip: continue
        buf = gcd(ans, A[i])
        if ans > buf:
            ans = buf
    gcd_list.append(ans)

gcd_list.sort()
print(gcd_list[-1])

Submission status: Time Limit Exceeded

Solution code:
N, = list(map(int, input().split()))
A  = list(map(int, input().split()))
P = [0 for i in range(10**5)]
for i in range(2, 10**5):
  if P[i]==1:
    continue
  for j in range(i*2, 10**5, i):
    P[j]=1
p = [i for i in range(2, 10**5) if P[i]==0 and i*i<=10**9]
k = set()
pf = [{} for i in range(N)]
for i in range(N):
  for j in p:
    while A[i]%j==0:
      pf[i][j] = pf[i].get(j,0)+1
      A[i] //= j
  if A[i]>1:
    pf[i][A[i]] = pf[i].get(A[i],0)+1
    if len(k)<2:
      k.add(A[i])
k |= set(p)
pfr = {}
for j in k:
  r = [pf[i].get(j,0) for i in range(N)]
  r.sort()
  pfr[j]=r[:2]
mcd = [1 for i in range(N)]
for i in range(N):
  for j in k:
    mcd[i] *= j**(pfr[j][1] if pf[i].get(j,0)==pfr[j][0] else pfr[j][0])
print(max(mcd))

Submission status: Time Limit Exceeded

Solution code:
#https://atcoder.jp/contests/abc125
# -*- coding: utf-8 -*-

import fractions
from functools import reduce

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

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

gcdList = []
for i in range(n):
        gcdList.append( getGcd(a[0:i]+a[i+1:n]) )

print( max(gcdList) )