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())
a = list(map(int,input().split()))
a.sort()
ans = 1
if n == 2:
  print(max(a))
else:
  a0 = a[0]
  a1 = a[1]
  r = 0
  while True:
    r = a1 % a0
    if r == 0:
      b0 = a0
      break
    elif r > a0:
      a1, a0 = r, a0
    else:
      a1, a0 = a0, r
  a1 = a[1]
  a2 = a[2]
  r = 0
  while True:
    r = a2 % a1
    if r == 0:
      b1 = a1
      break
    elif r > a1:
      a2, a1 = r, a1
    else:
      a2, a1 = a1, r
  a0 = a[0]
  a2 = a[2]
  r = 0
  while True:
    r = a2 % a0
    if r == 0:
      b2 = a0
      break
    elif r > a0:
      a2, a0 = r, a0
    else:
      a2, a0 = a0, r
  max_a = max(b0,b1,b2)
  kouho = []
  for j in range(max_a,1,-1):
    if max_a % j == 0:
      kouho.append(j)
  for j in kouho:
    c = list(map(lambda x: x % j,a))
    c_ng = [i for i in c if i > 0]
    #print("j=",j,"c=",c,"c_ng=",c_ng)
    if len(c_ng) <= 1:
      ans = j
      break
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 1
if n == 2:
  print(max(a))
else:
  a0 = a[0]
  a1 = a[1]
  r = 0
  while True:
    r = a1 % a0
    if r == 0:
      b0 = a0
      break
    elif r > a0:
      a1, a0 = r, a0
    else:
      a1, a0 = a0, r
  a1 = a[1]
  a2 = a[2]
  r = 0
  while True:
    r = a2 % a1
    if r == 0:
      b1 = a1
      break
    elif r > a1:
      a2, a1 = r, a1
    else:
      a2, a1 = a1, r
  a0 = a[0]
  a2 = a[2]
  r = 0
  while True:
    r = a2 % a0
    if r == 0:
      b2 = a0
      break
    elif r > a0:
      a2, a0 = r, a0
    else:
      a2, a0 = a0, r
  max_a = max(b0,b1,b2)
  for j in range(max_a,1,-1):
    c = list(map(lambda x: x % j,a))
    c_ng = [i for i in c if i > 0]
    #print("j=",j,"c=",c,"c_ng=",c_ng)
    if len(c_ng) <= 1:
      ans = j
      break
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(n):
  b = copy.deepcopy(a)
  b.pop(i)
  max_b = max(b)
  min_b = min(b)
  r = max_b
  while True:
    r = max_b % min_b
    if r == 0:
      break
    elif r > min_b:
      max_b, min_b = r, min_b
    else:
      max_b, min_b = min_b, r
  #print("min_b=",min_b)
  for j in range(min_b,1, -1):
    c = list(map(lambda x: x % j, b))
    if sum(c) == 0 and j > ans:
      ans = j
      break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 1
for j in range(a[1],1,-1):
  c = list(map(lambda x: x % j,a))
  c_ng = [i for i in c if i > 0]
  #print("j=",j,"c=",c,"c_ng=",c_ng)
  if len(c_ng) <= 1:
    ans = j
    break
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    A[i] = int(A[i])

A.sort()
A.reverse()

flag = 0
for term in A:

    count = 0

    for i in range(N):
        if(A[i] % term != 0):
            count += 1
        if(count == 2):
            break

    if(count <= 1):
        flag = 1
        break

if(flag == 1):
    print(term)
else:
    for term in reversed(range(0, A[0])):

        count = 0

        for i in range(N):
            if(A[i] % term !=0):
                count += 1
            if(count == 2):
                break

        if(count <= 1):
            break

    print(term)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 1
for j in range(max(a),1,-1):
  c = list(map(lambda x: x % j,a))
  c_ng = [i for i in c if i > 0]
  #print("j=",j,"c=",c,"c_ng=",c_ng)
  if len(c_ng) <= 1:
    ans = j
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(n):
  b = copy.deepcopy(a)
  b.pop(i)
  for j in range(2,max(b)+1):
    c = list(map(lambda x: x % j,b))
    if sum(c) == 0 and j > ans:
      ans = j
print(ans)

Submission status: Time Limit Exceeded

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

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

g_max = -1
for i in range(n):
    g = -1
    for j in range(n):
        if j == i:
            continue
        if g == -1:
            g = a[j]
        else:
            g = gcd(g, a[j])
    if g > g_max:
        g_max = g

print(g_max)

Submission status: Time Limit Exceeded

Solution code:
import fractions

n_str = input()
n = int(n_str)
a_str = input().split()
a = []
for i in range(n):
    a.append(int(a_str[i]))
tmp1 = [0] * (n + 1)
tmp2 = [0] * (n + 1)
tmp = []
for i in range(n + 1):
    for j in range(i):
        if j == 0:
            tmp1[j + 1] = fractions.gcd(0, a[j])
        else:
            tmp1[j + 1] = fractions.gcd(tmp1[j], a[j])
    for j in range(n - 1, i, -1):
        if j == n - 1:
            tmp2[j] = fractions.gcd(0, a[j])
        else:
            tmp2[j] = fractions.gcd(tmp2[j + 1], a[j])
    if i == 0:
        tmp.append(fractions.gcd(0, tmp2[i + 1]))
    elif i == n:
        tmp.append(fractions.gcd(tmp1[i], 0))
    else:
        tmp.append(fractions.gcd(tmp1[i], tmp2[i + 1]))
print(max(tmp))

Submission status: Time Limit Exceeded

Solution code:
# http://tsutaj.hatenablog.com/entry/2017/03/29/204841
class SegmentTree:
    def __init__(self, v, func, init_val):
        self.func = func
        self.init_val = init_val

        sz = len(v)

        self.n = 1
        while self.n < sz:
            self.n *= 2

        self.node = [self.init_val for _ in range(2 * self.n - 1)]
        # 元配列 v をセグメント木で表現する
        # 最下段のノード数は元配列のサイズ以上になる最小の 2 冪 -> これを n とおく
        # セグメント木全体で必要なノード数は 2n - 1 個である

        for i in range(sz):
            self.node[i + self.n - 1] = v[i]
        # 最下段に値を入れたあとに、下の段から順番に値を入れる

        for i in range(self.n - 2, -1, -1):
            self.node[i] = self.func(self.node[2 * i + 1], self.node[2 * i + 2])
        # 値を入れるには、自分の子の 2 値を参照すれば良い

    def update(self, x, val):
        x += self.n - 1
        # 最下段のノードにアクセスする

        self.node[x] = val
        while x > 0:
            x = (x - 1) // 2
            self.node[x] = self.func(self.node[2 * x + 1], self.node[2 * x + 2])
        # 最下段のノードを更新したら、あとは親に上って更新していく

    # 要求区間 [a, b) 中の要素の最小値を答える
    # k := 自分がいるノードのインデックス
    # 対象区間は [l, r) にあたる

    def get_val(self, a, b, k=0, l=0, r=-1):
        if r < 0: r = self.n
        # 最初に呼び出されたときの対象区間は [0, self.n)

        if r <= a or b <= l: return self.init_val
        # 要求区間と対象区間が交わらない -> 適当に返す

        if a <= l and r <= b: return self.node[k]
        # 要求区間が対象区間を完全に被覆 -> 対象区間を答えの計算に使う

        vl = self.get_val(a, b, 2 * k + 1, l, (l + r) // 2)
        vr = self.get_val(a, b, 2 * k + 2, (l + r) // 2, r)
        return self.func(vl, vr)
        # 要求区間が対象区間の一部を被覆 -> 子について探索を行う
        # 左側の子を vl ・ 右側の子を vr としている
        # 新しい対象区間は、現在の対象区間を半分に割ったもの


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


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

st = SegmentTree(v=a, func=gcd, init_val=0)

g = max(gcd(st.get_val(0, drop_idx), st.get_val(drop_idx + 1, N)) for drop_idx in range(N))
print(g)

Submission status: Time Limit Exceeded

Solution code:
# http://tsutaj.hatenablog.com/entry/2017/03/29/204841
class SegmentTree:
    def __init__(self, v, func, init_val):
        self.func = func
        self.init_val = init_val

        sz = len(v)

        self.n = 1
        while self.n < sz:
            self.n *= 2

        self.node = [self.init_val for _ in range(2 * self.n - 1)]
        # 元配列 v をセグメント木で表現する
        # 最下段のノード数は元配列のサイズ以上になる最小の 2 冪 -> これを n とおく
        # セグメント木全体で必要なノード数は 2n - 1 個である

        for i in range(sz):
            self.node[i + self.n - 1] = v[i]
        # 最下段に値を入れたあとに、下の段から順番に値を入れる

        for i in range(self.n - 2, -1, -1):
            self.node[i] = self.func(self.node[2 * i + 1], self.node[2 * i + 2])
        # 値を入れるには、自分の子の 2 値を参照すれば良い

    def update(self, x, val):
        x += self.n - 1
        # 最下段のノードにアクセスする

        self.node[x] = val
        while x > 0:
            x = (x - 1) // 2
            self.node[x] = min(self.node[2 * x + 1], self.node[2 * x + 2])
        # 最下段のノードを更新したら、あとは親に上って更新していく

    # 要求区間 [a, b) 中の要素の最小値を答える
    # k := 自分がいるノードのインデックス
    # 対象区間は [l, r) にあたる

    def get_val(self, a, b, k=0, l=0, r=-1):
        if r < 0: r = self.n
        # 最初に呼び出されたときの対象区間は [0, self.n)

        if r <= a or b <= l: return self.init_val
        # 要求区間と対象区間が交わらない -> 適当に返す

        if a <= l and r <= b: return self.node[k]
        # 要求区間が対象区間を完全に被覆 -> 対象区間を答えの計算に使う

        vl = self.get_val(a, b, 2 * k + 1, l, (l + r) // 2)
        vr = self.get_val(a, b, 2 * k + 2, (l + r) // 2, r)
        return self.func(vl, vr)
        # 要求区間が対象区間の一部を被覆 -> 子について探索を行う
        # 左側の子を vl ・ 右側の子を vr としている
        # 新しい対象区間は、現在の対象区間を半分に割ったもの


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


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

st = SegmentTree(v=a, func=gcd, init_val=0)

g = max(gcd(st.get_val(0, drop_idx), st.get_val(drop_idx + 1, N)) for drop_idx in range(N))
print(g)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import fractions

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        # nに最も近い2のべき乗を計算
        while self.n < n:
            self.n *= 2

        self.N0 = 2**(n-1).bit_length()

        # ノードの個数は2n-1個
        # 初期化はモノイドで
        self.dat = [0] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

        while(i > 0):
            i = (i-1) // 2
            self.dat[i] = fractions.gcd(self.dat[i * 2 + 1], self.dat[i * 2 + 2])

    def query(self, l, r):
        L = l + self.N0
        R = r + self.N0
        ret = 0
        
        while L < R:
            if R & 1:
                R -= 1
                ret = fractions.gcd(ret, self.dat[R-1])
                
            if L & 1:
                ret = fractions.gcd(ret, self.dat[L-1])
                L += 1
                
            L >>= 1; R >>= 1
        return ret


if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(N):
        if i == 0:
            leftv = 0
        else:
            leftv = st.query(0, i)
        
        if i == N-1:
            rightv = 0
        else:
            rightv = st.query(i+1, N)

        # for Debug    
        #print("**{0}** | {1} {2}".format(i, leftv, rightv))
        ans = max(ans, fractions.gcd(leftv, rightv)) 

    # for Debug
    #print(st.dat)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

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

    return divisors

kous = list(map(make_divisors, As))
all = []
for v in kous:
    all += v
all = sorted(list(set(all)), reverse=True)

ans = 1
i = 0
for v in all:
    count = 0
    for k in kous:
        if v in k:
            count += 1
    if count >= N-1:
        ans = all[i]
        break
    i+=1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions
import itertools

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


def gcds(list_):
    return list(itertools.accumulate(list_, fractions.gcd))


def rotate(list_):
    list_ = list_[1:] + list_[0:1]
    return list_


max_cand = []
for i in range(len(as_)):
    as_ = as_[1:] + as_[0:1]
    max_cand.append(gcds(as_)[-2])


print(max(max_cand))

Submission status: Time Limit Exceeded

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

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

    return divisors

kous = list(map(make_divisors, As))
all = []
for v in kous:
    all += v
all = sorted(list(set(all)), reverse=True)

counts = []
for v in all:
    count = 0
    for a in As:
        if v in make_divisors(a):
            count += 1
    counts.append(count)


ans = 1
for i in range(len(counts)):
    if counts[i]>=N-1:
        ans = all[i]
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# import math
import functools

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


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


def gcds(list_):
    return functools.reduce(gcd, list_)


def drop(list_, i):
    list_copy = list_.copy()
    del list_copy[i]
    return list_copy


print(max(gcds(drop(as_, i)) for i, _ in enumerate(as_)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
def gcd(L):
    
    

    while True:
        NL = []
        #print(L)
        minb = min(L)
        for l in L:
            r = l % minb
            if r != 0:
                NL.append(r)
        if(len(NL) == 0):
            return minb
        NL.append(minb)
        L = NL


max = 0
for i in range(1,len(A)+1):
    g = gcd(A[0:i-1]+A[i:])
    #print(i,g,(A[0:i-1]+A[i:]))
    if max < g:
        max = g
print(max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
def gcd(L):
    
    

    while True:
        NL = []
        #print(L)
        rs = 0
        minb = min(L)
        for l in L:
            r = l % minb
            if r != 0:
                NL.append(r)
                rs += r
        NL.append(minb)
        L = NL
        
        if rs == 0:
            return minb

max = 0
for i in range(1,len(A)+1):
    g = gcd(A[0:i-1]+A[i:])
    #print(i,g,(A[0:i-1]+A[i:]))
    if max < g:
        max = g
print(max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
def gcd(L):
    
    
    minb = L[0]
    while True:
        NL = []
        #print(L)
        rs = 0
        minb = min(L)
        for l in L:
            r = l % minb
            if r != 0:
                NL.append(r)
                rs += r
        NL.append(minb)
        L = NL
        
        if rs == 0:
            return minb

max = 0
for i in range(1,len(A)+1):
    g = gcd(A[0:i-1]+A[i:])
    #print(i,g,(A[0:i-1]+A[i:]))
    if max < g:
        max = g
print(max)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
def gcd(L):
    
    
    minb = L[0]
    while True:
        NL = []
        #print(L)
        rs = 0
        minb = L[0]
        for l in L:
            r = l % minb
            if r != 0:
                NL.append(r)
                rs += r
        NL.append(minb)
        L = NL
        
        if rs == 0:
            return minb

max = 0
for i in range(1,len(A)+1):
    g = gcd(A[0:i-1]+A[i:])
    #print(i,g,(A[0:i-1]+A[i:]))
    if max < g:
        max = g
print(max)

Submission status: Time Limit Exceeded

Solution code:
def GCDonBlack(N,A):
    if sum([a%min(A) for a in A]) == 0:
        return min(A)
    else:
        for i in reversed(range(1,max(A))):
            bar = N-1
            for ele in A:
                if ele % i == 0:
                    bar -= 1
                if bar == 0:
                    return i

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_arr=input().split()

for i in range(N):
    A_arr[i] = int(A_arr[i])
    
A_arr.sort(reverse=True)
_A_arr = [A_arr[i] for i in range(N)]


# import math
# from functools import reduce

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

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


def gcd_l(x):
    n = len(x)
    if n==1:
        return x[0]
    else:
        element = gcd(x[0], x[1])
        if n==2:
            return element
        else:
            for i in range(2,n):
                element=gcd(element,x[i])
            return element


ans = 0

for i in range(N):
    _A_arr.pop(i)
    if(gcd_l(_A_arr) > ans):
        ans = gcd_l(_A_arr)
    
    _A_arr = [A_arr[i] for i in range(N)]
    

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from functools import reduce
import bisect
import copy
import fractions
import math
# import statistics
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


def update(i, x, seg):
    i = len(a) + i - 1
    seg[i] = x
    while i > 0:
        i = math.floor((i-1) / 2)  # 上のノード
        # seg[i] = min(seg[2*i + 1], seg[2*i + 2])
        seg[i] = fractions.gcd(seg[2*i + 1], seg[2*i + 2])
    # 数列の1要素を変更可能＞消去で良い＞1要素を0に置換しても良い
    # 1要素を0にした時のリスト全体の最大公約数を今回は返す
    return seg[0]


n, a = II(), LI()
# 最大公約数を求める。gcd(0, x) = 0なので各要素を0で初期化する
segtree = [0] * (2*n -1)
maxi = 0

# node n, parent floor((n-1)/2), chile 2*n + 1 2*n + 2
for i in range(n):
    update(i, a[i], segtree)

for i in range(n):
    maxi = max(maxi, update(i, 0, segtree))
    # 元に戻す...???
    update(i, a[i], segtree)

print(maxi)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from functools import reduce
import bisect
import copy
import fractions
import math
# import statistics
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def P(x): return print(x)
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))


def update(i, x, seg):
    i = len(a) + i - 1
    seg[i] = x
    while i > 0:
        i = math.floor((i-1) / 2)  # 上のノード
        # seg[i] = min(seg[2*i + 1], seg[2*i + 2])
        seg[i] = fractions.gcd(seg[2*i + 1], seg[2*i + 2])
    # 数列の1要素を変更可能＞消去で良い＞1要素を0に置換しても良い
    # 1要素を0にした時のリスト全体の最大公約数を今回は返す
    return seg[0]


n, a = II(), LI()
# 最大公約数を求める。gcd(0, x) = 0なので各要素を0で初期化する
segtree = [0] * (2*n -1)
maxi = 0
# node n, parent floor((n-1)/2), chile 2*n + 1 2*n + 2
for i in range(n):
    update(i, a[i], segtree)

for i in range(n):
    tmp = copy.copy(segtree)
    maxi = max(maxi, update(i, 0, tmp))

print(maxi)

Submission status: Time Limit Exceeded

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
if N<=2:
    print(max(A))
else:
    gcds = []
    for n in range(N):
        if n == 0:
            lgcd = None
        else:
            lgcd = A[0]
            for i in range(n):
                lgcd = gcd(lgcd,A[i])
        if n == N-1:
            rgcd = None
        else:
            rgcd = A[n+1]
            for i in range(n+1,N):
                rgcd = gcd(rgcd,A[i])
        if lgcd == None:
            gcds.append(rgcd)
        elif rgcd == None:
            gcds.append(lgcd)
        else:
            gcds.append(gcd(lgcd,rgcd))
    print(max(gcds))

Submission status: Time Limit Exceeded

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

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

ans=[]
for i in range(n):
    if i==0:
      	ar=a[1]
    else:
     	ar=a[0]
    for j in range(n):
        if(i==j):
            pass
        else:
            ar=gcd(ar,a[j])
    ans.append(ar)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
answer = sorted(a)[1]
if n == 2:
    answer = max(a)
else:
    for b in reversed(range(answer)):
        cnt = 2
        flag = True
        for i in range(n):
            if a[i] % answer != 0:
                cnt -= 1
            if cnt == 0:
                flag = False
        if flag:
            break
        answer -= 1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
answer = sorted(a)[1]
for b in reversed(range(answer)):
    cnt = 2
    flag = True
    for i in range(n):
        if a[i] % answer != 0:
            cnt -= 1
        if cnt == 0:
            flag = False
    if flag:
        break
    answer -= 1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
answer = sorted(a)[1]
for b in range(answer):
    cnt = 2
    flag = True
    for i in range(n):
        if a[i] % answer != 0:
            cnt -= 1
        if cnt == 0:
            flag = False
    if flag:
        break
    answer -= 1
print(answer)

Submission status: Time Limit Exceeded

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

def getNum(a, b):
    while(True):
        a, b = max(a, b), min(a, b)
        if a == b:
            return a
        a -= b

leftGBC, rightGBC= [AList[0]], [AList[-1]]
for i in range(1, N):
    leftGBC.append(getNum(leftGBC[i - 1], AList[i]))
for i in range(1, N):
    rightGBC.append(getNum(rightGBC[i - 1], AList[N - i - 1]))

rightGBC.reverse()

ans = max(leftGBC[-2], rightGBC[1])
for i in range(0, N-2):
    ans = max(getNum(leftGBC[i], rightGBC[i + 2]), ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=A[:]
C=list(set(C))
C.sort()

#print(A)
#print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

else:
  for i in range(len(C)):
    B=A[:]
    B.remove(C[i])
    #print(B)
    sol=0
    for b in B:
      sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from sys import exit

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

l_gcd_list = [0]
r_gcd_list = [0]
for i in range(N):
    l_gcd_list.append(gcd(l_gcd_list[i], A[i]))
    r_gcd_list.insert(0, gcd(r_gcd_list[0], A[-(i+1)]))
# print(l_gcd_list)
# print(r_gcd_list)

results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(max(results))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from sys import exit

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

l_gcd_list = [0]
r_gcd_list = [0]
for i in range(N):
    l_gcd_list.append(gcd(l_gcd_list[i], A[i]))
    r_gcd_list.insert(0, gcd(r_gcd_list[0], A[-(i+1)]))
# print(l_gcd_list)
# print(r_gcd_list)

result = 0
for i in range(N):
    result = max(result, gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(result)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from sys import exit

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

l_gcd_list = [0]
r_gcd_list = [0]
for i in range(N):
    l_gcd_list.append(gcd(l_gcd_list[i], A[i]))
    r_gcd_list.insert(0, gcd(r_gcd_list[0], A[-(i+1)]))
# print(l_gcd_list)
# print(r_gcd_list)

result = 0
for i, _ in enumerate(A):
    result = max(result, gcd(l_gcd_list[i], r_gcd_list[i+1]))

print(result)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

result = 1
previous_gcd = 0
for i, a in enumerate(A):
    # print("a", a)
    result = max(result, gcd(previous_gcd, reduce(gcd, A[i+1:] + [0])))
    previous_gcd = gcd(previous_gcd, a)
    # print("---")

print(result)

Submission status: Time Limit Exceeded

Solution code:

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


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

if N == 2:
  print(max(A))
else:
  m = -1
  for i in range(N):
    G = 0
    for j in range(0, i):
      G = gcd(G, A[j])
    for j in range(i+1, N):
      G = gcd(G, A[j])

    m = max(m, G)
  print(m)

Submission status: Time Limit Exceeded

Solution code:
import copy
import fractions

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

def gcd_list(numbers):
    g = numbers[0]
    for m in numbers:
        g = fractions.gcd(g, m)
    return g

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

gcds = list()
for a in A:
    B = copy.copy(A)
    B.remove(a)
    g = gcd_list(B)
    gcds.append(g)
print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def gcd_list(numbers):
    g = numbers[0]
    for m in numbers:
        g = gcd(g, m)
    return g

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

gcds = list()
for a in A:
    B = copy.copy(A)
    B.remove(a)
    g = gcd_list(B)
    gcds.append(g)
print(max(gcds))

Submission status: Time Limit Exceeded

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

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

l=make_divisors(a[0])
l2=make_divisors(a[1])
le=len(l)
le2=len(l2)         
an=[0]*le
an2=[0]*le2
for i in range(n):
    for j in range(le):
        if a[i]%l[j]==0:
            an[j]+=1
    for j in range(le2):
        if a[i]%l2[j]==0:
            an2[j]+=1
ans=1
ans2=1
for i in range(le):
    if an[i]>=n-1:
        ans=max(ans,l[i])
for i in range(le2):
    if an2[i]>=n-1:
        ans2=max(ans2,l2[i])

print(max(ans,ans2))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

def gcd_list(numbers):
    g = numbers[0]
    for m in numbers:
        g = gcd(g, m)
    return g

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

gcds = list()
for a in A:
    B = copy.copy(A)
    B.remove(a)
    g = gcd_list(B)
    gcds.append(g)
    del B
m = max(gcds)
print(int(m))

Submission status: Time Limit Exceeded

Solution code:
n = int(input(), 10)
a = list(map(int, input().split()))
greatCDlist = []


def gcd(m, n):
    if m < n:
        m ^= n
        n ^= m
        m ^= n
    if n == 0:
        return m
    else:
        return gcd(n, m % n)


for i in range(n):
    pop = a.pop(i)
    greatCD = 1
    for j in range(n-1):
        if j == 0:
            greatCD = a[0]
        else:
            greatCD = gcd(greatCD, a[j])
    a.insert(i, pop)
    greatCDlist.append(greatCD)

maxg = 1
for i in range(n):
    if maxg < greatCDlist[i]:
        maxg = greatCDlist[i]

print(maxg)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from functools import reduce

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


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

a_max = 0
for i in range(N):
    new_a = copy.deepcopy(A)
    new_a.pop(i)
    a_max = max(a_max,gcd(*new_a))

print(a_max)

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


def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=30
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0
for i in range(1,math.ceil(M**0.5)+1 ):
    if M%i==0:
        cnt=0
        for j in range(p,N):
            if a[j]%i!=0:
                cnt+=1
                if cnt==2: break
            elif j==N-1 and i>ans: ans=i
        
        x=M//i
        cnt=0
        for j in range(p,N):
            if a[j]%x!=0:
                cnt+=1
                if cnt==2: break
            elif j==N-1 and x>ans: ans=x

R=a[p]
for i in range(p+1,N):
    R=gcd(R,a[i])

if R>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),R)
        if s>ans: ans=s

print(ans)

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


def cul(n,x):
    l=len(x)
    b=[x[i] for i in range(l)]
    b.pop(n)
    tmp=b[0]
    for i in range(1,l-1):
        tmp=gcd(tmp,b[i])
    return tmp

import sys
import math
p=49
N=int(input())
a=list(map(int,input().split()))
if N<=p:
    ans=0
    
    for i in range(N):
        tmp=cul(i,a)        
        if tmp>ans: ans=tmp
    print(ans)
    sys.exit()
        
M=a[0]
for i in range(1,p):
    M=gcd(M,a[i])

ans=0
for i in range(1,math.ceil(M**0.5)+1 ):
    if M%i==0:
        cnt=0
        for j in range(p,N):
            if a[j]%i!=0:
                cnt+=1
                if cnt==2: break
            elif j==N-1 and i>ans: ans=i
        
        x=M//i
        cnt=0
        for j in range(p,N):
            if a[j]%x!=0:
                cnt+=1
                if cnt==2: break
            elif j==N-1 and x>ans: ans=x

R=a[p]
for i in range(p+1,N):
    R=gcd(R,a[i])

if R>ans:
    for i in range(p):
        s=gcd(cul(i,a[:p]),R)
        if s>ans: ans=s

print(ans)