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:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse = True)
ans = 1
for j in range(a[1],1,-1):
  c = list(map(lambda x: x % j,a))
  c_ng = [i for i in c if i > 0]
  #print("j=",j,"c=",c,"c_ng=",c_ng)
  if len(c_ng) <= 1:
    ans = j
    break
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    A[i] = int(A[i])

A_max = max(A)

flag = 0
for term in A:

    count = 0

    for i in range(N):
        if(A[i] % term != 0):
            count += 1
        if(count == 2):
            break

    if(count <= 1):
        flag = 1
        break

if(flag == 1):
    print(term)
else:
    for term in reversed(range(0, A_max)):

        count = 0

        for i in range(N):
            if(A[i] % term != 0):
                count += 1
            if(count == 2):
                break

        if(count <= 1):
            break

    print(term)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 1
for j in range(min(a),1,-1):
  c = list(map(lambda x: x % j,a))
  c_ng = [i for i in c if i > 0]
  #print("j=",j,"c=",c,"c_ng=",c_ng)
  if len(c_ng) <= 1:
    ans = j
    break
print(ans)

Submission status: Wrong Answer

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


a.sort()
min = a[len(a) - 1]
min_index = 0

for i in range(0, (len(a) - 1)):
    if min > a[i + 1] - a[i]:
        min = a[i + 1] - a[i]
        min_index = i

ans = a[0]
for i in range(1, len(a)):
    if i == min_index + 1:
        continue
    ans = fractions.gcd(ans, a[i])
    

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(n):
  b = copy.deepcopy(a)
  b.pop(i)
  for j in range(max(b)+1,2):
    c = list(map(lambda x: x % j,b))
    if sum(c) == 0:
      ans = j
      break
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
ra=a[::-1]
x,y,z=[a[0]],[a[len(a)-1]],[]
for i in range(N-1):
	x.append(fr.gcd(x[i],a[i+1]))
for i in range(N-1):
	y.append(fr.gcd(y[i],ra[i+1]))
z.append(y[N-1])
for i in range(1,N):
	z.append(fr.gcd(x[i-1],y[N-1-i]))
z.append(x[N-1])
print(max(z))

Submission status: Wrong Answer

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
x,y,z=[a[0]],[a[len(a)-1]],[]
for i in range(1,N):
	x.append(fr.gcd(x[i-1],a[i]))
	y.append(fr.gcd(y[i-1],a[N-1-i]))
z.append(y[N-1])
for i in range(1,N):
	z.append(fr.gcd(x[i-1],y[N-1-i]))
z.append(x[N-1])
print(max(z))

Submission status: Wrong Answer

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
x,y,z=[a[0]],[a[len(a)-1]],[]
for i in range(N):
	x.append(fr.gcd(x[i],a[i]))
	y.append(fr.gcd(y[i],a[N-1-i]))
z.append(y[N-1])
for i in range(1,N):
	z.append(fr.gcd(x[i-1],y[N-1-i]))
z.append(x[N-1])
print(max(z))

Submission status: Wrong Answer

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
x,y,z=[a[0]],[a[len(a)-1]],[]
for i in range(N-1):
	x.append(fr.gcd(x[i],a[i+1]))
	y.append(fr.gcd(y[i],a[N-2-i]))
z.append(y[N-1])
for i in range(1,N):
	z.append(fr.gcd(x[i-1],y[N-1-i]))
z.append(x[N-1])
print(max(z))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

count_0 = 0
gcd_0 = A[0]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if count_0 == 0:
            gcd_0 = tmp
            
        count_0 += 1
            
            
    
count_1 = 0
gcd_1 = A[1]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if count_1 == 0:
            gcd_1 = tmp
            
        count_1 += 1

# 入れ替えた数が1つの場合のみ反映
if count_0 != 1 or gcd_0 > 10 ** 9:
    gcd_0 = 1
    
if count_1 != 1or gcd_1 > 10 ** 9:
    gcd_1 = 1
    
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

g_max = 0
for i in range(n-1):
    g = gcd(a[i], a[i+1])
    if g > g_max:
        g_max = g
if n == 3:
    g = gcd(a[0], a[2])
    if g > g_max:
        g_max = g

print(g_max)

Submission status: Wrong Answer

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
x,y,z=[a[0]],[a[len(a)-1]],[]
for i in range(N-1):
	x.append(fr.gcd(x[i],a[i+1]))
	y.append(fr.gcd(y[i],a[N-1-i]))
z.append(y[N-1])
for i in range(N):
	z.append(fr.gcd(x[i],y[N-1-i]))
z.append(x[N-1])
print(max(z))

Submission status: Wrong Answer

Solution code:
import fractions as fr
N=int(input())
a=list(map(int,input().split()))
x,y,z=[a[0]],[a[len(a)-1]],[]
for i in range(N-1):
	x.append(fr.gcd(x[i],a[i+1]))
	y.append(fr.gcd(y[i],a[N-1-i]))
z.append(y[N-1])
for i in range(1,N):
	z.append(fr.gcd(x[i-1],y[N-1-i]))
z.append(x[N-1])
print(max(z))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

count_0 = 0
gcd_0 = A[0]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if count_0 == 0:
            gcd_0 = tmp
            
        count_0 += 1
            
            
    
count_1 = 0
gcd_1 = A[1]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if count_1 == 0:
            gcd_1 = tmp
            
        count_1 += 1
   
if count_0 != 1:
    gcd_0 = 1
if count_1 != 1:
    gcd_1 = 1
    
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

count_0 = 0
gcd_0 = A[0]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if count_0 == 0:
            gcd_0 = tmp
            
        count_0 += 1
            
            
    
count_1 = 0
gcd_1 = A[1]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if count_1 == 0:
            gcd_1 = tmp
            
        count_1 += 1
   
          
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

flag = True
gcd_0 = A[0]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if flag:
            gcd_0 = tmp
            flag = False
            
            
    
flag = True
gcd_1 = A[1]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if flag:
            gcd_1 = tmp
            flag = False
            
          
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
A.sort()
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

flag = False
gcd_0 = A[-1]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_0 = tmp
            
    
flag = False
gcd_1 = A[-2]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_1 = tmp
          
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
A.sort()
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

flag = False
gcd_0 = A[-1]
for i in range(1, N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_0 = tmp
            
    
flag = False
gcd_1 = A[-2]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_1 = tmp
          
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

flag = False
gcd_0 = A[0]
for i in range(1, N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_0 = tmp
            
    
flag = False
gcd_1 = A[1]
for i in range(N):
    if i != 1:
        tmp = gcd_1
        gcd_1 = gcd(gcd_1, A[i])

        if gcd_1 == 1:
            if flag:
                break
            else:
                flag = True
                gcd_1 = tmp
          
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd

A.sort()

flag = False
gcd_0 = A[0]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_0 = tmp
            
    
flag = False
gcd_1 = A[-1]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_1 = tmp
        
    
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

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

# とりあえず初期の整数同士の最大公約数を求める
tmp_gcd = A[0]
for i in range(1, N):
    tmp_gcd = gcd(tmp_gcd, A[i])
    
    
#最大公約数で全ての整数を割る(全て割り切れる)
for i in range(N):
    A[i] = A[i] // tmp_gcd
    
flag = False
gcd_0 = A[0]
for i in range(N):
    tmp = gcd_0
    gcd_0 = gcd(gcd_0, A[i])
    
    if gcd_0 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_0 = tmp
            
    
flag = False
gcd_1 = A[1]
for i in range(N):
    tmp = gcd_1
    gcd_1 = gcd(gcd_1, A[i])
    
    if gcd_1 == 1:
        if flag:
            break
        else:
            flag = True
            gcd_1 = tmp
        
    
print(tmp_gcd * max(gcd_0, gcd_1))

Submission status: Wrong Answer

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

n=0
sm=0
if A[0]>0:
  mn=A[0]
else:
  mn=(-1)*A[0]


for i in range(0,N):
  if A[i]<0:
    n=n+1
    AA=A.pop(i)
    A.insert(i,(-1)*AA)
    sm=sm+AA*(-1)
    if (-1)*AA<mn:
      mn=(-1)*AA
  else:
    sm=sm+A[i]
    if A[i]<mn:
      mn=A[i]

if n%2==0:
  print(sm)
else:
  print(sm-2*mn)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def gcd(a, b):
    if a != None and b == None:
        return a
    elif a == None and b != None:
        return b
    elif a == None and b == None:
        return None
    else:
        if a < b:
            tmp = a
            a = b
            b = tmp
        if b == 0:
            return a
        return gcd(a%b, b)
        

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        # nに最も近い2のべき乗を計算
        while self.n < n:
            self.n *= 2

        # ノードの個数は2n-1個
        self.dat = [None] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

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

    def __get_value(self, a, b, k, l, r):
        # 区間が[0, 1)みたいな感じで指定された時[0]を返すようにする
        if a == b-1:
            return self.dat[a + self.n - 1]

        if r < a or b < l:
            return None
        elif a <= l and r <= b:
            return self.dat[k]
        else:
            # 2*k+1->左の子ノード, 2*k+2->右の子ノード
            vl = self.__get_value(a, b, 2*k+1, l, (l+r)//2)
            vr = self.__get_value(a, b, 2*k+2, (l+r)//2+1, r)
            return gcd(vl, vr)

    def get_value(self, a, b):
        return self.__get_value(a, b, 0, 0, self.n-1)

if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(N):
        if i == 0:
            leftv = None
        else:
            leftv = st.get_value(0, i)
        
        if i == N-1:
            rightv = None
        else:
            rightv = st.get_value(i+1, N-1)

        # for Debug    
        #print("***{0} | {1} {2}".format(i, leftv, rightv))
        ans = max(ans, gcd(leftv, rightv)) 

    # for Debug
    #print(st.dat)

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def gcd(a, b):
    if a != None and b == None:
        return a
    elif a == None and b != None:
        return b
    elif a == None and b == None:
        return None
    else:
        if a < b:
            tmp = a
            a = b
            b = tmp
        if b == 0:
            return a
        return gcd(a%b, b)
        

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        # nに最も近い2のべき乗を計算
        while self.n < n:
            self.n *= 2

        # ノードの個数は2n-1個
        self.dat = [None] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

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

    def __get_value(self, a, b, k, l, r):
        # 区間が[0, 1)みたいな感じで指定された時[0]を返すようにする
        if a == b-1:
            return self.dat[a + self.n - 1]

        if r < a or b < l:
            return None
        elif a <= l and r <= b:
            return self.dat[k]
        else:
            # 2*k+1->左の子ノード, 2*k+2->右の子ノード
            vl = self.__get_value(a, b, 2*k+1, l, (l+r)//2)
            vr = self.__get_value(a, b, 2*k+2, (l+r)//2+1, r)
            return gcd(vl, vr)

    def get_value(self, a, b):
        return self.__get_value(a, b, 0, 0, self.n-1)

if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(N):
        if i == 0:
            leftv = None
        else:
            leftv = st.get_value(0, i)
        
        if i == N-1:
            rightv = None
        else:
            rightv = st.get_value(i+1, N)

        # for Debug    
        #print("***{0} | {1} {2}".format(i, leftv, rightv))
        ans = max(ans, gcd(leftv, rightv)) 

    # for Debug
    #print(st.dat)

    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
import copy

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
  
n = int(input())
A = list(int(i) for i in (input().split()))

front = []
end = []
for i in range(n):
  if(i == 0):
    front.append(gcd(*[A[0]]))
    end.append(gcd(*[A[n-1]]))
  else:
    front.append(gcd(*[front[i - 1],A[i]]))
    end.append(gcd(*[end[i - 1],A[n-1-i]]))

ans = 0
for i in range(n):
  if(i == 0):
    tmp = end[n-2]
  elif(i == n-1):
    tmp = front[n-2]
  else:
    tmp = gcd(*[front[i-1],end[n-i]])
  if(ans < tmp):
    ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
a.sort()
a=a[::-1]
res=[a[0]]
for i in range(1,n):
    t=res[0]
    x=gcd(t,a[i])
    if x!=t:
        res.append(x)
    if len(res)>1:
        t=res[1]
        x=gcd(t,a[i])
        if x!=t:
            res.append(x)
    res.sort()
    res=res[::-1]
    res=res[:2]
if len(res)>1:
    print(res[1])
else:
    print(res[0])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
a.sort()
a=a[::-1]
res=[a[0]]
for i in range(1,n):
    t=res[0]
    x=gcd(t,a[i])
    if x!=t:
        res.append(x)
    if len(res)>1:
        t=res[1]
        x=gcd(t,a[i])
        if x!=t:
            res.append(x)
    res.sort()
    res=res[:2]
if len(res)>1:
    print(res[1])
else:
    print(res[0])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
t=a[0]
for i in range(1,n):
    t=gcd(t,a[i])
print(t)

Submission status: Wrong Answer

Solution code:
def gcd(n,m):
  a=max(n,m)
  b=min(n,m)
  while b!=0:
    a,b=b,a%b
  return a
n=int(input())
l=list(map(int,input().split()))
l1=[]
l1.append(l[0])
for i in range(1,n):
  l1.append(gcd(l1[-1],l[i]))
l2=[]
l2.append(l[-1])
for i in range(n-2,-1,-1):
  l2.append(gcd(l2[-1],l[i]))
l3=[]
for i in range(2,n-1):
  l3.append(gcd(l1[i-2],l2[-(i+1)]))
l3.append(l2[-n+1])
l3.append(l1[n-2])
print(max(l3))

Submission status: Wrong Answer

Solution code:
import fractions
import functools
n=int(input())
a=list(map(int,input().split()))
b=[0]*(n-1)
for i in range(n-1):
  b[i]=fractions.gcd(a[i],a[i+1])
m=min(b)
cnt=b.count(m)
if cnt==1:
  idx_1=b.index(m)
  idx_2=b.index(m)+1
  z=a[idx_1]
  del a[idx_1]
  x=functools.reduce(fractions.gcd,a)
  a.insert(idx_1,z)
  del a[idx_2]
  y=functools.reduce(fractions.gcd,a)
  print(max(x,y))  
else:
  print(m)

Submission status: Wrong Answer

Solution code:
import fractions as frc

def g_c_d(x,y):
    if x == 0 and y != 0:
        return y
    elif x != 0 and y == 0:
        return x
    else:
        return frc.gcd(x,y)


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


if n == 2:
    print(max(a))
else:
    ls_front = [0] + [frc.gcd(a[0],a[1])] +[0]*(n-1)
    ls_back = [0]*(n-1) + [frc.gcd(a[n-1],a[n-2])]+[0]
    ls = []


    for i in range(n):
        ls_front[i+1] = g_c_d(ls_front[i],a[i])
        ls_back[n-1-i] = g_c_d(ls_back[i+1],a[n-1-i])
    #reversed(ls_back)
    #print(ls_back)
    #ls_back = ls_back[1:] + [0]
    #print(ls_front)
    #print(ls_back)
    #print(ls_front)
    #print(ls_back)
    for i in range(n-1):
        ls.append(frc.gcd(ls_front[i],ls_back[i+1]))
    #print(ls)
    print(max(ls[1:]))
    #print(ls)
    #print(ls_front)
    #print(ls_back)


"""
ls = [frc.gcd(a[0],a[1])]
#for i in range(n):
ans = []
if n == 2:
    print(max(a))
else:
    for i in range(1,n):
        new_ls = a[:i-1]+a[i:]
        if i == 1:
            m = frc.gcd(a[1],a[2])
        elif i == 2:
            m = frc.gcd(a[0],a[2])
        else:
            m = frc.gcd(a[0],a[1])

        for j in range(1,n-1):
            ans.append(frc.gcd(new_ls[j],m))

    print(max(ans))

"""

Submission status: Wrong Answer

Solution code:
N = int(input())
A_arr=input().split()

for i in range(N):
    A_arr[i] = int(A_arr[i])
    
A_arr.sort(reverse=True)
_A_arr = [A_arr[i] for i in range(N)]


# import math
# from functools import reduce

# def gcd_list(numbers):
    # return reduce(math.gcd, numbers)

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


def gcd_l(x):
    if len(x)==1:
        return x[0]
    else:
        element = gcd(x[0], x[1])
        if len(x)==2:
            return element
        else:
            for i in range(2,len(x)):
                element=gcd(element,x[i])
            return element

ans = 0
times = 0

for i in range(N):
    _A_arr.pop(i)
    now_n = gcd_l(_A_arr)
    if(now_n > ans):
        ans = now_n
    elif(now_n == ans):
        times += 1
        
    if(times == 2):
        break
    
    _A_arr = [A_arr[i] for i in range(N)]
    
    

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A_arr=input().split()

for i in range(N):
    A_arr[i] = int(A_arr[i])
    
A_arr.sort(reverse=True)
_A_arr = [A_arr[i] for i in range(N)]


# import math
# from functools import reduce

# def gcd_list(numbers):
    # return reduce(math.gcd, numbers)

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


def gcd_l(x):
    if len(x)==1:
        return x[0]
    else:
        element = gcd(x[0], x[1])
        if len(x)==2:
            return element
        else:
            for i in range(2,len(x)):
                element=gcd(element,x[i])
            return element

ans = 0
times = 0

for i in range(N):
    _A_arr.pop(i)
    now_n = gcd_l(_A_arr)
    if(now_n > ans):
        ans = now_n
    elif(now_n == ans):
        times += 1
        
    if(times == 1):
        break
    
    _A_arr = [A_arr[i] for i in range(N)]
    
    

print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n = int(input())
a_array = sorted([int(x) for x in input().split()])
gcd_array = []
gcd_reverse_array = []
tmp = a_array[0]
tmp_reverse = a_array[-1]
for i, j in zip(range(n), reversed(range(n))):
    i = a_array[i]
    j = a_array[j]
    tmp = gcd(tmp, i)
    tmp_reverse = gcd(tmp_reverse, j)
    gcd_array.append(tmp)
    gcd_reverse_array.append(tmp_reverse)

gcd_reverse_array = gcd_reverse_array
res = 0
for index in range(n):
    if index == 0:
        tmp = gcd_reverse_array[-2]
    elif index == n-2:
        tmp = gcd_array[-2]
    else:
        tmp = gcd(gcd_array[index-2], gcd_reverse_array[index-2])
    res = max(res, tmp)
print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import sys
# sys.setrecursionlimit(10**4)

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


def gcd(a, b):
    """ Greatest Common Divisor(最大公約数) """
    while b:
        a, b = b, a % b
    return a


N = d_in()
A_list = ds_in()

cum_gcd = [A_list[0]]
for a in A_list[1:]:
    pre = cum_gcd[-1]
    cum_gcd.append(gcd(a, pre))

reverse = [A_list[-1]]
for a in A_list[-2::-1]:
    pre = reverse[-1]
    reverse.append(gcd(a, pre))

ans = max(reverse[1], cum_gcd[-2])
for i in range(1, N-1):
    # i番目を抜かす
    ans = max(ans, gcd(cum_gcd[i-1], reverse[N-2-i]))

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions as frc

def g_c_d(x,y):
    if x == 0 and y != 0:
        return y
    elif x != 0 and y == 0:
        return x
    else:
        return frc.gcd(x,y)


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


if n == 2:
    print(max(a))
else:
    ls_front = [0] + [frc.gcd(a[0],a[1])] +[0]*(n-1)
    ls_back = [0]*(n-1) + [frc.gcd(a[n-1],a[n-2])]+[0]
    ls = []


    for i in range(n):
        ls_front[i+1] = g_c_d(ls_front[i],a[i])
        ls_back[i] = g_c_d(ls_back[i+1],a[i])
    #reversed(ls_back)
    #print(ls_back)
    #ls_back = ls_back[1:] + [0]
    #print(ls_front)
    #print(ls_back)
    #print(ls_front)
    #print(ls_back)
    for i in range(n-1):
        ls.append(g_c_d(ls_front[i],ls_back[i+1]))
    #print(ls)
    print(max(ls))
    #print(ls)
    #print(ls_front)
    #print(ls_back)


"""
ls = [frc.gcd(a[0],a[1])]
#for i in range(n):
ans = []
if n == 2:
    print(max(a))
else:
    for i in range(1,n):
        new_ls = a[:i-1]+a[i:]
        if i == 1:
            m = frc.gcd(a[1],a[2])
        elif i == 2:
            m = frc.gcd(a[0],a[2])
        else:
            m = frc.gcd(a[0],a[1])

        for j in range(1,n-1):
            ans.append(frc.gcd(new_ls[j],m))

    print(max(ans))

"""

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a  
lgcds = [A[0]]
rgcds = [A[N-1]]
for n in range(N-2):
    lgcds.append(gcd(lgcds[-1],A[n+1]))
    rgcds.append(gcd(rgcds[-1],A[-2-n]))
gcds = [lgcds[-1],rgcds[-1]]
for i in range(N-2):
    gcds.append(gcd(lgcds[i],rgcds[-2-i]))
print(gcds)

Submission status: Wrong Answer

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

L, R = [], []
for i in range(N):
  print(i)
  L.append(gcd(L[i-1],A[i-1]) if i else 0)
  R.append(gcd(R[i-1],A[N-i]) if i else 0)
print(max([gcd(L[i],R[N-i-1]) for i in range(N)]))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
g=[]
l=[a[0]]
r=[a[n-1]]
for i in range(1,n):
    l.append(gcd(l[i-1], a[i]))
    r.append(gcd(r[i-1], a[n-i-1]))
r.reverse()
g.append(r[1])
for i in range(n-2):
    g.append(gcd(l[i], r[n-i-1]))
g.append(l[n-2])
print(max(g))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
g=[]
l=[a[0]]
r=[a[n-1]]
for i in range(1,n):
    l.append(gcd(l[i-1], a[i]))
    r.append(gcd(r[i-1], a[n-i-1]))
r.reverse()
g.append(r[1])
for i in range(n-1):
    g.append(gcd(l[i], r[n-i-1]))
print(max(g))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import fractions

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        # nに最も近い2のべき乗を計算
        while self.n < n:
            self.n *= 2

        # ノードの個数は2n-1個
        self.dat = [0] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

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

    def __get_value(self, a, b, k, l, r):
        if r < a or b <= l:
            return 0
        elif a <= l and r <= b:
            return self.dat[k]
        else:
            vl = self.__get_value(a, b, 2*k+1, l, (l+r)//2)
            vr = self.__get_value(a, b, 2*k+2, (l+r)//2+1, r)
            return fractions.gcd(vl, vr)

    def get_value(self, a, b):
        return self.__get_value(a, b, 0, 0, self.n-1)

if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(0, N):
        M_i = fractions.gcd(st.get_value(0, i), st.get_value(i+1, N))
        ans = max(ans, M_i) 

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import fractions

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        # nに最も近い2のべき乗を計算
        while self.n < n:
            self.n *= 2

        # ノードの個数は2n-1個
        self.dat = [0] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

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

    def __get_value(self, a, b, k, l, r):
        if r <= a or b <= l:
            return 0
        elif a <= l and r <= b:
            return self.dat[k]
        else:
            vl = self.__get_value(a, b, 2*k+1, l, (l+r)//2)
            vr = self.__get_value(a, b, 2*k+2, (l+r)//2+1, r)
            return fractions.gcd(vl, vr)

    def get_value(self, a, b):
        return self.__get_value(a, b, 0, 0, self.n-1)

if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(0, N):
        M_i = fractions.gcd(st.get_value(0, i), st.get_value(i+1, N))
        ans = max(ans, M_i) 

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import fractions

class SegmentTree():

    def __init__(self, n):
        self.n = 1
        # nに最も近い2のべき乗を計算
        while self.n < n:
            self.n *= 2

        # ノードの個数は2n-1個
        self.dat = [0] * (self.n * 2 - 1)
        
    def update(self, i, x):
        i = i + self.n - 1
        self.dat[i] = x

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

    def __get_value(self, a, b, k, l, r):
        if r < a or b < l:
            return 0
        elif a <= l and r <= b:
            return self.dat[k]
        else:
            vl = self.__get_value(a, b, 2*k+1, l, (l+r)//2)
            vr = self.__get_value(a, b, 2*k+2, (l+r)//2+1, r)
            return fractions.gcd(vl, vr)

    def get_value(self, a, b):
        return self.__get_value(a, b, 0, 0, self.n-1)

if __name__ == "__main__":
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    st = SegmentTree(N)
    for i in range(N):
        st.update(i, A[i])
    
    ans = - (10 ** 10)
    for i in range(0, N):
        M_i = fractions.gcd(st.get_value(0, i), st.get_value(i+1, N))
        ans = max(ans, M_i) 

    print(ans)