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()))
B = 1
k = []
g = []
l = []
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

x = gcd(A[0], A[1])
k.append(x)
for i in range(2, N):
    x = gcd(x, A[i])
    k.append(x)
cost = 0
for i in range(N-2):
    if k[i] == k[i+1]:
        if cost == 0:
            g.append([i, i+1])
        cost += 1
    else:
        if len(g) < 1:
            continue
        l.append([cost, g[len(g)-1]])
        cost = 0
if len(g) <= 0:
    print(x)
    exit()
    
l.append([cost, g[len(g)-1]])
l.sort()

index1 = l[len(l)-1][1][0]
index2 = l[len(l)-1][1][1]
temp = A[index1]

for i in range(N):
    if A[index1] != A[i]:
        B = A[i]
        break

A[index1] = B

x = gcd(A[0], A[1])
for i in range(2, N):
    x = gcd(x, A[i])

A[index1] = temp

for i in range(N):
    if A[index2] != A[i]:
        B = A[i]
        break

A[index2] = B

y = gcd(A[0], A[1])
for i in range(2, N):
    y = gcd(y, A[i])

print(max(x, y))

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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

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

N = int(input())
A = [int(x) for x in input().split()]
cand = -1
mingcd = gcd_list(A)
if(N==2):
    print(max(A))
else:
    for i in range(N-1):
        if(mingcd==gcd(A[i],A[i+1])):
            if(i<N-2):
                if(mingcd==gcd(A[i],A[i+2])):
                    cand = i
                else:
                    cand = i+1
                break
            else:
                cand = i+1
    if(cand==-1):
        cand = N-1
    print(gcd_list(A[:cand]+A[cand+1:]))

Submission status: Wrong Answer

Solution code:
def main() -> None:
    N = int(input())
    A = [int(x) for x in input().split()]
    A.sort(reverse=True)
    m = 0
    for i in range(N - 2):
        t = A[i] - A[i + 2]
        if t > m:
            m = t
    if m == 0 and N == 2:
        m = max(A)
    print(m)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd


def gcd_check(data, ans):
    for i in range(len(data) - 1):
        ans.append(reduce(gcd, [data[i], data[i + 1]]))
 
N = int(input())
data = list(map(int, input().split()))
ans_data = []
gcd_check(data, ans_data)

print(max(ans_data))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
memo = {}

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

# current_gcd = A_list[0]
# skip_candidate = 0
# for i in range(1, len(A_list)):
#     next_gcd = gcd(A_list[i], current_gcd)
#     print("index:{} next_gcd:{}".format(i, next_gcd))
#     if next_gcd < current_gcd:
#         if skip_candidate < next_gcd:
#             current_gcd = next_gcd
#         else:
#             current_gcd = skip_candidate
#             skip_candidate = next_gcd
            
# print(current_gcd)        


if len(A_list) == 2:
    print(gcd(A_list[0], A_list[1]))
else:
    loop = len(A_list)
    current_max = 0
    for i in range(loop):
        if current_max != 0 and A_list[i] % current_max == 0:
            continue

        removed = A_list.pop(i)
        cur_gcd = 0
        for j in range(len(A_list)):
            cur_gcd = gcd(A_list[j], cur_gcd)
        
        current_max = max(cur_gcd, current_max)
        A_list.insert(i, removed)

    print(current_max)

Submission status: Wrong Answer

Solution code:
import math
import fractions
from collections import Counter as ct

def gcd_list(l):
    for n,i in enumerate(l):
        if n==0:cur=i
        cur=fractions.gcd(cur,i)
    return cur

def yakusulist(n):
    ans=[]
    for i in range(1,int(math.sqrt(n+1))+1):
        if n%i==0:
            ans.append(i)
            ans.append(n//i)
    return(set(ans))
    
def warennnokaomaera(n):
    if n==1:return(True)
    for k,i in enumerate(A):
        if k<3:continue
        if i%n:return(False)
    return(True)

N=int(input())
data=[int(i) for i in input().split()]
datagcd=gcd_list(data)
A=[i//datagcd for i in data]
if N>2:
    co=ct(A)
    alpha=sorted(co.keys())
    if len(alpha)>=3:
        min3=[alpha[0],alpha[1],alpha[2]]
        p,q,r=[yakusulist(n) for n in min3]
        pq=p&q
        qr=q&r
        rp=r&p
        overtwo=pq|qr|rp
        overtwo=sorted(list(overtwo),reverse=True)
        for i in overtwo:
            if warennnokaomaera(i):
                print(datagcd*i)
                break
    elif len(alpha)==2:#Aの数の種類が2種類以下->1個書き換えで違う数ってどっちかが一個じゃないと無理、それ以外はそのまま
        if co[alpha[0]]==1:
            print(datagcd*alpha[1])
        elif co[alpha[1]]==1:
            print(datagcd*alpha[0])
        else:print(datagcd)
    else:print(datagcd)
else:print(max(data))

Submission status: Wrong Answer

Solution code:
#C
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
N=int(input())
A = [int(j) for j in input().split()]
A=list(set(A))
A.sort()
ans=0


if len(A)==1:
    ans=A[0]
    print(ans)
    exit()

if len(A)==2:
    ans =max(ans,gcd(A[0],A[1]))
    print(ans)
    exit()
    
for a in A:
    if len(A)>2:
        tmp=A.copy()
        tmp.remove(a)
        ans =max(ans,gcd(max(tmp),sum(tmp)))
print(ans)

Submission status: Wrong Answer

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

mA=A[0]
div=set()

for n in range(1, int(mA**0.5)+1):
	if mA % n == 0:
		div.add(n)
		div.add(mA // n)

for d in sorted(div, reverse=True):
	if sum([0 if a % d == 0 else 1 for a in A]) < 2:
		break

print(d)

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)
G = []
A.sort()
current = A[0]
for i in range(N):
    current = fractions.gcd(current,A[i])
    if(gc==current):
        break
for j in range(i+1):
    b = A.pop(0)
    G.append(gcd_list(A))
    A.append(b)
print(G[-1])

Submission status: Wrong Answer

Solution code:
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

a.sort()
kouho_1=0
kouho_2=0
kouho_3=0

if n==2:
    print(max(a))
else:
    if a[0]!=a[1]:
        kouho_1=gcd(a[0], a[1])
    if a[1]!=a[2]:
        kouho_2=gcd(a[1], a[2])
    if a[0]!=a[2]:
        kouho_3=gcd(a[0], a[2])

    kouho=max(kouho_1, kouho_2, kouho_3)
    print(kouho)

Submission status: Wrong Answer

Solution code:
import fractions

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

def method(List):
    if List.count(1) > 1:
        return [1]
    newList = []
    for x in range(0, len(List) // 2, 2):
        number = fractions.gcd(List[x],List[x+1])
        newList.append(number)
    if len(List) % 2 == 1:
        newList.append(List[-1])
    return newList

answer = 0
for x in range(N):
    newList = A.copy()
    newList.pop(x)
    while len(newList) > 1:
        newList = method(newList)
    answer = max(answer, newList[0])
    
print(answer)

Submission status: Wrong Answer

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

A = list(set(A))

result = []
for i,a in enumerate(A):
    if len(A)==1:
        tmpA=A
        result = tmpA
        break
        
    tmpA = A[:i]+A[i+1:]
    while True:
        minA = min(tmpA)
        tmpA = set([a % minA for a in tmpA])
        tmpA.discard(0)
        tmpA.add(minA)
        if len(tmpA)==1:
            break
    result.append(int(list(tmpA)[0]))

print(max(result))

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
num_unique = list(set(num_list))
num_unique.sort()
num_unique.reverse()
max_num = 0
flag = False
for i in range(num_unique[-1], 0, -1):
    if flag and count < 2:
        break
    elif i == 1:
        max_num = 1
        break
    elif i == 0:
        max_num = 0
        break
    else:
        flag = True
        count = 0
        max_num = i
    for num in num_unique:
        if num / max_num < 1 or num % max_num != 0:
            count += 1
            if count > 1:
                break
print(max_num)

Submission status: Wrong Answer

Solution code:
#import math
def gcd(a,b):
    if a%b == 0:
        return b
    return gcd(b,a%b)


n = int(input())
A = [int(x) for x in input().split()]

gcd_1 = [gcd(A[0],A[i]) for i in range(1,n)]

gcd_1.sort()

gcd_2 = A[1]

for i in range(2,n):
      gcd_2 = gcd(gcd_2,A[i])

if n == 2:
    print(gcd(A[0],A[1]))
else:
    print(max(gcd_1[1],gcd_2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
min = min(a)
divisors = []
for i in range(1, min):
    if min % i == 0:
        divisors.append(i)
divisors.append(min)
divisors.sort(reverse=True)
if n == 2:
    print(max(divisors))
else:
    judge = False
    for i in divisors:
        for j in range(n):
            if judge == True and a[j] % i !=0:
                judge = False
                break
            elif a[j] % i != 0:
                judge = True
        if judge == True:
            print(i)
            break

Submission status: Wrong Answer

Solution code:
import copy
from functools import reduce
import fractions

def gcd(a, b):
    return fractions.gcd(a,b)

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

if __name__ == '__main__':
    N = int(input())
    A_list = list(map(int, input().split()))

    answer = 0

    for i in range(0, len(A_list)-1):
        A_list_copy = copy.deepcopy(A_list)
        A_list_copy.pop(i)

        gcd_number = gcd_list(A_list_copy)

        if answer < gcd_number:
            answer = gcd_number

    if len(A_list) == 1:
        answer = pow(10, 9)

    print(answer)

Submission status: Wrong Answer

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



N = input();
A = map(int, raw_input().split())

max_gcd = 0;

for i in range(N):
	for j in range(i+1,N):
		
		g = gcd(A[i],A[j]);

		if max_gcd < g:
			max_gcd = g;

print(max_gcd);

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 < 100:
  for i in range(n):
    b = copy.copy(a)
    b.pop(i)
    t = gcd(b)
    if ans < t:
      ans = t
else:
  for _ in range(100):
    t = gcd(random.sample(a, 20))
    if ans < t:
      ans = t

print(ans)

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

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

Submission status: Wrong Answer

Solution code:
import math
import copy
n=int(input())
num=list(map(int,input().split()))

def hcfnaive(a,b): 
  if(b==0): 
    return a 
  else: 
    return hcfnaive(b,a%b) 
      
def gcd(numbers):
  if len(numbers)==2:
    return hcfnaive(numbers[0],numbers[1])
  elif len(numbers)==1:
    return numbers[0]
  else:
    numbers[0]=hcfnaive(numbers[0],numbers[1])
    numbers.pop(1)
  return gcd(numbers)

ans=0
for i in range(n):
  num2=copy.deepcopy(num)
  num2.pop(i)
  #ab=gcd(num2)
  #if ab>ans:
  #  ans=ab
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

if n == 2:
    a.append(fractions.gcd(a[0],a[1]))

g0 = [fractions.gcd(a[0],a[1]),fractions.gcd(a[0],a[2]),fractions.gcd(a[1],a[2])]
s_g0 = sorted(g0,reverse=True)

maxg1 = s_g0[0]
maxg2 = s_g0[1]

if n >= 4:
    for i in range(n - 3):
        maxg1 = fractions.gcd(maxg1,a[i + 3])
        maxg2 = fractions.gcd(maxg2,a[i + 3])

if maxg1 >= maxg2:
    print(maxg1)
else:
    print(maxg2)

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=list(map(int, input().split()))
ans=[0]*(n-1)
ans2=[0]*(n-1)

for i in range(1,n):
    ans[i-1] = fractions.gcd(a[0], a[i])
for i in range(0,n):
    if i==0:
        ans2[0] = fractions.gcd(a[1], a[i])
    elif i==1:
        continue
    else:
        ans2[i-1] = fractions.gcd(a[1], a[i])
s_ans=sorted(ans)
s_ans2=sorted(ans2)

if n==2:
    print(max(a))
else:
    print(max(s_ans[1],s_ans2[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
def gcd(x,y):
  while y != 0:
    k = x
    x = y
    y = k%y
  return  x
ans = 1
for i in range(N):
  flag = True
  if i == 0:
    p = A[1]
    for j in range(2,N):
      p = gcd(A[i],p)
      if p == 1:
        flag = False
        break
    if flag:
      if ans <= p:
        ans = p
  else:
    p = A[0]
    if p != 1:
      for j in range(1,N):
        if j != i:
          p = gcd(A[i],p)
        if p == 1:
          flag = False
          break
      if flag:
        if ans <= p:
          ans = p
    else:
      p = A[1]
      for j in range(2,N):
        if j != i:
          p = gcd(A[i],p)
        if p == 1:
          flag = False
          break
      if flag:
        if ans <= p:
          ans = p
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
Alist = np.array(list(map(int, input().split())))

def gcd(nlist):
    tmp = nlist[0]
    while len(nlist) != 0:
        nlist = np.sort(nlist)
        tmp = nlist[0]
        nlist = nlist % tmp
        nlist = nlist[nlist != 0]
    return tmp

gcdlist = []
for i in range(len(Alist)):
    nlist = np.append(Alist[:i], Alist[i+1:])
    gcdlist.append(gcd(nlist))
print(max(gcdlist))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(sys.getrecursionlimit())
N = int(input())
A = list(map(int,input().split(" ")))

def gcd (a: int,b: int) -> int:
    if a < b: a,b = b,a
    return a if b == 0 else gcd(b,a%b)
def lcm (a: int,b: int) -> int:
    return a // gcd(a,b) * b


gcdForward = [0] * N
gcdBackWard = [0] * N
gcdForward[0] =  A[0]
gcdBackWard[N-1] = A[-1]
for i in range(N-1):
    gcdForward[i+1] = gcd(gcdForward[i],A[i+1])
    gcdBackWard[N-i-2] = gcd(gcdBackWard[N-i-1],A[N-i-2])


if N == 2:
    print(max(A))
    exit(0)
else:
    ans = max(gcdBackWard[N-2],gcdForward[1])
    for i in range(1,N-1):
        ans = max(ans,gcd(gcdForward[i-1],gcdBackWard[i+1]))
    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

gcd =[]
for i in range(number-1):
    gcd.append(fractions.gcd(A[i], A[i+1]))

gcd.append(fractions.gcd(A[0], A[number-1]))

gcd.sort(reverse=True)

if gcd[0] == gcd[number-1]:
    print(gcd[0])

elif gcd[number-2] == gcd[number-1] and gcd[number-3] != gcd[number-2]:
    print(gcd[number-3])

else:
    print(gcd[number-1])

Submission status: Wrong Answer

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

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

if len(a)==2:
    print(max(a))
else:
    for i in range(n-1):
        cnt.append(gcd(a[i],a[i+1]))
    cnt.append(gcd(a[0],a[n-1]))


    while 1:
        tmp = max(cnt)
        g = cnt.count(tmp)
        if g>=len(cnt)-2:
            print(tmp)
            break
        else:
            for i in range(g):
                cnt.remove(tmp)

Submission status: Wrong Answer

Solution code:

def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a%b)
n = int(input())
A = input().split()
m = [int(a) for a in A]
ans = 0
if n == 2:
    ans = max(m)
else:
    for i in range(n):
        B = m[:]
        B.pop(i)
        gcm = gcd(B[0], B[1])
        cand = gcm
        for j in range(len(B)-2):
            cand = gcd(B[j+2], gcm)
        if cand > ans:
            ans = cand
print(ans)

Submission status: Wrong Answer

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

N=int(input())
A=input().split()
Max=max(A)
Min=min(A)
flg2=False

if F(int(Max),int(Min)) == 1:
  flg1=False
else:
  flg1=True

if N == 2:
  print(int(Max))
  quit()

for i in range(N):
  if flg1 == False:
    if int(A[i]) == int(Min) and flg2==False:
      A[i] = Max
      flg2=True
  else:
    if int(A[i]) != int(Min) and int(A[i]) != int(Max) and flg2==False:
      A[i] = Max
      flg2=True
    
  if i == 0: continue
  if i == 1:
    ans=F(int(A[i-1]),int(A[i]))
  else:
    ans=F(ans,int(A[i]))
print(ans)

Submission status: Wrong Answer

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

num = A[0]


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


for i in range(N):
    if A[i]%num == 0:
        continue
    B = A[:]
    B.pop(i)
    gcd1 = int(B[0])

    for j in range(1, N - 1):
        gcd1 = gcd(gcd1, B[j])

    if num < gcd1:
        num = gcd1

print(num)

Submission status: Wrong Answer

Solution code:

def gcd(a, b):
    while a>0 and b >0:
        if a > b:
            a %= b
        else:
            b %= a
    return a + b

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

forwards = [A[0]]
backwards = [A[-1]]
for i in range(N):
    forwards.append(gcd(forwards[-1], A[i]))
    backwards.append(gcd(backwards[-1], A[-(i+1)]))
backwards = list(reversed(backwards))

ans = 0
for i in range(1, N-1):
    a = gcd(forwards[i-1], backwards[i+1])
    if a > ans:
        ans = a

ans = max([ans, forwards[-2], backwards[1]])


print(ans)

Submission status: Wrong Answer

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


def gcd(a, b):
    m, n = int(a/b), a%b
    if n == 0:
        return b
    return gcd(b, n)

def main():
    n = int(input())
    A = [int(e) for e in input().split(" ")]

    max_n = 0
    for i in range(n):
        if i < max_n:
            continue
        for j in range(i+1, n):
            current = gcd(A[i], A[j])
            if max_n < current:
                max_n = current

    print(max_n)

main()

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:
  maxg=max(gcd(arr[0],arr[1]),gcd(arr[1],arr[2]),gcd(arr[2],arr[0]))
  ming=min(gcd(arr[0],arr[1]),gcd(arr[1],arr[2]),gcd(arr[2],arr[0]))
  ans1=[]
  for i in range(3,n):
    ans1.append(gcd(maxg,arr[i]))
  ans2=[]
  for i in range(3,n):
    ans2.append(gcd(ming,arr[i]))
  ans2=sorted(ans2)
  finans1=min(ans1)
  if n==4:
    finans2=ans2[0]
  else:
    finans2=ans2[1]
  finalans=max(finans1,finans2)
  print(finalans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
s = max(a)
while True:
    if len(a) == 2:
        ans = s
        break
    b1 = a[0] % s
    b2 = a[1] % s
    b3 = a[2] % s
    if b1 + b2 == 0 or b2 + b3 == 0 or b1 + b3 == 0:
        ans = s
        break
    else:
        s -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
n=int(input())
a=list(map(int,input().split()))
if n==2:
    print(max(a[0],a[-1]))
    exit()
#mae
r1=a[0]
flag1=0
for i in range(1,n):
    x=fractions.gcd(r1,a[i])
    if x<r1 and flag1==0 and i!=0:
        flag1=1
    elif x<r1:
        r1=x
#usiro
r2=a[n-1]
flag2=0
for k in range(n-1):
    y=fractions.gcd(r2,a[n-k-2])
    if y<r2 and flag2==0 and k!=0:
        flag2=1
    elif y<r2:
        r2=y
p=a[0]
a[0]=a[1]
a[1]=p
q=a[n-1]
a[n-1]=a[n-2]
a[n-2]=q
#mae
r3=a[0]
flag3=0
for v in range(1,n):
    x=fractions.gcd(r3,a[v])
    if x<r3 and flag3==0 and v!=0:
        flag3=1
    elif x<r3:
        r3=x
#usiro
r4=a[n-1]
flag4=0
for w in range(n-1):
    y=fractions.gcd(r4,a[n-w-2])
    if y<r4 and flag4==0 and w!=0:
        flag4=1
    elif y<r4:
        r4=y
print(max(r1,r2,r3,r4))

Submission status: Wrong Answer

Solution code:
import fractions

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

gcd =[]
for i in range(number-1):
    gcd.append(fractions.gcd(A[i], A[i+1]))

gcd.append(fractions.gcd(A[0], A[number-1]))

gcd.sort(reverse=True)
print(gcd)
if gcd[0] == gcd[number-1]:
    print(gcd[0])

elif gcd[number-2] == gcd[number-1] and gcd[number-3] != gcd[number-2]:
    print(gcd[number-3])

else:
    print(gcd[number-1])

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce


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


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


n = int(input())
li = list(map(int, input().split()))
divisor_list = []
max_gcd = 0
for i in range(len(li)):
    if li[i] <= max_gcd:
        continue

    # 約数を求める
    tmp_div = make_divisors(li[i])
    # 共通の約数のみのリストを作る
    if i == 0:
        divisor_list = tmp_div[:]
    else:
        tmp_li = divisor_list[:]
        divisor_list = []
        for tag in tmp_div:
            divisor_list += filter(lambda str: str == tag, tmp_li)
    if i > 3:
        if len(divisor_list) == 1:
            break

    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:
        a, b = b, a % b
    return a


def get(lists, index):
    base = lists[index]
    lists.pop(index)

    lists = filter(lambda x: x > 1, [gcd(base, i) for i in lists])
    if not lists:
        return None
    elif len(lists) == 1:
        return lists[0]
    return sorted(lists)[1]


raw_input()
al = [int(i) for i in raw_input().split()]

print(max(*[get(list(al), i) for i in range(len(al))]))

Submission status: Wrong Answer

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

n = int(input())
a = [int(i) for i in input().split()]
if len(a) == 2:
    print(gcd(a[0],a[1]))
else:
    minval = min(a)
    maxgcd = max(gcd(a[0],a[1]),gcd(a[1],a[2]),gcd(a[0],a[2]))
    maxgcd2 = maxgcd
    for index, value in enumerate(a):
        if value == minval:
            continue
        temp = gcd(minval, value)
        if temp <= maxgcd:
            maxgcd2 = maxgcd
            maxgcd = temp
    print(maxgcd2)

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def gcd_list(numbers):
    return reduce(gcd, numbers)
n = ni()
a = nl()
minn = 10 ** 9 + 1
idx = -1
for i in range(n - 1):
    if minn >= gcd(a[i], a[i + 1]):
        minn = gcd(a[i], a[i + 1])
        idx = i
print(max(gcd_list(a[:idx] + a[idx + 1:]), gcd_list(a[:idx + 1] + a[idx + 2:])))

Submission status: Wrong Answer

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

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

if n == 2:
    print(max(a))
    exit()

p1,p2,p3 = gcd(a[0],a[1]),gcd(a[1],a[2]),gcd(a[0],a[2])

p = [p1,p2,p3]

f = 0
tmp = a[0]
res1,res2,res3 = p1,p2,p3

for i in range(n):
    if gcd(tmp,a[i])==1:
        f += 1
    else:
        tmp = gcd(tmp,a[i])

    if i>=3:
        res1 = gcd(res1,a[i])
        res2 = gcd(res2,a[i])
        res3 = gcd(res3,a[i])

    if f > 1:
        print(1)
        break
else:
    """
    if res1==res2:
        print(res1)
    elif res2==res3:
        print(res2)
    else:
        print(res3)
    """
    print(max(res1,res2,res3))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in 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)
 
    # divisors.sort()
    return divisors
 
 
 
 
min_a = min(A)
 
divisors = make_divisors(min_a)
 
divisors = sorted(divisors, reverse = True)
 

ans = 1
for d in divisors:
    #print(d)
    A_2 = [a/d for a in A]
    #print(A_2)
    num_float = 0
    for a in A_2:
        if a.is_integer() == False:
            num_float += 1
    #print(num_float)
    if num_float <= 1:
        ans = d
        break
    
        
print(ans)

Submission status: Wrong Answer

Solution code:
#素数or 最も公約数の少ない数を、公約数が多いやつに書き換える
N = int(input())
A = list(map(int,input().split()))

leastIndex = 0
sa = 0
# big = 1
# for r in A:
#     big *=r
def gcd(a,b):
    if a%b == 0:
        return b
    else:
        return gcd(b, a%b)
first = A[1]
for i in range(N-2):
    tmp =gcd(first, A[i+2])
    if first > tmp:
        if sa < first-tmp:
            sa = first-tmp
            leastIndex = i
    first = tmp
second = A[0]
for m in range(N-2):
    second = gcd(second, A[m+2])
del A[leastIndex]
if len(A) == 1: A.append(A[0])
third= gcd(A[0],A[1])
for m in range(N-3):
    third = gcd(third, A[m+2])
print(max(max(first,second),third))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

from functools import reduce
def pair_gcd(a,b):
        if a < b:
            a, b = b, a
        if b == 0:
            return a
        c = a % b
        return pair_gcd(b, c)
def gcd(numbers):
    return reduce(pair_gcd, numbers)

max_gcd = 1
for idx in range(N):
    remove_target = A_list.pop(0)
    if idx == 0:
        max_gcd = gcd(A_list)
        A_list.append(remove_target)
    else:
        newcomer = A_list[-1]
        if pair_gcd(A_list[0], newcomer) > max_gcd:
            gcd_value = gcd(A_list)
            A_list.append(remove_target)
            if max_gcd < gcd_value:
                max_gcd = gcd_value

        else:
            continue

print(max_gcd)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
As = list(map(int, input().split()))
As.sort()
ans = 1
for i in range(N):
    if i == 0:
        a = As[1]
    else:
        a = As[0]
    for j in range(N):
        if j != i:
            a = fractions.gcd(a, As[j])
            if a <= ans:
                break
    ans = a
print(ans)

Submission status: Wrong Answer

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

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

if A.count(1) > 1:
    print(1)
    exit()

dd = {}
for i in range(N):
    if A[i] not in dd.keys():
        dd[A[i]]= 0
    dd[A[i]] += 1
if len(dd.keys()) == 2:
    for key in dd.keys():
        if dd[key] == N - 1:
            print(max(A))
            exit()

import fractions
if min(A) == 1:
    A.remove(1)
    mgcd = fractions.gcd(A[0],A[1])
    for i in range(2,N-2):
        mgcd = fractions.gcd(A[i],mgcd)
    print(mgcd)
    exit()

mina = min(A)
d = {}
for i in range(N):
    k = fractions.gcd(mina , A[i])
    if k not in d.keys():
        d[k] = 0
    d[k] += 1
mind = min(d.keys())

if d[mind] == 1:
    j = sorted(d.keys())
    print(j[1])
else:
    print(mind)

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 < 100:
  for i in range(n):
    b = copy.copy(a)
    b.pop(i)
    t = gcd(b)
    if ans < t:
      ans = t
else:
  for _ in range(100):
    t = gcd(random.sample(a, 3))
    if ans < t:
      ans = t

print(ans)

Submission status: Wrong Answer

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

def gcd_array(A):
    n = len(A)
    s = A[0]
    i = 1
    Down = []
    while(i < n):
        t = gcd(s,A[i])
        if t < s:
            Down.append(i)
        s = t
        i += 1
    return s,Down

if __name__ == "__main__":
    N = int(input())
    B = list(map(int,input().split()))
    max = 0
    d,Down = gcd_array(B)
    C = [i//d for i in B]
    c = C[0]
    K = 1
    for j in Down:
        A = [i for i in C]
        c = A.pop(j)
        l = gcd_array(A)[0]
        if K < l:
            K = l
    print(K * d)

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 a1 == a2:
                continue
            elif len(sample) < n:
                sample.append(tmo)
            else:
                sample.sort()
                sample[n-1] = min(sample[n-1], tmo)

    sample = sorted(sample)
    print(sample[n-1])

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
As=input().split()
A = np.array([int(s) for s in As])
del(As)
minA = np.amin(A)
for i in range(minA+1)[::-1]:
    if np.sum((A%i)==0)>N-2:
        print(i)
        break

Submission status: Wrong Answer

Solution code:
import fractions
import functools
n = input()
lists = list(map(int, input().split()))
gcd1 = functools.reduce(fractions.gcd, lists)
i=1
y=1
judge = "true"
limit = min(lists)
#print(limit)
while i < limit:
  for index,list in enumerate(lists):
    if (list % i) != 0:
      lists[index] = i
      judge = "false"
      break
  if judge == "false":
    break
  i += 1
#print(lists)
gcd2 = functools.reduce(fractions.gcd, lists)
if gcd1 >= gcd2:
  print(gcd1)
else:
  print(gcd2)

Submission status: Wrong Answer

Solution code:
n = int(input())
an = list(map(int, input().split(' ')))
an = list(set(an))
if len(an) == 1:
  print(an[0])
  exit()
  
from fractions import gcd
from functools import reduce
import copy

max_g = 1
for i in range(len(an)):
  bn = copy.copy(an)
  bn.pop(i)
  g = reduce(gcd, bn)
  max_g = max(g, max_g)
print(max_g)