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
import sys
sys.setrecursionlimit(10**6)
class SegmentTree:
    """
    単位元のメモ書き（各演算に対する単位元の対応）
    f(x,y) := x+y -> 0
    f(x,y) := x-y -> 0
    f(x,y) := x*y -> 1
    f(x,y) := gcd(x, y) -> 0
    """

    def __init__(self, size, func=lambda x,y:x+y, default=0):
        """
        size : 配列のサイズ
        func : queryの内容(e.g. 区間和ならf(x, y)=x+y)
        default : 単位元
        """
        self.size = 2**(size-1).bit_length()
        self.default = default
        self.dat = [default for _ in range(self.size*2-1)]
        self.f = func

    def update(self, i, x):
        """
        i番目の値をxに変更する。0-indexedなので注意
        """
        i = i + self.size - 1 #一番下の層の値
        self.dat[i] = x

        while i > 0:
            i = (i-1)>>1
            self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2])

    def query(self, a, b, k=0, L=0, R=None):
        """
        区間[a, b]に関するクエリを処理します。
        例えばi=1,2,3に関して処理したいなら外からquery(1, 3+1)と呼ぶ必要があります。
        """
        if R is None:
            R = self.size

        #区間がかぶっていない場合
        if R <= a or b <= L:
            return self.default

        #区間が完全に収まっている場合
        if a <= L and R <= b:
            return self.dat[k]

        else:
            lres = self.query(a, b, 2*k+1, L, (L+R)>>1)
            rres = self.query(a, b, 2*k+2, (L+R)>>1, R)
            return self.f(lres, rres)


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

segTree = SegmentTree(N, func=lambda x,y:math.gcd(x,y), default=0)
for i in range(N):
    segTree.update(i, A[i])

ans = -1
for i in range(N-1):
    a = segTree.query(0, i)
    b = segTree.query(i+1, N)
    ans = max(ans, math.gcd(a,b))

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(10**6)
class SegmentTree:

    def __init__(self, size, func=lambda x,y:x+y, default=0):
        """
        size : 配列のサイズ
        func : queryの内容(e.g. 区間和ならf(x, y)=x+y)
        default : 単位元
        """
        self.size = 2**(size-1).bit_length()
        self.default = default
        self.dat = [default for _ in range(self.size*2-1)]
        self.f = func

    def update(self, i, x):
        """
        i番目の値をxに変更する。0-indexedなので注意
        """
        i = i + self.size - 1 #一番下の層の値
        self.dat[i] = x

        while i > 0:
            i = (i-1)>>1
            self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2])

    def query(self, a, b, k=0, L=0, R=None):
        """
        区間[a, b]に関するクエリを処理します。
        例えばi=1,2,3に関して処理したいなら外からquery(1, 3+1)と呼ぶ必要があります。
        """
        if R is None:
            R = self.size

        #区間がかぶっていない場合
        if R <= a or b <= L:
            return self.default

        #区間が完全に収まっている場合
        if a <= L and R <= b:
            return self.dat[k]

        else:
            lres = self.query(a, b, 2*k+1, L, (L+R)>>1)
            rres = self.query(a, b, 2*k+2, (L+R)>>1, R)
            return self.f(lres, rres)


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

segTree = SegmentTree(N, func=lambda x,y:math.gcd(x,y), default=0)
for i in range(N):
    segTree.update(i, A[i])

ans = -1
for i in range(N-1):
    a = segTree.query(0, i)
    b = segTree.query(i+1, N)
    ans = max(ans, math.gcd(a,b))

print(ans)

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce

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

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

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce

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

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

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce

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

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce

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

Submission status: Wrong Answer

Solution code:
from math import gcd

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

Submission status: Wrong Answer

Solution code:
from math import gcd

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

Submission status: Wrong Answer

Solution code:
from math import gcd
from functools import reduce

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

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

B = [None for _ in range(N)]

for i in range(N):
   store = A[i]
   del A[i]
   B[i] = [a for a in A]
   A.insert(i, store)

ans = [None for _ in range(N)]
print(B)

for i in range(N):
   ans[i] = gcd_list(B[i])

print(max(ans))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
# from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
import math
# from itertools import product, accumulate, combinations, product
#import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())



def run():
    N, *A = mapread()
    LEFT = [0] * (N+2)
    RIGHT = [0] * (N+2)
    for i, a in enumerate(A, 1):
        LEFT[i] = math.gcd(LEFT[i-1], a)
    for i in range(N-1, -1, -1):
        k = i+1# for RIGHT
        RIGHT[k] = math.gcd(RIGHT[k+1], A[i])

    ans = 0
    for i in range(N-1):
        k = i + 1# for LEFT and RIGHT
        v = math.gcd(LEFT[k-1], RIGHT[k+1])
        ans = max(ans, v)

    print(ans)



if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()

Submission status: Wrong Answer

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

a_left.append(a[0])


a_right=deque()

a_right.append(a[-1])

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


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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input()
import math
def ans125(N:int,A:str):
    A_list=list(map(int,A.split()))
    most=0
    for i in range(N):
        B_list=list(map(int,A.split()))
        del B_list[i]
        for j in range(N-1):
           m=math.gcd(A_list[i],B_list[j])
           if m>=most:
               most=m

    return most

print(ans125(N,A))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input()

import math


def ans125(N:int,A:str):
    A_list=list(map(int,A.split()))
    most=0
    for i in range(N):
        B_list = list(map(int, A.split()))
        del B_list[i]
        for j in range(N-1):
           if math.gcd(A_list[i],B_list[j])>=most:
               most=math.gcd(A_list[i],B_list[j])

    return most

print(ans125(N,A))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input()

import math


def ans125(N:int,A:str):
    A_list=list(map(int,A.split()))
    most=0
    for i in range(0,N-1):
           if math.gcd(A_list[i],A_list[i-1])>=most:
               most=math.gcd(A_list[i],A_list[i-1])
           if math.gcd(A_list[0],A_list[-1])>=most:
               most=math.gcd(A_list[0],A_list[-1])

    return most

print(ans125(N,A))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input()

import math


def ans125(N:int,A:str):
    A_list=list(map(int,A.split()))
    most=0
    for i in range(0,N-1):
           if math.gcd(A_list[i],A_list[i-1])>=most:
               most=math.gcd(A_list[i],A_list[i-1])
           if math.gcd(A_list[i],A_list[-1])>=most:
               most=math.gcd(A_list[i],A_list[-1])

    return most

print(ans125(N,A))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
from operator import itemgetter
#from heapq import heappush, heappop
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ni()
A = na()
b = [0] * N
c = [0] * N
b[0] = A[0]
c[-1] = A[-1]
for i in range(N-1):
    b[i+1] = math.gcd(b[i], A[i+1])
    c[-(i+2)] = math.gcd(c[-(i+1)], A[-(i+2)])

ans = 1
ans = max(b[1], ans)
ans = max(c[-2], ans)
for i in range(1, N-1):
    ans = max(math.gcd(b[i-1], c[i+1]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
"""
問題文を言い換えると、「最大公約数が最大になるように数字を一つ書き換えろ」ということ。
さらに言い換えると、「最大公約数が最大になるように数字を一つ消せ」ということ。
単純な線形探索でできるか？→NO

最大公約数を小さくする要因となっている数字を一つ消す。
どういう数字が最大公約数を小さくする要因になっているか？
他の数字と共通する素因数が少ない、小さい、次数が小さい。ただ、この3つを総合的に判断するのは難しすぎる。
素因数分解はやめて、単純に約数で探せばよいか。
各Aの約数を昇順で列挙しておく。
まずA1は必ず残すと仮定して、A1の約数の大きい方から順番に、ほかのAの約数になるかどうかを調べる。
その約数を含んでいないAが一つだけ存在するとき、そのAを消せば、その約数が最大公約数となる。

次に、A1を消す場合の最大公約数も求めて、A1を含める場合の最大公約数と比較して大きい方を採用する。
"""

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

#A1を必ず入れる場合の最大公約数を求める。
yakusu = []

a = A[0]
d = 2
while d*d < a:
    if a % d == 0:
        yakusu.append(d)
        yakusu.append(a//d)
    d += 1
if d*d == a:
    yakusu.append(d)
yakusu.sort(reverse=True)

ans = 1
for v in yakusu:
    cnt = 0
    for i in range(1,N):
        if A[i]%v != 0:
            cnt += 1
    if cnt <= 1:
        ans = v
        break

#A1を入れない場合の最大公約数を求める。
import math
g = A[1]
for i in range(2,N):
    g = math.gcd(g,A[i])

ans = max(ans,g)

print(ans)

Submission status: Wrong Answer

Solution code:
"""
問題文を言い換えると、「最大公約数が最大になるように数字を一つ書き換えろ」ということ。
さらに言い換えると、「最大公約数が最大になるように数字を一つ消せ」ということ。
単純な線形探索でできるか？→NO

最大公約数を小さくする要因となっている数字を一つ消す。
どういう数字が最大公約数を小さくする要因になっているか？
他の数字と共通する素因数が少ない、小さい、次数が小さい。ただ、この3つを総合的に判断するのは難しすぎる。
素因数分解はやめて、単純に約数で探せばよいか。
各Aの約数を昇順で列挙しておく。
まずA1は必ず残すと仮定して、A1の約数の大きい方から順番に、ほかのAの約数にもふくまれているかどうかを調べる。
その約数を含んでいないAが一つだけ存在するとき、そのAを消せば、その約数が最大公約数となる。

次に、A1を消す場合の最大公約数も求めて、A1を含める場合の最大公約数と比較して大きい方を採用する。
"""

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

#A1を必ず入れる場合の最大公約数を求める。
yakusu = []

for i in range(N):
    a = A[i]
    tmp = []
    d = 2
    while d*d < a:
        if a % d == 0:
            tmp.append(d)
            tmp.append(a//d)
        d += 1
    if d*d == a:
        tmp.append(d)
    yakusu.append(tmp)

yakusu[0].sort(reverse=True)
for i in range(1,N):
    set(yakusu[i])

ans = 1
for v in yakusu[0]:
    cnt = 0
    for i in range(1,N):
        if v not in yakusu[i]:
            cnt += 1
    if cnt <= 1:
        ans = v
        break

#A1を入れない場合の最大公約数を求める。
import math
g = A[1]
for i in range(2,N):
    g = math.gcd(g,A[i])

ans = max(ans,g)

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = sorted(list(map(int,input().split())))
if N==2:
    print(A[1])
else:
    a = A[0]
    for i in range(1,N):
        a = gcd(a,A[i])
    A1 = sorted([A[i]//a for i in range(N)])
    B = []
    for i in range(1,N):
        b = gcd(A1[0],A1[i])
        if b==1:
            B.append(b)
    if len(B)!=1:
        A1[0] = A1[1]
    elif len(B)==1:
        A1[B[0]] = A1[0]
    A = [A1[i]*a for i in range(N)]
    a = A[0]
    for i in range(1,N):
        a = gcd(a,A[i])
    print(a)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = sorted(list(map(int,input().split())))
a = A[0]
for i in range(1,N):
    a = gcd(a,A[i])
A1 = sorted([A[i]//a for i in range(N)])
B = []
for i in range(1,N):
    b = gcd(A1[0],A1[i])
    if b==1:
        B.append(b)
if len(B)!=1:
    A1[0] = A1[1]
elif len(B)==1:
    A1[B[0]] = A1[0]
A = [A1[i]*a for i in range(N)]
a = A[0]
for i in range(1,N):
    a = gcd(a,A[i])
print(a)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = sorted(list(map(int,input().split())))
a = A[0]
for i in range(1,N):
    a = gcd(a,A[i])
A1 = [A[i]//a for i in range(N)]
B = []
for i in range(1,N):
    b = gcd(A1[0],A1[i])
    if b==1:
        B.append(b)
if len(B)!=1:
    A[0] = A[1]
elif len(B)==1:
    A[B[0]] = A[0]
a = A[0]
for i in range(1,N):
    a = gcd(a,A[i])
print(a)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x
N = int(input())
A = sorted(list(map(int,input().split())))
B = []
for i in range(1,N):
    b = gcd(A[0],A[i])
    if b==1:
        B.append(b)
if len(B)==0 or len(B)>1:
    A[0] = A[1]
elif len(B)==1:
    A[B[0]] = A[0]
a = A[0]
for i in range(1,N):
    a = gcd(a,A[i])
print(a)

Submission status: Wrong Answer

Solution code:
# practice for SegTree
from math import gcd

INF = 10**10
class SegTree:
    def __init__(self, vals, seg_f, ide_ele):
        n = len(vals)
        self.seg_f = seg_f
        self.ide_ele = ide_ele
        self.leaf_n = 1 << (n-1).bit_length()
        self.tree = [ide_ele]*(2*self.leaf_n-1)
        for i in range(n):  # init leaf
            self.tree[i+self.leaf_n-1] = vals[i]
        for i in range(self.leaf_n-2, -1, -1):  # bottom-up
            self.tree[i] = self.seg_f(self.tree[2*i+1], self.tree[2*i+2])

    def update(self, i, x):
        i += (self.leaf_n-1)
        self.tree[i] = x
        while i > 0:
            i = (i-1)//2
            self.tree[i] = self.seg_f(self.tree[2*i+1], self.tree[2*i+2])

    # [l,r)
    def query(self, l, r):
        v_l = self.ide_ele
        v_r = self.ide_ele
        l += (self.leaf_n-1)
        r += (self.leaf_n-1)
        while l < r:
            if l&1 == 0:
                v_l = self.seg_f(v_l, self.tree[l])
            if r&1 == 0:
                v_r = self.seg_f(v_r, self.tree[r-1])
                r -= 1
            l >>= 1
            r >>= 1
        return self.seg_f(v_l ,v_r)

def gcd2(a,b):
    if a == INF:
        return b
    elif b == INF:
        return a
    else:
        return gcd(a,b)


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

st = SegTree(al,gcd2,INF)
print(st.tree)
ans = 0
for i in range(n):
    if i > 0: st.update(i-1,al[i-1])
    st.update(i,INF)
    c_gcd = st.query(0,n)
    print(c_gcd,'-')
    ans = max(ans,c_gcd)

print(ans)

Submission status: Wrong Answer

Solution code:
# practice for SegTree
from math import gcd

INF = 10**10
class SegTree:
    def __init__(self, vals, seg_f, ide_ele):
        n = len(vals)
        self.seg_f = seg_f
        self.ide_ele = ide_ele
        self.leaf_n = 1 << (n-1).bit_length()
        self.tree = [ide_ele]*(2*self.leaf_n-1)
        for i in range(n):  # init leaf
            self.tree[i+self.leaf_n-1] = vals[i]
        for i in range(self.leaf_n-2, -1, -1):  # bottom-up
            self.tree[i] = self.seg_f(self.tree[2*i+1], self.tree[2*i+2])

    def update(self, i, x):
        i += (self.leaf_n-1)
        self.tree[i] = x
        while i > 1:
            i = (i-1)//2
            self.tree[i] = self.seg_f(self.tree[2*i+1], self.tree[2*i+2])

    # [l,r)
    def query(self, l, r):
        v_l = self.ide_ele
        v_r = self.ide_ele
        l += (self.leaf_n-1)
        r += (self.leaf_n-1)
        while l < r:
            if l&1 == 0:
                v_l = self.seg_f(v_l, self.tree[l])
            if r&1 == 0:
                v_r = self.seg_f(v_r, self.tree[r-1])
                r -= 1
            l >>= 1
            r >>= 1
        return self.seg_f(v_l ,v_r)

def gcd2(a,b):
    if a == INF:
        return b
    elif b == INF:
        return a
    else:
        return gcd(a,b)


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

st = SegTree(al,gcd2,INF)
ans = 0
for i in range(n):
    if i > 0: st.update(i-1,al[i-1])
    st.update(i,INF)
    c_gcd = st.query(0,n)
    ans = max(ans,c_gcd)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n = onem()

a = l()

R = [1 for i in range(n)]
L = [1 for i in range(n)]

for i in range(n):
    ii = -(i+1) 
    if i == 0:
        R[ii] = a[ii]
        L[i] = a[i]
    else:
        R[ii] = gcd(R[ii+1],a[ii])
        L[i] = gcd(L[i-1],a[ii])
ma = 1

for i in range(n):
    if i == 0:
        ma = max(ma,R[1])
    elif i == n-1:
        ma = max(ma,L[-2])
    else:
        ma = max(ma,gcd(L[i-1],R[i+1]))
print(ma)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
import math
from functools import reduce
def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n = onem()

a = l()

R = [1 for i in range(n)]
L = [1 for i in range(n)]

for i in range(n):
    ii = -(i+1) 
    if i == 0:
        R[ii] = a[ii]
        L[i] = a[i]
    else:
        R[ii] = gcd(R[ii+1],a[ii])
        L[i] = gcd(L[i-1],a[ii])
ma = 1

for i in range(n):
    if i == 0:
        ma = max(ma,R[1])
    elif i == n-1:
        ma = max(ma,R[-2])
    else:
        ma = max(ma,gcd(L[i-1],R[i+1]))
print(ma)

Submission status: Wrong Answer

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


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

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

Right = [A[N-1]]
for i in range(N-1):
    Right.append(gcd(Right[i], A[N-2-i]))
Right.reverse()

ans_list = []
ans_list.append(Right[1])
for i in range(1, N-1):
    ans_list.append(gcd(Left[i-1], Right[i+1]))
ans_list.append(Left[N-1])

print(max(ans_list))

Submission status: Wrong Answer

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

B = [0 for _ in range(N)]
C = [0 for _ in range(N)]

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

C[N-1] = A[N-1]
for i in range(N-2,-1,-1):
  C[i] = math.gcd(C[i+1],A[i])
  
ans = max(C[1],B[N-2])
for i in range(1,N-1):
  print(i, math.gcd(C[i+1],B[i-1]))
  ans = max(math.gcd(C[i+1],B[i-1]), ans)
          
print(ans)
#print(B)
#print(C)

Submission status: Wrong Answer

Solution code:
def main():
    import math
    n = int(input())
    a = sorted(list(map(int,input().split())))
    if n==2:
        print(a[1])
        return
    if n==3:
        print(max(math.gcd(a[0],a[1]),math.gcd(a[0],a[2]),math.gcd(a[1],a[2])))
        return
    g = math.gcd(a[0],a[1])
    ans = 0
    ans_list = []
    for i in range(1,int(g**0.5)+1):
        if g%i==0:
            ans_list.append(i)
    if len(ans_list)==1:
        ans_list.append(g)
    for an in ans_list:
        cnt = 0
        for i in range(n):
            if a[i] % an == 0:
                cnt += 1
        if cnt > n-2:
            if an>ans:
                ans = an
    g = math.gcd(a[1],a[2])
    ans_list = []
    for i in range(1,int(g**0.5)+1):
        if g%i==0:
            ans_list.append(i)
    if len(ans_list)==1:
        ans_list.append(g)
    for an in ans_list:
        cnt = 0
        for i in range(n):
            if a[i] % an == 0:
                cnt += 1
        if cnt > n-2:
            if an>ans:
                ans = an
    g = math.gcd(a[0],a[2])
    ans_list = []
    for i in range(1,int(g**0.5)+1):
        if g%i==0:
            ans_list.append(i)
    if len(ans_list)==1:
        ans_list.append(g)
    for an in ans_list:
        cnt = 0
        for i in range(n):
            if a[i] % an == 0:
                cnt += 1
        if cnt > n-2:
            if an>ans:
                ans = an
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

g=a[0]
t=1
ch=0
for x in range(1,n):
  y=math.gcd(g,a[x])
  s=g//y
  
  if t<s:
    t=s
    ch=x
G=a[0]    
for z in range(1,n):
  if z!=ch:
    G=math.gcd(G,a[z])
    
W=a[1]
T=1
ch1=0
for t in range(n):
  if t!=1:
    
    Z=math.gcd(W,a[t])
    e=W//Z
    if T<e:
      T=e
      ch1=t
      
G1=a[1]
for u in range(n):
  if u!=1 and u!=ch1:
    G1=math.gcd(G1,a[u])
    
print(max(G,G1))

Submission status: Wrong Answer

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

g=a[0]
t=1
ch=0
for x in range(1,n):
  y=math.gcd(g,a[x])
  g=y
  s=g//y
  
  if t<s:
    t=s
    ch=x
G=a[0]    
for z in range(1,n):
  if z!=ch:
    G=math.gcd(G,a[z])
    
W=a[1]
if n>2:
  for t in range(2,n):
    W=math.gcd(W,a[t])
    
print(max(W,G))

Submission status: Wrong Answer

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

g=a[0]
t=1
ch=0
for x in range(1,n):
  y=math.gcd(g,a[x])
  s=g//y
  
  if t<s:
    t=s
    ch=x
G=a[0]    
for z in range(1,n):
  if z!=ch:
    G=math.gcd(G,a[z])
    
W=a[1]
if n>2:
  for t in range(2,n):
    W=math.gcd(W,a[t])
    
print(max(W,G))

Submission status: Wrong Answer

Solution code:
from math import gcd

n=int(input())
a=list(map(int,input().split()))
if n==2:
  print(max(a))
  exit()
  
a.sort()

pin=a[0]
pin2=a[1]

lst=[]
lst2=[]

for i in a:
  lst.append(gcd(pin,i))

a=a[1:]

for i in a:
  lst2.append(gcd(pin2,i))  

lst.sort()
lst2.sort()

print(max(lst[1],lst2[0]))

Submission status: Wrong Answer

Solution code:
from math import gcd

n=int(input())
a=list(map(int,input().split()))
if n==2:
  print(max(a))
  exit()
  
a.sort()

pin=a[0]
pin2=a[1]

lst=[]
lst2=[]

for i in a:
  lst.append(gcd(pin,i))
  lst2.append(gcd(pin2,i))
  
lst.sort()
lst2.sort()

print(max(lst[1],lst2[0]))

Submission status: Wrong Answer

Solution code:
from math import gcd

n=int(input())
a=list(map(int,input().split()))
if n==2:
  print(max(a))
  exit()
  
a.sort()

pin=a[0]
pin2=a[1]

lst=[]
lst2=[]

for i in a:
  lst.append(gcd(pin,i))
  lst2.append(gcd(pin2,i))
  
lst.sort()
lst2.sort()

print(max(lst[1],lst2[1]))

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from collections import deque

def gcd_list(numbers):
    #https://note.nkmk.me/python-gcd-lcm/
    return reduce(math.gcd, numbers)

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

if n==2:
    print(max(a))
ans = 0
for i in range(n):
    tmp = a_que.popleft()
    ans = max(ans,gcd_list(a_que))
    a_que.append(tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
import numpy as np
import math


n = int(input())
aList = list(map(int, input().split()))
gcdListLeft = [0 for i in range(n)]
gcdListRight = [0 for i in range(n)]

tmp = aList[0]
for i in range(n):
	gcdListLeft[i] = math.gcd(tmp, aList[i])

tmp = aList[n-1]
for i in range(n-1, -1, -1):
	gcdListRight[i] = math.gcd(tmp,aList[i])

ans = gcdListRight[1]
for i in range(1,n-1):
	ans = max(ans, math.gcd(gcdListLeft[i-1], gcdListRight[i+1]))
ans = max(ans, gcdListLeft[n-2])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
def gcd(a,b):
    if(b==0):
        return a;
    return gcd(b,a%b);
mx =0;

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

x = combinations(a,2)
for a,b in x:
    if mx<gcd(a,b):
        mx = gcd(a,b)
print(mx)

Submission status: Wrong Answer

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

ra=list(reversed(a))

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

k1=math.gcd(a[0],a[1])
left.append(k1)
k2=math.gcd(ra[0],ra[1])
right.append(k2)
for i in range(2,n-1):
  k1=math.gcd(k1,a[i])
  left.append(k1)
  k2=math.gcd(k2,ra[i])
  right.append(k2)
right.reverse()
#print(left,right)
ans=[]
ans.append(left[-1])
ans.append(right[0])
for i in range(1,len(left)-1):
  ans.append(math.gcd(left[i-1],right[i+1]))
print(max(ans))

Submission status: Wrong Answer

Solution code:
import math
#n=int(input())
#a=list(map(int,input().split()))
import random
n=random.randrange(2,1000)
a=[random.randrange(2,1000) for i in range(n)]

ra=list(reversed(a))

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

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

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

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