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()

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

list_koyaku1=[]
list_koyaku11=[]       
         
if N==2:
    ans=max(A)

else:
    b=min(A)
    c=max(A)
    for i in range(N):
      if gcd(A[i],c)==1:
        b=A[i]
        break
    
    for a in A:
        list_koyaku1.append(gcd(a,b))

    for a in list_koyaku1:
        list_koyaku11.append(gcd(a,c))
    
    number_prime=list_koyaku11.count(1)
    
    if number_prime>1:
        ans=1
    else:
        ans=max(list_koyaku11)

print(ans)

Submission status: Wrong Answer

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

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

l = [0]*N
r = [0]*N

l[0] = A[0]
r[-1] = A[-1]

for i in range(N):
	l[i] = gcd(l[i], A[i])
	idx = N-1-i
	r[i] = gcd(r[idx], A[idx])

#残りの N − 1 個の整数の最大公約数
mx = 0
for i in range(N):
	if i==N-1:	#後ろから1個めが抜かれた時
		val = l[N-2]
	elif i==0:	#前から1個めが抜かれた時
		val = r[1]
	else:		#どれでもない時
		val = gcd(r[i+1], l[i-1])
	mx = max(val, mx)
print(mx)

Submission status: Wrong Answer

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

X = 1
n = N
while N>>X > 0:
  X += 1
k = 2**X
d = k - N

Li = [[i] for i in A] + [[A[-1]] for i in range(d)]
m = k
for i in range(X):
  m = int(m/2)
  for j in range(m):
    Li[j:j+2] = [[fractions.gcd(Li[j][0],Li[j+1][0]),Li[j],Li[j+1]]]

ans = -1
for i in range(N):
  x = A[i-1]
  li = Li[0][1:]
  for j in range(X):
    if (i>>(X-j-1))%2 == 0:
      x = fractions.gcd(x,li[1][0])
      li = li[0][1:]
    else:
      x = fractions.gcd(x,li[0][0])
      li = li[1][1:]
  if x > ans:
    ans = x
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
##  input functions for me
def ria(sep = ''):
    if sep == '' :
        return list(map(int, input().split())) 
    else: return list(map(int, input().split(sep)))
def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()
##

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

## main ##
N = ri()
A = ria()

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

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

print(ma)

Submission status: Wrong Answer

Solution code:
 
#from functools import reduce
def gcd(m,n):
  x=max(m,n)
  y=min(m,n)
  while y!=0:
    x,y=y,x%y
  return x
 
a=int(input())
li=list(map(int,input().split()))
left_gcd=[0]
#left_gcd.append(li[0])
right_gcd=[0]
#right_gcd.append(li[a-1])

for i in range(a):
    left_gcd.append(gcd(li[i],left_gcd[i]))
    right_gcd.append(gcd(li[a-1-i],right_gcd[i]))
    #k=k+1
# def gcd_list(numbers):
#     return reduce(math.gcd, numbers)
print(right_gcd)
print(left_gcd)
num_li=[]
#num_li.append(right_gcd[a-2])
#num_li.append(left_gcd[a-2])
#s=2
for i in range(a):
    ans=gcd(left_gcd[(a-1)-i],right_gcd[i])
    #print(left_gcd[(a-3-i)],right_gcd[i])
  
    #print(ans)
    num_li.append(ans)
    

print(max(num_li))

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
A.sort()

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

list_koyaku1=[]
list_koyaku11=[]       
         
if N==2:
    ans=max(A)

else:
    b=min(A)
    c=max(A)
    if gcd(c,b)==1:
        b=A[-2]
    
    for a in A:
        list_koyaku1.append(gcd(a,b))

    for a in list_koyaku1:
        list_koyaku11.append(gcd(a,c))
    
    number_prime=list_koyaku11.count(1)
    
    if number_prime>1:
        ans=1
    else:
        ans=max(list_koyaku11)

print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd as gc
from functools import reduce
 
def gcd(x):
    return reduce(gc,x)
 
N=int(input())
A=list(map(int,input().split()))
B=A[:]
maxi=gcd(A)
for i in range(N):
    for j in range(gcd(A),int(max(A)/2)+1):
        B[i]=j
        #print("{} {}".format(B,gcd(B)))
        if(maxi<gcd(B)):
            maxi=gcd(B)
    B=A
    
print(maxi)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
a = raw_input().split()

cnt = 0
for i in a:
    a[cnt] = int(i)
    cnt += 1

chk = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        if k in chk:
            continue
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

    answer = max(chk.keys())
#    answer = 0
#    for i in chk:
#        if int(chk[i]) >= chk[1] - 1 and i > answer:
#            answer = i
    print answer

Submission status: Wrong Answer

Solution code:
from fractions import gcd
 
N = int(input())
A = list(map(int, input().split()))
l = []
r = []
k = 0
for i in range(N):
    k = gcd(k , A[i])
    l.append(k)
k = 0
for i in range(N - 1, -1, -1):
    k = gcd(k, A[i])
    r.append(k)
ans = 0
for i in range(N):
    ans = max(ans, gcd(l[i - 1], r[N - i - 2]))

ans = max(ans, l[N - 2], r[N - 2])

print(l, r)

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

l = [0]*N
r = [0]*N

l[0] = A[0]
r[-1] = A[-1]

for i in range(N):
	l[i] = fractions.gcd(l[i], A[i])
	idx = N-1-i
	r[i] = fractions.gcd(r[idx], A[idx])
mx = 0
for i in range(N):
	if i==N-1:#
		val = l[N-2]
	elif i==0:#
		val = r[1]
	else:#
		val = fractions.gcd(r[i+1], l[i-1])
	mx = max(val, mx)
print(mx)

Submission status: Wrong Answer

Solution code:
import numpy as np

def gcd(a,b):    
        if a < b:
            a, b = b, a
        if b == 0:
            return a
        c = a % b
        return gcd(b, c)
    
N = int(input())
A_list = list(map(int, input().split()))

# =====
# initialize L and R
# =====
L = np.zeros(N+1)
R = np.zeros(N+1)

for idx, element in enumerate(A_list):
    if idx == 0:
        continue
    else:
        if L[idx-1] == 0:
            L[idx] = element
        L[idx] = gcd(L[idx-1], element)

for idx, element in enumerate(A_list[-1::-1]):
    idx = N-idx
    if idx == N:
        continue
    else:
        if R[idx+1] == 0:
            R[idx] = element
        R[idx] = gcd(R[idx+1], element)

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

print(int(ans))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce
def gcd_list(numbers):
    return reduce(gcd, numbers)
n = int(input())
a = [int(_) for _ in input().split()]
a.sort()
ir = [a[0]]
for i in range(1,n):
    ir.append(gcd(a[0], a[i]))
mi = ir[1:].index(min(ir[1:]))+1
mr = []
for i in range(n):
    mr.append(gcd(a[mi], a[i]))
if ir.count(min(ir)) > mr.count(min(mr)):
    print(gcd_list(a[1::]))
elif ir.count(min(ir)) == mr.count(min(mr)):
    print(a[0])
elif ir.count(min(ir)) < mr.count(min(mr)):
    a.pop(mi)
    print(gcd_list(a))

Submission status: Wrong Answer

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


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


def m():
    left, right = A.copy(), A.copy()
    for i in range(1, N):
        left[i] = gcd(A[i], left[i-1])

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

    res = max(left[N-1], right[0])
    for i in range(2, N-2):
        res = max(gcd(left[i-1],right[i+1]), res)
    return res


print(m())

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = min(A)
tmp = 1
if M == 1:
    A.remove(1)
    M = min(A)
    
for i in reversed(range(1,M+1)):
    count = 0
    memo = 0
    for j in range(len(A)):
        if A[j]%i != 0:
            count += 1
            memo = j
            if count > 1:
                break
    if count <= 1:
        tmp = i
        break
        
print(tmp)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,fractions
from functools import reduce
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))

    _l = 0
    L = []
    for i in range(N):
        _l = fractions.gcd(_l, A[i])
        L.append(_l)

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

    print(max([fractions.gcd(l,r) for l, r in zip(L,R)]))



lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
a = raw_input().split()

cnt = 0
for i in a:
    a[cnt] = int(i)
    cnt += 1

chk = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        if k in chk:
            continue
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

#    answer = max(chk.keys())
    answer = 0
    for i in chk:
        if int(chk[i]) >= chk[1] - 1 and i > answer:
            answer = i
    print answer

Submission status: Wrong Answer

Solution code:
from fractions import gcd
 
N = int(input())
A = list(map(int, input().split()))
l = []
r = []
k = 0
for i in range(N):
    k = gcd(k , A[i])
    l.append(k)
k = 0
for i in range(N - 1, -1, -1):
    k = gcd(k, A[i])
    r.append(k)
ans = 0
for i in range(N):
    ans = max(ans, gcd(l[i - 1], r[N - i - 1]))

ans = max(ans, l[N - 2], r[N - 2])

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

def divs(n):
  d = []
  i = 1
  for i in range(1, int(n**0.5) + 1):
    if n % i == 0:
      d.append(i)
      d.append(n // i)
    i += 1
  d.append(n)
  return d

divlist = divs(A[0]) + divs(A[1])
divlist.sort()

if N == 2:
  print(max(A))
else:
  ans_idx = -1
  for a in A[2:]:
    while a % divlist[ans_idx] != 0:
      ans_idx -= 1
      if divlist[ans_idx] == 1:
        break
  print(divlist[ans_idx])

Submission status: Wrong Answer

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

def spaceinput():
    return list(map(int,input().split(" ")))


N=int(input())
a=sorted(spaceinput(),reverse=True)
#a=sorted(spaceinput())

notrewrite=[-1]*N
rewrite=[-1]*N
best=[-1]*N

notrewrite[0]=a[0]
notrewrite[1]=gcd(a[0],a[1])
rewrite[0]=a[0]
rewrite[1]=a[0]
best[0]=a[0]
best[1]=max(a[0],a[1])

for i in range(2,N):
    notrewrite[i]=gcd(notrewrite[i-1],a[i])
    rewrite[i]=notrewrite[i-1]
    best[i]=max([notrewrite[i],notrewrite[i-1],gcd(a[i],best[i-1])])

print(best[-1])

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
a = raw_input().split()

cnt = 0
for i in a:
    a[cnt] = int(i)
    cnt += 1

chk = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        if a[i] in chk:
            continue
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
        for j in del_list:
            del chk[j]
        cnt += 1

#    answer = max(chk.keys())
    answer = 0
    for i in chk:
        if int(chk[i]) >= n - 1 and i > answer:
            answer = i
    print answer

Submission status: Wrong Answer

Solution code:
import sys,collections as cl
import fractions
from functools import reduce
import copy
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[i]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a

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

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

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

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

n = int(input())

a = l()

m = gcd_list(a[1:])
m = max(m,gcd_list(a[:n-1]))
aa = [a[0]]
aaa = [a[-1]]
for i in range(n-1):
	aa.append(gcd(aa[-1],a[i+1]))
	aaa.append(gcd(aaa[-1],a[-(i+2)]))

for i in range(n-1):
	m = max(m,gcd(aa[i],aaa[-(i+2)]))
print(m)

Submission status: Wrong Answer

Solution code:
import numpy as np

def gcd(a,b):
        if a < b:
            a, b = b, a
        if b == 0:
            return a
        c = a % b
        return gcd(b, c)
    
N = int(input())
A_list = list(map(int, input().split()))

# =====
# initialize L and R
# =====
L = np.zeros(N+1)
R = np.zeros(N+1)

for idx, element in enumerate(A_list):
    if idx == 0:
        continue
    else:
        L[idx] = gcd(L[idx-1], element)

for idx, element in enumerate(A_list[-1::-1]):
    idx = N-idx
    if idx == N:
        continue
    else:
        R[idx] = gcd(R[idx+1], element)

ans = 0
for i in range(N):
    M = gcd(L[i], R[i+1])
    if ans < M:
        ans = M

print(int(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,fractions
from functools import reduce
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))

    _l = 0
    L = []
    for i in range(N):
        _l = fractions.gcd(_l, A[i])
        L.append(_l)

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

    print(max([fractions.gcd(l,r) for l, r in zip(L,R)]))



lines = [x.strip() for x in sys.stdin.readlines()]

Submission status: Wrong Answer

Solution code:
def GCD(a, b):
    if a == 0 and b == 0:
        return 0
    elif a == 0:
        return b
    elif b == 0:
        return 0
        
    while a % b != 0:
        r = a % b
        a, b = b, r
    return b

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

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])

for i in range(N):
    ans = max(ans,GCD(X[i],Y[i]))

print(ans)

Submission status: Wrong Answer

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

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

def gcd_list(list):
    a = list.pop()
    for b in list:
        a = gcd(a,b)
    return a

ans = []
for i in range(n-1):
    B = A.copy()
    B.pop(i)
    ans.append(gcd_list(B))
print(max(ans))

Submission status: Wrong Answer

Solution code:
import copy
import fractions
 
inp = int(input())
v = [int(d) for d in input().split()]
cc = []
m = []

if len(v) == 2:
    s = fractions.gcd(v[0],v[1])
    print(s)
    exit()
 
for e in range(len(v)):
    dd = copy.deepcopy(v)
    del dd[e]
    cc.append(dd)
 
for q in cc: #q 1抜き
    s = q[0]
    for a in range(len(q) -1): 
        s = fractions.gcd(s,q[a+1])
    m.append(s)
m.sort()
 
print(m[-1])

Submission status: Wrong Answer

Solution code:
from fractions import gcd
 
N = int(input())
A = list(map(int, input().split()))
l = []
r = []
k = 0
for i in range(N):
    l.append(gcd(k , A[i]))
k = 0
for i in range(N - 1, -1, -1):
    r.append(gcd(k, A[i]))
ans = 0
for i in range(N):
    ans = max(ans, gcd(l[i - 1], r[N - i - 1]))

ans = max(ans, l[N - 2], r[N - 2])

print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce
import copy

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

fValueList = []
lValueList = []
maxList = []

if n == 2:
  print(gcd(a[0], a[1]))
else:
  fValueList.append(a[0])
  lValueList.append(a[n-1])
  for i in range(1,n-1):
    g = fValueList[i-1]
    fValueList.append(gcd(g, a[i]))

  for i in range(1,n-1):
    g = lValueList[i-1]
    lValueList.append(gcd(g, a[n-1-i]))
  
  maxList.append(fValueList[n-2])
  maxList.append(lValueList[n-2])
  for k in range(n-2):
    g = gcd(fValueList[k], lValueList[n-3-k])
    maxList.append(g)
    
  print(max(maxList))

Submission status: Wrong Answer

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

def spaceinput():
    return list(map(int,input().split(" ")))


N=int(input())
a=sorted(spaceinput(),reverse=True)
#a=sorted(spaceinput())

notrewrite=[-1]*N
rewrite=[-1]*N
best=[-1]*N

notrewrite[0]=a[0]
notrewrite[1]=gcd(a[0],a[1])
rewrite[0]=a[0]
rewrite[1]=a[0]
best[0]=a[0]
best[1]=max(a[0],a[1])

for i in range(2,N):
    notrewrite[i]=gcd(notrewrite[i-1],a[i])
    rewrite[i]=notrewrite[i-1]
    best[i]=max([notrewrite[i-1],gcd(a[i],best[i-1])])

print(best[-1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = min(A)
tmp = 1
if M == 1:
    A.remove(1)
    M = min(A)
    
for i in reversed(range(2,M+1)):
    count = 0
    memo = 0
    for j in range(len(A)):
        if A[j]%i != 0:
            count += 1
            memo = j
            if count > 1:
                break
    if count <= 1:
        tmp = i
        break
        
print(tmp)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce
N = int(input())
Alist = list(map(int, input().split()))
Blist = Alist+[Alist[0]]
def gcd_list(numbers):
  return reduce(gcd, numbers)
if len(Alist) == 2:
    print(max(Alist))
else:
    gcdlist=[gcd_list(Blist[i:i+2]) for i in range(N)]
    ind = gcdlist.index(min(gcdlist))
    if ind == N:
      print(max([gcd_list(Alist[:N]), gcd_list(Alist[1:])]))
    else:
      print(max([gcd_list(Alist[:ind]+Alist[ind+1:]), gcd_list(Alist[:ind+1]+Alist[ind+2:])]))

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
import sys

sys.setrecursionlimit(100000)

def gcd(n1,n2):
  if n1 == 0:
    return n2
  elif n2 == 0:
    return n1
  else:
    return fractions.gcd(n1,n2)

n = int(input())
a = list(map(int,input().split()))
l = [0]*(n+1)
r = [0]*(n+1)
for i in range(n+1):
  if i != 0:
    l[i] = gcd(l[i-1],a[i-1])
    r[i] = gcd(r[n-i],a[n-i])
m = [gcd(l[i],r[i]) for i in range(n+1)]
print(max(m))

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
a = raw_input().split()

cnt = 0
for i in a:
    a[cnt] = int(i)
    cnt += 1

chk = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1
    for i in range(2,len(a)):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
#        for j in del_list:
#            del chk[j]
        cnt += 1

    answer = max(chk.keys())
#    answer = 0
#    for i in chk:
#        if int(chk[i]) >= n - 1 and i > answer:
#            answer = i
    print answer

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
if len(a)==2:
    print(max(a[0],a[1]))
    exit()
lis_gcd=[]
MIN=gcd(a[0],a[-1])
arg=-1
lis_gcd.append(MIN)
for i in range(n-1):
    tmp=gcd(a[i],a[i+1])
    lis_gcd.append(tmp)
    if MIN<=tmp:
        MIN=tmp
        arg=i
lis_gcd.sort()
if a[arg]%lis_gcd[1]==0:
    a[arg+1]=lis_gcd[-1]
else:
    a[arg]=lis_gcd[-1]
print(lis_gcd[2])

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce
N = int(input())
Alist = list(map(int, input().split()))
Alist = Alist+[Alist[0]]
def gcd_list(numbers):
  return reduce(gcd, numbers)
if len(Alist) == 2:
    print(max(Alist))
else:
    gcdlist=[gcd_list(Alist[i:i+2]) for i in range(N)]
    ind = gcdlist.index(min(gcdlist))
    if ind == N:
      print(max([gcd_list(Alist[:N]), gcd_list(Alist[1:])]))
    else:
      print(max([gcd_list(Alist[:ind]+Alist[ind+1:]), gcd_list(Alist[:ind+1]+Alist[ind+2:])]))

Submission status: Wrong Answer

Solution code:
"""
問題文

N個の整数 A1,A2,...,ANが黒板に書かれています。
あなたはこの中から整数を 1つ選んで、1 以上 109以下の好きな整数に書き換えます。
元の整数と同じ整数に書き換えても構いません。書き換えた後のN個の整数の最大公約数の最大値を求めてください。

制約
入力は全て整数である。
2≤N≤105
1≤Ai≤109
"""

"""
3 5 7
最大公約数: 1
最大の最大公約数: 1

8 4 6
最大公約数: 2
最大の最大公約数: 4
『aとbとcの最大公約数』　＝　『aとbの最大公約数』と『c』　の最大公約数

A1 , A2 , A3 ... An

gcd(A1 , A2 , A3 ... An-1) * N
= gcd * N * N
= gcd * N^2

gcd(A1 , A2 , A3 ... An-1)
gcd(A1 , A2 , A3 ... An-2 , An) = gcd(A1 , A2 , A3 ... An-2) , An
"""
def gcd(a, b):  # a>b
    a, b = max(a, b), min(a, b)
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

from functools import reduce
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    R = [0 for i in range(N - 1)]
    L = [0 for i in range(N - 1)]
    R[0] = a_list[0]
    L[0] = a_list[-1]

    for i in range(1 , N-1) : 
        R[i] = gcd(R[i - 1] , a_list[i])
    
    for j in range(1 , N-1) : 
        L[j] = gcd(L[j - 1] , a_list[- j - 1])
    
    maxValue = 0
    for i in range(N - 2) : 
        print(i , R[i] , L[N - i - 3])
        value = gcd(R[i] , L[N - i - 3])
        if value > maxValue : maxValue = value
    print(max(maxValue , R[-1] , L[-1]))
main()

Submission status: Wrong Answer

Solution code:

from fractions import gcd

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

if n == 1:
    print(a[0])
else:
    left_gcd = [0] * n
    right_gcd = [0] * n
    left_gcd[0] = a[0]
    right_gcd[n-1] = a[n-1]

    for i in range(1,n):
        left_gcd[i] = gcd(left_gcd[i-1],a[i])
        i2 = n-i-1
        right_gcd[i2] = gcd(right_gcd[i2+1],a[i2])

    ans = left_gcd[n-1]
    if ans < right_gcd[1]:
        ans = right_gcd[1]
    if ans < left_gcd[n-1]:
        ans = left_gcd[n-2]
    for i in range(1,n-1):
        gcd_ans = gcd(left_gcd[i-1],right_gcd[i+1])
        if ans < gcd_ans:
            ans = gcd_ans
    print(ans)

Submission status: Wrong Answer

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


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


def m():
    left, right = A.copy(), A.copy()
    for i in range(1, N):
        left[i] = gcd(A[i], left[i-1])

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

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


print(m())

Submission status: Wrong Answer

Solution code:

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

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

f = [0]
b = [0]

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

b.reverse()
ans = []
for i in range(n-1):
    ans.append(gcd(f[i], b[i+1]))
print(max(ans))

Submission status: Wrong Answer

Solution code:

from fractions import gcd

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

if n == 0:
    print(a[0])
else:
    left_gcd = [0] * n
    right_gcd = [0] * n
    left_gcd[0] = a[0]
    right_gcd[n-1] = a[n-1]

    for i in range(1,n):
        left_gcd[i] = gcd(left_gcd[i-1],a[i])
        i2 = n-i-1
        right_gcd[i2] = gcd(right_gcd[i2+1],a[i2])

    ans = left_gcd[n-1]
    if ans < right_gcd[1]:
        ans = right_gcd[1]
    if ans < left_gcd[n-1]:
        ans = left_gcd[n-2]
    for i in range(1,n-1):
        gcd_ans = gcd(left_gcd[i-1],right_gcd[i+1])
        if ans < gcd_ans:
            ans = gcd_ans
    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce


def main():

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

    gcds_r = [1] * N
    gcds_r[0] = A[0]
    for n in range(1, N):
        gcds_r[n] = fractions.gcd(gcds_r[n-1], A[n])

    gcds_l = [1] * N
    gcds_l[-1] = A[-1]
    for n in range(N-1)[::-1]:
        gcds_l[n] = fractions.gcd(gcds_l[n+1], A[n])

    max_gcd = 1
    for n in range(1, N-1):
        max_gcd = max(max_gcd, fractions.gcd(gcds_r[n-1], gcds_l[n+1]))

    max_gcd = max(max_gcd, gcds_l[N-2], gcds_r[1])

    print(max_gcd)  


main()

Submission status: Wrong Answer

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

def spaceinput():
    return list(map(int,input().split(" ")))


N=int(input())
#a=sorted(spaceinput(),reverse=True)
a=sorted(spaceinput())

notrewrite=[-1]*N
rewrite=[-1]*N
best=[-1]*N

notrewrite[0]=a[0]
notrewrite[1]=gcd(a[0],a[1])
rewrite[0]=a[0]
rewrite[1]=a[0]
best[0]=a[0]
best[1]=max(a[0],a[1])

for i in range(2,N):
    notrewrite[i]=gcd(notrewrite[i-1],a[i])
    rewrite[i]=notrewrite[i-1]
    best[i]=max([notrewrite[i-1],gcd(a[i],best[i-1])])

print(best[-1])

Submission status: Wrong Answer

Solution code:
from fractions import gcd
 
N = int(input())
A = list(map(int, input().split()))
l = []
r = []
k = 0
for i in range(N):
    l.append(gcd(k , A[i]))
k = 0
for i in range(N - 1, -1, -1):
    r.append(gcd(k, A[i]))
ans = 0
for i in range(N):
    ans = max(ans, gcd(l[i - 1], r[N - i - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:

from fractions import gcd

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

left_gcd = [0] * n
right_gcd = [0] * n
left_gcd[0] = a[0]
right_gcd[n-1] = a[n-1]

for i in range(1,n):
    left_gcd[i] = gcd(left_gcd[i-1],a[i])
    i2 = n-i-1
    right_gcd[i2] = gcd(right_gcd[i2+1],a[i2])

ans = left_gcd[n-1]
if ans < right_gcd[1]:
    ans = right_gcd[1]
if ans < left_gcd[n-1]:
    ans = left_gcd[n-2]
for i in range(1,n-1):
    gcd_ans = gcd(left_gcd[i-1],right_gcd[i+1])
    if ans < gcd_ans:
        ans = gcd_ans
print(ans)

Submission status: Wrong Answer

Solution code:

from fractions import gcd

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

left_gcd = [0] * n
right_gcd = [0] * n
left_gcd[0] = a[0]
right_gcd[n-1] = a[n-1]

for i in range(1,n):
    left_gcd[i] = gcd(left_gcd[i-1],a[i])
    i2 = n-i-1
    right_gcd[i2] = gcd(right_gcd[i2+1],a[i2])

ans = left_gcd[n-1]
if ans < right_gcd[1]:
    ans = right_gcd[1]
if ans < left_gcd[n-1]:
    ans = left_gcd[n-2]
print(ans)
for i in range(1,n-1):
    gcd_ans = gcd(left_gcd[i-1],right_gcd[i+1])
    if ans < gcd_ans:
        ans = gcd_ans
print(ans)

Submission status: Wrong Answer

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

chk = {}
def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            if i in chk:
                chk[i] += 1
            else:
                chk[i] = 1
            if i != k // i:
                key = int(k//i)
                if key in chk:
                    chk[key] += 1
                else:
                    chk[key] = 1

    return divisors

if n == 2:
    print max(a[0],a[1])
else:
    for i in range(2):
        make_divisors(a[i])
    cnt = 1      
    for i in range(2,len(a)):
        k = a[i]
        del_list = []
        for j in chk:
            if k % j == 0:
                chk[j] += 1
            if chk[j] < cnt+1:
                del_list.append(j)
#        for j in del_list:
#            del chk[j]
        cnt += 1

    answer = max(chk.keys())
    print answer

Submission status: Wrong Answer

Solution code:
"""
python3 main.py < input.txt
"""
import math
import itertools as it
import heapq

def gcd(a, b):
        return a if b == 0 else gcd(b, a%b)
N=int(input())
A=list(enumerate(map(int, input().split())))
 
if len(A) == 2:
        print(max(A[0][1], A[1][1]))
        exit()
 
maxX=-1
for i, ai in enumerate(A):
        x=10**20
        l=list(A)
        l0, l1=(l[1][1], l[2][1]) if i == 0 else (l[0][1], l[2][1]) if i == 1 else (l[0][1], l[1][1]) 
 
        x=gcd(l0, l1)
        for (j, lj) in l[3:]:
                if i != j:
                        x=gcd(x, lj)
        maxX=max(x, maxX)
print(maxX)

Submission status: Wrong Answer

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

if M == 1:
    A.remove(1)
    M = min(A)
tmp = 1
for i in reversed(range(2,M+1)):
    count = 0
    memo = 0
    for j in range(len(A)):
        if A[j]%i != 0:
            count += 1
            memo = j
            if count > 1:
                break
    if count <= 1:
        tmp = i
        break
        

print(tmp)