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:
import functools
import fractions


N = int(input())
a = list(map(int,input().split()))
ans = 1
gcd = functools.reduce(fractions.gcd,a)
s_front = [1]*N
s_back = [1]*N
s_front[0] = a[0]
s_back[0] = a[N-1]
if N == 2:
	print(max(a))
	exit()

for i in range(N):
	if i > 0:
		s_front[i] = fractions.gcd(s_front[i-1],a[i])
		s_back[i] = fractions.gcd(s_back[i-1],a[N-i-1])
for i in range(N):
	if i == 0:
		gcd_new = s_back[N-2]
	elif i == N:
		gcd_new = s_front[N-2]
	else:
		gcd_new = fractions.gcd(s_front[i-1],s_back[N-i-2])
	ans = max(ans,gcd_new)
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(aa, bb):
	while bb:
		aa, bb = bb, aa % bb
	return aa

n = int(input())

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

a = sorted(a, reverse=True)
ans = 1
for i in range(n):
    flag = False
    j = 0
    if i == 0:
        tmp = a[1]
    else:
        tmp = a[0]
    while not flag and j != n:
        if j != i:
            tmp = gcd(tmp, a[j])
        if tmp <= ans:
            flag = True
        j += 1
    if tmp > ans:
        ans = tmp
    if ans >= a[n-1]:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
def max_gcd(closed, rest):
    l = len(rest)
    if l == 1:
        return closed
    elif l == 2:
        return gcd(closed, max(rest))
    half = l // 2
    left = rest[:half]
    right = rest[half:]
    gcd_left = reduce(gcd, left) 
    gcd_right = reduce(gcd, right) 
    if gcd_left == gcd_right:
        return gcd_left
    elif gcd_left > gcd_right:
        return max_gcd(gcd_left, right)
    else:
        return max_gcd(gcd_right, left)
half = n // 2
left = a[:half]
right = a[half:]
gcd_left = reduce(gcd, left) 
gcd_right = reduce(gcd, right) 
print(max(max_gcd(gcd_left, right),
          max_gcd(gcd_right, left)))

Submission status: Wrong Answer

Solution code:
import collections

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


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


li = []
for i in range(N):
    li.extend(make_divisors(A[i]))
c = collections.Counter(li)
values, counts = zip(*c.most_common())

result = 0
for i in range(1, len(c)):
    if N-1 == counts[i]:
        result = max(values[i], result)

if 1 == len(list(set(A))):
    print(A[0])
else:
    print(result)

Submission status: Wrong Answer

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

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

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

for i in range(N-1):
    gcd1 = 0
    A2 = A[:i] + A[i+1:]
    for j in range(N-2):
        gcd1 = gcd(A2[j], A2[j+1])
        A2[j+1] = gcd1
    L.append(gcd1)

print(max(L))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

from functools import reduce
import copy
import fractions

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


gcds = []

for i in range(N):
    for x in set(integers):
        tmp_integers = copy.copy(integers)
        tmp_integers[i] = x
        print(tmp_integers)
        gcds.append(reduce(fractions.gcd, tmp_integers))
print(max(gcds))

Submission status: Wrong Answer

Solution code:
import fractions
import math
from functools import reduce


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


def using_sqrt(limit):
    n = 0
    for k in range(2, limit+1):
        factor = 0

        # 2以外の偶数は素数ではないので無視する
        if k % 2 == 0 and k != 2:
            continue

        # 繰り返しの上限を対象の平方根にする
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1

        if factor == 0:
            n += 1

    return n


n = int(input())
li_input = list(map(int, input().split()))
li = list(set(li_input))
    
count = 0
max_gcd = 0

if len(li) == 1:
    max_gcd = li[0]
else:
    for i in range(len(li)):
        if count > 1:
            ret = 1
            break

        if li[i] <= max_gcd:
            continue

        if using_sqrt(li[i] != 0):
            count += 1

        tmp = li[i]
        del li[i]
        tmp_gcd = gcd(*li)
        if max_gcd < tmp_gcd:
            max_gcd = tmp_gcd
        li.insert(0, tmp)

print(max_gcd)

Submission status: Wrong Answer

Solution code:
import heapq

def gcd(a,b):
  while a>0 and b>0:
    a=a%b
    if not a>0:
      break
    b=b%a

  if a>b:
    return a
  return b

N=int(input())
arr=list(map(int,input().split(" ")))
arr.append(arr[0])

r=[]

mp=0

for i in range(len(arr)-1):
  buf=gcd(arr[i],arr[i+1])
  r.append(buf)
  if r[mp]<buf:
    mp=len(r)-1

arr[mp]=max(arr)

r=[]
mp=0
for i in range(len(arr)-1):
  buf=gcd(arr[i],arr[i+1])
  r.append(buf)
  if r[mp]<buf:
    mp=len(r)-1

print(r[mp])

Submission status: Wrong Answer

Solution code:
from functools import reduce
import fractions
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

N = int(input())
A = [int(i) for i in input().split()]
gc = gcd_list(A)
A.sort()
current = A[0]
for i in range(N):
    current = fractions.gcd(current,A[i])
    if(gc==current):
        break
if(N==2):
    print(max(A))
elif(i<N-1):
    gc2 = gcd_list(A[i+1:])
    for j in range(i+1):
        if(gc==fractions.gcd(gc2,A[j])):
            break
    A.pop(j)
    print(gcd_list(A))
else:
    A.pop(N-1)
    print(gcd_list(A))

Submission status: Wrong Answer

Solution code:
from functools import reduce
import numpy as np


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


def gcd(*n):
    return reduce(_gcd, n)


def my_bisect(x):

    def find_pos(left, right):
        return left + (right - left - 1) // 2

    def func(left, right, prev):
        if right - left == 1:
            return [left]
        if right - left == 2:
            return [left, left+1]
        pos = find_pos(left, right)
        g1 = gcd(*x[left:pos+1])
        g2 = gcd(*x[pos+1:right])
        if prev is None:
            g1, g2 = gcd(g1, prev), gcd(g2, prev)
        if g1 < g2:
            return func(left, pos + 1, max(g1, g2))
        else:
            return func(pos + 1, right, max(g1, g2))
    return func(0, len(x), None)


N = int(input())
A = list(map(int, input().split()))
print(max([gcd(*np.delete(A, i)) for i in my_bisect(A)]))

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    a,b=sorted([a,b])
    while b%a!=0:
        a,b=b%a,a
    return a
n=int(input())
a=list(map(int, input().split()))
if(n==2):
    print(max(a))
    quit()
ans=0
for i in range(n):
    l=a[0:i]+a[i+1:n]
    while len(l)!=1:
        tmpl=[]
        for i in range(1,len(l),2):
            tmpl.append(gcd(l[i-1],l[i]))
        l=tmpl
    ans=max(ans,l[0])
print(ans)

Submission status: Wrong Answer

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

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

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

if n < 1000:
  for i in range(n):
    b = copy.copy(a)
    b.pop(i)
    t = gcd(b)
    if ans < t:
      ans = t
else:
  for _ in range(200):
    t = gcd(random.sample(a, 50))
    if ans < t:
      ans = t

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def gcd(a,b):
    if a%b == 0:
        return b
    else:
        return gcd(b, a%b)
    
n = ni()
a = list(li())

a.sort()

gcds = [0]*n

for i in range(n):
    gcdt = float('inf')
    for j in range(n):
        if i == j:
            continue
        
        gcdt = min(gcdt, gcd(a[i], a[j]))
        
        if gcdt == 1:
            gcds[i] = 1
            break
        
        if j == n-1:
            gcds[i] = gcdt
    
    
gcds.sort()
print(gcds[1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
w=sorted(a)
ans=1
end=False

flag=False
for h in range(n):
    if a[h]%w[0]!=0 and flag==True:
       break
    if a[h]%w[0]!=0:
        flag=True
    if h==n-1:
        ans=w[0]
        end=True

y=1000000001
for i in range(n):
    y=min(y,-(-a[i]//2))
    



for i in reversed(range(2,y+1)):
    flag=False
    if end==True:
        break
    for h in range(n):
        if a[h]%i!=0 and flag==True:
            break
        if a[h]%i!=0:
            flag=True
        if h==n-1:
            ans=i
            end=True
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy
N = int(input())
integerList = list(map(int, input().split()))

table = []
for s in integerList:
    miniTable = []
    for i in range(2,int(s**0.5)+1):
         if s % i == 0:
             miniTable.append(i)
             if i != s//i:
                 miniTable.append(s//i)
    miniTable.append(s)
    table.append(miniTable)
# print(table)

items = []
for v in table:
    for item in v:
        items.append(item)

print(items)
kouyakusuList = []
for item in items:

    if items.count(item) >= N-1:
        kouyakusuList.append(item)
        # print(item)

print(max(kouyakusuList))

Submission status: Wrong Answer

Solution code:
import math
import os
import random
import re
import sys
from collections import defaultdict

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


def solve(n, a):
    if n < 1:
        return 0
    if n == 1:
        return a[0]
    if n == 2:
        return gcd(a[0], a[1])

    dp = [False for _ in range(100000001)]

    gcd0 = gcd(a[0], a[1])
    dp[gcd0] = True
    gcd1 = gcd(gcd0, a[2])
    if gcd1 == gcd0:
        zero = gcd(a[1], a[2])
        one = gcd(a[0], a[2])
        if zero > one:
            dp[zero] = True
            gcd0 = zero
        else:
            dp[one] = True
            gcd0 = one
    dp[gcd0] = True
    for i in range(2,n):
        gcd0 = gcd(gcd0, a[i])
        dp[gcd0] = True

    count = 0
    start = 0
    for i in range(len(dp)):
        if dp[i] == True:
            start = i
            break
    if start == len(dp):
        return start
    for i in range(start+1, len(dp)):
        if dp[i] == True:
            return i
    return start

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    print(solve(n,a))

Submission status: Wrong Answer

Solution code:
def c():
    ans = 0
    N = long(raw_input())
    A = raw_input().split()
    for i in range(N):
        for j in range(i+1, N):
            C = long(A[i])
            D = long(A[j])
            #prlong "C:{}, D:{}".format(C,D)
            if C == D:
                g = C
            elif C > D:
                g = gcd(C, D, ans)
            else:
                g = gcd(D, C, ans)
            if g > ans:
                ans = g
    print ans

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

def main():
    c()
	
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a = sorted(A)
ans_list = []
def gcd(a,b):
    if a<b:
        a,b=b,a
    if b==0:
        return a
    c=a%b
    return gcd(b,c)

if len(A) == 2:
    ans_list.append(max(A))
try:
    for y in range(N):
        lis =list(A[:y:] + A[y+1:])
        fla = gcd(lis[0],lis[1])
        for x in range(N-1):
            ans_one = gcd(fla,lis[x])
        ans_list.append(ans_one)
except:
    pass
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
_=input()
l=list(map(int, input().split()))
def find_gcd(x, y): 
    while(y): 
        x, y = y, x % y   
    return x 
def multi_gcd(l):
    if len(l)==1:
        return l[0]
    else:
        gcd=find_gcd(l[0],l[1]) 
        for j in range(2,len(l)): 
            gcd=find_gcd(gcd,l[j])
        return gcd

gcd_max=0
key=[-1,-1]
if len(l)==2:
    max_gcd=(max(l[0],l[1]))
elif len(l)==3:
    max_gcd=max(find_gcd(l[0],l[1]),find_gcd(l[0],l[2]),find_gcd(l[2],l[1]))
else:
    l_left,l_right=l[:len(l)//2],l[len(l)//2:]
    a,b=multi_gcd(l_left),multi_gcd(l_right)
    max_gcd=multi_gcd(l)
    l_mod_flag_a=[int(i%a>0) for i in l ]
    l_mod_flag_b=[int(i%b>0) for i in l ]
    if sum(l_mod_flag_a)==1:
        try : key[0]=l_mod_flag_a.index(1)
        except : pass
    if sum(l_mod_flag_b)==1:
        try: key[1]=l_mod_flag_b.index(1)
        except : pass
    for i in range(2):
        if key[i]!=-1:
            idx=key[i]
            if idx==len(l)-1:
                l_pop=l[:-1]
            else:
                l_pop=l[0:idx]+l[idx:]
            gcd=multi_gcd(l_pop)
            if (max_gcd<=gcd):
                max_gcd=gcd
               
        else : 
            pass
        
print(max_gcd)

Submission status: Wrong Answer

Solution code:
# ユークリッドの互除法
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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


ans = 1
for j in range(2,max(aa)+1):
    count = 0
    for i in range(n):
        if aa[i]%j==0: count +=1
    if count >= n-1:
        ans = j
        print(ans)
        break
else:
    print(1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
gcdL = [0] * N
gcdR = [0] * N

gcdL[0] = A[0]
gcdR[-1] = A[-1]

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


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

ans = gcdR[1]
for i in range(1, N-1):
    tmp = gcd(gcdL[i-1], gcdR[i+1])
    ans = max(tmp, ans)
ans = max(ans, gcdL[N-1])
print(ans)

Submission status: Wrong Answer

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

def factors(num):
    L = []
    for i in range(1, int(num**(1/2))+1):
        if num%i == 0:
            L.append(i)
            L.append(num//i)
    return L

first = a.pop(0)
second = a.pop(0)
in_all = [i for i in factors(first) if i in factors(second)]
in_almost_all = [i for i in factors(second)+factors(first) if i not in in_all]

while len(a) > 0:
    x = a.pop(0)
    x_factors = factors(x)
    in_all = [i for i in in_all if i in x_factors]
    in_almost_all = [i for i in in_almost_all if i in x_factors]

print(max(in_all + in_almost_all))

Submission status: Wrong Answer

Solution code:
import functools
import fractions


N = int(input())
a = list(map(int,input().split()))
ans = 1
gcd = functools.reduce(fractions.gcd,a)
s_front = [1]*N
s_back = [1]*N
s_front[0] = a[0]
s_back[0] = a[N-1]
for i in range(N):
	if i > 0:
		s_front[i] = fractions.gcd(s_front[i-1],a[i])
		s_back[i] = fractions.gcd(s_back[i-1],a[N-i-1])
for i in range(N):
	if i == 0:
		gcd_new = s_back[N-2]
	elif i == N:
		gcd_new = s_front[N-2]
	else:
		gcd_new = fractions.gcd(s_front[i-1],s_back[N-i-2])
	ans = max(ans,gcd_new)
print(ans)

Submission status: Wrong Answer

Solution code:
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(map(int, input().split()))

#　書き換える前GCD
gcd_min, idnex_min = 0, 0
G = []
gcd_max = 0
for i in range(N):
    row = []
    for j in range(i):
        if i==j:
            row.append(A[j])
        else:
            val = gcd(A[i], A[j])
            row.append(val)
            if val > gcd_max:
                gcd_max = val
    for j in range(N-i):
        row.append(0)
    G.append(row)
    # pprint.pprint(G)

# pprint.pprint(G)
# print(gcd_min)
# Replace
import copy
bn = []
for i in range(N):
    vmin = 0
    for j in range(N):
        if G[j][i] != 0 and G[j][i] > vmin:
            vmin = G[j][i]
    bn.append(copy.copy(vmin))
# print(bn)

bn.sort()
cnt = 0
for i in range(N):
    if bn[i] != 0:
        vmin, index = bn[i], i
        break
for i in range(i,N):
    if vmin != bn[i]:
        print(bn[i])
        break

Submission status: Wrong Answer

Solution code:
# a>b
dic = {}

def gcd(a, b):
    key = (a, b)
    if key in dic:
        return dic[key]
    ans = 0
    if b == 0:
        ans = a
    else:
        ans = gcd(b, a%b)
    dic[key] = ans
    return ans

def gcd_arr(arr):
    if len(arr)==1:
        return arr[0]
    i = len(arr)//2
    l, r = (gcd_arr(arr[:i]), gcd_arr(arr[i:]))
    return gcd(l, r) if l>r else gcd(r, l)

N = int(input())
As = [int(s) for s in input().split()]
ans = 1
gcdall = gcd_arr(As)

for i in range(N):
    curr = None
    arr = [As[j]//gcdall for j in range(N) if i!=j]
    ans = max(gcd_arr(arr), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions as f

def calc_odd(n):
    if(n % 2 != 0):
        return True
    else:
        return False

N = int(input())
A = [int(i) for i in input().split()]
index_min = A.index(min(A))
index_max = A.index(max(A))
check_odd = list(map(calc_odd,A))
answer = 0

if(check_odd.count(True) == 1):
    index_odd = check_odd.index(True)
    A[index_odd] = max(A)
    answer = A[0]
    for i in range(1, N):
        answer = f.gcd(answer, A[i])
else:
    A[index_min] = A[index_max]
    answer = A[0]
    for i in range(1, N):
        answer = f.gcd(answer, A[i])
print(answer)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
w=sorted(a)
ans=1
end=False

flag=False
for h in range(n):
    if a[h]%w[0]!=0 and flag==True:
       break
    if a[h]%w[0]!=0:
        flag=True
    if h==n-1:
        ans=w[0]
        end=True

y=1000000001
for i in range(n):
    y=min(y,-(-a[i]//2))
    



for i in reversed(range(2,y+1)):
    print(i)
    flag=False
    if end==True:
        break
    for h in range(n):
        if a[h]%i!=0 and flag==True:
            break
        if a[h]%i!=0:
            flag=True
        if h==n-1:
            ans=i
            end=True
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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


maxv = 0
N = int(input())
Value = list(map(int,input().split()))
p = int(N/2)



if gcd_list(Value[:p]) > gcd_list(Value[p:]):
    for i in range(p,N):
        test = Value.copy()
        del test[i]
        if maxv <= gcd_list(test):
            maxv = gcd_list(test)

    for i in range(p):
        test = Value.copy()
        del test[i]
        if maxv <= gcd_list(test):
            maxv = gcd_list(test)

        
print(maxv)

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()))  # 複数数値をリストに格納

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

    if (n - 2) != i and n != 2:
        li.append(gcd(a[i], a[i + 2]))

if(n == 2):
    print(max(a[0], a[1]))
elif (n == 3):
    li.sort()
    print(li[2])
elif n == 4:
    li.sort()
    print(li[3])
else:
    usiro2 = li[-2]
    mae2 = li[1]
    usiro3 = li[-3]
    mae3 = li[2]
    li.sort()

    if li[2] == mae3 or li[1] == usiro3:
        print(li[3])
    elif li[1] == mae2 or li[1] == usiro2:
        print(li[2])
    else:
        print(li[4])

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
ans = 0
A = sorted(A)
def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
ans = 0
ans_ = 0
if N == 2:
  print(max(A))
elif N == 3:
  tmp = [A[0],A[1],A[N-1]]
  for i in range(len(tmp)):
    tmp_ = tmp[:]
    tmp_.pop(i)
    ans_ = gcd_list(tmp_)
    if ans < ans_:
      ans = ans_
  print(ans)
else:
  tmp = [A[0],A[1],A[N-2],A[N-1]]
  tmp_gcd = gcd_list(tmp)
  if tmp_gcd == 1:
    for i in range(len(tmp)):
      tmp_ = tmp[:]
      tmp_.pop(i)
      ans_ = gcd_list(tmp_)
      if ans < ans_:
        ans = ans_
    print(ans)
  else:
    print(tmp_gcd)

Submission status: Wrong Answer

Solution code:
import fractions
import math
from functools import reduce


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


def using_sqrt(limit):
    n = 0
    for k in range(2, limit+1):
        factor = 0

        # 2以外の偶数は素数ではないので無視する
        if k % 2 == 0 and k != 2:
            continue

        # 繰り返しの上限を対象の平方根にする
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1

        if factor == 0:
            n += 1

    return n


n = int(input())
li_input = list(map(int, input().split()))
li = list(set(li_input))
    
count = 0
max_gcd = 0

if len(li) == 1:
    max_gcd = li[0]
else:
    for i in range(len(li)):
        if count > 1:
            break

        if li[i] <= max_gcd:
            continue

        if using_sqrt(li[i] != 0):
            count += 1

        tmp = li[i]
        del li[i]
        tmp_gcd = gcd(*li)
        if max_gcd < tmp_gcd:
            max_gcd = tmp_gcd
        li.insert(0, tmp)

print(max_gcd)

Submission status: Wrong Answer

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

n = int(input())
an = [int(i) for i in input().split(" ")]
an = sorted(an)
if len(an) <= 2:
    print(max(an))
else:
    sample = []
    for i in range(len(an)):
        a1 = an[i]
        for a2 in an[i+1:]:
            tmo = gcd(a1, a2)
            if len(sample) == n:
                k = sample.index(max(sample))
                sample[k] = min(sample[k], tmo)
            else:
                sample.append(tmo)


    sample.sort()
    print(sample[-1])

Submission status: Wrong Answer

Solution code:
from sys import stdin

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

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

left = []
right = []

before = -1
for i in range(n):
  if(i==0):
    before = an[i]
  else:
    before = gcd(before, an[i])
  left.append(before)

before = -1
for i in range(n):
  if(i==0):
    before = an[n-1-i]
  else:
    before = gcd(before, an[n-1-i])
  right.append(before)


max = 0
for i in range(n):
  if(i==0):
    before = right[1];
  elif(i==(n-1)):
    before = left[n-2];
  else:
    before = gcd(left[i-1],right[n-2-i])
  if(max < before ):
    max = before

print(max)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline())


A = list(map(int, sys.stdin.readline().split()))

min_A = min(A)
ans = 1
for i in reversed(range(2, min_A + 1)):
    count = 0
    if i != min_A and i > min_A/2:
        continue
    for j in range(N):
        if A[j] % i != 0:
            count +=1
            if count > 2:
                break
    if count <= 1:
        ans = i
        break

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
  while b:a,b=b,a%b
  return a
n,a,b=int(input()),list(map(int,input().split())),[]
if n==2:print(gcd(a[0],a[1]))
elif n==3:print(max(gcd(a[0],a[1]),gcd(a[0],a[2]),gcd(a[1],a[2])))
else:
  for i in range(n-1):b.append(gcd(a[i],a[i+1]))
  b.append(gcd(a[0],a[-1]))
  print(sorted(b)[2])

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
  a,b=max(a,b),min(a,b)
  if b==0:
    return a
  else:
    return gcd(b,a%b)
  
n=int(input())
arr=list(map(int,input().split()))
if n==2:
  print(gcd(arr[0],arr[1]))
elif n==3:
  print(max(gcd(arr[0],arr[1]),gcd(arr[1],arr[2]),gcd(arr[2],arr[0])))
else:
  ans1=[]
  for i in range(n):
    ans1.append(gcd(gcd(arr[0],arr[1]),arr[i]))
  ans1=sorted(ans1)
  ans2=[]
  for i in range(n):
    ans2.append(gcd(gcd(arr[1],arr[2]),arr[i]))
  ans2=sorted(ans2)
  ans3=[]
  for i in range(n):
    ans3.append(gcd(gcd(arr[2],arr[0]),arr[i]))
  ans3=sorted(ans3)
  finans1=ans1[1]
  finans2=ans2[1]
  finans3=ans3[1]
  finalans=max(finans1,finans2,finans3)
  print(finalans)

Submission status: Wrong Answer

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

n=int(input())
A=list(map(int, input().split()))
#a=1
init=1
#maxa=99999999999999999999999999
#maxa=A[1]
B=[]
for i in A:
    if init==1:
        a=i
        init=0
        B.append(a)
    else:
        a=gcd(a,i)
        B.append(a)
#print(B)
removenum=min(enumerate(B), key=lambda x: x[1])[0]
#print(removenum)

init=1

#両はし
for num,i in enumerate(A):
    if num==removenum:
        continue
    if init==1:
        a=i
        init=0
    a=gcd(a,i)
#print(a)

init=1
for num,i in enumerate(A):
    if num==0:
        continue
    if init==1:
        b=i
        init=0
    b=gcd(b,i)

init=1
for num,i in enumerate(A):
    if num==len(A)-1:
        continue
    if init==1:
        c=i
        init=0
    c=gcd(c,i)

print(max(a,b,c))

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()))
a.sort()
if n < 3:
    print(max(a))
    exit(0)
g01 = gcd(a[0], a[1])
g012 = gcd(a[2], g01)
g02 = gcd(a[0], a[2])
ans = [g01, g012, g02]
alls = g012
for i in range(3, n):
    ans.append(alls)
    alls = gcd(alls, a[i])
    delete = []
    for x in range(len(ans) - 1):
        ans[x] = gcd(ans[x], a[i])
        if ans[x] == 0: delete.append(x)
    delete.sort(reverse = True)
    for d in delete: ans.pop(d)
print(max(ans))

Submission status: Wrong Answer

Solution code:
from functools import reduce


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


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


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

max_ = 0
for i in range(len(A)):
    num = gcd_list(A[:i-1] + A[i:])
    if max_ < num:
        max_ = num

print(max_)

Submission status: Wrong Answer

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

for x in range(2,int(1000000000**0.5)+1):
    count = 0

    for y in range(N):
        if A[y] % x != 0:
            count += 1
        if count >= 2:
            break
    else:
        seki = x

print(seki)

Submission status: Wrong Answer

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

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

A.sort()
inf = int(2e9)

m = inf
check = 0
for i in range(N):
    check1 = gcd(A[0], A[i])
    if check1 < m:
        m = check1
        check = i

# A[0]以外
check2 = A[1]
for i in range(0,N):
    check2 = gcd(check2, A[i])

ans = check2

# check以外
if check != 0:
    check3 = A[0]
    for i in range(N):
        if i == check:
            continue
        check3 = gcd(check3, A[i])
    ans = max(check2, check3)

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
def GCD(x,y):
	if x>y:
		a=x
		b=y
	else:
		a=y
		b=x
	while(True):
		c=a%b
		if c==0:
			return b
		else:
			a,b=b,c
 

if N==2:
	print(max(A))
else:
	B=[]
	a0=A[-1]
	for aa in A[0:-1]:
		B.append(GCD(aa,a0))
	gomikamo=B.index(min(B))+1
	g0=A[0]
	for i in range(0,N-1):
		g0=GCD(A[i],g0)
	del A[gomikamo]
	g1=A[0]
	for i in range(N-1):
		g1=GCD(A[i],g1)
	print(max(g0,g1))

Submission status: Wrong Answer

Solution code:
import fractions
import sys

sys.setrecursionlimit(10**9)
N = int(input())
A = list(map(int, input().split()))
A.sort()
mx = 10 ** 9
idx = 0
g = []
for i in range(N-1):
    tmp = fractions.gcd(A[i], A[i+1])
    #print(A[i], A[i+1], tmp)
    if tmp < mx:
        mx = tmp
        idx = i
    g.append([tmp, i])
g.sort(key=lambda x:x[0])
#print(g)
if N > 2:
    if g[0][1] + 1 == g[1][1]:
        A.remove(A[g[0][1]+1])
    else :
        tmp1 = fractions.gcd(A[g[0][1]], A[g[1][1]])
        tmp2 = fractions.gcd(A[g[0][1]+1], A[g[1][1]])
        A.remove(A[g[0][1]] if tmp1 < tmp2 else A[g[0][1]+1])
else :
    A = [max(A)]
tmp = A[0]
for i in A:
    tmp = fractions.gcd(i, tmp)
print(tmp)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
import fractions

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

# d[i][j]: i番目が抜けていて[j]番目までのgcdを計算

dp = [[1 for i in range(N+2)] for j in range(N+2)]

dp[1][2] = A[1]
dp[2][1] = A[0]
dp[2][2] = A[0]
for i in range(1,N+1):
    dp[i][1] = A[i-1]

for i in range(1,N+1):
    for j in range(2,N+1):
#      print(i,j, ":")
        if i == j:
            if dp[i][j] == 1:
                dp[i][j] = dp[i][j-1]
#           print(dp[i][j], dp[i][j-1], A[j-1])
        else:
            if dp[i][j] == 1:
                dp[i][j] = fractions.gcd(dp[i][j-1], A[j-1]) 
#           print(dp[i][j], dp[i][j-1], A[j-1])

max = 1
for i in range(1,N+1):
#    print(dp[i][N])
    if max < dp[i][N]:
        max = dp[i][N]

print(max)

Submission status: Wrong Answer

Solution code:
import fractions

N = int( input() )
A = list( map( int ,input().split()) )
A.sort()
ans = 1000000000
Al = []
if len(A) >= 3:
    for lop in range(2,len(A),3):
        A1 = fractions.gcd(A[lop-2],A[lop-1])
        A2 = fractions.gcd(A[lop-2],A[lop])
        A3 = fractions.gcd(A[lop-1],A[lop])
        Al.append(max(A1,A2,A3))
    if len(A) % 3 > 0:
        A1 = fractions.gcd(A[len(A)-2],A[len(A)-1])
        A2 = fractions.gcd(A[len(A)-3],A[len(A)-1])
        A3 = fractions.gcd(A[len(A)-2],A[len(A)-3])
        Al.append(max(A1,A2,A3))
    print(min(Al))
else:
    print(max(A))

Submission status: Wrong Answer

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

def main():
  ans = 0
  gcd_res = 0
  
  if len(list(set(A))) == 1:
    gcd_res = gcd(A[0], A[1])
    print(gcd_res)
    exit(0)
    
  
  for ind in range(n):
      for i in range(n):
          if A[ind] == A[i]:
            continue
          gcd_res = gcd(A[ind], A[i])
          if ans < gcd_res:
             ans = gcd_res
  
  print(ans)
  

  
  
def gcd(x, y):
  while x % y !=0:
    z = x % y
    x = y
    y = z
  else:
    return y
  
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

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


n = int(input())  # ���l
a = list(map(int, input().split()))  # �������l�����X�g�Ɋi�[

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

    if (n - 2) != i and n != 2:
        li.append(gcd(a[i], a[i + 2]))

li.sort()

if(n == 2):
    print(max(a[0], a[1]))
elif (n == 3):
    print(li[2])
elif n == 4:
    print(li[3])
else:
    print(li[4])