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 collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


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




class SegmentTree:
    def __init__(self, init_val, ide_ele, op):
        n = len(init_val)
        self.num = 2 ** (n - 1).bit_length()
        self.seg = [ide_ele] * 2 * self.num
        self.ide_ele = ide_ele
        self.op = op
        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.op(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.op(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.op(res, self.seg[p])
            if q & 1 == 1:
                res = self.op(res, self.seg[q])
                q -= 1
            p = p // 2
            q = (q - 1) // 2
        if p == q:
            res = self.op(res, self.seg[p])
        else:
            res = self.op(self.seg[p], self.seg[q])
        return res



n = I()
L = LI()
ST = SegmentTree(L, 0, gcd)
print(max([gcd(ST.query(0, i), ST.query(i + 1, n)) for i in range(n)]))

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)
import fractions    
c=listss.copy()
del c[0]
del listss[1]

ans=c[0]
for i in range(1, n-1):
    ans = fractions.gcd(ans, c[i])
    
    
bns=listss[0]
for i in range(1, n-1):
    bns = fractions.gcd(bns, listss[i])
print(max(ans,bns))

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)
import fractions 
if n>=2:    
    c=listss.copy()
    del c[0]
    del listss[1]

    ans=c[0]
    for i in range(1, n-1):
        ans = fractions.gcd(ans, c[i])
    
    
    bns=listss[0]
    for i in range(1, n-1):
        bns = fractions.gcd(bns, listss[i])
    print(max(ans,bns))

if n==1:
    print(listss[0])

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)
import fractions 
if n>=2:    
    c=lists.copy()
    del c[0]
    del listss[1]

    ans=c[0]
    for i in range(1, n-1):
        ans = fractions.gcd(ans, c[i])
    
    
    bns=listss[0]
    for i in range(1, n-1):
        bns = fractions.gcd(bns, listss[i])
       
    print(max(ans,bns))

if n==1:
    print(lists[0])

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)

if n>=2:    
    min1=listss[0]
    min2=listss[1]
    c=listss.copy()
    del c[0]
    del listss[1]
    gcd1=min1
    gcd2=min2
    while  not all(j%gcd1==0 for j in c):
        gcd1-=1
    while not all(j%gcd2==0 for j in listss):     
        gcd2-=1
    print(max(gcd1,gcd2))
else:
    print(listss[0])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()


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


l = list(accumulate(A, lambda x, y: gcd(x, y)))
r = list(reversed(list(accumulate(reversed(A), lambda x, y: gcd(x, y)))))
ret = max(l[n-1], r[1])
for i in range(n-2):
    ret = max(ret, gcd(l[i], r[i+2]))


print(ret)

Submission status: Wrong Answer

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

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

L=[]
R=[]
for i in range(N):
    L.append(0)
    R.append(0)

for i in range(N-1):
    L[i+1]=myGCD(L[i],A[i])

for i in range(N-2,-1,-1):
    #print(i)
    R[i] = myGCD(R[i+1],A[i])

M=[]
for i in range(N):
    M.append(myGCD(R[i],L[i]))

print(max(M))

Submission status: Wrong Answer

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

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

def list_gcd(numbers):
    tmp=numbers[0]
    for i in range(len(numbers)-1):
        tmp=min(tmp,myGCD(numbers[i],numbers[i+1]))
    return(tmp)

#print(list_gcd(A))

candidate=set()
t1 = A[0]
candidate.add(myGCD(A[0],A[1]))
candidate.add(min(A[0],A[1]))
for i in range(1,N):
    for tc in list(candidate):
        candidate.add(myGCD(tc,A[i]))
#print(min(candidate))
if len(candidate) > 1:
    candidate.remove(min(candidate))
print(min(candidate))

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
l=[a[0]]
r=[a[-1]]
for i in range(n-1):
  l.append(fractions.gcd(l[i],a[i+1])) #l[i] は最初i+1個
for i in range(n-1):
  r.append(fractions.gcd(r[i],a[-1-i])) #r[i]は右i+1 r[n-1]=全部 l[0]and r[n-3]
m=[l[n-2],r[n-2]]
for i in range(n-2):
  m.append(fractions.gcd(l[i],r[n-i-3]))
print(max(m))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
if n==2:
  print(a[-1])
else:
  b=[]
  for i in range(n-1):
    b.append(fractions.gcd(a[i],a[i+1]))
  b.append(fractions.gcd(a[0],a[-1]))
  b.sort()
  print(b[2])

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
if n==2:
  print(a[-1])
elif n==3:
  x=fractions.gcd(a[0],a[1])
  y=fractions.gcd(a[0],a[2])
  z=fractions.gcd(a[1],a[2])
  print(max(x,y,z))
else:
  b=[]
  for i in range(n-1):
    b.append(fractions.gcd(a[i],a[i+1]))
  b.sort()
  print(b[2])

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
a.sort()
if n==2:
  print(a[-1])
else:
  x=fractions.gcd(a[0],a[1])
  y=fractions.gcd(a[0],a[2])
  z=fractions.gcd(a[1],a[2])
  for i in range(3,n):
    if a[i]%x!=0:
      x=fractions.gcd(x,a[i])
    if a[i]%y!=0:
      y=fractions.gcd(y,a[i])
    if a[i]%z!=0:
      z=fractions.gcd(z,a[i])
  print(max(x,y,z))

Submission status: Wrong Answer

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

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

def list_gcd(numbers):
    tmp=numbers[0]
    for i in range(len(numbers)-1):
        tmp=min(tmp,myGCD(numbers[i],numbers[i+1]))
    return(tmp)

maxgcd = 0
for i in range(N):
    Atmp=A[0:i]
    if i < N-1:
        Atmp.extend(A[i+1:N])
    #print(Atmp)
    tmpgcd=list_gcd(Atmp)
    #print(tmpgcd)
    if tmpgcd > maxgcd:
        maxgcd = tmpgcd

print(maxgcd)

Submission status: Wrong Answer

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

    n, *a = map(int, open(0).read().split())

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

    m = r[-2]
    for i in range(1, n-1):
        t = gcd(l[i-1], r[i-1])
        m = t if t>m else m
    m = l[-2] if m<l[-2] else m

    print(m)

if __name__ == "__main__":
    f_c()

Submission status: Wrong Answer

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

    n, *a = map(int, open(0).read().split())

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

    m = r[-2]
    for i in range(1, n-1):
        t = gcd(l[i-1], r[i-1])
        m = t if t>m else m
    m = l[-2] if m<l[-1] else m

    print(m)

if __name__ == "__main__":
    f_c()

Submission status: Wrong Answer

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


N = int(input())
a = list(map(int, input().split()))
max_gcd = 0
L = [0] * (N + 1)
R = [0] * (N + 1)

for i in range(N):
    L[i+1] = gcd(L[i], a[i])
for i in range(N):
    R[-2-i] = gcd(R[i], a[-1-i])
for i in range(N+1):
    max_gcd = max(max_gcd, gcd(R[i],L[i]))
print(max_gcd)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n=int(input())
A=list(map(int,input().split()))

ans=0
if n==2:
  print(gcd(A[0],A[-1]))
else:
  for i in range(n):
    if i==0:
      tmp=A[2]
      for j in range(3,n):
        tmp=gcd(tmp,A[j])
      now=gcd(A[1],tmp)
    elif i==n:
      tmp=A[-2]
      for j in range(n-2,0,-1):
        tmp=gcd(A[j],tmp)
      now=gcd(tmp,A[-2])
    else:
      tmp1=A[0]
      for j in range(1,i):
        tmp1=gcd(tmp1,A[j])
      tmp2=A[-1]
      for j in range(n-1,i,-1):
        tmp2=gcd(A[j],tmp2)
      now=gcd(tmp1,tmp2)
    ans=max(ans,now)
  print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
l = [int(i) for i in input().split()]
f = lambda x,y: fractions.gcd(x,y) 

if len(l) ==2:
    print(fractions.gcd(l[0],l[1]))
else:
    
    l2 = sorted([f(i,j) for i in l for j in l if i != j],reverse = True)
    print(l2[0])

Submission status: Wrong Answer

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

n=int(input())
a=[int(i) for i in input().split()]
l=[None for i in range(n)]
l[0]=a[0]
now=a[0]
for i in range(1,n):
    l[i]=gcd(l[0],a[i])
r=[None for i in range(n)]
r[-1]=a[-1]
for i in range(n-2,-1,-1):
    r[i]=gcd(r[i+1],a[i])
res=max(r[1],l[-2])
for i in range(1,n-1):
    res=max(res,gcd(l[i-1],r[i+1]))
print(res)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

def getlist():
	return list(map(int, input().split()))

N = int(input())
A = getlist()

X = [0] * N
Y = [0] * N

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

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

ans = []

ans.append(X[-2])
ans.append(Y[1])

for i in range(1, N - 1):
	ans.append(gcd(X[i + 1], Y[i - 1]))

print(max(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
h = list(map(int,input().split()))
count = [0]*n
i = 0
while i < n:
    j = i
    while j < n -1 and h[j] >= h[j+1]:
        count[i] += 1
        j += 1
    i += count[i] + 1
print(max(count))

Submission status: Wrong Answer

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

n=int(input())
a=[0] + list(map(int,input().split())) + [0]#a[n+1]=0とするため

l=[0]*(n+2)
r=[0]*(n+2)#r[n+1]=0としたいから
for i in range(1,n+1):
    l[i] = gcd(l[i-1],a[i])
for i in range(n,0,-1):
    r[i] = gcd(r[i+1],a[i+1])

z=[gcd(l[i-1],r[i]) for i in range(1,n)]

print(max(z))

Submission status: Wrong Answer

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

n=int(input())
a=[0] + list(map(int,input().split())) + [0]#a[n+1]=0とするため

l=[0]*(n+2)
r=[0]*(n+2)#r[n+1]=0としたいから
for i in range(1,n+1):
    l[i] = gcd(l[i-1],a[i])
for i in range(n-1,0,-1):
    r[i] = gcd(r[i+1],a[i+1])

z=[gcd(l[i-1],r[i]) for i in range(1,n)]

print(max(z))

Submission status: Wrong Answer

Solution code:
INFTY = 10**10
def gcd(x,y):
    if y>x:
        x,y = y,x
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = list(map(int,input().split()))
if N==2:
    print(max(A[0],A[1]))
else:
    A = sorted(A,reverse=True)
    cmin = INFTY
    ind1 = INFTY
    for i in range(1,N):
        c = gcd(A[i],A[0])
        if c<cmin:
            cmin = c
            ind1 = i
    ind0 = 0
    B = A[:]
    B.remove(A[ind0])
    bmin = INFTY
    for i in range(N-2):
        bmin = min(bmin,gcd(B[i+1],B[i]))
    C = A[:]
    C.remove(A[ind1])
    cmin = INFTY
    for i in range(N-2):
        cmin = min(cmin,gcd(C[i+1],C[i]))
    print(max(bmin,cmin))

Submission status: Wrong Answer

Solution code:
INFTY = 10**10
def gcd(x,y):
    if y>x:
        x,y = y,x
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = list(map(int,input().split()))
if N==2:
    print(max(A[0],A[1]))
else:
    A = sorted(A)
    cmin = INFTY
    ind1 = INFTY
    for i in range(1,N):
        c = gcd(A[i],A[0])
        if c<cmin:
            cmin = c
            ind1 = i
    ind0 = 0
    B = A[:]
    B.remove(A[ind0])
    bmin = INFTY
    for i in range(N-2):
        bmin = min(bmin,gcd(B[i+1],B[i]))
    C = A[:]
    C.remove(A[ind1])
    cmin = INFTY
    for i in range(N-2):
        cmin = min(cmin,gcd(C[i+1],C[i]))
    print(max(bmin,cmin))

Submission status: Wrong Answer

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

ans = 0
min_v = min(a)
for i in range(min_v, 0, -1):
    a_t = tuple(map(lambda x: x % i == 0, a))
    if a_t.count(False) <= 1 and ans < i:
        ans = i
        break
print(ans)

Submission status: Wrong Answer

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

if len(a) == 2:
  print(max(a))
else:
  gcd_1 = fractions.gcd(a[0], a[1])
  gcd_2 = fractions.gcd(a[0], a[2])
  gcd_3 = fractions.gcd(a[1], a[2])
  if gcd_1 == gcd_2 and gcd_2 == gcd_3:
    print(gcd_1)
  elif gcd_1 == gcd_2:
    if gcd_1 < gcd_3:
      print(gcd_3)
    else:
      pirnt(gcd_1)
  elif gcd_2 == gcd_3:
    if gcd_2 < gcd_1:
      print(gcd_1)
    else:
      print(gcd_2)
  elif gcd_1 == gcd_3:
    if gcd_1 < gcd_2:
      print(gcd_2)
    else:
      print(gcd_1)
  else:
    temp=[gcd_1, gcd_2, gcd_3]
    print(max(temp))

Submission status: Wrong Answer

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

if len(a) == 2:
  print(max(a))
else:
  gcd_1 = fractions.gcd(a[0], a[1])
  gcd_2 = fractions.gcd(a[0], a[2])
  gcd_3 = fractions.gcd(a[1], a[2])
  if gcd_1 == gcd_2 and gcd_2 == gcd_3:
    print(gcd_1)
  elif gcd_1 == gcd_2:
    if gcd_1 < gcd_3:
      print(gcd_3)
    else:
      pirnt(gcd_1)
  elif gcd_2 == gcd_3:
    if gcd_2 < gcd_1:
      print(gcd_1)
    else:
      print(gcd_2)
  elif gcd_1 == gcd_3:
    if gcd_1 < gcd_2:
      print(gcd_2)
    else:
      print(gcd_1)

Submission status: Wrong Answer

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

if len(a) == 2:
  print(max(a))
else:
  temp = []
  for i in range(len(a)):
    b = a.pop(i)
    temp.append(fractions.gcd(max(a), min(a)))
    a.insert(i, b)
  print(max(temp))

Submission status: Wrong Answer

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

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

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

M = [gcd(L[i], R[i+1]) for i in range(N)]

print(max(M))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

L = [0] * (N+1)
R = [0] * (N+1)

L[0] = A[0]
R[-1] = A[-1]

for i in range(len(A)):
    L[i] = gcd(L[i-1], A[i])
    
for i in reversed(range(len(A))):
    R[i] = gcd(R[i+1], A[i])
    
ans = 0    

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

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
a = list(map(int,input().split(" ")))
b = a[0]
for i in range(n-1):
    b = fractions.gcd(b,a[i+1])
for i in range(n-1):
    if fractions.gcd(a[i],a[i+1]) == b:
        a[i] = min(a[i],a[i+1])
        a[i+1] = min(a[i],a[i+1])
        break;
b = a[0]
for i in range(n-1):
    b = fractions.gcd(b,a[i+1])
print(b)

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
l = [int(i) for i in input().split()]
m = [i for i in set(l) if l.count(i) > 1]

if m:
    print(max(m))
else:
    f = lambda x,y: fractions.gcd(x,y) 
    l2 = sorted([f(i,j) for i in l for j in l if i != j],reverse = True)
    print(l2[0])

Submission status: Wrong Answer

Solution code:
from itertools import combinations
import fractions
n = int(input())
l = [int(i) for i in input().split()]

ans = 0
for i in list(combinations(l,2)):
    num = fractions.gcd(i[0],i[1])
    ans = max(ans,num)
    
print(ans)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys, fractions
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    f = [0] * (n + 1)
    b = [0] * (n + 1)
    for i in range(n):
        f[i + 1] = fractions.gcd(f[i], a[i])
        b[-i - 2] = fractions.gcd(b[-i-1], a[-i-1])
    m_gcd = 0
    for i in range(n - 1):
        m_gcd = max(m_gcd, fractions.gcd(f[i], b[i+1]))
    print(m_gcd)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys, fractions
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    f = [0] * (n + 1)
    b = [0] * (n + 1)
    for i in range(n):
        f[i + 1] = fractions.gcd(f[i], a[i])
        b[-i - 2] = fractions.gcd(b[-i-1], a[-i-1])
    m_gcd = 0
    for i in range(n - 1):
        m_gcd = max(m_gcd, fractions.gcd(f[i], b[i+1]))
    print(f)
    print(b)
    print(m_gcd)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys, fractions
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    f = [0] * (n + 1)
    b = [0] * (n + 1)
    for i in range(n):
        f[i + 1] = fractions.gcd(f[i], a[i])
        b[-i + 2] = fractions.gcd(b[-i-1], a[-i-1])
    m_gcd = 0
    for i in range(n - 1):
        m_gcd = max(m_gcd, fractions.gcd(f[i], b[i+1]))
    print(m_gcd)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
N =int(input())
A = list(map(int,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

A = sorted(A)

dicts = {}

if len(A) <=2:
    for i in A:
        for num in make_divisors(i):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    max_value=0
    for k,v in dicts.items():
        if v>=N-1 and max_value<k:
            max_value=k

    print(max_value)
else:
    for i in range(3):
        for num in make_divisors(A[i]):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    
    #
    new_list=[]
    for k,v in dicts.items():
        if v>=2:
            new_list.append(k)
    new_set=set(new_list)
    for i in range(3,len(A)):
        hoge=[]
        for num in new_set:
            if A[i]%num!=0:
                hoge.append(num)
        new_set -= set(hoge)
        
    if len(new_set)>=1:
        print(max(new_set))
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N =int(input())
A = list(map(int,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

A = sorted(A)

dicts = {}

if len(A) <=2:
    for i in A:
        for num in make_divisors(i):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    max_value=0
    for k,v in dicts.items():
        if v>=N-1 and max_value<k:
            max_value=k

    print(max_value)
else:
    for i in range(3):
        for num in make_divisors(A[i]):
            if num in dicts:
                dicts[num]+=1
            else:
                dicts[num]=1
    
    #
    new_list=[]
    for k,v in dicts.items():
        if v>=2:
            new_list.append(k)
    new_set=set(new_list)
    for i in range(3,len(A)):
        hoge=[]
        for num in new_set:
            if A[i]%num!=0:
                hoge.append(num)
        new_set -= set(hoge)
    print(max(new_set))

Submission status: Wrong Answer

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


def compute_gcd(ns):
    a = gcd(ns[0], ns[1])
    for i in range(2, len(ns)):
        a = gcd(a, ns[i])
    return a


n = int(input())
nums = list(map(int, input().split()))
gcds = []
if n == 2:
    gcds.append(compute_gcd(nums))
else:
    for i in range(n):
        ns = [nums[j] for j in range(n) if j != i]
        a = compute_gcd(ns)
        gcds.append(a)
print(max(gcds))

Submission status: Wrong Answer

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

import fractions
import numpy as np

L=[]
R=[]
for i in range(N-1):
    if i==0:
        L_new = A[i]
    else:
        L_new = fractions.gcd(L[i-1], A[i])
    L.append(L_new)    
    j = N-1-i
    if j==N-1:
        R_new = A[j]
    else:
        R_new = fractions.gcd(R[i-1], A[j])
    R.append(R_new)  
R.reverse()    
B =[]  #GCD removing one element
for i in range(N-1):
    B.append(fractions.gcd(L[i], R[i]))
print(max(B))

Submission status: Wrong Answer

Solution code:
N = int(input())

A_list = list(map(int, input().split())) # case1 : [7, 6, 8]

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

left_gcd = [0] * (N+1)
right_gcd = [0] * (N+1)
for i in range(N):
    left_gcd[i] = gcd(A_list[i], left_gcd[i-1])
for i in range(N-1, 0, -1):
    right_gcd[i] = gcd(A_list[i], right_gcd[i+1])

ans = 0

for i in range(N):
    i_gcd = gcd(left_gcd[i], right_gcd[i+1])
    if ans < i_gcd:
        ans = i_gcd

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

def gcd(a, b):
  if a == 0:
    return b
  elif b == 0:
    return a
  else:
    return fractions.gcd(a, b)

L = [0 for _ in range(N+1)]
M = [0 for _ in range(N+1)]
R = [0 for _ in range(N+2)]

for i in range(N):
  L[i+1] = gcd(L[i], A[i+1])
for i in range(N,1,-1):
  R[i] = gcd(R[i+1], A[i])
for i in range(N):
  M[i] = gcd(L[i-1], R[i+1])
print (max(M))

Submission status: Wrong Answer

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

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))

accum_gcd_pop = []

for j in range(n):
    if j == 0:
        accum_gcd_pop.append(accum_gcd_i[n-1])
    elif j == n-1:
        accum_gcd_pop.append(accum_gcd[n-1])
    else:
        accum_gcd_pop.append(gcd(accum_gcd[j-1],accum_gcd_i[n-j-2]))
print(max(accum_gcd_pop))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [int(i) for i in input().split()]
d = a.copy()
a.sort()
result = []
for i in range(n):
    ans = a[i] - a[0]
    result.append(ans)
if max(result) ==0:
    print(d[0])
else:
    print(max(result))

Submission status: Wrong Answer

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

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)

A_pop0 = A[1:]
gcd_A_pop0 = A_pop0[0]
for k in range(n-1):
    gcd_A_pop0 = gcd(gcd_A_pop0,A_pop0[k])

A_i_pop0 = A_i[1:]
gcd_A_i_pop0 = A_i_pop0[0]
for k in range(n-1):
    gcd_A_i_pop0 = gcd(gcd_A_i_pop0,A_i_pop0[k])

for j in range(n-1):
    if gcd(accum_gcd[j],accum_gcd_i[j]) == gcd(accum_gcd[n-2],accum_gcd_i[n-2]):
        A_pop = [k for k in A[0:j+1] + A[j+2:n]]
        A_i_pop = [k for k in A_i[0:j+1] + A_i[j+2:n]]
        
        gcd_A_pop = A_pop[0]
        for k in range(n-1):
            gcd_A_pop = gcd(gcd_A_pop,A_pop[k])
            
        gcd_A_i_pop = A_i_pop[0]
        for k in range(n-1):
            gcd_A_i_pop = gcd(gcd_A_i_pop,A_i_pop[k])
        
        print(max(gcd_A_pop,gcd_A_i_pop,gcd_A_pop0,gcd_A_i_pop0))
        exit()

Submission status: Wrong Answer

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

n = int(input())
A = list(map(int,input().split())) #要素n個

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j] or (accum_gcd[j] == 1 and accum_gcd_i[j] == 1):
        A_pop = [k for k in A[0:j] + A[j+1:n]]
        A_i_pop = [k for k in A_i[0:j] + A_i[j+1
        :n]]
        
        gcd_A_pop = A_pop[0]
        for k in range(n-1):
            gcd_A_pop = gcd(gcd_A_pop,A_pop[k])
            
        gcd_A_i_pop = A_i_pop[0]
        for k in range(n-1):
            gcd_A_i_pop = gcd(gcd_A_i_pop,A_i_pop[k])
            
        print(max(gcd_A_pop,gcd_A_i_pop))
        exit()

Submission status: Wrong Answer

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

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j]:
        A_pop = [k for k in A[0:j+1] + A[j+2:n]]
        A_i_pop = [k for k in A_i[0:j+1] + A_i[j+2:n]]
        gcd_A_pop = A_pop[0]
        for k in range(n-1):
            gcd_A_pop = gcd(gcd_A_pop,A_pop[k])
        gcd_A_i_pop = A_i_pop[0]
        for k in range(n-1):
            gcd_A_i_pop = gcd(gcd_A_i_pop,A_i_pop[k])
        print(max(gcd_A_pop,gcd_A_i_pop))
        exit()

Submission status: Wrong Answer

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

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)

print(accum_gcd)
print(accum_gcd_i)

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j]:
        A_pop = [k for k in A[0:j+1] + A[j+2:n]]
        print(A_pop)
        A_i_pop = [k for k in A_i[0:j+1] + A_i[j+2:n]]
        print(A_i_pop)
        gcd_A_pop = A_pop[0]
        for k in range(n-1):
            gcd_A_pop = gcd(gcd_A_pop,A_pop[k])
        gcd_A_i_pop = A_i_pop[0]
        for k in range(n-1):
            gcd_A_i_pop = gcd(gcd_A_i_pop,A_i_pop[k])
        print(max(gcd_A_pop,gcd_A_i_pop))
        exit()

Submission status: Wrong Answer

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

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

accum_gcd = []
gcd_A = A[0]
for j in range(n):
    gcd_A = gcd(gcd_A,A[j])
    accum_gcd.append(gcd(gcd_A,A[j]))
accum_gcd.pop(0)

accum_gcd_i = []
A_i = A[::-1]
gcd_A_i = A_i[0]
for j in range(n):
    gcd_A_i = gcd(gcd_A_i,A_i[j])
    accum_gcd_i.append(gcd(gcd_A_i,A_i[j]))
accum_gcd_i.pop(0)
accum_gcd_i = accum_gcd_i[::-1]

for j in range(n-1):
    if accum_gcd[j] == accum_gcd_i[j]:
        print(accum_gcd[j])
        exit()

Submission status: Wrong Answer

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

import fractions

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

    if N <= 2:
        print(max(A))
        return
    
    L = [0]*(N+1)
    for i in range(N):
        L[i+1] = fractions.gcd(L[i],A[i])
#    print(L)
    R = [0]*(N+1)
    for i in range(N-1,-1,-1):
        R[i] = fractions.gcd(R[i+1],A[i])
#    print(R)
    ma = 1
    for i in range(1,N):
        ma = max(ma, fractions.gcd(L[i-1], R[i]))
    print(ma)
    
if __name__ == '__main__':
    main()