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: Runtime Error

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, end="")


N=_I()
A=LI()

if N==2:
	print(max(A))
	exit()
	

a=math.gcd(A[0],A[1])
b=math.gcd(A[0],A[2])
c=math.gcd(A[1],A[2])
print(max(a,b,c))

Submission status: Runtime Error

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

from math import gcd

if N == 2:
    maxL = max(A[0], A[1])
else:
    maxL = 0
    for n in range(N):
        AA = A[:n]+A[n+1:]
        minGCD = min( [ gcd(AA[i], AA[i+1]) for i in range(len(AA)-1)] )
        #print(AA, minGCD)
        maxL = max(maxL, minGCD )
            
print(maxL )

Submission status: Runtime Error

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

from math import gcd

if N == 2:
    maxL = max(A[0], A[1])
else:
    maxL = 0
    for n in range(N):
        AA = A[:n]+A[n+1:]
        #print( AA, gcd(AA[0], AA[1]) )
        L = gcd(AA[0], AA[1])
        maxL = max(maxL, gcd(AA[0], AA[1]))
        for l in range(1, len(AA)):
            maxL = max(maxL, gcd(L, AA[l]))

print(maxL )

Submission status: Runtime Error

Solution code:
import fractions
N = int(input())
A = list(map(int,input().split()))
hard = fractions.gcd(A[0],A[1])
easy = [A[0],A[1]]
del A[0],A[1]
while len(A) != 0:
  easyt1 = fractions.gcd(easy[0],A[0])
  easyt2 = fractions.gcd(easy[1],A[0])
  easyt3 = hard
  easy = sorted([easyt1,easyt2,easyt3])[1:3]
  hard = fractions.gcd(hard,A[0])
  del A[0]
print(max(easy))

Submission status: Runtime Error

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

# Common divisor
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
    exit()
if N == 3:
    x1 = gcd_wrapper(A[0], A[1])
    x2 = gcd_wrapper(A[1], A[2])
    x3 = gcd_wrapper(A[0], A[2])
    print(max([x1, x2, x3]))
    exit()

init_results = []
for k, g in enumerate(A[0:3]):
    for h in A[k+1:4]:
        init_results.append(gcd_wrapper(g, h))
if max(init_results) == 1:
    print(1)
    exit()
stop_flag = 0
x = max(init_results)
final_result = set()
final_result.add(x)
for i in A:
    tmp = gcd_wrapper(x, i)
    if tmp == 1:
        stop_flag += 1
    else:
        x = tmp
        final_result.add(tmp)
if stop_flag >= 2:
    print(1)
else:
    sorted_final_result = sorted(list(final_result))
    divable = []
    for j in A:
        if j % sorted_final_result[1] != 0:
            divable.append(1)
    if sum(divable) > 1:
        print(sorted_final_result[0])
    else:
        print(sorted_final_result[1])

Submission status: Runtime Error

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

# Common divisor
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
    exit()
if N == 3:
    x1 = gcd_wrapper(A[0], A[1])
    x2 = gcd_wrapper(A[1], A[2])
    x3 = gcd_wrapper(A[0], A[2])
    print(max([x1, x2, x3]))
    exit()

init_results = []
for k, g in enumerate(A[0:3]):
    for h in A[k+1:4]:
        init_results.append(gcd_wrapper(g, h))
if max(init_results) == 1:
    print(1)
    exit()
stop_flag = 0
x = max(init_results)
final_result = set()
final_result.add(x)
for i in A:
    tmp = gcd_wrapper(x, i)
    if tmp == 1:
        stop_flag += 1
    else:
        x = tmp
        final_result.add(tmp)
if stop_flag >= 2:
    print(1)
else:
    sorted_final_result = sorted(list(final_result))
    divable = []
    for j in A:
        if j % sorted_final_result[1] != 0:
            divable.append(1)
    if sum(divable) > 1:
        print(sorted_final_result[1])
    else:
        print(sorted_final_result[0])

Submission status: Runtime Error

Solution code:
 def gcd(x, y):
    a = max(x, y)
    b = min(x, y)
    if b == 0:
        return a
    remainder = a % b
    if remainder == 0:
        return b
    return gcd(b, remainder)


N = int(input())
integers = [int(x) for x in input().split()]

left = [0]
right = [0]

for i in range(N):
    left.append(gcd(left[-1], integers[i]))
    right.append(gcd(right[-1], integers[-i-1]))

# left[0] = 0
# right[0] = 0
right.reverse()

lr = []
for i in range(N-1):
    lr.append(gcd(left[i+1], right[i+2]))

print(max(lr))

Submission status: Runtime Error

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

# Common divisor
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
    exit()
if N == 3:
    x1 = gcd_wrapper(A[0], A[1])
    x2 = gcd_wrapper(A[1], A[2])
    x3 = gcd_wrapper(A[0], A[2])
    print(max([x1, x2, x3]))
    exit()

init_results = []
for k, g in enumerate(A[0:3]):
    for h in A[k+1:4]:
        init_results.append(gcd_wrapper(g, h))
if max(init_results) == 1:
    print(1)
    exit()
stop_flag = 0
x = max(init_results)
final_result = set()
final_result.add(x)
for i in A:
    tmp = gcd_wrapper(x, i)
    if tmp == 1:
        stop_flag += 1
    else:
        x = tmp
        final_result.add(tmp)
if stop_flag >= 2:
    print(1)
else:
    sorted_final_result = sorted(list(final_result))
    divable = []
    for j in A:
        if j % sorted_final_result[1] != 0:
            divable.append(1)
    if sum(divable) == 1:
        print(sorted_final_result[1])
    else:
        print(sorted_final_result[0])

Submission status: Runtime Error

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

# Common divisor
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
    exit()
if N == 3:
    x1 = gcd_wrapper(A[0], A[1])
    x2 = gcd_wrapper(A[1], A[2])
    x3 = gcd_wrapper(A[0], A[2])
    print(max([x1, x2, x3]))
    exit()

init_results = []
for g in A[0:4]:
    for h in A[g:4]:
        init_results.append(gcd_wrapper(g, h))
if max(init_results) == 1:
    print(1)
    exit()
stop_flag = 0
x = max(init_results)
for i in A:
    tmp = gcd_wrapper(x, i)
    if tmp == 1:
        stop_flag =+ 1
    else:
        x = tmp
if stop_flag >= 2:
    print(1)
else:
    print(x)

Submission status: Runtime Error

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

    if(a < b):
        a, b = b, a
    r = a % b
    while(r != 0):
        a, b = b, r
        r = a % b
    return b

def L(i, A):
    if(i == 0):
        return 0
    return gcd(L(i - 1, A), A[i])

def R(i, A, N):
    if(i == N):
        return 0
    return gcd(R(i + 1, A, N), A[i])

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

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

print(max(M))

Submission status: Runtime Error

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

    if(a < b):
        a, b = b, a
    r = a % b
    while(r != 0):
        a, b = b, r
        r = a % b
    return b

def L(i, A):
    if(i == 0):
        return 0
    return gcd(L(i - 1, A), A[i - 1])

def R(i, A, N):
    if(i == N):
        return 0
    return gcd(R(i + 1, A, N), A[i])

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

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

print(max(M))

Submission status: Runtime Error

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

def GCD(L):
  while(len(L)>1):
    a=min(L)
    M=map(lambda x: x%a, L)
    L=[a]+list(filter(lambda x: x!=0, M))
  return a

cogcd=[ 0 for i in range(N)]

for i in range(N):
  cogcd[i]=GCD(A[:i]+A[i+1:])

print(max(cogcd))

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=[]
tmp=0
sumx=0
val=[]
A = list(map(int, input().split()))

for i in range(0,N):
    for j in range(0,N):
        if(i!=j):
            val.append(math.gcd(A[i],A[j]))
print(max(val))

Submission status: Runtime Error

Solution code:
import functools
import math

n = int(input())
a = [int(i) for i in input().split()]
l = []
for i in range(n):
 b = a[i]
 del a[i]
 l.append(functools.reduce(math.gcd, a))
 a.insert(i,b)
print(max(l))

Submission status: Runtime Error

Solution code:
import math
from  functools import  reduce

n = int(input())
li = list(map(int,input().split()))
kotae = 1

for i in range(n):
	lis = li[:i]+li[i+1:]
	g = reduce(math.gcd,lis)
	kotae = max(kotae,g)
	
print(kotae)

Submission status: Runtime Error

Solution code:
from sys import exit

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


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

    if b == 0:
        return a

    temp = a % b
    while temp != 0:
        a = b
        b = temp
        temp = a % b

    return b


# 0 ~ i
def left(i):
    if i == 0:
        return 0

    if left(i - 1) > A[i - 1]:
        return gcd(left(i - 1), A[i - 1])
    else:
        return gcd(A[i - 1], left(i - 1))


# i ~ N-1
def right(i):
    if i == N - 1:
        return 0

    if right(i + 1) > A[i + 1]:
        return gcd(right(i + 1), A[i + 1])
    else:
        return gcd(A[i + 1], right(i + 1))


ans = []

for i in range(N):
    ans.append(gcd(left(i), right(i)))

print(max(ans))

Submission status: Runtime Error

Solution code:
from sys import exit

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


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

    if b == 0:
        return a

    temp = a % b
    while temp != 0:
        a = b
        b = temp
        temp = a % b

    return b


# 0 ~ i
def left(i):
    if i == 0:
        return 0

    return gcd(left(i - 1), A[i - 1])


# i ~ N-1
def right(i):
    if i == N - 1:
        return 0

    return gcd(right(i + 1), A[i + 1])


ans = []

for i in range(N):
    ans.append(gcd(left(i), right(i)))

print(max(ans))

Submission status: Runtime Error

Solution code:
import sys
if sys.version_info.minor >= 5:
    from math import gcd
else:
    from fractions import gcd

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

left_gcds = [gcd(As[0], As[0])]
for i in range(1, len(As)):
    left_gcds.append(gcd(left_gcds[i-1], As[i]))

right_gcds = [gcd(As[-1], As[-1])]
for i in reversed(range(len(As)-1)):
    right_gcds.append(gcd(right_gcds[i-1], As[i]))
right_gcds.reverse()

gcds = []
for i in range(N):
    if i == 0:
        gcds.append(right_gcds[1])
    elif i == N-1:
        gcds.append(left_gcds[-2])
    else:
        gcds.append(gcd(left_gcds[i-1], right_gcds[i+1]))
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
v = [int(i) for i in input().split()]
if n == 2:
    print(max(v))


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


l = [0 for i in range(n)]
Y = [0 for i in range(n)]

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

Submission status: Runtime Error

Solution code:
import math
n = int(input())
v = [int(i) for i in input().split()]
if n == 2:
    print(max(v))

l = [0 for i in range(n)]
r = l[:]


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


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

Submission status: Runtime Error

Solution code:
N=int(input())
A= [int(a) for a in input().split()]
maxg=1
if N == 2:
    maxg=max(A)
else:
    for n in range(N):
        Apop=copy.copy(A)
        Apop.pop(n)
        gd=Apop[0]
        for inn in range(N-2):
            gd=gcd(gd,Apop[inn+1])
        if maxg<gd:
            maxg=gd
print(maxg)

Submission status: Runtime Error

Solution code:
from math import gcd

n = int(input())
num = list(map(int, input().split(' ')))

def find_max(idx):
    
    prev_gcd = gcd(num[idx[0]], num[idx[1]])
    curr_excluded_num = None
    curr_gcd = None

    for i in range(n):
        if i == idx[0] or i == idx[1]:
            continue
        
        curr_gcd = gcd(prev_gcd, num[i])
        if curr_gcd == prev_gcd:
            continue
        else:
            if curr_excluded_num is None:
                curr_excluded_num = num[i]
            else:
                if curr_gcd > gcd(prev_gcd, curr_excluded_num):
                    prev_gcd = curr_gcd
                else:
                    prev_gcd = gcd(prev_gcd, curr_excluded_num)
                    curr_excluded_num = num[i]

    
    return prev_gcd



if n == 2:
    print(max(num[0], num[1]))
elif n == 3:
    a = gcd(num[0], num[1])
    b = gcd(num[0], num[2])
    c = gcd(num[2], num[1])
    print(max(max(a, b), c))

else:
    ans = find_max((0, 1))
    ans = max(find_max((2, 3)), ans)
    print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import math
import random



def solve(N, A):
    left = [A[0]]
    right = [A[N-1]]
    for i in range(1, N):
        left.append(math.gcd(left[i-1], A[i]))
    for i,j in zip(range(1,N),range(N-2, -1, -1)):
        right.append(math.gcd(right[i-1], A[j]))

    result = -1
    max_value = -1

    for i in range(N):
        if i == 0:
            current_value = right[N-2]
        elif i == N-1:
            current_value = left[N-2]
        else:
            current_value = math.gcd(left[i-1], right[N-i-2])
        if current_value > max_value:
            max_value = current_value
    print(max_value)

# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

import math
import copy
from functools import reduce

def gcd(numbers):
    return reduce(math.gcd, numbers)
N=int(input())
A= [int(a) for a in input().split()]
maxg=1
if N == 2:
    maxg=max(A)
else:
    for n in range(N):
        Apop=copy.copy(A)
        Apop.pop(n)
        if maxg<gcd(Apop):
            maxg=gcd(Apop)
print(maxg)

Submission status: Runtime Error

Solution code:

def gcd(numbers):
    return reduce(math.gcd, numbers)
N=int(input())
A= [int(a) for a in input().split()]
maxg=1
if N == 2:
    maxg=max(A)
else:
    for n in range(N):
        Apop=copy.copy(A)
        Apop.pop(n)
        if maxg<gcd(Apop):
            maxg=gcd(Apop)
print(maxg)

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
def gcd(a,b):
  if a*b==0:
    return a if b==0 else b
  else:
    while b:
      a,b=b,a%b
  return a

B=[]
B[0]=0
C=[]
C[0]=0
for i in range(n-1):
  B[i+1]=gcd(B[i],A[i])
  C[i+1]=gcd(C[i],A[n-i-1])
po=0
for i in range(n):
  po=max(po,B[i]*C[n-1-i])
print(po)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 1
a0 = a[0]
a1 = a[1]
r = 0
while True:
  r = a1 % a0
  if r == 0:
    b0 = a0
    break
  elif r > a0:
    a1, a0 = r, a0
  else:
    a1, a0 = a0, r
a1 = a[1]
a2 = a[2]
r = 0
while True:
  r = a2 % a1
  if r == 0:
    b1 = a1
    break
  elif r > a1:
    a2, a1 = r, a1
  else:
    a2, a1 = a1, r
a0 = a[0]
a2 = a[2]
r = 0
while True:
  r = a2 % a0
  if r == 0:
    b2 = a0
    break
  elif r > a0:
    a2, a0 = r, a0
  else:
    a2, a0 = a0, r
max_a = max(b0,b1,b2)
for j in range(max_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: Runtime Error

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

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

max = A.max

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, 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: Runtime Error

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

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

A.sort()
A.reverse()

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[-1])):

        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: Runtime Error

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

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

A.sort()
A.reverse()

max = A[0]
min = A[-1]

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)

for term in reversed(range(0, A[-1])):

    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: Runtime Error

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

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

X = [0] * (n + 1)
Y = [0] * (n + 1)

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

gcds = []

for i in range(n+1):
  gcds.append(gcd(X[i], Y[i+1]))

print(max(gcds))

Submission status: Runtime Error

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

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

X = [0] * (n + 1)
Y = [0] * (n + 1)

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

gcds = []

for i in range(n+1):
  gcds.append(gcd(X[i], Y[i+1]))

print(max(gcds))

Submission status: Runtime Error

Solution code:
import fraction
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 = fraction.gcd(ans, a[i])
    

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
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 = math.gcd(ans, a[i])
    

print(ans)

Submission status: Runtime Error

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]))
a=reversed(a)
for i in range(N-1):
	y.append(fr.gcd(y[i],a[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: Runtime Error

Solution code:
from math import gcd

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: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

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

firstgcds = [0]
lastgcds = [0]
for i in range(0, N):
    firstgcds.append(math.gcd(firstgcds[i], A[i]))
    lastgcds.append(math.gcd(lastgcds[i], A[-i-1]))
ans = 0
for i in range(0, N):
    preans = math.gcd(firstgcds[i], lastgcds[-i-2])
    if ans < preans:
        ans = preans
if ans < firstgcds[N-1]:
    ans = firstgcds[N-1]
print(ans)

Submission status: Runtime Error

Solution code:
import fractions

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

left_gcds = [fractions.gcd(As[0], As[0])]
for i in range(1, len(As)):
    left_gcds.append(fractions.gcd(left_gcds[i-1], As[i]))

right_gcds = [fractions.gcd(As[-1], As[-1])]
for i in reversed(range(len(As)-1)):
    right_gcds.append(fractions.gcd(right_gcds[i-1], As[i]))
right_gcds.reverse()

gcds = []
for i in range(N):
    if i == 0:
        gcds.append(right_gcds[1])
    elif i == N-1:
        gcds.append(left_gcds[-2])
    else:
        gcds.append(fractions.gcd(left_gcds[i-1], right_gcds[i+1]))
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math

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

left_gcds = [math.gcd(As[0], As[0])]
for i in range(1, len(As)):
    left_gcds.append(math.gcd(left_gcds[i-1], As[i]))

right_gcds = [math.gcd(As[-1], As[-1])]
for i in reversed(range(len(As)-1)):
    right_gcds.append(math.gcd(right_gcds[i-1], As[i]))
right_gcds.reverse()

gcds = []
for i in range(N):
    if i == 0:
        gcds.append(right_gcds[1])
    elif i == N-1:
        gcds.append(left_gcds[-2])
    else:
        gcds.append(math.gcd(left_gcds[i-1], right_gcds[i+1]))
print(max(gcds))

Submission status: Runtime Error

Solution code:
import math
import functools

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


def gcds(list_):
    return functools.reduce(math.gcd, list_)


def drop(list_, i):
    list_copy = list_.copy()
    del list_copy[i]
    return list_copy


print(max(gcds(drop(as_, i)) for i, _ in enumerate(as_)))

Submission status: Runtime Error

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):
    for t in res:
        x=gcd(t,a[i])
        if x!=t:
            res.append(x)
res.sort()
print(res[1])

Submission status: Runtime Error

Solution code:
#import math
from fractions import gcd

N = int(input())
As = [int(elm) for elm in input().split(" ")] 

if N >= 3:
    L = []
    R = []

    gl = As[0]
    gr = As[N-1]
    L.append(gl)
    R.append(gr)

    for i in range(1, N-1):
        gl = gcd(gl, As[i]) 
        L.append(gl)

    for i in range(1, N-1):
        gr = gcd(gr, As[N-i-1])
        R.append(gr)

    for i in range(N):
        if i == 0:
            max_g = R[N-2]
        else:
            if i == N-1:
                cur_g = L[N-2]
            else:
                cur_g = math.gcd(L[i-1], R[N-2-i])
            max_g = max(max_g, cur_g)

    out = max_g
else:
    out = max(As)

print(out)

Submission status: Runtime Error

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
b=[a[0]]*(n-1)
c=[a[n-1]]*(n-1)
for i in range(n-1):
  b[i+1]=min(b[i],fractions.gcd(a[i],a[i+1]))
  c[n-i-2]=min(c[n-i-1],fractions.gcd(a[n-i-1],a[n-i-2]))
x=1
for j in range(n-1):
  if j==0:
    print(c[1])
  else:
    y=fractions.gcd(b[j-1],c[j+1])
    if x<y:
      x=y
    else:
      x=x
print(x)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
As = [int(elm) for elm in input().split(" ")] 

if N >= 3:
    L = []
    R = []

    gl = As[0]
    gr = As[N-1]
    L.append(gl)
    R.append(gr)

    for i in range(1, N-1):
        gl = math.gcd(gl, As[i]) 
        L.append(gl)

    for i in range(1, N-1):
        gr = math.gcd(gr, As[N-i-1])
        R.append(gr)

    for i in range(N):
        if i == 0:
            max_g = R[N-2]
        else:
            if i == N-1:
                cur_g = L[N-2]
            else:
                cur_g = math.gcd(L[i-1], R[N-2-i])
            max_g = max(max_g, cur_g)

    out = max_g
else:
    out = max(As)

print(out)

Submission status: Runtime Error

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 z
  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: Runtime Error

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):
    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 > 3):
        break
    
    _A_arr = [A_arr[i] for i in range(N)]
    
    

print(ans)

Submission status: Runtime Error

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=a.index(b.index(m))
  idx_2=a.index(b.index(m)+1)
  z=a[idx_1]
  del z
  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: Runtime Error

Solution code:
def GCDonBlack(N,A):
    #count = 0
    rem=[a%min(A) for a in A]
    if sum(rem) == 0:
        return min(A)
    else:
        for i in reversed(range(1,max(rem)+1)):
            count += 1
            bar = N-1
            for ele in A:
                if ele % i == 0:
                    bar -= 1
                if bar == 0:
                    #print("count:{}".format(count))
                    return i
N = int(input())
A = list(map(int, input().split()))
print(GCDonBlack(N,A))

Submission status: Runtime Error

Solution code:
import math

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

l = [0]*(n+2) # l(i) = A1, A2, ..., A(i-1)のgcd
r = [0]*(n+2) # r(i) = Ai, A(i+1), ..., A(n)のgcd

for i in range(n):
	l[i+1] = math.gcd(l[i],a[i])
for i in reversed(range(n)):
	r[i] = math.gcd(r[i+1],a[i])

ext = [0] * n
for i in range(n):
	ext[i] = math.gcd(l[i],r[i+1])

print (max(ext))

Submission status: Runtime Error

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)
c=b.count(m)
if c==1:
  idx=b.index(m)
  del b[idx]
  print(min(b))
else:
  print(min(b))

Submission status: Runtime Error

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):
  c=[a[i],a[i+1]]
  b[i]=fractions.gcd(c)
m=min(b)
c=b.count(m)
if c==1:
  idx=b.index(m)
  del b[idx]
  print(min(b))
else:
  print(min(b))

Submission status: Runtime Error

Solution code:
#GCD blackboard

import  math
from functools import reduce

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

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

for i in range(N):
    B = A.copy()
    B.pop(i)
    GCD.append(gcd_list(B))
    
GCD_max = max(GCD)
print(GCD_max)

Submission status: Runtime Error

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)

ans = 0

for i in range(N):
    _A_arr.pop(i)
    if(gcd_list(_A_arr) > ans):
        ans = gcd_list(_A_arr)
    
    _A_arr = [A_arr[i] for i in range(N)]
    

print(ans)

Submission status: Runtime Error

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
if N<=2:
    print(max(A))
else:
    gcds = []
    for n in range(N):
        if n == 0:
            lgcd = None
        else:
            lgcd = A[0]
            for i in range(n):
                lgcd = gcd(lgcd,A[i])
        if n == N-1:
            rgcd = None
        else:
            rgcd = A[n+1]
            for i in range(n+1,N):
                rgcd = gcd(rgcd,A[i])
        if lgcd == None:
            gcds.append(rgcd)
        elif rgcd == None:
            gcds.append(lgcd)
        else:
            gcds.append(gcd(lgcd,rgcd))
print(max(gcds))