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: Accepted

Solution code:
def euclid(x, y):
    if x == 0:
        return y
    if y == 0:
        return x
    while x % y != 0:
        temp = x
        x = y
        y = temp % y
    return y

n = int(input())
data = list(map(int, input().split()))
array = data
L = []
L.append(0)
R = []
R.append(0)
m = []
for i in range(n):
    L.append(euclid(L[i], data[i]))
data.reverse()
for i in range(n):
    R.append(euclid(R[i], data[i]))
R.reverse()
for i in range(0,n):
    m.append(euclid(L[i] , R[i + 1]))
print(max(m))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import fractions

N = int(input())
arr = list(map(int, input().split()))
L = [0]
R = [0]
B = []

for i in range(N):
    L.append(fractions.gcd(arr[i], L[i]))
    R.append(fractions.gcd(arr[N - i - 1], R[i]))
for i in range(N):
    B.append(fractions.gcd(L[i],R[N - i - 1]))
print(max(B))

Submission status: Accepted

Solution code:
# b = 0 のとき、aを返す
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
  
n = int(input())
a = [int(x) for x in input().split()]
reversed_a = list(reversed(a))

length = len(a)
left_gcd = [0] * length
right_gcd = [0] * length

for i in range(1, length):
  left_gcd[i] = gcd(a[i-1], left_gcd[i-1])
  right_gcd[i] = gcd(reversed_a[i-1], right_gcd[i-1])
  
right_gcd = right_gcd[::-1]
m = [gcd(r, l) for l, r in zip(left_gcd, right_gcd)]

# print(left_gcd)
# print(right_gcd)
# print(m)

print(max(m))

Submission status: Accepted

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3
#sys.setrecursionlimit(int(1e+6))
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

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

def main():
    N = i2n()
    A = i2nn()
    # n = -1
    # for i in range(N):
    #   Aからi番目を取り除く
    #   t = gcd(A[0:i], A[i+1:N])
    #   if n < t:
    #     n = t
    L = [0] * N
    R = [0] * N
    L[0] = A[0]
    R[-1] = A[-1]
    for i in range(1, N):
        L[i] = gcd(L[i-1], A[i])
    for i in range(N-2, -1, -1):
        R[i] = gcd(R[i+1], A[i])
    n = -1
    if n < R[1]:
        n = R[1]
    if n < L[-2]:
        n = L[-2]
    for i in range(1, N-1):
        t = gcd(L[i-1], R[i+1])
        if n < t:
            n = t
    print(n)

main()

Submission status: Accepted

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

N = int(input())

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

L = [0] * N
R = [0] * N

for i in range(N) :  # iより左(右)
    if i == 0 :
        L[i] = A[i]
        R[-i-1] = A[-i-1]
    else :
        L[i] = gcd(L[i-1], A[i-1])
        R[-i-1] = gcd(R[-i],A[-i])

ans = 1

for i in range(N) :
    if i == 0 :
        new = R[i]
    elif i == N-1 :
        new = L[i]
    else :
        new = gcd(L[i],R[i])
    if ans < new :
        ans = new

print(ans)

Submission status: Accepted

Solution code:
import sys
from copy import deepcopy
def gcd(a, b):
    if b > a:
        a, b = b, a
    if b == 0:
        return a
    return gcd(b, a%b)

def process(arr):
    L = [0]*(n+1)
    for i in range(1, n+1):
        gcd1 = gcd(L[i-1], arr[i-1])
        L[i] = gcd1
    R = [0]*(n+1)
    for i in range(1, n+1):
        gcd2 = gcd(R[-1-i+1], arr[-1-i+1])
        R[-1-i] = gcd2
    return L, R
    
n = int(raw_input())
arr = map(int, raw_input().split())
if n <= 2:
    print max(arr)
    exit()
L, R = process(arr)
M = [-1] * (n+1)
for i in range(n):
    M[i] = gcd(L[i], R[i+1])

print max(M)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# using main() makes code faster from the point of view of "access to variables in global name-space"
# def main():

""" convenient functions
# for i, a in enumerate(iterable)
# q, mod = divmod(a, b)
# divmod(x, y) returns the tuple (x//y, x%y)
# manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
"""

import sys
sys.setrecursionlimit(10**7)
from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
# accumulate() returns iterator! to get list: list(accumulate())
from math import factorial, ceil, floor
def factorize(n):
    """return the factors of the Arg and count of each factor
    
    Args:
        n (long): number to be resolved into factors
    
    Returns:
        list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
    """
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct
def combinations_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    # TODO: How should I do when n - r is negative?
    if n < 0 or r < 0:
        raise Exception('combinations_count(n, r) not defined when n or r is negative')
    if n - r < r: r = n - r
    if r < 0: return 0
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result
def combinations_with_replacement_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    if n < 0 or r < 0:
        raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
    elif n == 0:
        return 1
    else:
        return combinations_count(n + r - 1, r)
from bisect import bisect_left, bisect_right
from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
from operator import itemgetter
# ex1: List.sort(key=itemgetter(1))
# ex2: sorted(tuples, key=itemgetter(1,2))
from functools import reduce
def chmin(x, y):
    """change minimum
    if x > y, x = y and return (x, True).
    convenient when solving problems of dp[i]
    
    Args:
        x (long): current minimum value
        y (long): potential minimum value
    
    Returns:
        (x, bool): (x, True) when updated, else (x, False)
    """
    if x > y:
        x = y
        return (x, True)
    else:
        return (x, False)
def chmax(x, y):
    """change maximum
    if x < y, x = y and return (x, True).
    convenient when solving problems of dp[i]
    
    Args:
        x (long): current maximum value
        y (long): potential maximum value
    
    Returns:
        (x, bool): (x, True) when updated, else (x, False)
    """
    if x < y:
        x = y
        return (x, True)
    else:
        return (x, False)

from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers):
    return reduce(lcm, numbers, 1)

# first create factorial_list
# fac_list = mod_factorial_list(n)
INF = 10 ** 18
MOD = 10 ** 9 + 7
modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
def modinv(a, p = MOD):
    # evaluate reciprocal using Fermat's little theorem:
    # a**(p-1) is identical to 1 (mod p) when a and p is coprime
    return modpow(a, p-2, p)
def modinv_list(n, p = MOD):
    if n <= 1:
        return [0,1][:n+1]
    else:
        inv_t = [0,1]
        for i in range(2, n+1):
            inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
        return inv_t
def modfactorial_list(n, p = MOD):
    if n == 0:
        return [1]
    else:
        l = [0] * (n+1)
        tmp = 1
        for i in range(1, n+1):
            tmp = tmp * i % p
            l[i] = tmp
        return l
def modcomb(n, k, fac_list = [], p = MOD):
    # fac_list = modfactorial_list(100)
    # print(modcomb(100, 5, modfactorial_list(100)))
    from math import factorial
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    if len(fac_list) <= n:
        a = factorial(n) % p
        b = factorial(k) % p
        c = factorial(n-k) % p
    else:
        a = fac_list[n]
        b = fac_list[k]
        c = fac_list[n-k]
    return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
def modadd(a, b, p = MOD):
    return (a + b) % MOD
def modsub(a, b, p = MOD):
    return (a - b) % p
def modmul(a, b, p = MOD):
    return ((a % p) * (b % p)) % p
def moddiv(a, b, p = MOD):
    return modmul(a, modpow(b, p-2, p))

""" initialize variables and set inputs
# initialize variables
    # to initialize list, use [0] * n
    # to initialize two dimentional array, use [[0] * N for _ in range(N)]
# set inputs
    # open(0).read() is a convenient method:
    # ex) n, m, *x = map(int, open(0).read().split())
    #     min(x[::2]) - max(x[1::2])
    # ex2) *x, = map(int, open(0).read().split())
    #     don't forget to add comma after *x if only one variable is used
# preprocessing
    # transpose = [x for x in zip(*data)]
    # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    # flat = [flatten for inner in data for flatten in inner]
    # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
# calculate and output
    # output pattern
    # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
"""

# functions used
r = lambda: sys.stdin.readline().strip()
r_int = lambda: int(r())
R = lambda: list(map(int, r().split()))
Rfloat = lambda: list(map(float, r().split()))
Rtuple = lambda: tuple(map(int, r().split()))
Rmap = lambda: map(int, r().split())

""" how to treat input
# single int: int(r())
# single string: r()
# single float: float(r())
# line int: R()
# line string: r().split()
# line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
# lines int: [R() for _ in range(n)]
"""


# ----------------------------------
# main

N = int(r())
A = R()
B = [0] * N
C = [0] * N
for i, a in enumerate(A):
    if i == 0:
        tmp = a
    else:
        tmp = gcd(a, tmp)
    B[i] = tmp

A.reverse()
for i, a in enumerate(A):
    if i == 0:
        tmp = a
    else:
        tmp = gcd(a, tmp)
    C[i] = tmp
C.reverse()

ans = 1
for i in range(N):
    if i == 0:
        ans = max(ans, C[i+1])
    elif i == N-1:
        ans = max(ans, B[i-1])
    else:
        ans = max(ans, gcd(B[i-1],C[i+1]))

print(ans)
# end of main
# ----------------------------------

"""memo: how to use defaultdict of list
# initialize
Dic = defaultdict(list)
# append / extend
Dic[x].append(y)
# for
for k, v in Dic.items():
"""

# if __name__ == '__main__':
#     main()

Submission status: Accepted

Solution code:
from fractions import gcd


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

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


main()

Submission status: Accepted

Solution code:
from fractions import gcd


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

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


main()

Submission status: Accepted

Solution code:
# ABC125C - GCD on Blackboard
# Accumulative GCD
from fractions import gcd

def main():
    n = int(input())
    a = tuple(map(int, input().rstrip().split()))

    left = [0] * (n + 1)
    right = [0] * (n + 1)
    for i in range(n):
        left[i + 1] = gcd(left[i], a[i])
        right[n - i - 1] = gcd(right[n - i], a[n - i - 1])

    ans = 0
    for i in range(n):
        ans = max(ans, gcd(left[i], right[i + 1]))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import fractions

N = int(input())

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

L = [0] * N
R = [0] * N

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

    if i >= 1:

        now = fractions.gcd(now,A[i-1])
        L[i] = now

A.reverse()

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

    if i >= 1:

        now = fractions.gcd(now,A[i-1])
        R[i] = now

A.reverse()
R.reverse()

maxi = 0
for i in range(N):

    if L[i] == 0 or R[i] == 0:
        maxi = max(maxi,L[i],R[i])

    else:
        maxi = max(maxi, fractions.gcd(L[i],R[i]))

print (maxi)

Submission status: Accepted

Solution code:
from fractions import gcd
#from math import gcd

N = int(input()) # N < 10 ** 5
A = [int(x) for x in input().split()]

g = A[0]
gcd1 = [0] * N
gcd1[0] = A[0]
for i in range(1, N):
    g = gcd1[i] = gcd(A[i], g)

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

gcd0 = [0] * N
gcd0[0] = gcd2[1]
gcd0[N-1] = gcd1[N-2]

for i in range(1, N-1):
    gcd0[i] = gcd(gcd1[i-1], gcd2[i+1])

print(max(gcd0))

Submission status: Accepted

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 = [0]*(n+1)
right = [0]*(n+1)
for i in range(n):
    left[i+1] = gcd(left[i],a[i])
    right[i+1] = gcd(right[i],a[n-i-1])
left.append(0)
right.append(0)
right.reverse()
ans = 0    
for i in range(n):
    gcdi = gcd(left[i],right[i+2])
    ans = max(ans, gcdi)
    
print(ans)

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
B = [A[0]]
C = [A[N-1]]
for i in range(1,N):
  B.append(gcd(A[i],B[i-1]))
  C.append(gcd(A[N-i-1],C[i-1]))
D = [C[N-2]]
for j in range(N-2):
  D.append(gcd(B[j],C[N-j-3]))
D.append(B[N-2])
print(max(D))

Submission status: Accepted

Solution code:
import fractions

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

l = [0]
r = [0]

for i in range(n):
    l.append(fractions.gcd(l[i], a[i]))
    r.append(fractions.gcd(r[i], a[n - i - 1]))

ans = 0
for i in range(n):
    ans = max(ans, fractions.gcd(l[i], r[n-i-1]))

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

left = [0]
right = [0]

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

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

print(ans)

Submission status: Accepted

Solution code:
def gcd(x,y):
    while y != 0:
        x,y = y,x%y
    return x
n = int(input())
a = list(map(int,input().split()))

L,R = [0],[0]
for i in range(1,n+1): #L[i]は左i個,R[i]は右i個
    L.append(gcd(L[-1],a[i-1]))
    R.append(gcd(R[-1],a[n-i]))
print(max(gcd(L[i],R[n-i-1]) for i in range(n)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 29 20:51:40 2019

@author: makigon
"""

import fractions
from functools import reduce
 
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
 
N = int(input())
a = list(map(int,input().split()))
ans = 0

t_left = [0 for i in range(N)]
t_right = [0 for i in range(N)]

t_left[0] = a[0]
t_left[1] = fractions.gcd(a[0],a[1])

for i in range(2,N):
    t_left[i] = fractions.gcd(t_left[i-1],a[i])

t_right[N-1] = a[N-1]
t_right[N-2] = fractions.gcd(a[N-1],a[N-2])

for j in range(N-3,0,-1):
    t_right[j] = fractions.gcd(t_right[j+1],a[j])
    

for i in range(0,N-2):
    tmp = fractions.gcd(t_left[i],t_right[i+2])
    if tmp > ans:
        ans = tmp
    
print(max(ans,t_right[1],t_left[N-2]))

Submission status: Accepted

Solution code:
import fractions

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

  right = []
  left  = []

  right.append(a[0])
  left.append(a[-1])

  for i in range(1, len(a)-1):
    right.append(fractions.gcd(right[i-1], a[i]))
    left.append(fractions.gcd(left[i-1], a[-i-1]))
  
  ans = max(right[-1], left[-1])
  
  for i in range(len(right)-1):
    ans = max(ans, fractions.gcd(right[i], left[-i-2]))
    
  print(ans)
  
main()

Submission status: Accepted

Solution code:
import fractions
N = int(input())
A=[int(i) for i in input().split()]
B=[]
L = [0]
R = [0]
for i in range(N):
    L.append(fractions.gcd(A[i],L[i]))
    R.append(fractions.gcd(A[N-i-1],R[i]))
for i in range(N):
    B.append(fractions.gcd(L[i],R[N-i-1]))
print(max(B))

Submission status: Accepted

Solution code:
from fractions import gcd

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

# DP
dp_l = [0] * (N + 1)
dp_r = [0] * (N + 1)
for i in range(1, N):
    dp_l[i] = gcd(dp_l[i - 1], AAA[i-1])
    dp_r[i] = gcd(dp_r[i-1], AAA[-i])

max_m = -1
for i in range(N+1):
    m = gcd(dp_l[i], dp_r[N-i-1])
    max_m = max(max_m, m)
print(max_m)

Submission status: Accepted

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

from fractions import gcd
r = []
l = []
r.append(a[-1])

l.append(a[0])

for i in range(1,n):
    l.append(gcd(l[i-1],a[i]))
    r.append(gcd(r[i-1],a[(n-1)-i]))

l.insert(0,a[-1])
r.insert(0,a[0])


def solve(num):
    ans = gcd(l[num],r[n-1-num])
    return ans

can = []
for i in range(n):
    can.append(solve(i))

print(max(can))

Submission status: Accepted

Solution code:
try :
    from math import gcd
except ImportError :
    from fractions import gcd


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


X = [A[0]]
Y = [A[-1]]

for i in range(1,N) :
    x = gcd(X[i-1], A[i])
    y = gcd(Y[i-1], A[N-i-1])
    X.append(x)
    Y.append(y)

ans = 0

Y = Y[::-1]

for i in range(N) :
    if i == 0 :
        calc = Y[1]
    elif i == N-1 :
        calc = X[N-2]
    else :
        calc = gcd(X[i-1],Y[i+1])
    
    ans = max(ans, calc)

print(ans)

Submission status: Accepted

Solution code:
import fractions

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

gcd_l = [0]
gcd_r = [0]

for i in range(n):
  gcd_l.append(fractions.gcd(gcd_l[i], l[i]))
  gcd_r.append(fractions.gcd(gcd_r[i], l[-(i+1)]))
gcd_r = gcd_r[::-1]
# print(gcd_l)
# print(gcd_r)

ans = [0]*n
for i in range(n):
  ans[i] = fractions.gcd(gcd_l[i], gcd_r[i+1])
print(max(ans))

Submission status: Accepted

Solution code:
from fractions import gcd
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
# print(N, A)

L = [0]*(N - 1)
R = [0]*(N - 1)
L[0] = A[0]
R[0] = A[-1]

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

# print(L)
# print(R)

B = [R[-1]]
for i in range(N - 2):
    l = L[i]
    r = R[N - 3 - i]
    # print(l, r)
    B.append(gcd(l, r))
B.append(L[-1])

# print(B)
ans = max(B)
print(ans)

Submission status: Accepted

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

N, = map(int, input().split())
L = [int(v) for v in input().split()]

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

l1 = [None] * N
l2 = [None] * N

l1[0] = L[0]
for i in range(1, N):
    l1[i] = gcd(L[i], l1[i - 1])
    
l2[-1] = L[-1]
for i in reversed(range(N - 1)):
    l2[i] = gcd(L[i], l2[i + 1])
    
ans = 1
for i in range(N):
    if i == 0:
        ans = max(ans, l2[1])
    elif i == N - 1:
        ans = max(ans, l1[-2])
    else:
        ans = max(ans, gcd(l1[i - 1], l2[i + 1]))
        
print(ans)

Submission status: Accepted

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

def solve(n, a):
    l = [0] * n
    l[0] = a[0]
    for i in range(1, n):
        l[i] = gcd(l[i-1], a[i])
    r = [0] * n
    r[n-1] = a[n-1]
    for i in range(n-2, -1, -1):
        r[i] = gcd(r[i+1], a[i])
    b = []
    for i in range(n):
        if i == 0:
            b.append(r[i+1])
        elif i == n-1:
            b.append(l[i-1])
        else:
            b.append(gcd(l[i-1], r[i+1]))
    return max(b)

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

Submission status: Accepted

Solution code:
from fractions import gcd
from collections import deque

N = int(input())
A = [int(a) for a in input().split()]
L = deque([0])
R = deque([0])

for i in range(N):
  L.append(gcd(L[i],A[i]))
for i in range(N)[::-1]:
  R.appendleft(gcd(R[0],A[i]))

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

Submission status: Accepted

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

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

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

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

for i in range(N):
    if i == 0:
        ans.append(R[N - 2])
    elif i == N - 1:
        ans.append(L[N - 2])
    else:
        ans.append(gcd(L[i - 1], R[N - i - 2]))

print(max(ans))

Submission status: Accepted

Solution code:
import fractions

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

left = [0]
right = [0]
for a in a_list:
    left.append(fractions.gcd(left[-1], a))
    
a_list.reverse()
for a in a_list:
    right.append(fractions.gcd(right[-1], a))

right.reverse()

m = []
for i in range(len(a_list)):
    m.append(fractions.gcd(left[i], right[i + 1]))
    
print(max(m))

Submission status: Accepted

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

def gcd(x, y):
    if x < y:
        x, y = y, x
    while y != 0:
        x, y = y, x%y
    return x

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

R.reverse()

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

print(max(M))

Submission status: Accepted

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

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

l, r = [A[0]], [A[-1]]
for n in range(1, N):
	l += [gcd(l[-1], A[n])]
	r += [gcd(r[-1], A[N - n - 1])]
r = r[::-1]

list_gcd = [l[-2], r[1]]
for n in range(N - 2):
	list_gcd += [gcd(l[n], r[n + 2])]

#print(l)
#print(r)
#print(list_gcd)

print(max(list_gcd))

Submission status: Accepted

Solution code:
import fractions

N = int(input())
A = [int(i) for i in input().split()]
A.insert(0,0)
A.append(0)
tmp = 0

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

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

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

Submission status: Accepted

Solution code:
import math

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

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

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

#print(A)
lgcd=[A[0]]*N
for i in range(1,N):
    lgcd[i]=gcd(lgcd[i-1],A[i])

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

#print(lgcd)
#print(rgcd)

ans=[0]*N
ans[0]=rgcd[1]
ans[N-1]=lgcd[N-2]
for i in range(1,N-1):
    ans[i]=gcd(lgcd[i-1],rgcd[i+1])
print(max(ans))

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
input = sys.stdin.readline

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

l_gcd = []
r_gcd = []

for i, a in enumerate(A):
    if i == 0:
        l_gcd.append(a)
    else:
        l_gcd.append(gcd(l_gcd[i-1], a))
for i, a in enumerate(reversed(A)):
    if i == 0:
        r_gcd.append(a)
    else:
        r_gcd.append(gcd(r_gcd[i-1], a))
ans = 1
for i in range(N):
    if i == 0:
        ans = max(ans, r_gcd[N-2])
    elif i == N-1:
        ans = max(ans, l_gcd[i-1])
    else:
        ans = max(ans, gcd(l_gcd[i-1], r_gcd[N-2-i]))
print(ans)

Submission status: Accepted

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
As = nl()

from fractions import gcd
gcds_left = [0] * N
gcds_right = [0] * N
gcds_left[0] = As[0]
gcds_right[N-1] = As[N-1]

for i in range(1, N):
    gcds_left[i] = gcd(As[i], gcds_left[i-1])
    gcds_right[N-1-i] = gcd(As[N-1-i], gcds_right[N-i])

max_gcd = -1
for i in range(1, N-1):
    g = gcd(gcds_left[i-1], gcds_right[i+1])
    if g > max_gcd:
        max_gcd = g
print(max(max_gcd, gcds_right[1], gcds_left[N-2]))

Submission status: Accepted

Solution code:
from fractions import gcd

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

li=[0]*(n+1)
ri=[0]*(n+1)

for i in range(n):
    li[i+1]=gcd(li[i], a[i])
for i in range(n-1, -1, -1):
    ri[i]=gcd(ri[i+1], a[i])

ans=0

for i in range(n):
    ans=max(gcd(li[i], ri[i+1]),ans)
ans=max(ans, li[-1])
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd


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

total=0
left=[0]*(N+1)
right=[0]*(N+1)

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

for i in range(N):
    total=max(total,gcd(left[i],right[i+1]))

print(total)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import defaultdict
import math
import re

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

def main():
    #A, B, T = map(int, input().split())
    N = int(input())
    A_list = [int(a) for a in input().split()]

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

    max_val = 0
    for i in range(N):
        if i == 0:
            g = right[N-2]
        elif i == N-1:
            g = left[N-2]
        else:
            g = gcd(left[i-1], right[N-i-2])
        
        if g > max_val:
            max_val = g

    print(max_val)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def gcds(t,X,V):
	if X[t] > V[t]:
		x, y = X[t], V[t]
	else:
		x, y = V[t], X[t]
	while(y>0):
		x, y = y, x%y
		V[t+1] = x

def gcde(t,X,V):
	if X[t] > V[t+1]:
		x, y = X[t], V[t+1]
	else:
		x, y = V[t+1], X[t]
	while(y>0):
		x, y = y, x%y
		V[t] = x

def main():
	N = int(input())
	A = list(map(int,input().split()))
	ans = 1
	S = [A[0]]*(N+1)
	E = [A[N-1]]*(N+1)
	for t in range(N):
		gcds(t, A, S)
		gcde(N-t-1, A, E)
	ans = max(ans, E[1])
	ans = max(ans, S[N-1])
	for t in range(1,N-1):
		ans = max(ans, gcd(S[t], E[t+1]))
	print(ans)

main()

Submission status: Accepted

Solution code:
#coding:utf-8
N=input()
A=map(int,raw_input().split())

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


# L[i]:=gcd(A_0, ..., A_i)
# R[i]:=gcd(A_i, ..., A_{N-1})
L=[0]*N
R=[0]*N

L[0]=A[0]
R[N-1]=A[N-1]
for i,a in enumerate(A):
    if i==0:continue
    L[i]=gcd(L[i-1],a)
for i,a in enumerate(reversed(A)):
    if i==0:continue
    R[N-i-1]=gcd(R[N-i],a)
ans=R[1] 
ans=max(ans,L[N-2])
for i in range(N-1):
    if i==0:continue
    ans=max(ans,gcd(L[i-1],R[i+1]))
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

# GCD
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x

def main():
  n=I()
  l=LI()

  l1=[l[0]]
  for i in range(1,n):
    l1.append(gcd(l1[i-1],l[i]))

  l2=[l[-1]]
  for i in range(1,n):
    l2.append(gcd(l2[i-1],l[n-i-1]))
  l2.reverse()

  mx=max(l1[-2],l2[1])
  for i in range(n-2):
    mx=max(mx,gcd(l1[i],l2[i+2]))

  return mx

print(main())

Submission status: Accepted

Solution code:
#abc125c

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

n=int(raw_input())
a=map(int,raw_input().split())
res=0
x=[0]*(n+1)
y=[0]*(n+1)
for i in range(n):
 x[i+1]=gcd(x[i],a[i])
 y[i+1]=gcd(y[i],a[n-i-1])
for i in range(n):
 res=max(gcd(x[i],y[n-i-1]),res)
print res

Submission status: Accepted

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
    c = a % b
    return gcd(b, c)

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

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

Z = [0]*N
for i in range(N):
    Z[i]=gcd(X[i],Y[i])
print(max(Z))