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:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None
"""
書き換えたあとの最大公約数の最大値を求める
書き換える値の最適解は、最小値を最大値に書き換えること
例えば、1,6,3があるとき、1を3に書き換えることで最大公約数が3になる
しかし、7 6 8の場合、最小の6ではなく7を書き換えることが最適となる
結局全部の数について書き換えたときの最大値を求めるしか無いか
そうするとO(N**2)になってしまう
全体の最大公約数はgcd(gcd(gcd(1,2), 3),4)
1つ書き換えるは、消すと同義
逆でやる？
全部のgcdを求めて、そこから途中結果を保存しておく
7 6 8 なら
None 1 1と
None 2 1
Noneと2なので2

"""
def main(N, A):
    tmp = A[0]
    tmp_list = [A[-1], A[0]]

    for i in range(N-1):
        tmp = fractions.gcd(tmp, A[i+1])
        tmp_list.append(tmp)

    tmp = A[-1]
    reversed_tmp_list = [A[0], A[-1]]
    for i in range(N-1):
        tmp = fractions.gcd(tmp, A[-(i+2)])
        reversed_tmp_list.append(tmp)

    debug(tmp_list, locals())
    debug(reversed_tmp_list, locals())

    answers = []
    for i in range(N-1):
        #  if i == 0:
        #      answers.append(reversed_tmp_list[-(i+2)])
        #      continue
        #  if i == N - 2:
        #      answers.append(tmp_list[i])
            #  continue
        tmp = fractions.gcd(tmp_list[i], reversed_tmp_list[-(i+2)])
        answers.append(tmp)
    debug(answers, locals())
    return max(answers)


if __name__ == "__main__":
    if 'LOCAL' in os.environ:
        values = [
                    [
                        4,
                        [2, 4, 6, 8],
                        2
                    ],
                    [
                        2,
                        [100000, 100000],
                        100000
                        ]
                ]
        for i in values:
            assert main(i[0], i[1]) == i[2]
    else:
        N = _I()
        A = LI()
        print(main(N, A))

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N = _I()
A = LI()

"""
書き換えたあとの最大公約数の最大値を求める
書き換える値の最適解は、最小値を最大値に書き換えること
例えば、1,6,3があるとき、1を3に書き換えることで最大公約数が3になる
しかし、7 6 8の場合、最小の6ではなく7を書き換えることが最適となる
結局全部の数について書き換えたときの最大値を求めるしか無いか
そうするとO(N**2)になってしまう
全体の最大公約数はgcd(gcd(gcd(1,2), 3),4)
1つ書き換えるは、消すと同義
逆でやる？
全部のgcdを求めて、そこから途中結果を保存しておく
7 6 8 なら
None 1 1と
None 2 1
Noneと2なので2

"""
tmp = A[0]
tmp_list = [A[-1], A[0]]

for i in range(N-1):
    tmp = fractions.gcd(tmp, A[i+1])
    tmp_list.append(tmp)

#  print(tmp_list)

tmp = A[-1]
reversed_tmp_list = [A[0], A[-1]]
for i in range(N-1):
    tmp = fractions.gcd(tmp, A[-(i+2)])
    reversed_tmp_list.append(tmp)

#  print(reversed_tmp_list)

answers = []
for i in range(N-1):
    #  if i == 0:
    #      answers.append(reversed_tmp_list[-(i+2)])
    #      continue
    #  if i == N - 2:
    #      answers.append(tmp_list[i])
        #  continue
    tmp = fractions.gcd(tmp_list[i], reversed_tmp_list[-(i+2)])
    answers.append(tmp)
#  print(answers)
print(max(answers))

Submission status: Wrong Answer

Solution code:
# GCD on Blackboad
import fractions

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

L = [A[0]]
d = A[0]
for a in A[1:]:
	d = fractions.gcd(d,a)
	L.append(d)

R = [A[-1]]
d = A[-1]
for a in A[-2::-1]:
	d = fractions.gcd(d, a)
	R.append(d)
R = R[::-1]

#print(L)
#print(R)	

gcd = [0]*N
for n in range(N):
	if n == 0:
		gcd[n] = R[1]
	elif n == N-1:
		gcd[n] = L[n]
	else:
		gcd[n] = fractions.gcd(L[n-1], R[n+1])

#print(gcd)
print(max(gcd))

Submission status: Wrong Answer

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

# GCDは最小の数に制約を受ける
A.sort()


def euclidian(A, B):
    A = A % B
    if A == 0:
        return B
    else:
        return euclidian(B, A)


if N == 2:
    print(max(A[0], A[1]))
else:
    firstGCD = 10e+9
    for other in A[2:]:
        firstGCD = min(firstGCD, euclidian(other, A[0]))

    secondGCD = 10e+9
    A.pop(1)
    for other in A[1:]:
        secondGCD = min(secondGCD, euclidian(other, A[0]))

    print(max(firstGCD, secondGCD))

Submission status: Wrong Answer

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

import numpy as np
import fractions as frc
from functools import reduce
from copy import deepcopy

x2=deepcopy(x)

def gcd_list(lst):
    return reduce(frc.gcd, lst)

while True:
	n=len(x)
	if len(set(x))==1 or len(x)==2:
		res=max(x)
		break    
	g=[]
	if n<=5:
		for i in range(n):
			f=[]
			for j in range(n):
				if j!=i:            
					f.append(x[j])
			g.append(gcd_list(f))
		idx=np.argmax(g)
		rm=x[idx]
		x3=[c for c in x2 if c!=rm]
		res=gcd_list(x3)
		break
	sp=n//2
	y=x[:sp]
	z=x[sp:]
	y_gcd=gcd_list(y)
	z_gcd=gcd_list(z)
	if y_gcd>z_gcd:
		x=z
	else:
		x=y

print(res)

Submission status: Wrong Answer

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

import numpy as np
import fractions as frc
from functools import reduce
from copy import deepcopy

x2=deepcopy(x)

def gcd_list(lst):
    return reduce(frc.gcd, lst)

while True:
	n=len(x)
	if len(set(x))==1 or len(set(x))==2:
		res=max(x)
		break    
	g=[]
	if n<=5:
		for i in range(n):
			f=[]
			for j in range(n):
				if j!=i:            
					f.append(x[j])
			g.append(gcd_list(f))
		idx=np.argmax(g)
		rm=x[idx]
		x3=[c for c in x2 if c!=rm]
		res=gcd_list(x3)
		break
	sp=n//2
	y=x[:sp]
	z=x[sp:]
	y_gcd=gcd_list(y)
	z_gcd=gcd_list(z)
	if y_gcd>z_gcd:
		x=z
	else:
		x=y

print(res)

Submission status: Wrong Answer

Solution code:
#-*- using:utf-8 -*-

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

def gcd(a,b):
#    print("gcd %d %d" %(a,b))
    n = 0
    if a == b:
        return a
    elif a > b:
        n = euclidean(a,b)

    elif a < b:
        n = euclidean(b,a)

    else:
        pass

    return n 

def left(i,a):
    cd = 0
    for p in range(i):
        #cd0 = left(0) = 0
        #cd1 = left(1) = gcd(cd0,a[0])
        #cd2 = left(2) = gcd(cd1,a[1])
#        print("left %d %d" %(cd,a[p]))
        cd = gcd(cd,a[p])
#        print("left %d" %cd)
    return cd

def right(i,a):
    cd = 0
    for p in range(i,m)[::-1]:
        #cdn+1 = right(n+1) = 0
        #cdn   = right(n)   = gcd(cdn+1,a[n])
        #cdn-1 = right(n-1) = gcd(cdn,a[n-1])
#        print("right %d %d" %(cd,a[p]))
        cd = gcd(cd,a[p])
#        print("right %d" %cd)
    return cd

def blackboard(n,a):
    candidate = 0
    maxnum = 0
    b = list(map(int,a))
    
    p = m // 2 # round down
    q = -(-m // 2) #round up
#    print("i %d j %d" %(p,q))
    maxnum = gcd(left(p,b),right(q,b))

    return maxnum


if __name__ == '__main__':
    n = raw_input()
    a = raw_input().split()
    global m 
    m = int(n)
    print("%d" %(blackboard(m,a)))

Submission status: Wrong Answer

Solution code:
#-*- using:utf-8 -*-

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

def gcd(a,b):
    n = 0
    if a == b:
        return a
    elif a > b:
        n = euclidean(a,b)

    elif a < b:
        n = euclidean(b,a)

    else:
        pass

    return n

def blackboard(n,a):
    candidate = 0
    maxnum = 0
    m = int(n)
    k = m - 1
    b = list(map(int,a))
    for i in range(k):
        x = b[i]
        for j in range(m):
            if i == j:
                continue
            else:
                y = b[j]
                candidate = gcd(x,y)
                if maxnum < candidate:
                    maxnum = candidate
                else:
                    pass
    else:
        return maxnum

if __name__ == '__main__':
    n = raw_input()
    a = raw_input().split()
    print("%d" %(blackboard(n,a)))

Submission status: Wrong Answer

Solution code:
def euclidean(a,b):
#    print("%d %d" %(a,b))
    if b == 0:
        return a
    else:
        return  euclidean(b,a%b)

def gcd(a,b):
    n = 0
#    print("gcd %d %d" %(a,b))
    if a == b:
        return a
    elif a > b:
        n = euclidean(a,b)
    elif a < b:
        n = euclidean(b,a)
    else:
        pass

    return n

def blackboard(n,a):
    candidate = 0
    maxnum = 0
    m = int(n)
    for i in range(m-1):
        x = int(a[i])
        for j in range(1,m):
            if i == j:
                continue
            else:
                y = int(a[j])
                candidate = gcd(x,y)
#                print("candidate => %d" %candidate)
                if maxnum < candidate:
                    maxnum = candidate
#                    print("max => %d" %maxnum)
                else:
                    pass
    else:
        return maxnum

if __name__ == '__main__':
    n = raw_input()
    a = raw_input().split()

    print("%d" %(blackboard(n,a)))

Submission status: Wrong Answer

Solution code:
import copy

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

#最大公約数を求めるfunction
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a  

def gcd_list(list,N):
  a = list[0]
  for i in range(1, N):
      b = gcd(a, list[i])
  return b

for i in range(N):
  newA = copy.copy(A)
  if i != N-1:
    newA[i] = newA[i+1]
  else:
    newA[i] = newA[i-1]
  ans = max(gcd_list(newA,N),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b, t = LI()
    print(b * (t // a))
    return

#B
def B():
    n = II()
    v = LI()
    c = LI()
    ans = 0
    for i in range(n):
        if v[i] > c[i]:
            ans += v[i] - c[i]
    print(ans)
    return

#C
def C():
    def gcd(a, b):
        if b > a: a, b = b, a
        while b:
            a, b = b, a % b
        return a
    n = II()
    a = LI()
    dp1 = [None] * (n - 1)
    dp1[0] = a[0]
    dp2 = [None] * (n - 1)
    dp2[-1] = a[-1]
    for i in range(1, n - 1):
        dp1[i] = gcd(dp1[i - 1], a[i])
        dp2[-i - 1] = gcd(dp2[-i], a[-i - 1])
    ans = 0
    dp1.insert(0, dp2[0])
    dp2.append(dp1[-1])
    for i in range(n - 1):
        ans = max(ans, gcd(dp1[i], dp2[i]))
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

Solution code:
import copy
def gcd(a, b):
    if b == 0: return a
    return gcd(b, a%b)
n = int(input())
l = list(map(int,input().split()))
#import collections
#c=collections.Counter(l)
#mc=c.most_common()
#s_d=set(i[0] for i in mc if i[1]>=2)
ans=1
s=set(l)
for i in range(n):
    s_new=copy.deepcopy(s)
#    if l[i] not in s_d:
    s_new.remove(l[i])
    g = l[(i+1)%2]
    for j in s_new:
        g=gcd(g,j)
    if g>ans:ans=g
print(ans)

Submission status: Wrong Answer

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


def gcd_over2(list_gcd):
    c = gcd(list_gcd[0], list_gcd[1])
    if len(list_gcd) <= 2:
        return c
    for i in range(1, len(list_gcd)-1):
        c = gcd(c, list_gcd[i + 1])
    return c


a1 = input()

a = input().split()
a = sorted([int(i) for i in a])
c = gcd_over2(a)
a = [i / c for i in a]
for u in range(len(a) - 1):
    if gcd(a[u], a[u + 1]) == 1:
        a[u + 1] = max(a)
d = gcd_over2(a)
print(int(c * d))

Submission status: Wrong Answer

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


def gcd_over2(list_gcd):
    c = gcd(list_gcd[0], list_gcd[1])
    if len(list_gcd) <= 2:
        return c
    for i in range(1, len(list_gcd)-1):
        c = gcd(c, list_gcd[i + 1])
    return c


a1 = input()

a = input().split()
a = sorted([int(i) for i in a])
c = gcd_over2(a)
a = [i / c for i in a]
for u in range(len(a) - 1):
    if gcd(a[u], a[u + 1]) == 1:
        a[u + 1] = a[u]
d = gcd_over2(a)
print(int(c * d))

Submission status: Wrong Answer

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))

    #累積GCD
    left = [0]*n
    right = [0]*n

    for i in range(n-1):
        left[i+1] = gcd(left[i],a[i])
        right[-i-2] = gcd(right[-i-1],a[-i-1])
        # print("left:",left)
        # print("right:",right)

    gcd_t = 1
    for i in range(n-1):
        gcd_t = max(gcd_t,gcd(left[i],right[i]))
    print(gcd_t)

Submission status: Wrong Answer

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    A = sorted(list(map(int, sys.stdin.readline().split())))
    if n==2:
        print(gcd(*A))
    else:
        ans = gcd(A[-2],A[-1])
        temp_ans = gcd(A[-3],A[-1])
        if temp_ans > ans:
            ans = temp_ans
        print(ans)

    # else:
    #     ans = gcd(*A[1:n])
    #     for i in range(n):
    #         p = A.pop(i)
    #         temp_ans = gcd(*A)
    #         if temp_ans > ans:
    #             ans = temp_ans
    #         A.insert(i,p)
    #     print(ans)

Submission status: Wrong Answer

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    A = sorted(list(map(int, sys.stdin.readline().split())))
    if n==2:
        print(gcd(*A))
    else:
        ans = gcd(A[0],A[-1])
        temp_ans = gcd(A[1],A[-1])
        if temp_ans > ans:
            ans = temp_ans
        print(ans)

    # else:
    #     ans = gcd(*A[1:n])
    #     for i in range(n):
    #         p = A.pop(i)
    #         temp_ans = gcd(*A)
    #         if temp_ans > ans:
    #             ans = temp_ans
    #         A.insert(i,p)
    #     print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

left = [0]*n
right = [0]*n
left[0] = a[0]
right[n-1] = a[n-1]

ans = 0
for i in range(n-1):
    left[i+1] = gcd(left[i], a[i+1])
for j in range(n-2, 0, -1):
    right[i] = gcd(right[i+1], a[i])
for k in range(n):
    if k == 0:
        b = right[1]
    elif k == n-1:
        b = left[n-2]
    else:
        b = gcd(left[k-1], right[k+1])
    if b > ans:
        ans = b

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
A = list(map(int, input().split()))
L = [0] * (N+1)
R = [0] * (N+2)
M = [0] * (N+1)

for i in range(1, N+1):
  L[i] = fractions.gcd(L[i-1], A[i-1])
  R[N-i+1] = fractions.gcd(R[N-i+2], A[i-1])

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

print(max(M[1:]))

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
A = list(map(int, input().split()))
L = [0] * (N+1)
R = [0] * (N+2)
M = [0] * (N+1)

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

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

print(max(M[1:]))

Submission status: Wrong Answer

Solution code:
#125

import math
import fractions

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

L = [0] *(N+2)
R = [0] *(N+2)
ans = 0

#漸化式的に最大公約数算出(前から順に算出)
for j in range(0,N-1):
    L[j + 1] = fractions.gcd(L[j], A[j])
#漸化式的に最大公約数算出(後ろから順に算出)
for j in range(N -1,1,-1):
    R[j - 1] = fractions.gcd(R[j], A[j])
for j in range(0,N):
    if ans < fractions.gcd(L[j],R[j]):
        ans = fractions.gcd(L[j],R[j])

print(ans)

Submission status: Wrong Answer

Solution code:
#125

import math
import fractions

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

L = [0] *(N+2)
R = [0] *(N+2)
ans = 0

#漸化式的に最大公約数算出(前から順に算出)
for j in range(0,N):
    L[j + 1] = fractions.gcd(L[j], A[j])
#漸化式的に最大公約数算出(後ろから順に算出)
for j in range(0,N):
    R[j + 1] = fractions.gcd(R[j], A[N - j - 1])
for j in range(0,N):
    if ans < fractions.gcd(L[j],R[j]):
        ans = fractions.gcd(L[j],R[j])

print(ans)

Submission status: Wrong Answer

Solution code:
#125

import math
import fractions

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

L = [0] *(N+2)
R = [0] *(N+2)
ans = -20000

#漸化式的に最大公約数算出(前から順に算出)
for j in range(0,N):
    L[j + 1] = fractions.gcd(L[j], A[j])
#漸化式的に最大公約数算出(後ろから順に算出)
for j in range(0,N):
    R[j + 1] = fractions.gcd(R[j], A[N - j - 1])
for j in range(0,N+1):
    if ans < fractions.gcd(L[j],R[j]):
        ans = fractions.gcd(L[j],R[j])

print(ans)

Submission status: Wrong Answer

Solution code:

import math
import fractions

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

L = [0] *(N+2)
R = [0] *(N+2)
ans = -20000

#漸化式的に最大公約数算出(前から順に算出)
for j in range(0,N):
    L[j + 1] = fractions.gcd(L[j], A[j])
#漸化式的に最大公約数算出(後ろから順に算出)
for j in range(0,N):
    R[j + 1] = fractions.gcd(R[j], A[N - j - 1])
for j in range(1,N+1):
    if ans < fractions.gcd(L[j],R[j]):
        ans = fractions.gcd(L[j],R[j])

print(ans)

Submission status: Wrong Answer

Solution code:
#125

import math
import fractions

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

L = [0] *(N+2)
R = [0] *(N+2)
ans = -20000

#漸化式的に最大公約数算出(前から順に算出)
for j in range(0,N):
    L[j + 1] = fractions.gcd(L[j], A[j])
#漸化式的に最大公約数算出(後ろから順に算出)
for j in range(0,N):
    R[j + 1] = fractions.gcd(R[j], A[N - j - 1])
for j in range(1,N+1):
    if ans < fractions.gcd(L[j-1],R[j+1]):
        ans = fractions.gcd(L[j-1],R[j+1])

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import fractions

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

L = [0] *(N+2)
R = [0] *(N+2)
ans = -20000

#漸化式的に最大公約数算出(前から順に算出)
for j in range(1,N):
    L[j + 1] = fractions.gcd(L[j], A[j-1])
    print(A[j-1],j)
#漸化式的に最大公約数算出(後ろから順に算出)
for j in range(N,0,-1):
    R[j + 1] = fractions.gcd(R[j], A[j-1])

for j in range(1,N+1):
    if ans < fractions.gcd(L[j],R[j]):
        ans = fractions.gcd(L[j],R[j])
    
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
import itertools
from functools import reduce
import sys
n = int(input())
a = sorted(set(list(map(int,input().split()))))
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
tmp=1
if len(a) == 1:
  print(a[0])
  sys.exit()
for j in range(len(a)):
  new = a[:j]+a[j+1:]
  tmp = max(tmp, gcd_list(new))
print(tmp)

Submission status: Wrong Answer

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

if len(a) > 1:
  for i in range(a[1],0,-1):
    cnt = 0
    for j in a:
      if j % i != 0:
        cnt+=1
    if cnt < 2:
      print(i)
      break
else:
  print(a[0])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
from fractions import gcd
from functools import reduce
minimumgcd=10**10
Agcd=A[0]
for i in range(1,N):
    Agcd=gcd(Agcd,A[i])
    if minimumgcd>Agcd:
        minimumgcd=Agcd
        Achange=i
if Achange==N-1:
    B=A[:Achange]
else:
    B=A[:Achange]+A[Achange+1:]

print(max(reduce(gcd,B),reduce(gcd,A[1:])))

Submission status: Wrong Answer

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

def gcd(m, n) :
    if m < n :
        m, n = n, m
    while m % n :
        m, n = n, m % n
    return n

gcdL = [0] * N
gcdR = [0] * N
gcdL[0] = A[0]
gcdR[N-1] = A[N-1]

for i in range(1, N) :
    gcdL[i] = gcd(gcdL[i-1], A[i])
    gcdR[N-1-i] = gcd(gcdR[N-i], A[N-1-i])
    
ret = max(gcdL[1], gcdR[N-2])
for i in range(1, N - 1) :
    ret = max(ret, gcd(gcdL[i-1], gcdR[i+1]))

print(ret)

Submission status: Wrong Answer

Solution code:
import fractions as f

N = int(input())

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

rightGCD = [0]*N
leftGCD = [0]*N

for i in range(N-1):
    leftGCD[i+1] = f.gcd(leftGCD[i],A[i])
    rightGCD[-i-2] = f.gcd(rightGCD[-i-1],A[-i-1])

print(leftGCD)
print(rightGCD)

ans = 0

for i in range(N):
    ans = max(ans,f.gcd(rightGCD[i],leftGCD[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
from fractions import gcd
from functools import reduce
minimumgcd=10**10
Agcd=A[0]
for i in range(1,N):
    Agcd=gcd(Agcd,A[i])
    if minimumgcd>Agcd:
        minimumgcd=Agcd
        Achange=i
B=A[:Achange]+A[Achange+1:]

print(max(reduce(gcd,B),reduce(gcd,A[1:])))

Submission status: Wrong Answer

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

st = set()
for tar in a[:2]:
    i = 1
    while i * i <= tar:
        if tar % i == 0:
            st.add(i)
            st.add(tar // i)
        i += 1

for d in sorted(st, reverse=True):
    for aa in a[2:]:
        if aa % d != 0:
            break
    else:
        print(d)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


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


def gcd_m(*args):
    if len(args) == 0:
        return 0
    if len(args) == 1:
        return args[0]
    if len(args) >= 2:
        ret = gcd(args[0], args[1])

    for value in args[2:]:
        ret = gcd(ret, value)
    return ret


def solve(inputs):
    [N] = string_to_int(inputs[0])
    # A = [0] + (string_to_int(inputs[1])) + [0]
    A = string_to_int(inputs[1])

    if N == 2:
        return max(A)

    left_gcd_memo = deque([0])
    right_gcd_memo = deque([0])

    for v in A:
        left_gcd_memo.append(gcd(left_gcd_memo[-1], v))
    for v in reversed(A):
        right_gcd_memo.appendleft(gcd(right_gcd_memo[0], v))

    max_gcd = 0
    for i in range(0, len(A)-1):
        g = gcd(left_gcd_memo[i], right_gcd_memo[N-i])
        max_gcd = g if g > max_gcd else max_gcd

    return max_gcd


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)

Submission status: Wrong Answer

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


N = int(input())
A = list(map(int,input().split()))
L = [] #[A[0], gcd(A[0], A[1]), gcd(A[0], A[1], A[2]), ...,gcd(A[0], ..., A[N-2]) -> N-1個の要素
R = [] #[A[N-1], gcd(A[N-1], A[N-2]), gcd(A[N-1], A[N-2], A[N-3]), ... ,gcd(A[N-1], A[N-2], A[N-3], ..., A[1])]

L.append(A[0])
R.append(A[-1])
for i in range(1, N-1):
    L.append(gcd(L[i-1], A[i]))
    R.append(gcd(R[i-1], A[N-1-i]))
ans = R[-1]
for i in range(N-2):
    val = gcd(L[i], R[N-i-3])
    if val > ans:
        ans = val
print(str(ans))

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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

# l[i] := [0, i]の数のGCDメモ
l = [None for _ in range(N)]
l[0] = A[0]
for i in range(1, N):
    l[i] = gcd(l[i - 1], A[i])

# r[i] := [i, N - 1]の数のGCDメモ
r = [None for _ in range(N)]
r[N - 1] = A[N - 1]
for i in reversed(range(N - 1)):
    r[i] = gcd(r[i + 1], A[i])

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

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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

# l[i] := [0, i]の数のGCDメモ
l = [None for _ in range(N)]
l[0] = A[0]
for i in range(1, N):
    l[i] = gcd(l[i - 1], A[i])

# r[i] := [i, N - 1]の数のGCDメモ
r = [None for _ in range(N)]
r[N - 1] = A[N - 1]
for i in reversed(range(N - 1)):
    r[i] = gcd(r[i + 1], A[i])

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

Submission status: Wrong Answer

Solution code:
def larger(x, y):
	if x > y:
		return x
	return y

def euc(x, y):
	if y == 0:
		return x
	return euc(y, x%y)

N = int(input())
A = list(map(int, input().split()))
gcdL = [A[0]]
for i in range(1, N):
	gcdL.append(euc(gcdL[-1], A[i]))
gcdR = [A[-1]]
for i in range(1, N):
	gcdR.append(euc(gcdR[-1], A[-1-i]))
gcdR.reverse()
max = gcdR[0]
for i in range(1, N-1):
	max = larger(max, euc(gcdL[i-1], gcdR[i+1]))
max = larger(max, gcdL[-1])
print(max)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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

ans=1
for i in range(N):
#    b=A[:i]+A[i+1:]
#    c=reduce(fractions.gcd, b)
    c=reduce(fractions.gcd, A)
    ans=max(ans,c)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

def ret_dividable(n):
  arr = []
  for i in range(1,int(math.sqrt(n))+1):
    if n%i==0:
      arr.append(i)
      arr.append(n//i)
    else:
      pass  
  return sorted(arr)


n = int(input())
a = list(map(int,input().split()))
d = {}
for i in range(n):
  for ri in ret_dividable(a[i]):
    if ri in d.keys():
      d[ri]+=1
    else:
      d[ri]=1 

# 出現数が1番目に多い値を取得(二段階で並べ替え)
tmp = sorted(d.items(),key = lambda x:(x[1],x[0]) ,reverse=True)


# 全ての ai が同じ場合、答えは ai
issame = (sum([1 for ai in a if ai==a[0]]) == n)
if issame:
  print(a[0])
else:  
  # 出現数が2番目に多いものだけ抜き取る
  freq_2 = [t for t in tmp if t[1]==n-1]
  if len(freq_2)>0:
    print(freq_2[0][0])
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
import numpy as np
N = int(input())
X = list(map(int, input().split()))
 
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
gcd = 1
 
for i in range(N):
  Y = X.pop(i)
  gcd_1 = gcd * gcd_list(X)
  gcd = max(int(gcd), int(gcd_1))
  if Y%gcd != 0:
    break
  X.insert(i,Y)
  
print(gcd)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
N = int(input())
X = list(map(int, input().split()))
 
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
  
gcd = 1
 
for i in range(N):
  Y = X.pop(i)
  gcd_1 = gcd_list(X)
  if gcd == gcd_1:
    break
  gcd = max(int(gcd), int(gcd_1))
  X.insert(i,Y)
print(gcd)

Submission status: Wrong Answer

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

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

n = int(input())
ls = list(map(int, input().split()))
ans = 0

for i in range(n-1):
    s = cul(i, ls[:n])
    if s > ans:
        ans = s
print(ans)

Submission status: Wrong Answer

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


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

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

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

ret = 0
for i in range(N):
    ret = max(ret, gcd(L[i], R[i + 1]))

print(ret)