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 sys
from fractions import gcd


def gcd_n(args):
    acc = args[0]
    for n in args[1:]:
        acc = gcd(acc, n)

    return acc

def parse(file):
    n = int(file.readline())
    nums = list(map(int, file.readline().split()))
    return n, nums

def main(n, nums):
    ans = 0
    for i in range(n):
        subnums = nums[:i] + nums[i+1:]
        ans_cand = gcd_n(subnums)
        if ans_cand > ans:
            ans = ans_cand

    return ans

if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Time Limit Exceeded

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

max = max(a)
for i in range(max,0,-1):
    b = [x%i for x in a]
    # print(b, i, b.count(0))
    if b.count(0) >= n-1:
        break
print(i)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

print(result)

Submission status: Time Limit Exceeded

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

j = max(A)

while(1):
    n = 0
    for i in range(N):
        if(A[i] % j != 0):
            n+=1
    if(n<2):
        print(j)
        break
    j -=1

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
N=int(input())
A=list(map(int, input().split()))
Z=0
ans=0
for i in range(N):
    if i==0:
        B=A.copy()
        B[0]=B[1]
        ans=functools.reduce(fractions.gcd, B)


    else :
        B=A.copy()
        B[i]=B[0]
        Z =functools.reduce(fractions.gcd, B)


    if ans >Z:
        ans=ans
    else:
        ans=Z

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd as gcd_
from functools import reduce
def gcd(numbers):
  return reduce(gcd_, numbers)
I=lambda:list(map(int,input().split()))

N=int(input())
A=I()

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
c = a
d = 0


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


def gcd2(x):
    o = 0
    for i in range(N-1):
        if i == 0:
            o = gcd(x[i], x[i + 1])
        else:
            o = gcd(o, x[i + 1])
    return o


for i in range(N):
    if i != N - 1:
        r = c[i]
        c[i] = c[i + 1]
        e = gcd2(c)
        c[i] = r
        if d < e:
            d = e
    else:
        c[i] = c[0]
        e = gcd2(c)
        if d < e:
            d = e
print(d)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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


import fractions
from functools import reduce

ans=0
proc=lambda x: reduce(fractions.gcd, x)
GCD_target=proc(A)
#print("GCD_target = {}".format(GCD_target))

for i in range(N):
   #copied_A = A[:]
   remove=A.popleft()
   
   GCD_current= proc(A) #gcd_list(list(A))
   A.append(remove)
   #print(GCD_current)
   ans=max(ans,GCD_current)

print(ans)

Submission status: Time Limit Exceeded

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

@lru_cache(maxsize=100000)
def gcd(a, b):
    return reduce(fractions.gcd, [a, b])

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

ans = 1

for i in range(n):
    tmp_list = copy.copy(a)
    tmp_list.pop(i)
    tmp = tmp_list[0]
    for i in range(len(tmp_list)):
        tmp = gcd(tmp, tmp_list[i])
        if tmp < ans:
            break
    if ans < tmp:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

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

n = int(input())

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

ans = 1

for i in range(n):
	aa = a[:]
	aa.pop(i)
	temp = gcd_list(aa)
	if(temp > ans):
		ans = temp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce


def gcdd(x, y):
    x, y = max(x, y), min(x, y)
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

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

n = int(input())
a = [int(i) for i in input().split()]
b = []
for i in range(n):
    d = a[:i] + a[i+1:]
    b.append(gcd(d))
print(max(b))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = max(A)
tmp = 0
for i in reversed(range(1,M+1)):
    count = 0
    memo = 0
    for j in range(N):
        if A[j]%i != 0:
            count += 1
            memo = j
            if count > 1:
                break
    if count <= 1:
        tmp = i
        break
print(tmp)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

def gcd(a, b):
    if a>b: a,b = b,a
    if (a,b) in gcd_memo.keys():
        return gcd_memo[(a,b)]
    while b:
        a, b = b, a % b
    gcd_memo[(a,b)] = 0
    return a

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

ans = 0

for i in range(n):
    a_reduce = a.copy()
    del(a_reduce[i])
    ans = max(ans, gcd_reduce(*a_reduce))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

def cal_divs(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
  
def check(num, kouho, N, i):
  for val in kouho.keys():
    if num%val == 0:
      kouho[val] += 1
      
  return {k: v for k, v in kouho.items() if v > i-1}
  
  
N = int(input())
A= list(map(int, input().split()))
pri_flag = True

if N > 4:
  divs = []
  for i in range(3):
      divs.extend(cal_divs(A[i]))
  kouho = {}
  c = collections.Counter(divs)
  for div, n in c.most_common():
      kouho[div] = n

  kouho = {k: v for k, v in kouho.items() if v > 1}

  for i,num in enumerate(A[3:]):
    if len(kouho) == 1:
      print(1)
      pri_flag = False
      break
    kouho = check(num, kouho, N, i+3)

  if pri_flag:
      print(max(kouho))
else:
    divs = []
    kouho = {}
    for i in range(N):
        divs.extend(cal_divs(A[i]))
        c = collections.Counter(divs)
    for div, n in c.most_common():
        kouho[div] = n
    kouho = {k: v for k, v in kouho.items() if v > N-2}
    print(max(kouho))

Submission status: Time Limit Exceeded

Solution code:
import fractions

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
def gcd(aa, bb):
	while bb:
		aa, bb = bb, aa % bb
	return aa

n = int(input())

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

a = sorted(a)
ans = 1
for i in range(n):
    flag = False
    j = 0
    if i == 0:
        tmp = a[1]
    else:
        tmp = a[0]
    while not flag and j != n:
        if j != i:
            tmp = gcd(tmp, a[j])
        if tmp <= ans:
            flag = True
        j += 1
    if tmp > ans:
        ans = tmp
    if (i!=0) and ans >= a[0]:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:

from fractions import gcd

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

def gcd_list(a):
    a2 = a[0]
    for i in range(1,len(a)):
        a2 = gcd(a2, a[i])
    return a2

ans = gcd_list(a)
a_stock = a.copy()
for i in range(n):
    a = a_stock.copy()
    a.pop(i)
    ans2 = gcd_list(a)
    if ans < ans2:
        ans = ans2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
import copy

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

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

result = 0
for i in range(n):
    tmp = copy.copy(a)
    tmp.pop(i)
    result = max(reduce(gcd, tmp), result)

print(result)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools
n=int(input())
a=list(map(int,input().split()))
A=functools.reduce(fractions.gcd,a)
for i in range(n):
    c=a[i]
    del a[i]
    B=functools.reduce(fractions.gcd,a)
    if A<B:
        A=B
    else:
        A=A
    a.insert(i,c)
print(A)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import math
from collections import Counter
INF = float("inf")


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


def GCDs(*a):
    # リストで入力しないよう注意。
    if len(a) == 0:
        return -1  # エラー

    if len(a) == 1:
        return a[0]

    res = a[0]
    for i in range(1, len(a)):
        res = GCD(res, a[i])
    return res


class SegmentTree(object):
    # セグメント木
    # 内部的には1-index
    # 外部からの更新、クエリなどは0-index
    def __init__(self, N):
        # サイズNの配列を元にしたセグメント木
        self.INF = 1
        self.N0 = 2**(int(math.log2(N-1))+1)
        self.tree = [0]*(2*self.N0+1)

    def update(self, k, x):
        # k番目の値をxに更新
        k += self.N0
        self.tree[k] = x
        while k > 1:
            k = k // 2
            a = sorted([self.tree[2*k], self.tree[2*k+1]])
            self.tree[k] = GCD(*a)

    def query(self, l, r):
        # 区間[l, r)の最小値
        l += self.N0
        r += self.N0-1
        s = 0
        while l < r:
            if not r & 1:       # rが左の葉
                a = sorted([s, self.tree[r]])
                s = GCD(*a)
                r -= 1

            if l & 1:           # lが右の葉
                a = sorted([s, self.tree[l]])
                s = GCD(*a)
                l += 1
            l //= 2
            r //= 2
        s = GCD(s, self.tree[l])
        return s


def solve(N: int, A: "List[int]"):
    b = A[0]
    m = INF
    mi = -1
    # for i in range(N):
    #     g = GCD(A[i], b)
    #     print(g)
    #     if m > g:
    #         m = g
    #         mi = i
    # buf = None
    # for i in range(N):
    #     if mi == i:
    #         continue
    #     if buf is None:
    #         buf = A[i]
    #     else:
    #         buf = GCD(buf, A[i])
    # print([buf, GCDs(*A[1:])])
    # print("回答: ", max([buf, GCDs(*A[1:])]))
    # # print(g)

    # s = SegmentTree(N)
    # for i in range(N):
    #     s.update(i, A[i])
    # # print("tree ", s.tree)
    # g = []
    # for i in range(N):
    #     # iを覗いたGCDをとる
    #     if i == 0:
    #         g.append(s.query(1, N))
    #     elif i == N-1:
    #         g.append(s.query(0, N-1))
    #     else:
    #         # print(s.query(0, i), s.query(i+1, N))
    #         g.append(GCD(s.query(0, i), s.query(i+1, N)))
    # # print(g)
    # # print("回答2: ", max(g))
    # print(max(g))

    g = []
    for i in range(N):
        buf = None
        for j in range(N):
            if i == j:
                continue
            if buf is None:
                buf = A[j]
            else:
                buf = GCD(buf, A[j])
        g.append(buf)
    # print(g)
    print(max(g))
    # print("正解：", max(g))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

import fractions
math = fractions
table = {}  ## (start, end) => gcd

for i in range(0, n):
    if i > 0:
        gcd = aa[0]
        for j in range(0, i):
            gcd = math.gcd(gcd, aa[j])
        table[(0, i)] = gcd

    if i < n - 1:
        gcd = aa[i + 1]
        for j in range(i + 1, n):
            gcd = fractions.gcd(gcd, aa[j])

        table[(i + 1, n)] = gcd

candidate = 1
for i in range(0, n):
    if i == 0:
        gcd2 = table[(i + 1, n)]
        candidate = max(gcd2, candidate)
    elif i == n - 1:
        gcd1 = table[(0, i)]
        candidate = max(gcd1, candidate)
    else:
        gcd1 = table[(0, i)]
        gcd2 = table[(i + 1, n)]
        candidate = max(math.gcd(gcd1, gcd2), candidate)

print(candidate)

Submission status: Time Limit Exceeded

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

N = (int(input())+1)
A = [int(i) for i in input().split()]
max = 0

for i in range(N-1):
  G = 0
  for j in range(N-1):
    if i==j : continue
    if G==0 : G = A[j] ; continue
    else : G = gcd(G,A[j])
  if max<G :max = G

print(max)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools

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

max_gcd = 1
for i in range(N):
        tmp_list =[]
        tmp_list.extend(A[:i])
        tmp_list.extend(A[i+1:])
        # print(tmp_list)
        tmp_gcd = functools.reduce(fractions.gcd, tmp_list)
        if max_gcd < tmp_gcd:
                max_gcd = tmp_gcd

print(max_gcd)

Submission status: Time Limit Exceeded

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

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

def get_gcd(alist):
    return reduce(fractions.gcd, alist)

max_gcd = 0

for i in range(N):
    gcd = get_gcd(np.delete(A, i, 0))
    if max_gcd < gcd:
        max_gcd = gcd
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

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

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

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

print(max(C))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import fractions as f
import copy

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

max = 1
for i in range(N):
    if i == 0:
        tmp_gcd = a[1]
    else:
        tmp_gcd = a[0]
    for j in range(1,N):
        if j != i:
            tmp_gcd = f.gcd(tmp_gcd, a[j])
        if tmp_gcd < max:
            break
    if tmp_gcd > max:
        max = tmp_gcd        

print(max)

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)
            m = n // i
            if i != m:
                divisors.append(m)
    return divisors

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

div = {i:0 for i in range(max(A)+1)}
for a in A:
    divisor = make_divisors(a)
    for d in divisor:
        div[d] += 1

for num, a in sorted(div.items(), reverse=True):
    if a > N - 2:
        print(num)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
huga = list(map(int, input().split()))
unchi=list(map(int, input().split()))
n=len(unchi)

import sys
if n==2:
  if unchi[0]>=unchi[1]:
    print(unchi[0])
    sys.exit()
  else:
    print(unchi[1])
    sys.exit()    

def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
kouyaku=1
geri =[[0 for i in range(n)] for j in range(n)]
for i in range(0,n):
  for j in range(n):
    geri[i][j]=unchi[j]


    
for i in range(n):
  sore=geri[i]
  sore.pop(i)
  if len(sore)==2:
    u=gcd(sore[0],sore[1])
  else:
    u=sore[0]
    for j in range(1,len(sore)):
      u=gcd(u,sore[j])

  if u>=kouyaku:
  	kouyaku=u
print(kouyaku)

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    *A, = map(int, input().split())
    mini = []
    for i in range(N):
        mini.append(gcd_list(A[:i] + A[i + 1:]))
    mini.sort()
    print(mini[-1])
main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
x=list(map(int,input().split()))
S=max(x)
v=0
element=0
w=[]
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
for j in range(n):
  c=x[j]
  if x[j]==S:
    x.pop(j)
    v=1
  else:
    x[j]=S
  if n==1:
    print(x[0])
  else:
    if n==2:
      w.append(S)
    else:
      element = gcd(x[0], x[1])
    if n==2:
      w.append(element)
    else:
      for i in range(2,len(x)):
          element=gcd(element,x[i])
      w.append(element)
  if v==1:
    x.insert(j,S)
    v=0
  else:
    x[j]=c
print(max(w))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import math
from collections import Counter
INF = float("inf")


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


def GCDs(*a):
    # リストで入力しないよう注意。
    if len(a) == 0:
        return -1  # エラー

    if len(a) == 1:
        return a[0]

    res = a[0]
    for i in range(1, len(a)):
        res = GCD(res, a[i])
    return res


class SegmentTree(object):
    # セグメント木
    # 内部的には1-index
    # 外部からの更新、クエリなどは0-index
    def __init__(self, N):
        # サイズNの配列を元にしたセグメント木
        self.INF = 1
        self.N0 = 2**(int(math.log2(N-1))+1)
        self.tree = [0]*(2*self.N0+1)

    def update(self, k, x):
        # k番目の値をxに更新
        k += self.N0
        self.tree[k] = x
        while k > 1:
            k = k // 2
            a = sorted([self.tree[2*k], self.tree[2*k+1]])
            self.tree[k] = GCD(*a)

    def query(self, l, r):
        # 区間[l, r)の最小値
        l += self.N0
        r += self.N0-1
        s = 0
        while l < r:
            if not r & 1:       # rが左の葉
                a = sorted([s, self.tree[r]])
                s = GCD(*a)
                r -= 1

            if l & 1:           # lが右の葉
                a = sorted([s, self.tree[l]])
                s = GCD(*a)
                l += 1
            l //= 2
            r //= 2
        s = GCD(s, self.tree[l])
        return s


def solve(N: int, A: "List[int]"):
    b = A[0]
    m = INF
    mi = -1
    # for i in range(N):
    #     g = GCD(A[i], b)
    #     print(g)
    #     if m > g:
    #         m = g
    #         mi = i
    # buf = None
    # for i in range(N):
    #     if mi == i:
    #         continue
    #     if buf is None:
    #         buf = A[i]
    #     else:
    #         buf = GCD(buf, A[i])
    # print([buf, GCDs(*A[1:])])
    # print("回答: ", max([buf, GCDs(*A[1:])]))
    # # print(g)

    s = SegmentTree(N)
    for i in range(N):
        s.update(i, A[i])
    # print("tree ", s.tree)
    g = []
    for i in range(N):
        # iを覗いたGCDをとる
        if i == 0:
            g.append(s.query(1, N))
        elif i == N-1:
            g.append(s.query(0, N-1))
        else:
            # print(s.query(0, i), s.query(i+1, N))
            g.append(GCD(s.query(0, i), s.query(i+1, N)))
    # print(g)
    # print("回答2: ", max(g))
    print(max(g))

    # g = []
    # for i in range(N):
    #     buf = None
    #     for j in range(N):
    #         if i == j:
    #             continue
    #         if buf is None:
    #             buf = A[j]
    #         else:
    #             buf = GCD(buf, A[j])
    #     g.append(buf)
    # # print(g)
    # print("正解：", max(g))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))

    numdct = {}
    for i in range(2):
        for div in make_divisors(a[i]):
            if(not div in numdct):
                numdct[div] = 0
            numdct[div] += 1

    for i in range(2, n):
        for num in numdct.keys():
            if(numdct[num] > 0):
                if(a[i] % num != 0):
                    numdct[num] -= 1
    
    numlist = sorted(numdct.items(), key=lambda x: -x[1])

    maxnum = 0
    for item in numlist:
        if(item[1] > 0):
            if(maxnum <= item[0]):
                maxnum = item[0]
        else:
            break

    print(maxnum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]


def max_GCD(m_list):
  m_list.sort()
  a = m_list.pop()
  result = a
  while m_list:
    b = m_list.pop()
    a = GCD(a, b)
    if a < result:
      result = a
    if result == 1:
      return result
  return result

def GCD(m, n):
  a = max(m, n)
  b = min(m, n)
  while b > 0:
    r = a % b
    if r == 0:
      return b
    a = b
    b = r  

r_list = [0] * N
for i in range(N):
  A_ = A.copy()
  A_.pop(i)
  r_list[i] = max_GCD(A_)
  
print(max(r_list))

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
#import math
import fractions
#from functools import reduce
from fractions import gcd

def test(x,k,m):
    #print("i=",k)
    tmp=[0]*len(x)
    for j in range(m):
        tmp[j]=x[j]
    #print('a=',x)
    #print('tmp=',tmp)
    del tmp[k]
    #print('tmp=',tmp)
    #print("a==",x)
    #XX=gcd(*tmp)
    ans=0
    for aa in range(len(tmp)):
        ans = fractions.gcd(ans, tmp[aa])
    #print("XX=",XX)
    return ans
#def gcd(*numbers):
#    return reduce(math.gcd, numbers)
#def gcd_list(numbers):
#    return reduce(math.gcd, numbers)
n=int(input())
a= list(map(int,input().split()))

yakusu=[0]*len(a)
for i in range(n):
    yakusu[i]=test(a,i,n)

ans=max(yakusu)
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

sorted_num = sorted(nums)

res = 1
for i in range(sorted_num[1], 0, -1):
    nums_div_i = [num % i for num in nums]

    _len = len(tuple(filter(lambda x: x > 0, nums_div_i)))
    if _len == 1 or not _len:
        res = i
        break

print(res)

Submission status: Time Limit Exceeded

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

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

#print(N,A)

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

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

print(output)

Submission status: Time Limit Exceeded

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

maxnum=1
for i in range(1,max(nums)+1):
    counter=0
    
    for j in range(n):
        if counter > 1:
            break
        elif nums[j]%i != 0:
            counter+=1
    if counter==0 or counter==1:
        maxnum=i
        
        
        
print(maxnum)

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)
# gcd = []
max_gcd = 0
kesusoezi = 0

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

A = copy.deepcopy(origin_list)

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

A[kesusoezi] = max_gcd

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

# print(kesusoezi)
print(test)

# for i in range(len(A)):
#     try:
#         gcd.append(math.gcd(A[i], A[i + 1]))
#     except IndexError:
#         pass
# gcd = functools.reduce(math.gcd, A)
# print(max(gcd))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

from functools import reduce
import copy
import fractions

N = int(input())
integers = list(map(int, input().split()))
integer_set = set(integers)


gcds = []

for i in range(N):
    tmp_integers = copy.copy(integers)
    for x in integer_set:
        tmp_integers[i] = x
        gcds.append(reduce(fractions.gcd, tmp_integers))
print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

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

if N == 2:
	print(max(ls))
	exit()

tempx = 1


for i in range(N):
	temp = ls[:]
	temp.pop(i)
	a = gcd(temp[0], temp[1])
	if a <= tempx:
		continue
	for j in range(2, N - 1):
		a = gcd(a, temp[j])
		if a <= tempx:
			break
	if a > tempx:
		tempx = a
print(tempx)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import fractions
from functools import reduce

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

max = 1

for comb in itertools.combinations(a, n-1):
    ret = reduce(fractions.gcd, comb)
    if ret > max:
        max = ret

print(max)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
  
N= int(input())
A = list(map(int, input().split()))
gcd = []
 
for i in range(N):
  a = A[i]
  A[i] = 0
  gcd.append(reduce(fractions.gcd, A))
  A[i] = a
print(max(gcd))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

n = int(input())
A = list(map(int, input().split()))
maxGcd = 0

for i in range(n):
    g = reduce(lambda a, b: gcd(a, b), A[:i]+A[i+1:])
    maxGcd = max(maxGcd, g)

print(maxGcd)

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
N = int(input())
As=input().split()
A = np.array([int(s) for s in As])
del(As)

min2A = A[A.argsort()[1]]
minA = np.amin(A)
if np.sum((A%min2A)==0)>=N-1:
    print(min2A)
elif np.sum((A%minA)==0)>=N-1:
    print(minA)
else:
    for i in range((min2A//2)+1)[::-1]:
        if np.sum((A%i)==0)>=N-1:
            print(i)
            break

Submission status: Time Limit Exceeded

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

def o3gcd(array):
    x = array[0]
    for y in array:
        x = gcd(x, y)

    return x

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

result = 0

for i in range(N):
    array = A[:i]
    array.extend(A[i+1:])
    x = o3gcd(array)

    result = max(result, x)

print(result)

Submission status: Time Limit Exceeded

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

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


N = len(C)
A = C

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

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

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
def gcd(a,b):
    while b>0 :
        a0=a
        b0=b
        a=b0
        b=a0%b0
    return a
def gcd_hy(B,b):
    C=[]
    for i in range(len(B)):
        C.append(gcd(B[i],b))
    C.append(B[0])
    B=C
    return B
B=[gcd(A[0],A[1]),A[0],A[1]]
for i in range(N-2):
    B=gcd_hy(B,A[i+2])
print(max(B))

Submission status: Time Limit Exceeded

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

N = int(input())
A = list(map(int, input().split()))
ans=0
for i in range(N):
    a = A[0]
    del A[0]
    
    b = gcd_list(A)
    if ans < b:
        ans = b
    A.append(a)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

n = int(input())
list_a = list(map(int, input().split()))
result = reduce(gcd, list_a)
for i in range(n):
    copied = list_a.copy()
    del copied[i]
    res = reduce(gcd, copied) if len(copied) > 1 else copied[0]
    if res > result:
        result = res

print(result)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

l = [0] * (N-1)
max = 0
for j in range(N):
  k = 0
  for i in range(N):
    if i != j:
      l[k] = int(a[i])
      k += 1
  gcd = reduce(fractions.gcd,l)
  if gcd > max:
    max = gcd

print(max)