Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

3
7 6 8

Sample Output 1

2

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

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
import functools
import copy

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

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

def gcd(nums): 
    return functools.reduce(euclid, nums)

max = -1e+10
for i in range(N):
    tmp = copy.deepcopy(A)
    tmp.pop(i)
    g = gcd(tmp)
    if g > max:
        max = g

print(max)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
# def GCD(a: int, b: int) -> int:
#     return a if b == 0 else GCD(b, a % b)


class SegTree():
    def __init__(self, n_, ele_id, operation_func):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.data = [ele_id] * (2 * self.n - 1)
        self.ele_id = ele_id
        self.operation_func = operation_func

    def __getitem__(self, idx):
        return self.data[idx + self.n - 1]

    def init(self, data_init):
        for i in range(len(data_init)):
            self.data[i + self.n - 1] = data_init[i]
        for i in range(self.n - 2, -1, -1):
            self.data[i] = self.operation_func(self.data[2 * i + 1],
                                               self.data[2 * i + 2])

    def update(self, idx, x):
        # idx番目の要素をxに更新する
        idx += self.n - 1
        self.data[idx] = x
        while idx > 0:
            idx = (idx - 1) // 2
            self.data[idx] = self.operation_func(self.data[2 * idx + 1],
                                                 self.data[2 * idx + 2])

    def query(self, a, b):
        return self.query_(a, b, 0, 0, self.n)

    def query_(self, a, b, k, l, r):
        # [a, b)の最小値を求める
        if r <= a or b <= l:
            # [a, b)が[l, r)と交差しない
            return self.ele_id

        if a <= l and r <= b:
            # [a, b)が[l, r)を完全に含む
            return self.data[k]
        else:
            # [a, b)と[l, r)は一部被る
            child_l = self.query_(a, b, 2 * k + 1, l, (l + r) // 2)
            child_r = self.query_(a, b, 2 * k + 2, (l + r) // 2, r)
            return self.operation_func(child_l, child_r)


n = int(input())
a = list(map(int, input().split()))
seg_gcd = SegTree(n, 0, gcd)
seg_gcd.init(a)

ans = seg_gcd.data[0]
for i in range(n):
    # seg_gcd.update(i, 0)
    # ans = max(ans, seg_gcd.data[0])
    # seg_gcd.update(i, a[i])
    ans = max(ans, gcd(seg_gcd.query(0, i), seg_gcd.query(i + 1, n)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from fractions import gcd
    import sys
    input = sys.stdin.readline

    # def GCD(a: int, b: int) -> int:
    #     return a if b == 0 else GCD(b, a % b)

    class SegTree():
        def __init__(self, n_, ele_id, operation_func):
            self.n = 1
            while self.n < n_:
                self.n *= 2
            self.data = [ele_id] * (2 * self.n - 1)
            self.ele_id = ele_id
            self.operation_func = operation_func

        def __getitem__(self, idx):
            return self.data[idx + self.n - 1]

        def init(self, data_init):
            for i in range(len(data_init)):
                self.data[i + self.n - 1] = data_init[i]
            for i in range(self.n - 2, -1, -1):
                self.data[i] = self.operation_func(self.data[2 * i + 1],
                                                   self.data[2 * i + 2])

        def update(self, idx, x):
            # idx番目の要素をxに更新する
            idx += self.n - 1
            self.data[idx] = x
            while idx > 0:
                idx = (idx - 1) // 2
                self.data[idx] = self.operation_func(self.data[2 * idx + 1],
                                                     self.data[2 * idx + 2])

        def query(self, a, b):
            return self.query_(a, b, 0, 0, self.n)

        def query_(self, a, b, k, l, r):
            # [a, b)の最小値を求める
            if r <= a or b <= l:
                # [a, b)が[l, r)と交差しない
                return self.ele_id

            if a <= l and r <= b:
                # [a, b)が[l, r)を完全に含む
                return self.data[k]
            else:
                # [a, b)と[l, r)は一部被る
                child_l = self.query_(a, b, 2 * k + 1, l, (l + r) // 2)
                child_r = self.query_(a, b, 2 * k + 2, (l + r) // 2, r)
                return self.operation_func(child_l, child_r)

    n = int(input())
    a = tuple(map(int, input().split()))
    seg_gcd = SegTree(n, 0, gcd)
    seg_gcd.init(a)

    ans = seg_gcd.data[0]
    for i in range(n):
        seg_gcd.update(i, 0)
        ans = max(ans, seg_gcd.data[0])
        seg_gcd.update(i, a[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
import sys
input = sys.stdin.readline
# def GCD(a: int, b: int) -> int:
#     return a if b == 0 else GCD(b, a % b)


class SegTree():
    def __init__(self, n_, ele_id, operation_func):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.data = [ele_id] * (2 * self.n - 1)
        self.ele_id = ele_id
        self.operation_func = operation_func

    def __getitem__(self, idx):
        return self.data[idx + self.n - 1]

    def init(self, data_init):
        for i in range(len(data_init)):
            self.data[i + self.n - 1] = data_init[i]
        for i in range(self.n - 2, -1, -1):
            self.data[i] = self.operation_func(self.data[2 * i + 1],
                                               self.data[2 * i + 2])

    def update(self, idx, x):
        # idx番目の要素をxに更新する
        idx += self.n - 1
        self.data[idx] = x
        while idx > 0:
            idx = (idx - 1) // 2
            self.data[idx] = self.operation_func(self.data[2 * idx + 1],
                                                 self.data[2 * idx + 2])

    def query(self, a, b):
        return self.query_(a, b, 0, 0, self.n)

    def query_(self, a, b, k, l, r):
        # [a, b)の最小値を求める
        if r <= a or b <= l:
            # [a, b)が[l, r)と交差しない
            return self.ele_id

        if a <= l and r <= b:
            # [a, b)が[l, r)を完全に含む
            return self.data[k]
        else:
            # [a, b)と[l, r)は一部被る
            child_l = self.query_(a, b, 2 * k + 1, l, (l + r) // 2)
            child_r = self.query_(a, b, 2 * k + 2, (l + r) // 2, r)
            return self.operation_func(child_l, child_r)


n = int(input())
a = tuple(map(int, input().split()))
seg_gcd = SegTree(n, 0, gcd)
seg_gcd.init(a)

ans = seg_gcd.data[0]
for i in range(n):
    seg_gcd.update(i, 0)
    ans = max(ans, seg_gcd.data[0])
    seg_gcd.update(i, a[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
# def GCD(a: int, b: int) -> int:
#     return a if b == 0 else GCD(b, a % b)


class SegTree():
    def __init__(self, n_, ele_id, operation_func):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.data = [ele_id] * (2 * self.n - 1)
        self.ele_id = ele_id
        self.operation_func = operation_func

    def __getitem__(self, idx):
        return self.data[idx + self.n - 1]

    def init(self, data_init):
        for i in range(len(data_init)):
            self.data[i + self.n - 1] = data_init[i]
        for i in range(self.n - 2, -1, -1):
            self.data[i] = self.operation_func(self.data[2 * i + 1],
                                               self.data[2 * i + 2])

    def update(self, idx, x):
        # idx番目の要素をxに更新する
        idx += self.n - 1
        self.data[idx] = x
        while idx > 0:
            idx = (idx - 1) // 2
            self.data[idx] = self.operation_func(self.data[2 * idx + 1],
                                                 self.data[2 * idx + 2])

    def query(self, a, b):
        return self.query_(a, b, 0, 0, self.n)

    def query_(self, a, b, k, l, r):
        # [a, b)の最小値を求める
        if r <= a or b <= l:
            # [a, b)が[l, r)と交差しない
            return self.ele_id

        if a <= l and r <= b:
            # [a, b)が[l, r)を完全に含む
            return self.data[k]
        else:
            # [a, b)と[l, r)は一部被る
            child_l = self.query_(a, b, 2 * k + 1, l, (l + r) // 2)
            child_r = self.query_(a, b, 2 * k + 2, (l + r) // 2, r)
            return self.operation_func(child_l, child_r)


n = int(input())
a = tuple(map(int, input().split()))
seg_gcd = SegTree(n, 0, gcd)
seg_gcd.init(a)

ans = seg_gcd.data[0]
for i in range(n):
    seg_gcd.update(i, 0)
    ans = max(ans, seg_gcd.data[0])
    seg_gcd.update(i, a[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
# def GCD(a: int, b: int) -> int:
#     return a if b == 0 else GCD(b, a % b)


class SegTree():
    def __init__(self, n_, ele_id, operation_func):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.data = [ele_id] * (2 * self.n - 1)
        self.ele_id = ele_id
        self.operation_func = operation_func

    def __getitem__(self, idx):
        return self.data[idx + self.n - 1]

    def init(self, data_init):
        for i in range(len(data_init)):
            self.data[i + self.n - 1] = data_init[i]
        for i in range(self.n - 2, -1, -1):
            self.data[i] = self.operation_func(self.data[2 * i + 1],
                                               self.data[2 * i + 2])

    def update(self, idx, x):
        # idx番目の要素をxに更新する
        idx += self.n - 1
        self.data[idx] = x
        while idx > 0:
            idx = (idx - 1) // 2
            self.data[idx] = self.operation_func(self.data[2 * idx + 1],
                                                 self.data[2 * idx + 2])

    def query(self, a, b):
        return self.query_(a, b, 0, 0, self.n)

    def query_(self, a, b, k, l, r):
        # [a, b)の最小値を求める
        if r <= a or b <= l:
            # [a, b)が[l, r)と交差しない
            return self.ele_id

        if a <= l and r <= b:
            # [a, b)が[l, r)を完全に含む
            return self.data[k]
        else:
            # [a, b)と[l, r)は一部被る
            child_l = self.query_(a, b, 2 * k + 1, l, (l + r) // 2)
            child_r = self.query_(a, b, 2 * k + 2, (l + r) // 2, r)
            return self.operation_func(child_l, child_r)


n = int(input())
a = list(map(int, input().split()))
seg_gcd = SegTree(n, 0, gcd)
seg_gcd.init(a)

ans = seg_gcd.data[0]
for i in range(n):
    seg_gcd.update(i, 0)
    ans = max(ans, seg_gcd.data[0])
    seg_gcd.update(i, a[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def GCD(a: int, b: int) -> int:
    return a if b == 0 else GCD(b, a % b)


class SegTree():
    def __init__(self, n_, ele_id, operation_func):
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.data = [ele_id] * (2 * self.n - 1)
        self.ele_id = ele_id
        self.operation_func = operation_func

    def __getitem__(self, idx):
        return self.data[idx + self.n - 1]

    def init(self, data_init):
        for i in range(len(data_init)):
            self.data[i + self.n - 1] = data_init[i]
        for i in range(self.n - 2, -1, -1):
            self.data[i] = self.operation_func(self.data[2 * i + 1],
                                               self.data[2 * i + 2])

    def update(self, idx, x):
        # idx番目の要素をxに更新する
        idx += self.n - 1
        self.data[idx] = x
        while idx > 0:
            idx = (idx - 1) // 2
            self.data[idx] = self.operation_func(self.data[2 * idx + 1],
                                                 self.data[2 * idx + 2])

    def query(self, a, b):
        return self.query_(a, b, 0, 0, self.n)

    def query_(self, a, b, k, l, r):
        # [a, b)の最小値を求める
        if r <= a or b <= l:
            # [a, b)が[l, r)と交差しない
            return self.ele_id

        if a <= l and r <= b:
            # [a, b)が[l, r)を完全に含む
            return self.data[k]
        else:
            # [a, b)と[l, r)は一部被る
            child_l = self.query_(a, b, 2 * k + 1, l, (l + r) // 2)
            child_r = self.query_(a, b, 2 * k + 2, (l + r) // 2, r)
            return self.operation_func(child_l, child_r)


n = int(input())
a = list(map(int, input().split()))
seg_gcd = SegTree(n, 0, GCD)
seg_gcd.init(a)

ans = seg_gcd.data[0]
for i in range(n):
    seg_gcd.update(i, 0)
    ans = max(ans, seg_gcd.data[0])
    seg_gcd.update(i, a[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

n = int(input())
a = [int(_) for _ in input().split()]
ans = 0

for i in range(n):
    r = reduce(fractions.gcd, a[:i] + a[i+1:])
    if ans < r:
        ans = r

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import sys
import bisect
input = sys.stdin.readline

def divide(x): #約数配列
    div=[1]
    check=2
    for check in range(1,int(x**0.5)+1):
        if(x%check==0):
            div.append(check)
            div.append(x//check)
    return div

def main():
    n=int(input())
    a = list(map(int,input().split()))
    ans={}
    for i in range(n):
        div=divide(a[i])
        for c in div:
            if c not in ans:
                ans[c]=1
            else:
                ans[c]+=1
    M=1
    for c in ans:
        if ans[c]>=n-1:
            M=max(M,c)
    print(M)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def find_gcm(a,b):  #i>=j
    if a>=b:
        i=a
        j=b
    else:
        i=b
        j=a
    while j!=0:
        r=i%j
        i=j
        j=r
    return i
ans=set()
for i in range(n):
    k = a.pop(i)
    gcm = a[0]
    for j in a:
        gcm = find_gcm(gcm,j)
        if gcm == 1:
            break
    ans.add(gcm)
    a.insert(i,k)
print(max(ans))

Submission status: Time Limit Exceeded

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

def find_gcm(a,b):  #i>=j
    if a>=b:
        i=a
        j=b
    else:
        i=b
        j=a
    while j!=0:
        r=i%j
        i=j
        j=r
    return i
ans=[]
for i in range(n):
    k = a.pop(i)
    gcm = a[0]
    for j in range(1,n-1):
        gcm = find_gcm(gcm,a[j])
        if gcm == 1:
            break
    ans.append(gcm)
    a.insert(i,k)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a % b)

def list_euclid(int_list):
    ans = int_list[0]
    if len(int_list) == 1:
        return ans
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans

n = int(input())
a = list(map(int, input().split()))
max_gcd = 0
b_int = {}
min = min(a)
if n == 2:
    max_gcd = max(a)
elif n < 5:
    for i in range(0, n):
        if a[0] in b_int:
            p = a.pop(0)
            # a.append(p)
        else:
            p = a.pop(0)
            b_int[p] = p
            x = list_euclid(a)
            if max_gcd < x:
                max_gcd = x
            a.append(p)
else:
    for i in range(0, n):
        if a[0] in b_int:
            p = a.pop(0)
            # a.append(p)
        else:
            if max_gcd == 0:
                p = a.pop(0)
                b_int[p] = p
                k = len(a) // 2
                x = 0
                y = list_euclid(a[0:k])
                if y == 1:
                    x = 1
                else:
                    z = list_euclid(a[k:len(a)])
                    x = euclid(y, z)
                if max_gcd < x:
                    max_gcd = x
                a.append(p)
            elif min <= max_gcd:
                break
            else:
                p = a.pop(0)
                b_int[p] = p
                k = len(a) // 2
                x = 0
                if k < 1:
                    x = list_euclid(a)
                else:
                    y = list_euclid(a[0:k])
                    if y == 1:
                        x = 1
                    else:
                        z = list_euclid(a[k:len(a)])
                        x = euclid(y, z)
                if max_gcd < x:
                    max_gcd = x
                a.append(p)
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a % b)

def list_euclid(int_list):
    ans = int_list[0]
    if len(int_list) == 1:
        return ans
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans

n = int(input())
a = list(map(int, input().split()))
a.sort()
max_gcd = 0
b_int = 0
min = 1
if n == 2:
    max_gcd = max(a)
elif n < 5:
    for i in range(0, n):
        if b_int == a[0]:
            p = a.pop(0)
            a.append(p)
        else:
            p = a.pop(0)
            b_int = p
            x = list_euclid(a)
            if max_gcd < x:
                max_gcd = x
            a.append(p)
else:
    for i in range(0, n):
        if b_int == a[0]:
            p = a.pop(0)
            # a.append(p)
        else:
            if max_gcd == 0:
                p = a.pop(0)
                b_int = p
                k = len(a) // 2
                x = 0
                y = list_euclid(a[0:k])
                if y == 1:
                    x = 1
                else:
                    z = list_euclid(a[k:len(a)])
                    x = euclid(y, z)
                if max_gcd < x:
                    max_gcd = x
                a.append(p)
                min = p
            elif min <= max_gcd:
                break
            else:
                p = a.pop(0)
                b_int = p
                k = len(a) // 2
                x = 0
                if k < 2:
                    x = list_euclid(a)
                else:
                    y = list_euclid(a[0:k])
                    if y == 1:
                        x = 1
                    else:
                        z = list_euclid(a[k:len(a)])
                        x = euclid(y, z)
                if max_gcd < x:
                    max_gcd = x
                a.append(p)
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:

#math
def gcd(a, b): #最大公約数
  while b:
    a, b = b, a % b
  return int(a)

def gcd3(lis): #最大公約数(3over)
  ans = lis[0]
  for li in lis:
    ans = gcd(ans,li)
  return ans
    
n = int(input())

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

maxgcd = 0
for i in range(n):
  nums = [a[j] for j in range(n) if j!=i]
  #print(nums)
  tmp = gcd3(nums)
  if maxgcd<tmp:
    maxgcd=tmp
    
print(maxgcd)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

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

ans = 0
for i in range(n):
    ret = 0
    for j in range(n):
        if j == i:
            continue
        else:
            ret = gcd(ret, a[j])
    ans = max(ret, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

def main():
    n = int(input())
    A = list(map(int, input().split()))

    gcd_max = 1
    L = A[0]
    for i in range(n):
        if i == 0:
            tempt = reduce(gcd, A[i+1:])
            gcd_max = max(gcd_max, tempt)
        elif i != n-1:
            L = gcd(L,A[i-1])
            tempt = gcd(L,reduce(gcd, A[i+1:]))
            gcd_max = max(gcd_max, tempt)
        else:
            L = gcd(L,A[i-1])
            tempt = L
            gcd_max = max(gcd_max, tempt)
    print(gcd_max)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

def main():
    n = int(input())
    A = list(map(int, input().split()))

    gcd_max = 1
    for i in range(n):
        if i == n-1:
            temp = A[i]
            A[i] = A[i-1]
            gcd_max = max(gcd_max, gcd_list(A))
            A[i] = temp
            break
        temp = A[i]
        A[i] = A[i+1]
        gcd_max = max(gcd_max, gcd_list(A))
        A[i] = temp
    print(gcd_max)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())

#約数を列挙
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

A_list = [make_divisors(int(e)) for e in input().split()]

all_divisors_list=[]
for i in A_list:
  all_divisors_list += i

collected_adl = Counter(all_divisors_list)

ans = 1
for a in collected_adl.most_common():
  if a[1] <= N-2:
    break
  elif a[0]>ans:
    ans=a[0]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A_list = [int(e) for e in input().split()]

#約数を列挙
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

divisors_list = [make_divisors(n) for n in A_list]
all_divisors_list = []
for i in divisors_list:
  all_divisors_list += i
  
#print(all_divisors_list)
collected_adl = Counter(all_divisors_list)

ans = 1
for a in collected_adl.most_common():
  #print(a)
  if a[1] <= N-2:
    break
  elif a[0]>ans:
    ans=a[0]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A_list = [int(e) for e in input().split()]

#約数を列挙
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

divisors_list = [make_divisors(n) for n in A_list]
all_divisors_list = []
for i in divisors_list:
  all_divisors_list += i
  
#print(all_divisors_list)
collected_adl = Counter(all_divisors_list)

ans = 1
for a in collected_adl.most_common():
  #print(a)
  if a[1] <= N-2:
    break
  elif a[0]>ans:
    ans=a[0]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans
n = int(input())
a = list(map(int, input().split()))
a.sort()
max = 0
b_int = 0
if n < 5:
  for i in range(0,n):
    if b_int == a[0]:
      p = a.pop(0)
      a.append(p)
    else:
      p = a.pop(0)
      b_int = p
      x = list_euclid(a)
      if max < x:
        max = x
      a.append(p)
else:
  for i in range(0,n):
    if b_int == a[0]:
      p = a.pop(0)
      a.append(p)
    else:
      p = a.pop(0)
      b_int = p
      k = len(a) // 2
      x = 0
      y = list_euclid(a[0:k])
      if y == 1:
        x = 1
      else:
        z = list_euclid(a[k:n-1])
        x = euclid(y, z)
      if max < x:
        max = x
      a.append(p)
print(max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans
n = int(input())
a = list(map(int, input().split()))
a.sort()
max = 0
b_int = 0
if n < 5:
  for i in range(0,n):
    if b_int == a[0]:
      p = a.pop(0)
      a.append(p)
    else:
      p = a.pop(0)
      b_int = p
      x = list_euclid(a)
      if max < x:
        max = x
      a.append(p)
else:
  for i in range(0,n):
    if b_int == a[0]:
      p = a.pop(0)
      a.append(p)
    else:
      p = a.pop(0)
      b_int = p
      k = n // 2
      x = 0
      y = list_euclid(a[0:k])
      if y == 1:
        x = 1
      else:
        z = list_euclid(a[k:n-1])
        x = euclid(y, z)
      if max < x:
        max = x
      a.append(p)
print(max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans
n = int(input())
a = list(map(int, input().split()))
max = 0
if n < 5:
    for i in range(0,n):
        p = a.pop(0)
        x = list_euclid(a)
        if max < x:
            max = x
        a.append(p)
else:
    for i in range(0,n):
        p = a.pop(0)
        k = n // 2
        y = list_euclid(a[0:k])
        z = list_euclid(a[k:n-1])
        x = euclid(y, z)
        if max < x:
            max = x
        a.append(p)
print(max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
    return ans
n = int(input())
a = list(map(int, input().split()))
max = 0
if n < 5:
    for i in range(0,n):
        p = a.pop(0)
        x = list_euclid(a)
        if max < x:
            max = x
        a.append(p)
else:
    for i in range(0,n):
        p = a.pop(0)
        k = n // 2
        y = list_euclid(a[0:k])
        z = list_euclid(a[k:n-1])
        x = euclid(y, z)
        if max < x:
            max = x
        a.append(p)
print(max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
    return ans
n = int(input())
a = list(map(int, input().split()))
max = 0
if n < 5:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        x = list_euclid(new_list)
        if max < x:
            max = x
else:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        k = n // 2
        y = list_euclid(new_list[0:k])
        z = list_euclid(new_list[k:n-1])
        x = euclid(y, z)
        if max < x:
            max = x
print(max)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
    return ans

n = int(input())
a = list(map(int, input().split()))
max = 0
for i in range(0,n):
    new_list = a[:]
    del new_list[i]
    x = list_euclid(new_list)
    if max < x:
        max = x
print(max)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
def mgcd(l):
  if len(l) == 1:
    return l[0]
  else:
    new_l = []
    for i in range(-(-len(l)//2)):
      temp = gcd(l[i], l[-i-1])
      if temp == 1:
        return 1
      else:
        new_l.append(temp)
    return mgcd(new_l)

n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
  a_gcd = mgcd(a[:i]+a[i+1:])
  if ans < a_gcd:
    ans = a_gcd
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
def multi_gcd(l):
  l = sorted(l)
  if len(l) == 1:
    return l[0]
  else:
    new_l = []
    if len(l)%2 == 0:
      for i in range(len(l)//2):
        temp_gcd = gcd(l[2*i], l[2*i+1])
        if temp_gcd == 1:
          return 1
        else:
          if temp_gcd not in new_l:
            new_l.append(temp_gcd)
      return multi_gcd(new_l)
    else:
      for i in range(len(l)//2):
        temp_gcd = gcd(l[2*i], l[2*i+1])
        if temp_gcd == 1:
          return 1
        else:
          if temp_gcd not in new_l:
            new_l.append(temp_gcd)
      new_l.append(l[-1])
      return multi_gcd(new_l)
     
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
  a_gcd = multi_gcd(a[:i]+a[i+1:])
  if ans < a_gcd:
    ans = a_gcd
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
def multi_gcd(l):
  if len(l) == 1:
    return l[0]
  else:
    new_l = []
    if len(l)%2 == 0:
      for i in range(len(l)//2):
        new_l.append(gcd(l[2*i], l[2*i+1]))
      return multi_gcd(new_l)
    else:
      for i in range(len(l)//2):
        new_l.append(gcd(l[2*i], l[2*i+1]))
      new_l.append(l[-1])
      return multi_gcd(new_l)
     
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
  a_gcd = multi_gcd(a[:i]+a[i+1:])
  if ans < a_gcd:
    ans = a_gcd
print(ans)

Submission status: Time Limit Exceeded

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


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


n = int(input())
a = list(map(int, input().split()))
max_value = max(a)
ans = 0
for i in range(n):
    target = [a[j] for j in range(n)]
    target.pop(i)
    ans = max(ans, gcd_list(target))
print(ans)

Submission status: Time Limit Exceeded

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


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


n = int(input())
a = list(map(int, input().split()))
max_value = max(a)
ans = 0
for i in range(n):
    target = a[:i]
    target.extend(a[i+1:])
    target.append(max_value)
    ans = max(ans, gcd_list(target))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
a = 0
for i in range(n):
	L,R = 0,0
	for j in range(0,i):
		L = math.gcd(L,list1[j])
	for j in range(i+1,n):
		R = math.gcd(R,list1[j])
	a = max(a,math.gcd(L,R))

print(a)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools

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

gcdlist = []

def gcdpy(numbers):
	return functools.reduce(fractions.gcd, numbers)

for i in range(0,N):
	I = A[i]
	A[i] = A[i-1]
	gcdlist.append(gcdpy(A))
	A[i] = I
	
print(max(gcdlist))