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: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd
import sys

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

# 隣同士でgcd
# 最小の値のindex（最小の中でも最後のindex）特定
# 最小の値のindexの元のリストの値を削除（最大公約数を最大化する上で無視）
# 全体のgcd求める

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

gcds = []
for i in range(n - 1):
  gcds.append(gcd(nums[i], nums[i+1]))

min_index = -1
min_num = 10 ** 9 + 1
for i, g in enumerate(gcds):
  if g <= min_num:
    min_num = g
    min_index = i
  if min_num == 1:
    break

# print('nums[{}]:{}を削除する'.format(min_index, nums[min_index]))
del nums[min_index]
# print('nums:{}'.format(nums))
ans = gcd_list(nums)
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

n = int(input())
a_lst = list(map(int,input().split()))
gcd_lst = []

for i in range(n):
    b_lst = a_lst.copy()
    del b_lst[i]
    max_gcd = b_lst[0]

    for j in range(n-2):
        b1 = b_lst[j]
        b2 = b_lst[j+1]
        max_gcd = fractions.gcd(b1,b2)

    gcd_lst.append(max_gcd)

answer = max(gcd_lst)
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)
if N == 2:
    print(max(A[0],A[1]))
else:
    t = [gcd(A[0],A[1]),gcd(A[0],A[2]),gcd(A[1],A[2])]
    ans = 1
    for e in t:
        f = 0
        for g in A:
            if g%e != 0:
                f += 1
            if f == 2:
                break
        if f <= 1:
            ans = max(ans,e)
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)
if N == 2:
    print(max(A[0],A[1]))
else:
    t1 = gcd(A[0],A[1])
    t2 = gcd(A[0],A[2])
    t3 = gcd(A[1],A[2])

    f = 0
    ans = 1
    for e in A:
        if e%t1 != 0:
            f += 1
        if f == 2:
            break
    if f <= 1:
        ans = max(ans,t1)

    f = 0
    for e in A:
        if e%t2 != 0:
            f += 1
        if f == 2:
            break
    if f <= 1:
        ans = max(ans,t2)

    f = 0
    for e in A:
        if e%t3 != 0:
            f += 1
        if f == 2:
            break
    if f <= 1:
        ans = max(ans,t3)

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
class SegTree:
  def segfunc(self, x, y):
    return gcd(x, y) #ここ判定関数（ただし結合則があるもの）
  def __init__(self, n, ide_ele, init_val):
    #####単位元######
    self.ide_ele = ide_ele
    #num:n以上の最小の2のべき乗
    self.num = 2 ** (n - 1).bit_length()
    self.seg = [self.ide_ele] * 2 * self.num
    #set_val
    for i in range(n):
      self.seg[i + self.num - 1] = init_val[i]    
    #built
    for i in range(self.num - 2, -1, -1) :
      self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) 
  def update(self, k, x):
    k += self.num - 1
    self.seg[k] = x
    while k + 1:
      k = (k - 1) // 2
      self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) 
  def query(self, p, q):
    if q <= p:
      return self.ide_ele
    p += self.num - 1
    q += self.num - 2
    res = self.ide_ele
    while q - p > 1:
      if p & 1 == 0:
        res = self.segfunc(res, self.seg[p])
      if q & 1 == 1:
        res = self.segfunc(res, self.seg[q])
        q -= 1
      p = p // 2
      q = (q - 1) // 2
    if p == q:
      res = self.segfunc(res, self.seg[p])
    else:
      res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
    return res
seg = SegTree(N, 0, a)
res = 0
for i in range(N):
  res = max(res, gcd(seg.query(0, i - 1), seg.query(i, N)))
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
lgcd = [0]*N
rgcd = [0]*N
from fractions import gcd
#def gcd(a,b):
#    if b == 0:return a
#    else:return gcd(b, a%b)

temp_lgcd = A[0]
for i in range(N):
    temp_lgcd = gcd(temp_lgcd,A[i])
    lgcd[i] = temp_lgcd

temp_rgcd = A[-1]
for i in range(N-1,-1,-1):
    temp_rgcd = gcd(temp_rgcd,A[i])
    rgcd[i] = temp_rgcd

ans = 0
for i in range(N):
    if i == 0:
        calc_gcd = rgcd[1]
    elif i == N-1:
        calc_gcd = lgcd[-2]
    else:
        calc_gcd = gcd(lgcd[i-1],rgcd[-i])
    ans = max(ans,calc_gcd)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
lgcd = [0]*N
rgcd = [0]*N
def gcd(a,b):
    if b == 0:return a
    else:return gcd(b, a%b)

temp_lgcd = A[0]
for i in range(N):
    temp_lgcd = gcd(temp_lgcd,A[i])
    lgcd[i] = temp_lgcd

temp_rgcd = A[-1]
for i in range(N-1,-1,-1):
    temp_rgcd = gcd(temp_rgcd,A[i])
    rgcd[i] = temp_rgcd

ans = 0
for i in range(N):
    if i == 0:
        calc_gcd = rgcd[1]
    elif i == N-1:
        calc_gcd = lgcd[-2]
    else:
        calc_gcd = gcd(lgcd[i-1],rgcd[-i])
    ans = max(ans,calc_gcd)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
lgcd = [0]*N
rgcd = [0]*N
def gcd(a,b):
    if b == 0:return a
    else:return gcd(b, a%b)

temp_lgcd = A[0]
for i in range(N):
    temp_lgcd = gcd(temp_lgcd,A[i])
    lgcd[i] = temp_lgcd

temp_rgcd = A[-1]
for i in range(N-1,0,-1):
    temp_rgcd = gcd(temp_rgcd,A[i])
    rgcd[i] = temp_rgcd

ans = 0
for i in range(N):
    if i == 0:
        calc_gcd = rgcd[1]
    elif i == N-1:
        calc_gcd = lgcd[-2]
    else:
        calc_gcd = gcd(lgcd[i-1],rgcd[-i])
    ans = max(ans,calc_gcd)
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(n,m):#n,mは正の整数、最大公約数　許容は？
    if n==0:
        return m
    elif m==0:
        return n
    elif n>m:
        n=n%m
        return gcd(n,m)
    else:
        m=m%n
        return gcd(n,m)

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

list1=[1 for i in range(0,N)]
list2=[1 for i in range(0,N)]
list1[0]=A[0]
list2[0]=A[N-1]

for i in range(1,N):
    list1[i]=gcd(list1[i-1],A[i])
    list2[i]=gcd(list2[i-1],A[N-1-i])
m=list2[N-2]
for i in range(0,N-2):
    test=gcd(list1[i],list2[N-3-i])
    if test>m:
        m=test
test=list2[N-2]
if test>m:
    m=test
print(m)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(1000000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    n = int(input())
    a = list(map(int,input().split()))
    l = [0]*n
    r = [0]*n
    for i in range(n):
        if i == 0:
            l[0] = a[0]
            r[n-1] =a[n-1]
        else:
            l[i] = gcd(a[i],l[i-1])
            r[n-i-1] = gcd(a[n-i-1],r[n-i])
    if n > 2:
        ans = max(r[1],l[n-2])
    else:
        ans = a[0]
    for i in range(1,n-1):
        ans = max(ans,gcd(l[i-1],r[i+1]))

    print(ans)
if __name__ == "__main__":
    main() 
    
#endline===============================================

Submission status: Wrong Answer

Solution code:
import fractions as fr

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

def gcd_i(l,i):
  if i==0:
    tmp=l[1]
    for j in range(1,n):
      tmp=fr.gcd(tmp,l[j])
  if i==n-1:
    tmp=l[0]
    for j in range(1,n-1):
      tmp=fr.gcd(tmp,l[j])
  else:
    tmp=l[0]
    for j in range(1,i):
      tmp=fr.gcd(tmp,l[j])
    for j in range(i+1,n):
      tmp=fr.gcd(tmp,l[j])
  return tmp

for i in range(n):
  tmp=0
  tmp=max(tmp,gcd_i(l,i))
  
print(tmp)

Submission status: Wrong Answer

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

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

s1 = [a[0]]
s2 = [a[-1]]
for i in range(1, len(a)-1):
    s1.append(gcd(s1[-1], a[i]))
    s2 = [gcd(s2[-1], a[-(i+1)])] + s2
s1 = [a[-1]] + s1
s2 = s2 + [a[0]]


ans = 0
for i in range(len(s1)):
    ans = max(ans, (gcd(s1[i], s2[i])))

print(ans)

Submission status: Wrong Answer

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

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

s1 = [a[0]]
s2 = [a[-1]]
for i in range(1, len(a)-1):
    s1.append(gcd(s1[-1], a[i]))
    s2 = [gcd(s2[-1], a[-(i+1)])] + s2
s1 = [a[-1]] + s1
s2 = s2 + [a[0]]


ans = 0
for i in range(len(s1)):
    ans = max(ans, (gcd(s1[i], s2[i])))

Submission status: Wrong Answer

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

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

s1 = [a[0]]
s2 = [a[-1]]
for i in range(1, len(a)-1):
    s1.append(gcd(s1[-1], a[i]))
    s2 = [gcd(s2[-1], a[-(i+1)])] + s2
s1 = [a[-1]] + s1
s2 = s2 + [a[0]]


s3 = []
for i in range(len(s1)):
    s3.append(gcd(s1[i], s2[i]))

print(max(s3))

Submission status: Wrong Answer

Solution code:

import sys
from fractions import gcd

N=input()
A=map(int, sys.stdin.readline().split())

L=[]
R=[]

for i,a in enumerate(A):
    if i==0:
        L.append(a)
    else:
        L.append( gcd(L[-1],a) )

for i,a in enumerate(A[::-1]):
    if i==0:
        R.append(a)
    else:
        R.append( gcd(R[-1],a) )
R=R[::-1]

ans=float("-inf")
for i in range(N):
    if i==0:
        ans=max(ans,R[1])
    elif i==N-1:
        ans=max(ans,L[1])
    else:
        ans=max( ans,gcd(L[i-1],R[i+1]) )

print ans

Submission status: Wrong Answer

Solution code:
import fractions

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

    ans = 1
    for i in range(n):
        tmp = num_list[i]
        num_list[i] = max(num_list)
        ans = max(ans, fractions.gcd(num_list[i],min(num_list)))
        num_list[i] = tmp

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd

n = int(input())
*A, = map(int, input().split())
B = A[::-1]
L, R = [0]*(n+1), [0]*(n+1)
for i in range(n):
    L[i+1] = gcd(L[i], A[i])
    R[i+1] = gcd(R[i], B[i])
R = R[1:][::-1]
print(max(gcd(l, r) for l, r in zip(L, R)))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

n = int(input())
*A, = map(int, input().split())
B = A[::-1]
L, R = [0]*(n+1), [0]*(n+1)
for i in range(n):
    L[i+1] = gcd(L[i], A[i])
    R[i+1] = gcd(R[i], B[i])
R = [0] + R[1:][::-1]
print(max(gcd(L[i-1], R[i+1]) for i in range(1, n)))

Submission status: Wrong Answer

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

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

l = [0]*(n+1)
r = [0]*(n+1)
for i in range(n):
    l[i+1] = gcd(l[i],L[i])
    r[i+1] = gcd(r[i],L[n-i-1])
r = r[::-1]

ans = 0
for i in range(n-1):
    ans = max(ans,gcd(l[i],r[i+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

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

else:
    a.append(a[0])
    neighbors_gcd = [gcd(a[i], a[i+1]) for i in range(n)]
    print(sorted(neighbors_gcd)[2])

Submission status: Wrong Answer

Solution code:
from fractions import gcd


class SegmentTree():
    _data = []
    _offset = 0
    _size = 0

    def __init__(self, size):
        _size = size
        t = 1
        while t <= size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)

    def update_all(self, iterable):
        self._data[self._offset:self._offset+self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def query(self, start, stop):
        result = 0
        l = start + self._offset
        r = stop + self._offset
        while l < r:
            if l & 1 == 1:
                result = gcd(result, self._data[l])
                l += 1
            if r & 1 == 1:
                r -= 1
                result = gcd(result, self._data[r])
            l >>= 1
            r >>= 1
        return result


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

st = SegmentTree(N)
st.update_all(A)

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

Submission status: Wrong Answer

Solution code:
from fractions import gcd


class SegmentTree():
    _data = []
    _offset = 0
    _size = 0

    def __init__(self, size):
        _size = size
        t = 1
        while t <= size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)

    def update_all(self, iterable):
        self._data[self._offset:self._offset+self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def query(self, start, stop):
        result = 0
        l = start + self._offset
        r = stop + self._offset
        while l < r:
            if l & 1 == 1:
                result = gcd(result, self._data[l])
                l += 1
            if r & 1 == 1:
                result = gcd(result, self._data[r])
                r -= 1
            l >>= 1
            r >>= 1
        return result


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

st = SegmentTree(N)
st.update_all(A)

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

Submission status: Wrong Answer

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

gcd_from_left = [0] * n
gcd_from_right = [0] * n

gcd_from_left[0] = a[0]
gcd_from_right[0] = rev_a[0]

for i in range(1, n):
    gcd_from_left[i] = fractions.gcd(gcd_from_left[i - 1], a[i])
    gcd_from_right[i] = fractions.gcd(gcd_from_right[i - 1], rev_a[i])

ans = 1
for i in range(n):
    if i == 0:
        ans = max(ans, gcd_from_right[n - 2])
    elif i == (n - 1):
        ans = max(ans, gcd_from_left[n - 2])
    else:
        ans = max(ans, fractions.gcd(gcd_from_left[i - 1], gcd_from_right[i - 1]))

print(ans)

Submission status: Wrong Answer

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

gcd_from_left = [0] * n
gcd_from_right = [0] * n

gcd_from_left[0] = a[0]
gcd_from_right[0] = rev_a[0]

for i in range(1, n):
    gcd_from_left[i] = fractions.gcd(gcd_from_left[i - 1], a[i])
    gcd_from_right[i] = fractions.gcd(gcd_from_right[i - 1], rev_a[i])

ans = 0
for i in range(n):
    if i == 0:
        ans = max(ans, gcd_from_right[n - 2])
    elif i == (n - 1):
        ans = max(ans, gcd_from_left[n - 2])
    else:
        ans = max(ans, fractions.gcd(gcd_from_left[i - 1], gcd_from_right[i - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int, input().split()))
A.sort()
gcd_list = list()
now_gcd = A[0]
gcd_list.append(now_gcd)
for i in range(1, N):
    tmp = fractions.gcd(now_gcd, A[i])
    gcd_list.append(tmp)
    
gcd_list.sort()

print(gcd_list[1])

Submission status: Wrong Answer

Solution code:
import fractions
import functools
import copy
def gcd_list(num_list):
    return functools.reduce(fractions.gcd, num_list)


N = int(input())
A = list(map(int, input().split()))
A.sort()
A1 = copy.deepcopy(A)
A2 = copy.deepcopy(A)
A1.pop(0)
A2.pop(1)
print(max(gcd_list(A1), gcd_list(A2)))

Submission status: Wrong Answer

Solution code:
import fractions
import itertools
from functools import reduce

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

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

tmp = 0
if len(a) == 1:
    tmp = list(a)[0]
else:
    for v in itertools.permutations(a, len(a)-1):
        tmp = max(gcd(v), tmp)

print(tmp)

Submission status: Wrong Answer

Solution code:
import fractions
import itertools
from functools import reduce

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

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

tmp = 0
if len(a) == 1:
    tmp = list(a)[0]
else:
    for v in itertools.permutations(a, len(a)-1):
        tmp = max(gcd(*v), tmp)

print(tmp)

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict
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
def factorize(m):
    d = []
    for i in range(1, int(m**0.5)+1):
        if m % i == 0:
            d.append(i)
            if i != m // i:
                d.append(m//i)
    return d
def main():
    N = int(input())
    A = list(map(int, input().split()))
    l = list(set(factorize(A[0]) + factorize(A[1])))
    l.sort(reverse=True)
    d = OrderedDict({i:0 for i in l})
    for i in A:
        for j in d:
            if i % j == 0:
                d[j] += 1
    m = 1
    for k in d:
        if d[k] >= N-1:
            print(k)
            return
    print(1)
main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce

def main():
    N = int(input())
    A = sorted(map(int, input().split()))

    def pleaceAC():
        for i in range(N):
            a = A[:i] + A[i+i:]
            yield reduce(gcd, a)

    print(max(pleaceAC()))


main()

Submission status: Wrong Answer

Solution code:
import fractions
#from functools import reduce
N,*a=map(int,open(0).read().split())
L=[]
L_base=a[0]
b=a[:]
b.reverse()
R=[]
R_base=b[0]
for i in range(N-1):
    L.append(fractions.gcd(L_base,a[i]))
    R.append(fractions.gcd(R_base,b[i]))
#R.reverse()
LR=[R[-1]]
for i in range(N-2):
    LR.append(fractions.gcd(L[i],R[-2-i]))
LR.append(L[-1])
print(max(LR))

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
N,*a=map(int,open(0).read().split())
_max=0
a.sort()
a.reverse()
for i in a:
    b=a[:]
    b.remove(i)
    _max=max(_max,reduce(fractions.gcd,b))
    if _max in a:
        a.remove(_max)
print(_max)

Submission status: Wrong Answer

Solution code:
import sys
from functools import lru_cache

@lru_cache(maxsize=None)
def gcd(a, b):
    if a % b == 0:
        return b
    else:
        return gcd(b, a % b)


N, *A = map(int, sys.stdin.read().split())
L, R = [0] * (N + 1), [0] * (N + 1)
for i in range(N):
    L[i + 1] = gcd(L[i], A[i])
    R[-i - 2] = gcd(R[-i - 1], A[-i - 1])


res = 1
for i in range(N - 1):
    tmp = gcd(L[i], R[i + 1])
    if res < tmp:
        res = tmp
print(res)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

def bit_length(self):
    s = bin(self)
    s = s.lstrip('-0b')
    return len(s)

def segfunc(x, y):
    return gcd(x, y)
    
def init(init_val):
    for i in range(N):
        seg[i+num-1] = init_val[i]
    for i in range(num-2, -1, -1):
        seg[i] = segfunc(seg[2*i+1], seg[2*i+2])

def update(k, x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1], seg[k*2+2])

def query(p, q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res = ide_ele
    while q-p > 1:
        if p&1 == 0:
            res = segfunc(res, seg[p])
        if q&1 == 0:
            res = segfunc(res, seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = gcd(res, seg[p])
    else:
        res = gcd(gcd(res, seg[p]), seg[q])
    return res

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

# identity element
ide_ele = 0

# least 2-powered value greater than N
num = bit_length(2**(N-1))
seg = [ide_ele]*2*num

init(a)
ans = -1
for i in range(N):
    ans = max(ans, gcd(query(0, i), query(i+1, N)))
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
import sys

def chmax(a, b):
  if (a > b):
    return a

  return b

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

if N == 2:
  if li[0] > li[1]:
    print(li[0])
  else:
    print(li[1])
  sys.exit()

left = []
right = []

for index, i in enumerate(li):
  if index == 0:
    left.append(i)
    continue

  left.append(fractions.gcd(left[index-1], i))

for index, i in enumerate(reversed(li)):
  if index == 0:
    right.append(i)
    continue

  right.append(fractions.gcd(right[index-1], i))

ans = 0

for n in range(N):
  if n == 0:
    ans = chmax(ans, right[-2])
    continue
  
  if n == N-1:
    ans = chmax(ans, left[-2])
    continue

  ans = chmax(ans, fractions.gcd(left[n-1], right[n-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
import sys

def chmax(a, b):
  if (a > b):
    return a

  return b

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

if N == 2:
  if li[0] > li[1]:
    print(li[0])
  else:
    print(li[1])
  sys.exit()

left = []
right = []

for index, i in enumerate(li):
  if index == 0:
    left.append(i)
    continue

  left.append(fractions.gcd(left[index-1], i))

for index, i in enumerate(reversed(li)):
  if index == 0:
    right.append(i)
    continue

  right.append(fractions.gcd(right[index-1], i))

ans = 0

for n in range(N):
  if n == 0:
    ans = chmax(ans, right[N-2])
    continue
  
  if n == N-1:
    ans = chmax(ans, left[n-1])
    continue

  ans = chmax(ans, fractions.gcd(left[n-1], right[n-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
import sys

def chmax(a, b):
  if (a > b):
    return a

  return b

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

if N == 2:
  if li[0] > li[1]:
    print(li[0])
  else:
    print(li[1])
  sys.exit()

left = []
right = []

for index, i in enumerate(li):
  if index == 0:
    left.append(i)
    continue

  left.append(fractions.gcd(left[index-1], i))

for index, i in enumerate(reversed(li)):
  if index == 0:
    right.append(i)
    continue

  right.append(fractions.gcd(right[index-1], i))

ans = 0

for n in range(N):
  if n == 0:
    ans = chmax(ans, right[N-2])
    continue
  
  if n == N-1:
    ans = chmax(ans, left[n])
    continue

  ans = chmax(ans, fractions.gcd(left[n-1], right[n-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

def chmax(a, b):
  if (a > b):
    return a

  return b

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

left = []
right = []

for index, i in enumerate(li):
  if index == 0:
    left.append(i)
    continue

  left.append(fractions.gcd(left[index-1], i))

for index, i in enumerate(reversed(li)):
  if index == 0:
    right.append(i)
    continue

  right.append(fractions.gcd(right[index-1], i))

ans = 0

for n in range(N):
  if n == 0:
    ans = chmax(ans, right[N-2])
    continue
  
  if n == N-1:
    ans = chmax(ans, left[n])
    continue

  ans = chmax(ans, fractions.gcd(left[n-1], right[n-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=2**9
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)

    gcdv=0

    for mi in range(m):
        rgcd1=list(rgcd)
        del rgcd1[mi]
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdam=gcdf(am)
        for i in range(len(am)):
            acpy=list(am)
#            del acpy[i]
#            acpy.appenf(gcdam)
            acpy[i]=gcdam
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx

#    print(gcdv,a1)
    print(gcdv)

Submission status: Wrong Answer

Solution code:
def gcdf(a,n,a0):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
    if len(a)==0:
        return a0
    else:
        a0=min(a)
        return gcdf(a,n,a0) 

#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())

a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<=2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        a0=acpy[0]
        gcdx=gcdf(acpy,n-1,a0)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    a.sort()
    ns=int(n/4)
    a1=a[0:ns]
    a2=a[ns:ns*2]
    a3=a[ns*2:ns*3]
    a4=a[ns*3:n]
#    print(a1)
#    print(a2)
#    print(a3)
#    print(a4)
    gcdv=0
    a10=a1[0]
    a20=a2[0]
    a30=a3[0]
    a40=a4[0]
    gcd1=gcdf(a1,len(a1),a10)
    gcd2=gcdf(a2,len(a2),a20)
    gcd3=gcdf(a3,len(a3),a30)
    gcd4=gcdf(a4,len(a4),a40)
    for i in range(ns):
#        acpy=list(a1)
        acpy=a1.copy()
        acpy.append(gcd2)
        acpy.append(gcd3)
        acpy.append(gcd4)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a10)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a1)
    for i in range(ns):
#        acpy=list(a2)
        acpy=a2.copy()
        acpy.append(gcd1)
        acpy.append(gcd3)
        acpy.append(gcd4)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a20)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a2)
    for i in range(ns):
#        acpy=list(a3)
        acpy=a3.copy()
        acpy.append(gcd1)
        acpy.append(gcd2)
        acpy.append(gcd4)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a30)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a3)
    for i in range(ns):
#        acpy=list(a4)
        acpy=a4.copy()
        acpy.append(gcd1)
        acpy.append(gcd2)
        acpy.append(gcd3)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a40)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a4)
    print(gcdv)

Submission status: Wrong Answer

Solution code:
def gcdf(a,n,a0):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
    if len(a)==0:
        return a0
    else:
        a0=min(a)
        return gcdf(a,n,a0) 

#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())

a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<=2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        a0=acpy[0]
        gcdx=gcdf(acpy,n-1,a0)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv)
else:
    a.sort()
    ns=int(n/4)
    a1=a[0:ns]
    a2=a[ns:ns*2]
    a3=a[ns*2:ns*3]
    a4=a[ns*3:n]
#    print(a1)
#    print(a2)
#    print(a3)
#    print(a4)
    gcdv=0
    a10=a1[0]
    a20=a2[0]
    a30=a3[0]
    a40=a4[0]
    gcd1=gcdf(a1,len(a1),a10)
    gcd2=gcdf(a2,len(a2),a20)
    gcd3=gcdf(a3,len(a3),a30)
    gcd4=gcdf(a4,len(a4),a40)
    for i in range(ns):
#        acpy=list(a1)
        acpy=a1.copy()
        acpy.append(gcd2)
        acpy.append(gcd3)
        acpy.append(gcd4)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a10)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a1)
    for i in range(ns):
#        acpy=list(a2)
        acpy=a2.copy()
        acpy.append(gcd1)
        acpy.append(gcd3)
        acpy.append(gcd4)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a20)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a2)
    for i in range(ns):
#        acpy=list(a3)
        acpy=a3.copy()
        acpy.append(gcd1)
        acpy.append(gcd2)
        acpy.append(gcd4)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a30)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a3)
    for i in range(ns):
#        acpy=list(a4)
        acpy=a4.copy()
        acpy.append(gcd1)
        acpy.append(gcd2)
        acpy.append(gcd3)
        del acpy[i]
#        print(i,acpy)
        gcdx=gcdf(acpy,len(acpy),a40)
        if gcdx>gcdv:
            gcdv=gcdx
#    print(gcdv,a4)
    print(gcdv)

Submission status: Wrong Answer

Solution code:
def gcdf(a,a0):
    a0=min(a)
    a=[x%a0 for x in a if x%a0 > 0]
    if len(a)==0:
        return a0
    else:
        return gcdf(a,a0) 

n=int(input())

a=[]
for i in map(int,input().split()):
    a.append(i)

if n==2:
    print(max(a))
else:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        a0=acpy[0]
        gcdx=gcdf(acpy,a0)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)