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: Accepted

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


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

prefix_gcd = []

prefix_gcd.append(nums[0])

for i in nums[1:]:
    prefix_gcd.append(gcd(i,prefix_gcd[-1]))

postfix_gcd = []
postfix_gcd.append(nums[-1])

for j in range(n-2,-1,-1):
    postfix_gcd.append(gcd(nums[j],postfix_gcd[-1]))

postfix_gcd = postfix_gcd[::-1]

ans = 1

for i in range(n):
    # del i
    if i == 0:
        ans = max(ans,postfix_gcd[i+1])
    elif i == n-1:
        ans = max(ans,prefix_gcd[i-1])
    else:
        ans = max(ans, gcd(prefix_gcd[i-1], postfix_gcd[i+1]))
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

n = int(raw_input())
a = list(map(int, raw_input().split()))
p = list(a)
s = list(a)
for i in range(1, n):
    p[i] = gcd(p[i - 1], p[i])
for i in range(n - 2, -1, -1):
    s[i] = gcd(s[i], s[i + 1])

ans = max(p[-2], s[1])
if n > 2:
    ans = max(ans, max(gcd(p[i - 1], s[i + 1]) for i in range(1, n - 1)))

print(ans)

Submission status: Accepted

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n

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

L = [0]*N
L[0] = A[0]
v = A[0]
for i in range(N-1):
    L[i+1] = v = gcd(v, A[i+1])
R = [0]*N
R[-1] = A[-1]
v = A[-1]
for i in range(N-1, -1, -1):
    R[i] = v = gcd(v, A[i])
ans = max(L[-1], L[-2], R[1])
for i in range(1, N-1):
    ans = max(ans, gcd(L[i-1], R[i+1]))
print(ans)

Submission status: Accepted

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

from fractions import gcd
from functools import reduce

ans = reduce(gcd, A[1:]) # 0 番目を除いた　
ans = max(ans, reduce(gcd, A[:-1]))
stock = [A[0]] * N
revS  = [A[-1]] * N
for i in range(1, N):
    stock[i] = gcd(stock[i-1], A[i])

for i in range(N-2, -1, -1):
    revS[i] = gcd(revS[i+1], A[i] )

for i in range(1, N-1):
    # i はカウントしない
    ans = max( ans,  gcd(stock[i-1], revS[i+1]))
print(ans)

Submission status: Accepted

Solution code:
from fractions import *
n = int(input())
a = [0]+list(map(int, input().split()))+[0]
b = [0]*(n+2)
c = [0]*(n+2)
for i in range(1, n+1):
	b[i] = gcd(b[i-1], a[i])
for i in range(n, 0, -1):
	c[i] = gcd(c[i+1], a[i])
print(max(gcd(b[i-1], c[i+1]) for i in range(1, n+1)))

Submission status: Accepted

Solution code:
from fractions import gcd

read_ints = lambda : map(int, raw_input().strip().split())

n = input()
a = read_ints()

pre = [0] * (n + 1)
for i in xrange(1, n + 1):
    pre[i] = gcd(pre[i - 1], a[i - 1])

suf = [0] * (n + 1)
suf[n - 1] = a[n - 1]
for i in xrange(n - 2, -1, -1):
    suf[i] = gcd(suf[i + 1], a[i])

ans = pre[n]
for i in xrange(n):
    g1 = pre[i]
    g2 = suf[i + 1]
    g = gcd(g1, g2)
    ans = max(ans, g)

print ans

Submission status: Runtime Error

Solution code:
from math import gcd

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    if N == 2:
        print(max(A[0], A[1]))
        exit()
    max_gcd = 0
    for i in range(N):
        nums = []
        for j in range(N):
            if i==j:
                continue 
            nums.append(A[j])
        max_gcd = max(max_gcd, gcd(*nums))
    print(max_gcd)

Submission status: Runtime Error

Solution code:
from math import gcd

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    smaller = A[:2]
    bigger = A[:3]
    max_gcd = 0
    for i in range(3):
        nums = []
        for j in range(3):
            if i==j:
                continue 
            nums.append(bigger[j])
        max_gcd = max(max_gcd, gcd(*nums))
        
    print(max_gcd)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import deque
from math import gcd
a_left=deque()

a_left.append(a[0])


a_right=deque()

a_right.append(a[-1])

for i in range(n-1):
    a_left.append(gcd(a_left[-1],a[i+1]))
    a_right.appendleft(gcd(a_right[0],a[n-i-2]))


a_right=list(a_right)
a_left=list(a_left)

count=max(a_left[-2],a_right[2])

for j in range(n-2):
    count=max(gcd(a_left[j],a_right[j+2]),count)
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
r=[0]+list(map(int,input().split()))+[0]
u=r[1]
for i in range(2,n+1):u=gcd(u,r[i])
prefix=r
suffix=r
from math import gcd
for i in range(2,n+1):
  prefix[i]=gcd(prefix[i-1],r[i])
for i in range(n-1,0,-1):
  suffix[i]=gcd(suffix[i+1],r[i])
m=0
for i in range(1,n+1):
  m=max(m,gcd(prefix[i-1],suffix[i+1]))
print(max(m,u))

Submission status: Runtime Error

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

ans = 1
for i in range(N):
    g = reduce(math.gcd,A[:i]+A[i+1:])
    ans = max(ans,g)
print(ans)

Submission status: Runtime Error

Solution code:
"""
最大公約数をもっとも落としている数Xを削る（＝見ないことにする）、という発想に置き換える。
方針１：
Xを特定する。
どうやって特定する？
各Aを素因数分解する。このとき、他の

方針２：
約数をカウントする。すべてのAの約数をカウントして総和する。
このとき、カウントがN個になる約数は初期状態での公約数である。
一方で、カウントがN-1個の約数は、その約数をもたない数が一つだけ存在するということなので、
そのような数Aを削ったときに、公約数になる。
したがって、カウントがN-1個以上の約数のうち最大のものが、数Xを削ったときの最大公約数である。
問題はどうやって約数をカウントするかだ。愚直にやるとO(N√max(A))かかるため間に合わない。
ただ、約数の数自体はせいぜい1000個くらいのはず。いったん素因数分解してから、各素因数を何個累乗させるかで考えればよいか。
"""
def main():
    N = int(input())
    A = list(map(int,input().split()))
    count = {}

    for a in A:
        tmp = []
        d = 2
        while d*d <= a:
            while a%d == 0:
                tmp.append(d)
                a //= d
            d += 1
        if a != 1:
            tmp.append(a)
        divides = [1]
        for k,v in Counter(tmp).items():
            M = len(divides)
            for i in range(1,v+1):
                for j in range(M):
                    divides.append(divides[j]*pow(k,i))
        
        for d in divides:
            if d not in count:
                count[d] = 0
            count[d] += 1

    ans = 0
    for k,v in count.items():
        if v >= N-1:
            ans = max(ans,k)
    print(ans)
main()

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))

count = np.zeros(max(A)+1)
count[1] = N
for i in range(N):
  count[A[i]] += 1		# 自身
  for j in range(2, int(A[i]//2)+2):
    if A[i] % j == 0:
      count[j] += 1

ans = 0
for i in range(len(count)-1,-1,-1):
  if i == N-1 and count[i] == N:
    ans = i
    break
  elif count[i] == N-1:
    ans = i
    break
if ans == 0:
  print(1)
else:
  print(ans)

Submission status: Runtime Error

Solution code:
#これだとTLEする
from fractions import gcd
ans=0
for i in range(N):
	temp=0
	for idx, a in enumerate(A):
		if idx==i:
			continue
		temp=gcd(temp, a)
	ans=max(ans,temp)
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations
from collections import Counter

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

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

fact = []
for a in A:
    x = prime_factorize(a)
    fact.append(x)

result = []
for f in fact:
    for i in range(1, len(f)+1):
        c = list(set(combinations(f, i)))
        for cc in c:
            result.append(cc)

count = dict(Counter(result))

ans = []
for key, value in count.items():
    temp = 1
    if value >= N-1:
        for k in key:
            temp *= k
        ans.append(temp)
        

print(max(ans))

Submission status: Runtime Error

Solution code:
class SegmentTree():
    def __init__(self, arr, func=min, ie=2**63):
        self.h = (len(arr) - 1).bit_length()
        self.n = 2**self.h
        self.ie = ie
        self.func = func
        self.tree = [ie for _ in range(2 * self.n)]
        for i in range(len(arr)):
            self.tree[self.n + i] = arr[i]
        for i in range(1, self.n)[::-1]:
            self.tree[i] = func(self.tree[2 * i], self.tree[2 * i + 1])

    def set(self, idx, x):
        idx += self.n
        self.tree[idx] = x
        while idx:
            idx >>= 1
            self.tree[idx] = self.func(self.tree[2 * idx], self.tree[2 * idx + 1])

    def query(self, lt, rt):
        lt += self.n
        rt += self.n
        vl = vr = self.ie
        while rt - lt > 0:
            if lt & 1:
                vl = self.func(vl, self.tree[lt])
                lt += 1
            if rt & 1:
                rt -= 1
                vr = self.func(self.tree[rt], vr)
            lt >>= 1
            rt >>= 1
        return self.func(vl, vr)

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

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

st = SegmentTree(A, gcd, 0)
res = 1

for i in range(N):
    res = max(res, gcd(st.query(0, i), st.query(i + 1, N + 1)))

print(res)

Submission status: Runtime Error

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

ra=list(reversed(a))

left=[]
right=[]
right.append(ra[0])
left.append(a[0])

if n>2:
  left.append(math.gcd(a[0],a[1]))
  for i in range(2,n-1):
    k=math.gcd(k,a[i])
    left.append(k)

  right.append(math.gcd(ra[0],ra[1]))
  for i in range(2,n-1):
    k=math.gcd(k,ra[i])
    right.append(k)

  ans=[right[-1]]
  for i in range(1,n-1):
    ans.append(math.gcd(left[i-1],right[len(right)-i-1]))
  ans.append(left[-1])
  print(max(ans))
else:
  print(max(ra[0],a[0]))

Submission status: Runtime Error

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

ra=list(reversed(a))

left=[]
right=[]
right.append(ra[0])
left.append(a[0])

if n>2:
  left.append(math.gcd(a[0],a[1]))
  for i in range(2,n-1):
    k=math.gcd(k,a[i])
    left.append(k)

  right.append(math.gcd(ra[0],ra[1]))
  for i in range(2,n-1):
    k=math.gcd(k,ra[i])
    right.append(k)

  ans=[right[-1]]
  for i in range(1,n-1):
    ans.append(math.gcd(left[i-1],right[len(left)-i-1]))
  ans.append(left[-1])
  print(max(ans))
else:
  print(max(ra[0],a[0]))

Submission status: Runtime Error

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

ra=list(reversed(a))

left=[]
right=[]
right.append(ra[0])
left.append(a[0])

if n>2:
  import math
  left.append(math.gcd(a[0],a[1]))
  for i in range(2,n-1):
    k=math.gcd(k,a[i])
    left.append(k)

  right.append(math.gcd(ra[0],ra[1]))
  for i in range(2,n-1):
    k=math.gcd(k,ra[i])
    right.append(k)

  ans=[right[-1]]
  for i in range(1,n-1):
    ans.append(math.gcd(left[i-1],right[len(left)-i-1]))
  ans.append(left[-1])
  print(max(ans))
else:
  print(max(ra[0],a[0]))

Submission status: Runtime Error

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

ra=list(reversed(a))

left=[]
right=[]
right.append(ra[0])
left.append(a[0])

if n>2:
  import math
  left.append(math.gcd(a[0],a[1]))
  for i in range(2,n-1):
    k=math.gcd(k,a[i])
    left.append(k)

  right.append(math.gcd(ra[0],ra[1]))
  for i in range(2,n-1):
    k=math.gcd(k,ra[i])
    right.append(k)

  print(left)
  print(right)
  ans=[right[-1]]
  for i in range(1,n-1):
    ans.append(math.gcd(left[i-1],right[len(left)-i-1]))
  ans.append(left[-1])
  print(max(ans))
else:
  print(max(ra[0],a[0]))

Submission status: Runtime Error

Solution code:
import math
import sys

n = input()
a = list(map(int, input().split()))
b = sorted(a)
c = set(b)
if len(c) == 1:
  print(c[0])
  sys.exit()
pl = []
ql = []
for i in b[1:]:
  pl.append(math.gcd(b[0], i))
for i in b[2:]:
  ql.append(math.gcd(b[1], i))
pls = sorted(pl)
qls = sorted(ql)
if len(qls) == 0:
  print(pls[min(1, len(pls) - 1)])
else:
  print(max(pls[min(1, len(pls) - 1)], qls[0]))

Submission status: Runtime Error

Solution code:
import math
import sys

n = input()
a = list(map(int, input().split()))
b = sorted(set(a))
if len(b) == 1:
  print(b[0])
  sys.exit()
pl = []
ql = []
for i in b[1:]:
  pl.append(math.gcd(b[0], i))
for i in b[2:]:
  ql.append(math.gcd(b[1], i))
pls = sorted(pl)
qls = sorted(ql)
print(max(pls[1], qls[min(1, len(qls) - 1)]))

Submission status: Runtime Error

Solution code:

import math
n = int(input())
a_lis = list(map(int, input().split()))
a_lis.sort()
#print(a_lis)
ans = a_lis[0]
ans_list = []

for i in a_lis:
    an = math.gcd(ans, i) 
    ans_list.append(an)
    ans = an
    
#print(ans_list)
ts = ans_list.index(ans_list[-1])
if ts ==1:
  if math.gcd(a_lis[0], a_lis[1]) == math.gcd(a_lis[1],a_lis[2]):
    a_lis.pop(1)
  else:
    a_lis.pop(0)
else:
  a_lis.pop(ts)
#print(a_lis)
ans_0 = a_lis[0]
for i in a_lis:
  ans_0 = math.gcd(i,ans_0)
print(ans_0)

Submission status: Runtime Error

Solution code:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1000000007
#define INF 1e9
#define PIE 3.14159265358979323

template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
T GCD(T a, T b) {
    if (a == -1)
        return b;
    else if (b == -1)
        return a;
    if (b == 0)
        return a;
    else
        return GCD(b, a % b);
}
template <class T>
inline T LCM(T a, T b) {
    return (a * b) / GCD(a, b);
}

using namespace std;
//#inculude <bits/stdc++.h>
//#define int long long

template <class T>
class SegmentTree {
  private:
    int n = 1;
    vector<T> segtree;
    function<T(T, T)> operation;
    T ie;

    T _query(int a, int b, int k, int l, int r) {
        if (a >= r || b <= l)
            return ie;
        else if (a <= l && b >= r)
            return segtree[k];
        else {
            int vl = _query(a, b, 2 * k + 1, l, (l + r) / 2);
            int vr = _query(a, b, 2 * k + 2, (l + r) / 2, r);
            return operation(vl, vr);
        }
    }

  public:
    SegmentTree(vector<T> v, function<T(T, T)> _operation, T _ie)
        : operation(_operation), ie(_ie) {
        while (v.size() > n)
            n *= 2;
        segtree.resize(2 * n - 1, ie);
        for (int i = 0; i < v.size(); i++)
            segtree[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--)
            segtree[i] = operation(segtree[2 * i + 1], segtree[2 * i + 2]);
    }

    T operator[](int i) {
        return segtree[i + n - 1];
    }

    void update(int i, T x) {
        i += n - 1;
        segtree[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;
            segtree[i] = operation(segtree[2 * i + 1], segtree[2 * i + 2]);
        }
    }

    T query(int a, int b) {
        return _query(a, b, 0, 0, n);
    }
};

signed main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    auto f = [](int a, int b) -> int { return GCD(a, b); };
    SegmentTree<int> segtree(a, f, -1);
    int res = max(segtree.query(1,n),segtree.query(0,n-1));
    for (int i = 1; i < n; i++)
        chmax(res, GCD(segtree.query(0, i), segtree.query(i + 1, n)));
    cout << res << endl;
}

Submission status: Runtime Error

Solution code:
# import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")


class SegmentTree:
    def __init__(self, size, f=lambda x, y: x + y, default=0):
        self.size = 2 ** (size - 1).bit_length()
        self.default = default
        self.dat = [default] * (self.size * 2)
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def query(self, l, r):
        """return f([l,r))
        """
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res


N = int(input())
A = list(map(int, input().split()))
if N == 2:
    print(max(A))
    exit()
s = SegmentTree(N, gcd, 0)
for i, a in enu(A):
    s.update(i, a)
res = 0
for i in range(N):
    if i == 0:
        val = s.query(i + 1, N + 1)
    elif i == N - 1:
        val = s.query(0, i)
    else:
        l = s.query(0, i)
        r = s.query(i + 1, N + 1)
        val = gcd(l, r)
    if res < val:
        res = val
print(res)
# for i in range(q):
#     com, x, y = map(int, input().split())
#     if com == 0:
#         s.update(x, y)
#     else:
#         print(s.query(x, y + 1))

Submission status: Runtime Error

Solution code:
# import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")


class SegmentTree:
    def __init__(self, size, f=lambda x, y: x + y, default=0):
        self.size = 2 ** (size - 1).bit_length()
        self.default = default
        self.dat = [default] * (self.size * 2)
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def query(self, l, r):
        """return f([l,r))
        """
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res


N = int(input())
A = list(map(int, input().split()))
if N == 2:
    print(max(A))
    exit()
s = SegmentTree(N, gcd, 0)
for i, a in enu(A):
    s.update(i, a)
res = 0
for i in range(N):
    l = s.query(0, i)
    r = s.query(i + 1, N + 1)
    val = gcd(l, r)
    # print(i, l, r)
    if res < val:
        res = val
print(res)

Submission status: Runtime Error

Solution code:
import math
from itertools import accumulate

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

fromleft = list(accumulate(a, math.gcd))
fromright = list(accumulate(a[::-1], math.gcd)).reverse()

gcd = []

gcd.append(fromright[1])
for i in range(1, n-1):
    gcd.append(math.gcd(fromleft[i-1], fromright[i+1]))
gcd.append(fromleft[n-2])

print(max(gcd))

Submission status: Runtime Error

Solution code:
import math
from itertools import accumulate

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

fromright = list(accumulate(a, math.gcd))
a.reverse()
fromleft = list(accumulate(a, math.gcd))
fromleft.reverse()

gcd = []

for i in range(1, n-1):
  gcd.append(math.gcd(fromright[i-1], fromleft[i+1]))

print(max(gcd))

Submission status: Runtime Error

Solution code:
import math
from intertools import accumulate

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

g = []
gcd = []
gcd_u = np.frompyfunc(gcd, 2, 1)

for i in range(n):
  r = a[i]
  a.remove(r)
  gcd = accmulate(a, math.gcd)
  g.append(min(gcd))
  a.insert(i, r)

print(max(g))

Submission status: Runtime Error

Solution code:
import math

def gcd(list):
    ans = list[0]
    for i in range(len(list)):
        ans = math.gcd(ans,list[i])
    return ans

N = int(input())

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

ans = 1

for i in range(N):
    lef = nums[:i]
    rig = nums[i+1:]
    
    gcd = math.gcd(gcd(lef),gcd(rig))
    if ans < gcd:
        ans = gcd
        
print(ans)

Submission status: Runtime Error

Solution code:
def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]
n=int(input())
a=list(map(int, input().split()))
a.sort()
num=a[1]
alist=[0]*(num+2)
for i in range(n):
  for j in make_divisors(a[i]):
    if j<=num:
      alist[j]+=1
ans=0
for i in range(num+2):
  if alist[i]>=n-1:
    ans=i
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd

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

if n == 2:
    print(max(a))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(A[0], A[1]), gcd(A[1], A[2]), gcd(A[2], A[0]))
    # リストaの最初3個の最大公約数
    a = gcd(A[0], gcd(A[1], A[2]))

    for i in range(3, n):
        r = max(gcd(A[i], r), a)
        a = gcd(A[i], a)

    print(r)

Submission status: Runtime Error

Solution code:
from math import gcd

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

if n == 2:
    print(max(a))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(a[0], a[1]), gcd(a[1], a[2]), gcd(a[2], a[0]))
    # リストaの最初3個の最大公約数
    a = gcd(a[0], gcd(a[1], a[2]))

    for i in range(3, n):
        r = max(gcd(a[i], r), a)
        a = gcd(a[i], a)

    print(r)

Submission status: Runtime Error

Solution code:
n=int(input())
import math
from functools import reduce
ans=0
def gcd(numbers):
  return reduce(math.gcd, numbers)
import itertools
l=list(map(int, input().split()))
l=set(l)
if len(l)==1:
  print(l[0])
  exit()
elif len(l)==2:
  print(max(l))
  exit()
else: 
  for v in itertools.combinations(l, len(l)-1):
    ans=max(ans, gcd(v))
print(ans)

Submission status: Runtime Error

Solution code:
import  sympy
n = int(input())
a = list(map(int,input().split()))
d = {}
for i in [0,1]:
    for j in sympy.divisors(a[i]):
        d[j] = 2
key = list(d.keys())
for i in range(n):
    c = key[:]
    b = sympy.divisors(a[i])
    for j in c:
        if not(j in b):
            d[j] -= 1
            if d[j] == 0:
                key.remove(j)
key = sorted(key,reverse = True)
print(key[0])

Submission status: Runtime Error

Solution code:
from math import gcd
from functools import reduce
n=int(input())
a=list(map(int,input().split()))
ans=reduce(gcd,a[1:])
ans=max(ans,reduce(gcd,a[:n-1]))
for i in range(1,n-1):
  ans=max(gcd(reduce(gcd,a[:i]),reduce(gcd,a[i+1:])),ans)
print(ans*gc)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

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

if len(a) == 2:
    ans = max(a)
else:
    ans = 1
    a.sort()
    m = a[1]
    check = np.array([-1]*(m+1))
    
    for i in range(2, m+1):
        if check[i] == 0:
            continue
        cnt = 0
        for j in range(n):
            if a[j] % i > 0:
                cnt += 1
            if cnt >= 2:
                check[i::i] = 0
                break
            if j == n-1:
                ans = copy.deepcopy(i)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

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

if len(a) == 2:
    ans = max(a)
else:
    a.sort()
    m = a[1]
    check = np.array([-1]*(m+1))
    
    for i in range(2, m+1):
        if check[i] == 0:
            continue
        cnt = 0
        for j in range(n):
            if a[j] % i > 0:
                cnt += 1
            if cnt >= 2:
                check[i::i] = 0
                break
            if j == n-1:
                ans = copy.deepcopy(i)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

import math
from functools import reduce

# 最大公約数（リスト引数）
def GCD_list(numbers):
    return reduce(math.gcd, numbers)

# 最大公約数
# 計算量：O(logA) (A < Bとした時)
def GCD(*numbers):
    return reduce(math.gcd, numbers)

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

maxgcd = 1
for i in range(N):
    if N == 2:
        gcd = A[i]
    else:
        gcd = GCD(A[j] for j in range(N) if j != i)
    if gcd > maxgcd:
        maxgcd = gcd

print(maxgcd)

Submission status: Runtime Error

Solution code:
from sympy import divisors

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

p=divisors(lst[0])
q=divisors(lst[1])
pq=set(p+q)
pq=sorted(pq)
for i in range(len(pq)-1,-1,-1):
    out=0
    for j in lst:
        if j%pq[i]!=0:
            out+=1
    if out<=1:
        print(pq[i])
        exit()

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read
from math import gcd
from collections import deque
from operator import mul
from functools import reduce
def main():
    n = int(input())
    a = list(map(int, read().split()))
    if n == 2:
        print(max(a))
        sys.exit()
    from math import sqrt
    from math import ceil
    maxa = max(a)
    maxa2 = ceil(sqrt(maxa))
    maxa2 = max(6, maxa2)  # maxa2が6未満だとエラーになる
    p = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(maxa2)]
    p[0] = p[1] = False
    p[2] = p[3] = p[5] = True
    for is1 in range(3, maxa2, 2):
        for is2 in range(is1 ** 2, maxa2, is1):
            p[is2] = False
    prime_list = [i for i, b in enumerate(p) if b]
    div = []
    koho = -1
    for pe in prime_list:
        while True:
            check = [i % pe == 0 for i in a]
            if all(check):
                div.append(pe)
                a = [i // pe for i in a]
            else:
                if n - sum(check) == 1:
                    koho = pe
                break
    if koho > 0:
        div.append(koho)
    r = reduce(mul, div)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read
from math import gcd
from collections import deque
from operator import mul
from functools import reduce
def main():
    n = int(input())
    a = list(map(int, read().split()))

    from math import sqrt
    from math import ceil
    maxa = max(a)
    maxa2 = ceil(sqrt(maxa))
    maxa2 = max(6, maxa2)  # maxa2が6未満だとエラーになる
    p = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(maxa2)]
    p[0] = p[1] = False
    p[2] = p[3] = p[5] = True
    for is1 in range(3, maxa2, 2):
        for is2 in range(is1 ** 2, maxa2, is1):
            p[is2] = False
    prime_list = [i for i, b in enumerate(p) if b]
    div = []
    koho = -1
    for pe in prime_list:
        while True:
            check = [i % pe == 0 for i in a]
            if all(check):
                div.append(pe)
                a = [i // pe for i in a]
            else:
                if n - sum(check) == 1:
                    koho = pe
                break
    if koho > 0:
        div.append(koho)
    r = reduce(mul, div)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
import math


def compute_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# This function computes LCM
def compute_lcm(x, y):
   lcm = (x*y)//compute_gcd(x,y)
   return lcm

n = int(input())
arr = []
left = []
right = []
mx=1


for i in range (0,n):
    lmt = int(input())
    arr.append(lmt)
    mx= mx* arr[i]

left.append(arr[0])
for i in range(1,n):
    left.append(compute_lcm(left[i-1],arr[i]))
    
right.append(arr[n-1])

for i in range(n-2,-1,-1):
    right.append(compute_lcm(right[-1],arr[i]))

right.reverse()



#print(left)
#print(right)

ans=0


ans = max( (mx/arr[0])/right[1] , (mx/arr[n-1])/left[n-2] )

for i in range (1,n-1):
    ans = max( ans, (mx/arr[i])/(compute_lcm(left[i-1],right[i+1]) ))
    
print(int(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
import math


def compute_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# This function computes LCM
def compute_lcm(x, y):
   lcm = (x*y)//compute_gcd(x,y)
   return lcm

n = int(input())
arr = []
left = []
right = []
mx=1


for i in range (0,n):
    arr.append(int(input()))
    mx= mx* arr[i]

left.append(arr[0])
for i in range(1,n):
    left.append(compute_lcm(left[i-1],arr[i]))
    
right.append(arr[n-1])

for i in range(n-2,-1,-1):
    right.append(compute_lcm(right[-1],arr[i]))

right.reverse()



#print(left)
#print(right)

ans=0


ans = max( (mx/arr[0])/right[1] , (mx/arr[n-1])/left[n-2] )

for i in range (1,n-1):
    ans = max( ans, (mx/arr[i])/(compute_lcm(left[i-1],right[i+1]) ))
    
print(int(ans))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def gcd(numbers):
    return reduce(math.gcdm numbers)

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

ans = []

if len(A) == 1:
    ans.append(A[0])
else:
    ans.append(gcd(A[1:]))
    ans.append(gcd(A[:N]))

    for i in range(1, N-1):
        l = []
        l.append(gcd(A[:i]))
        l.append(gcd(A[i+1:]))
        ans.append(gcd(l))

print(max(ans))

Submission status: Runtime Error

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

res = []
ans = []
x = 0
if n > 2:
  while x < math.factorial(n):
    c = random.sample(a, 2)
    if c not in res:
      res.append(c)
      x += 1 
      ans.append(math.gcd(c[0],c[1]))
  print(max(ans))
elif n == 2:
  while x < math.factorial(n)/2:
    c = random.sample(a, 2)
    if c not in res:
      res.append(c)
      x += 1 
      ans.append(math.gcd(c[0],c[1]))
  print(max(ans))

Submission status: Runtime Error

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

res = []
ans = []
x = 0
while x < math.factorial(n)/2:
  c = random.sample(a, 2)
  if c not in res:
    res.append(c)
    x += 1 
    ans.append(math.gcd(c[0],c[1]))
print(max(ans))