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()))
ans = 0

from functools import reduce
def gcd(x,y=0):
    while y:
        x,y = y, x % y
    return x

ans = max(reduce(gcd,a[1:n]),reduce(gcd,a[0:n-1]))
if n>2:
    for i in range(1,n - 1):
        ans = max(ans,gcd(reduce(gcd,a[0:i]),reduce(gcd,a[i + 1:n])))
ans

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

L_A = [0] * (N+1)
R_A = [0] * (N+1)
for i in range(N):
  L_A[1+1] = gcd(L_A[i],A[i])
for i in reversed(range(N)):
  R_A[i] = gcd(R_A[i+1],A[i])
print(max(gcd(L_A[i],R_A[i]) for i in range(N)))

Submission status: Wrong Answer

Solution code:
import fractions as fc
import copy
from functools import reduce

N = int(input())
yaku = [0]*N
X = input().split()
X = list(set(X))
if len(X) == 1:
    print(X[0])
    exit()
X = list(map(int, X))


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


for i in range(len(X)):
    Xwork = copy.copy(X)
    del(Xwork[i])
    yaku[i] = gcd_list(Xwork)

print(max(yaku))

Submission status: Wrong Answer

Solution code:
import fractions as fc
import copy
from functools import reduce

N = int(input())
yaku = [0]*N
X = input().split()
X = list(set(X))
if len(X) == 1:
    print(X[0])
    exit()
print(X)
X = list(map(int, X))


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


for i in range(len(X)):
    Xwork = copy.copy(X)
    del(Xwork[i])
    yaku[i] = gcd_list(Xwork)

print(max(yaku))

Submission status: Wrong Answer

Solution code:
import fractions as fr
import copy
from functools import reduce
 
N = int(input())
yaku = [0]*N
X = input().split()
X = list(set(X))
if len(X) == 1:
    print(X[0])
    exit()
X = list(map(int, X))
 
 
def gcd_list(numbers):
    return reduce(fr.gcd, numbers)
 
 
for i in range(len(X)):
    Xwork = copy.copy(X)
    del(Xwork[i])
    yaku[i] = gcd_list(Xwork)
 
print(max(yaku))

Submission status: Wrong Answer

Solution code:
N=int(input())
As=[int(i) for i in input().split()]
def gcd(m,n):
    x=max(m,n)
    y=min(m,n)
    if x%y==0:
        return y
    else:
        while x%y!=0:
            z=x%y
            x=y
            y=z
        else:
            return z

As.sort()
gcd1=[gcd(As[0], As[i]) for i in range(N)]
gcd2=[gcd(As[1], As[i]) for i in range(N)]
gcd1.sort()
gcd2.sort()
if gcd1.count(min(gcd1))<2:
    print(gcd1[1])
else:
    del gcd2[0]
    print(min(gcd2))

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd
N = int(input())
A = list(map(int,input().split()))
gcd_max = 1
for i in range(N):
    for j in range(N-i-1):
        gcd_now = gcd(A[i], A[N-j-1])
        if(gcd_max < gcd_now): gcd_max = gcd_now

print(gcd_max)

Submission status: Wrong Answer

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

def gcd(m,n):
    x=max(m,n)
    y=min(m,n)
    if x%y==0:
        return y
    else:
        while x%y!=0:
            z=x%y
            x=y
            y=z
        else:
            return z
          
As.sort()
gcd1=[gcd(As[0], As[i]) for i in range(N)]
gcd2=[gcd(As[1], As[i]) for i in range(N)]
gcd1.sort()
gcd2.sort()
if gcd1.count(min(gcd1))==N-1:
    print(gcd2[1])
else:
    print(gcd1[1])

Submission status: Wrong Answer

Solution code:
num = int(input())
ls = [int(x) for x in input().split(' ')]
divisors = []

# 入力値に含まれる値の最大値から、公約数すべてを取得する
max_num = max(ls)
for i in range(1, int(max_num**0.5)+1):
    if max_num % i == 0:
        divisors.append(i)
        if i != max_num // i:
            divisors.append(max_num//i)
divisors = sorted(divisors, reverse = True) # 降順に並び替え 
for div in divisors:
    count = 0
    for num in ls:
        if count > 1:
            break
        elif num % div != 0:
            count += 1
    if count <= 1:
        print(div)
        break

Submission status: Wrong Answer

Solution code:
num = int(input())
ls = [int(x) for x in input().split(' ')]
divisors = []

# 入力値に含まれる値の最大値から、公約数すべてを取得する
max_num = max(ls)
for i in range(1, int(max_num**0.5)+1):
    if max_num % i == 0:
        divisors.append(i)
        if i != max_num // i:
            divisors.append(max_num//i)
divisors = sorted(divisors, reverse = True) # 降順に並び替え 
for div in divisors:
    count = 0
    for num in ls:
        if num % div != 0:
            count += 1
    if count <= 1:
        print(div)
        break

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd

N = int(input())
A = [int(a) for a in input().split(" ")]
if N==2:
    print(gcd(*A))
else:
    left = reduce(lambda acc, x: acc+[gcd(x, acc[-1])], A, A[:1])
    right = reduce(lambda acc, x: acc+[gcd(x, acc[-1])], A[::-1], A[-1:])[::-1]

    ans = max([gcd(left[i-1], right[i+1]) for i in range(1,N-1)])
    ans = max([right[1], ans, left[-2]])
    print(ans)

Submission status: Wrong Answer

Solution code:
def insu(n):
    ys = []
    for i in range(2,  n//2):
        if n % i == 0:
            ys.append(i)
    if n > n//2:
        ys.append(n)

    return ys


l = int(input())
nums = list(map(int, input().split(" ")))
count = {}

for i in range(0, len(nums)):
    tmp = insu(nums[i])
    for n in tmp:
        count.setdefault(n, 0)
        count[n] += 1

rev = {}
for k, v in sorted(count.items(), key=lambda x: -x[0]):
    rev[k] = v

for k, v in rev.items():
    if v == l-1:
        print(k)
        break

Submission status: Wrong Answer

Solution code:
def insu(n):
    ys = []
    for d in range(1, n+1):
        if n % d == 0:
            ys.append(d)

    return ys


l = int(input())
nums = list(map(int, input().split(" ")))
count = {}

for i in range(0, len(nums)):
    tmp = insu(nums[i])
    for n in tmp:
        count.setdefault(n, 0)
        count[n] += 1

rev = {}
for k, v in sorted(count.items(), key=lambda x: -x[0]):
    rev[k] = v

for k, v in rev.items():
    if v == l-1:
        print(k)
        break

Submission status: Wrong Answer

Solution code:
size=input()
size=int(size)
A=input().split()
GCD=1
for i in range(0,size):
    for j in range(1,size):
        a=int(A[i])
        b=int(A[j])
        if (i==j):
            continue
        candidate=1
        k=1
        while(True):
            if(k>max(a,b)):
                break
            if(a%k==0 and b%k==0):
                candidate=candidate*k
                a=a/k
                b=b/k
                k=2
            else:
                k+=1
        if (candidate>GCD):
            GCD=candidate

print(GCD)

Submission status: Wrong Answer

Solution code:
size=input()
size=int(size)
A=input().split()
GCD=1
for i in range(0,size):
    for j in range(1,size):
        a=int(A[i])
        b=int(A[j])
        if (i==j):
            continue
        candidate=1
        for k in range(1,max(a,b)):
            if (k>max(a,b)):
                break
            if(a%k==0 and b%k==0):
                candidate=candidate*k
                a=a/k
                b=b/k
        if (candidate>GCD):
            GCD=candidate

print(GCD)

Submission status: Wrong Answer

Solution code:
MAX = 10**9
N = int(input())
A = list(map(int,input().split()))

def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if x%y == 0:
        return y
    else:
        while x%y != 0:
            z = x%y
            x = y
            y = z
        else:
            return z

def is_prime(n):
    i = 2
    while i*i <= n:
        if n%i == 0:
            return False
        i += 1
    return True

G = A[0]
for a in A:
    G = gcd(G,a)

B = []
for a in A:
    n = a/G
    if not is_prime(n):
        B.append(n)
if len(B) == 0:
    B = [1]

G_ = B[0]
for b in B:
    G_ = gcd(G_,b)

print(int(G_*G))

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
ints = list(map(int, input().split()))
ints = list(set(ints))

gcd = 0
result = 1
holed_ints = []
for i in range(len(ints)):
    holed_ints = ints[:i] + ints[i+1:]
    gcd = 0
    for j in range(len(holed_ints)):
        gcd = fractions.gcd(gcd, holed_ints[j])
    if gcd > result:
        result = gcd

print(result)

Submission status: Wrong Answer

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()
	
#ある要素がない時の、gcdの最小値を求める
#答えは最小の要素以下と決まっている。
#累積和
ans=inf
acc=[]
for idx in range(N):
	if idx==0:
		acc.append(A[idx])
	else:
		acc.append(fractions.gcd(acc[-1],A[idx]))
		
		
#print(acc)
	
minidx = acc.index(min(acc))
#print(minidx)
original_A=[i for i in A]
del A[minidx]

acc=[]
for idx in range(N-1):
	if idx==0:
		acc.append(A[idx])
	else:
		acc.append(fractions.gcd(acc[-1],A[idx]))
		
#print(acc)

ans=min(acc)
if minidx==0:
	print(ans)
	exit()

A=original_A
del A[minidx-1]
acc=[]
for idx in range(N-1):
	if idx==0:
		acc.append(A[idx])
	else:
		acc.append(fractions.gcd(acc[-1],A[idx]))
		
#print(acc)
ans=max(ans, min(acc))


print(ans)

Submission status: Wrong Answer

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()
	
#ある要素がない時の、gcdの最小値を求める
#答えは最小の要素以下と決まっている。
#累積和
ans=inf
acc=[]
for idx in range(N):
	if idx==0:
		acc.append(A[idx])
	else:
		acc.append(fractions.gcd(acc[-1],A[idx]))
		
		
#print(acc)
	
minidx = acc.index(min(acc))
#print(minidx)
original_A=[i for i in A]
del A[minidx]

acc=[]
for idx in range(N-1):
	if idx==0:
		acc.append(A[idx])
	else:
		acc.append(fractions.gcd(acc[-1],A[idx]))
		
#print(acc)

ans=min(acc)

A=original_A
del A[minidx-1]
acc=[]
for idx in range(N-1):
	if idx==0:
		acc.append(A[idx])
	else:
		acc.append(fractions.gcd(acc[-1],A[idx]))
		
#print(acc)
ans=max(ans, min(acc))


print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
oc = []
b = 0
for i, v in c.items():
    if v == 1:
        oc.append(i)
    else:
        if b == 0:
            b = i
        else:
            b = gcd(b, i)

loc = len(oc)
if loc == 0:
    print(b)
    exit()
elif loc == 1:
    print(gcd(oc[0], b))
    exit()

c = [oc[0], oc[1]]
if b:
    c += [b]
e = gcd(oc[0], oc[1])
for i in range(2, loc):
    g1 = gcd(e, oc[i])
    d = []
    for j in c:
        g2 = gcd(j, oc[i])
        if g2 > 1:
            d.append(g2)
    c = d
    if e > 1:
        c.append(e)
    e = g1
    
if c:
    print(max(c))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n = int(input())
a = list(set(map(int,input().split())))
a.sort(reverse=True)
la = len(a)
if la == 1:
    print(a[0])
    exit()

b = gcd(a[0], a[1])
c = [a[0], a[1]]
for i in range(2, la):
    g1 = gcd(b, a[i])
    d = []
    for j in c:
        g2 = gcd(j, a[i])
        if g2 > 1:
            d.append(g2)
    c = d
    if b > 1:
        c.append(b)
    b = g1

if c:
    print(max(c))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)

n = ip()                                     #入力整数1つ
#a,b,t= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#k = ip()                      #入力セット(整数改行あり)(1/2)
#v=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
if n==2:
    print(max(a))
else:
    ans = []
    s = [a[0]]
    for i in range(1,n):
        s.append(fractions.gcd(s[i-1],a[i]))
    r = [a[n-1]]
    for i in range(n-2,-1,-1):
        r.append(fractions.gcd(r[0],a[i]))
    r = r[::-1]
 #   print(s,"from left")
 #   print(r,"from right")
    ans.append(r[1])
    for i in range(1,n-1):
        ans.append(fractions.gcd(s[i-1],r[i+1]))
    ans.append(s[n-2])
    print(max(ans))

Submission status: Wrong Answer

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

for x in range(a + 1, 1, -1):
    l = [aa for aa in lst if aa % x == 0]
    if len(l) + 1 >= N:
        print(x)
        exit(0)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if y>x:
        a=y
        y=x
        x=a
    a=1
    while a*y:
        a=x%y
        x=y
        y=a
    return x

a=input()
b=list(map(int,input().split()))
l=[0]
r=[0]
g=[]
for a in range(len(b)-1):
    l.append(gcd(l[-1],b[a]))
    r.append(gcd(r[-1],b[-1-a]))
print(l)
print(r)
for a in range(len(l)):
    g.append(gcd(l[a],r[-1-a]))
print(max(g))

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if y>x:
        a=y
        y=x
        x=a
    a=1
    while a*y:
        a=x%y
        x=y
        y=a
    return x

a=input()
b=list(map(int,input().split()))
l=[0]
r=[0]
g=[]
for a in range(len(b)-1):
    l.append(gcd(max(l),b[a]))
    r.append(gcd(max(r),b[-1-a]))
for a in range(len(l)):
    g.append(gcd(l[a],r[-1-a]))
print(max(g))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
for i in range(n):
    if A[i]<0:
        A[i] = -A[i]
if n % 2 == 1:
    print(sum(A)-2*min(A))
else:
    print(sum(A))

Submission status: Wrong Answer

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

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

L_gcd = [0]*(N)
R_gcd = [0]*(N)

for i in range(N):
    L_gcd[i] = gcd(L_gcd[i-1],A[i])
for i in reversed(range(N)):   
    R_gcd[i] = gcd(R_gcd[i-1],A[i])

ans = max(L_gcd[-1],R_gcd[0])
for i in range(N):
    if gcd(L_gcd[i],R_gcd[N-i-1])>ans:
        ans = gcd(L_gcd[i],R_gcd[N-i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions

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

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

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

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

print(A)
print(L)
print(R)
print(max(fractions.gcd(L[i], R[i + 2]) for i in range(0, N)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions

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

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

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

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

print(A)
print(L)
print(R)
print(max(fractions.gcd(L[i], R[i + 1]) for i in range(0, N)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions

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

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

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

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

print(max(fractions.gcd(L[i], R[i + 1]) for i in range(0, N)))

Submission status: Wrong Answer

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
for i in range(max(ar),1,-1):
    count = 0
    for r in ar:
        if r % i == 0:
            count += 1
    if count >= a-1:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import fractions

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

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

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

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

print(max(fractions.gcd(L[i], R[i + 1]) for i in range(0, N)))

Submission status: Wrong Answer

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

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

L_gcd = [0]
R_gcd = [0]

for i in range(1,N):
    L_gcd.append(gcd(L_gcd[i-1],A[i]))
    R_gcd.insert(0,gcd(A[N-i-1],R_gcd[0]))

ans = [max(L_gcd[-2],R_gcd[1])]
for i in range(N-2):
    ans.append(gcd(L_gcd[i],R_gcd[i+2]))
print(max(ans))

Submission status: Wrong Answer

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

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

L_gcd = [A[0]]
R_gcd = [A[-1]]

for i in range(1,N):
    L_gcd.append(gcd(L_gcd[i-1],A[i]))
    R_gcd.insert(0,gcd(A[N-i-1],R_gcd[0]))

ans = [max(L_gcd[-1],R_gcd[0])]
for i in range(N-2):
    ans.append(gcd(L_gcd[i],R_gcd[i+2]))
print(max(ans))

Submission status: Wrong Answer

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

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

L_gcd = [A[0]]
R_gcd = [A[-1]]

for i in range(1,N):
    L_gcd.append(gcd(L_gcd[i-1],A[i]))
    R_gcd.insert(0,gcd(A[N-i-1],R_gcd[0]))

ans = max(L_gcd[-1],R_gcd[0])

for i in range(N-2):
    if max(L_gcd[i],R_gcd[i+2])<=ans:
        break
    if gcd(L_gcd[i],R_gcd[i+2])>ans:
        ans = gcd(L_gcd[i],R_gcd[i+2])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
from functools import reduce

def fgcd(num1,num2):

    a = max(num1,num2)
    b = min(num1,num2)
    
    tmp = a%b
    
    while tmp != 0 :
        a = b
        b = tmp
        tmp = a%b
    
    return(b)      


def gcd(*numbers):
    return reduce(fgcd, numbers)

str_input = input()
n = int(str_input)

str_input = input()
a = []
for tmp in str_input.split():
    a.append(int(tmp))

index_min = a.index(min(a))
a[index_min] = max(a)

print(gcd(*a))

Submission status: Wrong Answer

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

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

L_gcd = [A[0]]
R_gcd = [A[-1]]

for i in range(1,N):
    L_gcd.append(gcd(L_gcd[i-1],A[i]))
    R_gcd.insert(0,gcd(A[N-i-1],R_gcd[0]))

ans = max(L_gcd[-2],R_gcd[1])

for i in range(N-2):
    if min(L_gcd[i],R_gcd[i+2])<=ans:
        break
    if gcd(L_gcd[i],R_gcd[i+2])>ans:
        ans = gcd(L_gcd[i],R_gcd[i+2])
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=[0]+list(map(int,input().split()))+[0]
b=a[::-1]
l=[0]*n
r=[0]*n
ans=[]
for i in range(1,n):
  l[i]=fractions.gcd(l[i-1],a[i-1])
for j in range(1,n):
  r[i]=fractions.gcd(r[i-1],b[i-1])
c=b[::-1]
for k in range(n):
  ans.append(fractions.gcd(l[k],r[k]))
print(max(ans))

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
a = list(map(int,input().split()))
ans = []  
if len(a) == 2:
    print(max(a))
elif len(a) == 3:
    x1 = fractions.gcd(a[0],a[1])
    x2 = fractions.gcd(a[1],a[2])
    x3 = fractions.gcd(a[0],a[2])
    print(max(x1,x2,x3))
else:
    x = a[0]
    for i in range(1,len(a)//2-1):
        x = fractions.gcd(x,a[i])
    ans.append(x)
    x = a[len(a)//2]
    for i in range(len(a)//2,n-1):
        x = fractions.gcd(x,a[i])
    ans.append(x)
    print(max(ans))

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
a = list(map(int,input().split()))
ans = []  
if len(a) == 2:
    print(max(a))
elif len(a) == 3:
    x1 = fractions.gcd(a[0],a[1])
    x2 = fractions.gcd(a[1],a[2])
    x3 = fractions.gcd(a[0],a[2])
    print(max(x1,x2,x3))
else:
    x = a[0]
    for i in range(1,len(a)//2-1):
        x = fractions.gcd(x,a[i])
    ans.append(x)
    x = a[len(a)//2]
    for i in range(len(a)//2,n-1):
        x = fractions.gcd(a[i],a[i+1])
    ans.append(x)
    print(max(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
lista = [int(i) for i in input().split()]

#2数の最大公約数を返す関数
#ユークリッドの互除法より
def gcd(a,b):
    if b>a:
        a,b = b,a
    if a%b == 0:
        return b
    else:
        return gcd(b,a%b)
    
#頭からとおしりから順にそこまでの最大公約数を計算する
head = lista[0]
tail = lista[n-1]
headlist,taillist = [],[]
for i in range(n):
    headlist.append(gcd(head,lista[i]))
    head = headlist[i]
    taillist.append(gcd(tail,lista[-1*(i+1)]))
    tail = taillist[i]
    
#print(headlist)
#print(taillist)

if n==2:
    print(gcd(lista[0],lista[1]))
else:
    mhead = taillist[-2]
    mtail = headlist[-2]
    maxval=max(mhead,mtail)
    for i in range(1,n-1):
        maxtemp = gcd(headlist[i-1],taillist[-1*i-2])
        if maxtemp>maxval:
            maxval= maxtemp
    print(maxval)

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
a = list(map(int,input().split()))
b = []
c = []
d = []

b.append(fractions.gcd(a[0],a[1]))
for i in range(1,n-1):
  b.append(fractions.gcd(b[i-1],a[i+1]))
c.append(fractions.gcd(a[n-1],a[n-2]))
for i in range(1,n-1):
  c.append(fractions.gcd(c[i-1],a[n-i-2]))
if n == 2:
  d.append(max(a))
elif n == 3:
  d.append(max(b+c))
  d.append(fractions.gcd(a[0],a[2]))
elif n == 4:
  d.append(c[n-3])
  d.append(fractions.gcd(a[0],c[n-4]))
  d.append(fractions.gcd(a[n-1],b[n-4]))
  d.append(b[n-3])
else:
  d.append(c[n-3])
  d.append(fractions.gcd(a[0],c[n-4]))
  for i in range(0,n-3):
  	d.append(fractions.gcd(b[i],c[n-4-i]))
  d.append(fractions.gcd(a[n-1],b[n-4]))
  d.append(b[n-3])
print(max(d))

Submission status: Wrong Answer

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

n = int(input())
a = list(map(int, input().split()))
left, right = [0] * (n+1), [0] * (n+1)
print(left, right)
for i in range(n):
    left[i+1] = gcd(left[i], a[i])
for i in reversed(range(n)):
    right[i] = gcd(right[i+1], a[i])
ans = 0
for i in range(n):
    l = left[i]
    r = right[i+1]
    ans = max(ans, gcd(l, r))
print(ans)

Submission status: Wrong Answer

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

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


# import math
# from functools import reduce

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

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


def gcd_l(x):
    max_element = 1
    if len(x)==1:
        return x[0]
    elif len(x) == 2:
        return max(x[0], x[1])
    else:
        element_1 = gcd(x[0], x[1])
        element_2 = gcd(x[len(x) - 3], x[len(x)-1])
        element = max(element_1, element_2)
        
        for i in range(2,len(x)):
            element=gcd(element,x[i])
            if gcd(element, x[i]) > max_element:
                max_element = element
            
        return max_element

ans = 0
times = 0


print(gcd_l(_A_arr))

Submission status: Wrong Answer

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=fractions.gcd(A[0],A[1])
b=fractions.gcd(A[0],A[2])
c=fractions.gcd(A[1],A[2])
print(max(a,b,c))

Submission status: Wrong Answer

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

from fractions 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: Wrong Answer

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

from fractions 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: Wrong Answer

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))
    if len(sorted_final_result) == 1:
        print(sorted_final_result[0])
        exit()
    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[2])

Submission status: Wrong Answer

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

def gcd(x, y):
    x, y = max(x, y), min(x, y)
    while x%y > 0:
        x, y = y, x%y
    return y

a = sorted(a)
ans = [gcd(a[0], a[1]), a[0]]
for i in range(2, n):
    ans_0, ans_1 = ans
    ans_0_new = gcd(ans_0, a[i])
    ans_1_new = max(ans_0, gcd(ans_1, a[i]))
    ans = [ans_0_new, ans_1_new]
print(max(ans))

Submission status: Wrong Answer

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

def gcd(x, y):
    x, y = max(x, y), min(x, y)
    while x%y > 0:
        x, y = y, x%y
    return y

a = sorted(a)
gcd_tmp = a[1]
for i in range(2, n):
    gcd_tmp = gcd(gcd_tmp, a[i])
ans = [a[0], gcd_tmp]
for i in range(1, n):
    ans_0, ans_1 = ans
    ans_0_new = gcd(ans_0, a[i])
    ans_1_new = max(ans_0, gcd(ans_1, a[i]))
    ans = [ans_0_new, ans_1_new]
print(max(ans))

Submission status: Wrong Answer

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))
    if len(sorted_final_result) == 1:
        print(sorted_final_result[0])
        exit()
    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: Wrong Answer

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