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:
_=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
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+1:]
            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):
  if b == 0:
    ret = a
  else:
    ret = gcd(b, a%b)
  return ret

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
gu = [0 for i in range(N)]
gl = [0 for i in range(N)]
if N == 2:
  if A[0] > A[1]:
    print(A[0])
  else:
    print(A[1])
else:
  max = 0
  gu[0] = A[0]
  for i in range(1,N):
    y = gu[i-1]
    x = A[i]
    gcd(y,x)
    gu[i] = gcd(y,x)
    
  gl[0] = A[N-1]
  for i in range(1,N):
    y = gl[i-1]
    x = A[N-1-i]
    gl[i] = gcd(y,x)

  for i in range(N):
    if i == 0:
      x = gl[N-1]
      y = x
    elif i == N-1:
      x = gu[N-1]
      y = x
    else:
      y = gu[i-1]
      x = gl[N-2-i]
    x = gcd(y,x)
    if max < x:
      max = x
  print(max)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int,input().split()))
lis.sort(reverse=True)
answer = 0
i = lis[1]
while i >= 1:
  counter = 0
  for j in range(n):
    if lis[j]%i > 0:
      counter += 1
  if counter < 2:
    answer = i
    break
  i -= 1
print(answer)

Submission status: Wrong Answer

Solution code:
from functools import reduce

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

n = int(input())
a = [ int(x) for x in input().split() ]
_gcd = reduce(gcd, a)

b = [ int(x)//_gcd for x in a ]

t = b[0]
cnt = 0
for i in b[1:]:
    if (gcd(t, i) == 1):
        if cnt == 0:
            cnt += 1
        else:
            t = 1
    else:
        t = gcd(t, i)

ans = (_gcd * t)

t = b[1]
cnt = 0

for i in [b[0]] + b[2:]:
    if (gcd(t, i) == 1):
        if cnt == 0:
            cnt += 1
        else:
            t = 1
    else:
        t = gcd(t, i)

ans = max(ans, (_gcd * t))

t = b[-1]
cnt = 0

for i in b[:-1]:
    if (gcd(t, i) == 1):
        if cnt == 0:
            cnt += 1
        else:
            t = 1
    else:
        t = gcd(t, i)

ans = max(ans, (_gcd * t))

print(ans)

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:
    b = A.pop(N-1)
    gc1 = gcd_list(A)
    A.append(b)
    A.pop(N-2)
    gc2 = gcd_list(A)
    print(min(gc1,gc2))

Submission status: Wrong Answer

Solution code:
import fractions
import functools
N = int(input())
A = [int(i) for i in input().split()] 
num = 0
for i in range(N):
	# 3から始まって、2づつ増えていく
	sosu = True
	if A[i] % 2 == 0:
		sosu = False
		break
	for n in range(3, i, 2):
		if i % n == 0:
			sosu = False
			break
			# 割り切れる場合（素数ではない場合）
			# 一回でも何かの数値で割り切れたら素数ではない
	if sosu:
		num += 1

if num >= 3:
	max_gcd = 1


else:
	max_gcd = functools.reduce(fractions.gcd, A)
	for i in range(N):
		tmp = A.copy()
		tmp.pop(i)
		gcd = functools.reduce(fractions.gcd, tmp)
		if max_gcd < gcd:
			max_gcd = gcd
print(max_gcd)

Submission status: Wrong Answer

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

if N < 3 :
    print(max(A))
    exit()

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

divi = A[1]
flag = True
mindiv = 0
if N >= 4 :
    for i in range(2, len(A)) :
        tmp = gcd(A[i], divi)
        if i >= 3 :
            if tmp != mindiv :
                flag = False
                break
        mindiv = tmp

    if not flag :
        mindiv = 0
        #print(mindiv)
        #exit()


divi = A[0]
divList = []

for i in range(1, N) :
    divList.append(gcd(A[i], divi))

divList.sort()

if divList.count(divList[0]) == 1 :
    print(max(mindiv, divList[1]))
else :
    print(max(mindiv, divList[0]))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import fractions
 
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
A.sort()

kisu_cnt = 0
gusu_cnt = 0

for i in range(N):
    if A[i] % 2 == 1:
        kisu_cnt += 1
    else:
        gusu_cnt += 1
    
    if kisu_cnt >= 2 and gusu_cnt >= 2:
        break

if kisu_cnt >= 2 and gusu_cnt >= 2:
    print(1)
    exit()

if N >= 3:
    gcd_num = max(fractions.gcd(A[0], A[1]), fractions.gcd(A[0], A[2]), fractions.gcd(A[1], A[2]))
else:
    gcd_num = max(A[0], A[1])
        
print(gcd_num)

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

del arr[-1]
arr[mp]=max(arr)
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

print(r[mp])

Submission status: Wrong Answer

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

gcd = lambda x, y: gcd(y, x % y) if x % y else y


def m(v1, v2):  # monoid
    if v1 and v2:
        return gcd(v1, v2)
    else:
        return max(v1, v2)


e = 0  # identity element

SIZE = 10**6  # limit for array size

t = [e] * SIZE
t += A
t += [e] * (SIZE - N)
for i in range(N - 1, 0, -1):  # build
    t[i] = m(t[i << 1], t[i << 1 | 1])


def modify(p, v):  # set value at position p (0-indexed)
    p += SIZE
    t[p] = v
    while p > 1:
        t[p >> 1] = m(t[p], t[p ^ 1])
        p >>= 1


def query(l, r):  # result on interval [l, r) (0-indexed)
    res = e
    l += SIZE
    r += SIZE
    while l < r:
        if l & 1:
            res = m(res, t[l])
            l += 1
        if r & 1:
            r -= 1
            res = m(res, t[r])
        l >>= 1
        r >>= 1
    return res


ans = 0
for i, a in enumerate(A):
    modify(i, e)
    ans = max(ans, query(0, N))
    modify(i, a)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

out=0

for i in range(N):
    t=copy.copy(A)
    t.pop(i)
    t1=10**9
    for j in range(N-2):
        t1=min(gcd(t[j],t[j+1]),t1)
    out=max(out,t1)
        
            

print(out)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

data = {}


def gcv(a, b):
    global data
    # if a >= b:
    #     a, b = b, a
    if data.get('{}:{}'.format(a, b), None):
        return data['{}:{}'.format(a, b)]
    elif data.get('{}:{}'.format(b, a), None):
        return data['{}:{}'.format(b, a)]

    if b == 0:
        data['{}:{}'.format(a, b)] = a
        return a
    else:
        # print(a, b)
        data['{}:{}'.format(a, b)] = gcv(b, a % b)

        return data['{}:{}'.format(a, b)]


def solve(n, a):
    if n == 2 and a[0] == a[1]:
        return a[0]
    n_dict = defaultdict(int)
    for i in range(len(a)):
        for j in range(len(list(a)) - i + 1):
            n_dict[gcv(a[i], a[j + i - 1])] += 1
    return sorted(n_dict.items(), key=lambda x: x[0])[1][0]


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


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import functools
import fractions


N = int(input())
A = list(map(int, input().split()))
gcd = functools.reduce(fractions.gcd, A)
for i in range(N):
    A_sub = (A[0:i]+A[i+1:]).copy()
    gcd_new = functools.reduce(fractions.gcd, A_sub)
    if gcd_new > gcd:
        gcd = gcd_new
        break
print(gcd)

Submission status: Wrong Answer

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


n = int(input())
a_lst = list(map(int,input().split()))
a_lst.sort()
cnt = 0
ans = 1
temp_ans = 0
diff_1 = [1,0,0,0,-1]
diff_2 = [0,1,0,-1,0]

for i in range(0,len(diff_1)):
    d1,d2 = diff_1[i],diff_2[i]
    if (d1+d2) != 0:
        cnt = 1
    else:
        cnt = 0
    
    temp_ans = gcd(max(a_lst)+d1,min(a_lst)+d2)
    for a in a_lst[1:-1]:
        if cnt > 1:
            break
        if a%temp_ans != 0:
            cnt += 1
    if cnt <= 1:
        ans = max(ans,temp_ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
 
l = list(map(int, input().split()))
max_gcd = 0
for n, _ in enumerate(l):
    sub = [v for i, v in enumerate(l) if i != n]
    if not sub:
        continue
    if len(sub) > 1:
        res = sub[0]
        for c in sub[1::]:
            res = math.gcd(res, c)
        sub[0] = res
    max_gcd = max(max_gcd, sub[0])

Submission status: Wrong Answer

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

max_g = 1
ag = an_s[0]
for i in range(len(an_s)):
  bn = copy.copy(an_s)
  bn.pop(i)
  g = reduce(gcd, bn)
  if g > max_g:
    max_g = g
    ag = an_s[i]
an.remove(ag)
g = reduce(gcd, an)
print(g)

Submission status: Wrong Answer

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

def ngcd(li):
    temp=gcd(li[0], li[1])
    if len(li)-3 >=0:
        for i in range(len(li)-2):
            temp=gcd(temp, li[i+2])
    return temp

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

t=a[0]
t2=a[1]
out=0
f=1
min=10**9+1
for i in range(0, len(a)):
  gcd2=gcd(t, a[i])
  if gcd2<min:
    min=gcd2
    out=i
    #print(t, a[i],out, min)
"""
min=10**9+1
for i in range(0, len(a)):
  gcd2=gcd(t2, a[i])
  if gcd2<min:
    min=gcd2
    out=i
    print(t2, a[i],out, min)


print(out)
"""

"""
  if not (a[i]%t==0 or t %a[i]==0):
    if f:
      out=i
      f=0
    elif not f:
      out=0
"""
if not len(a)==2:
  temp=a[0:out]
  temp.extend(a[out+1:])
  #print(temp)
  itemp=ngcd(temp)
  itemp2=ngcd(a[1:])
  #print(itemp, itemp2)
  print(max(itemp, itemp2))
else:
  print(max(a[0], a[1]))


"""
max=0
for i in range(len(a)):
    temp=a[0:i]
    temp.extend(a[i+1:])
    itemp=ngcd(temp)
    if max < itemp:
        max=itemp

print(max)
"""

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]  # No 1
for i in range(1, N-1):
    tmp = gcd(gcdL[i-1], gcdR[i+1])
    # print(tmp)
    ans = max(tmp, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input()) 
def gcd(m, n):
  a = max(m,n)
  b = min(m,n)
  if a%b==0:
    return b
  while not a % b == 0:
      a, b = b, a % b
  return b

arr = [int(item) for item in input().split()]
a = [arr[0]]
b = [arr[-1]]
for i in range(1, N):
  a.append(gcd(a[i-1], arr[i]))
  b.append(gcd(b[i-1], arr[N-i-1]))
ans = max(a[-2], b[-2])

for i, j in zip(a[:-3], b[:-3:-1]):
  ans = max(ans, gcd(i,j))
print(ans)

Submission status: Wrong Answer

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

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

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

gcd_list = []

A.sort()

if len(A) == 2:
    if A[0] == A[1]:
        print(A[0])
        exit()
    elif A[0] > A[1]:
        print(A[0])
        exit()
    elif A[1] > A[0]:
        print(A[1])
        exit()

B = A.copy()

del A[0]
A.insert(0, A[1])

gcd_list = []
for a in A:
    for b in A:
        tmp = gcd(a, b)
        gcd_list.append(tmp)

    break


del B[1]
B.insert(1, B[0])

gcd_list2 = []
for a in B:
    for b in B:
        tmp = gcd(a, b)
        gcd_list2.append(tmp)

    break


print(max(min(gcd_list), min(gcd_list2)))

Submission status: Wrong Answer

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

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


n = int(input())

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

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

g = []
gg = []
ggg = []
for i in range(0, n):
    if i!=0: g.append(gcd(a[0], a[i]))
    if i!=1: gg.append(gcd(a[1], a[i]))
    if i!=2: ggg.append(gcd(a[2], a[i]))

g.sort()
gg.sort()
ggg.sort()

g.pop(0)
gg.pop(0)
ggg.pop(0)



if g[0]==gg[0] or g[0]==ggg[0]: print(g[0])
elif g[0]==gg[0]: print(g[0])
elif g[0]==ggg[0]: print(ggg[0])
elif gg[0]==ggg[0]: print(gg[0])

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]:
    if len(gcd) == 2 and gcd[0] == 1:
        print(max(A))
    else:
        print(gcd[0])
    
elif gcd[number-2] == gcd[number-1] and gcd[number-3] != gcd[number-2]:
    print(gcd[number-3])
    
elif gcd[number-3] == gcd[number-2] and gcd[number-2] != gcd[number-1]:
    print(gcd[number-2])

else:
    print(gcd[number-1])

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

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

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

kouho = [a[0]]

for i in range(n - 1):
    hoge = []
    for j in range(len(kouho)):
        hoge.append(fractions.gcd(a[i + 1], kouho[j]))
    if i == 0:
        kouho = [hoge[0]]
    elif min(kouho) == max(hoge):
        kouho = [max(hoge)]
    elif len(hoge) == 1:
        kouho = [hoge[0], kouho[0]]
    else:
        kouho = hoge

a1 = max(kouho)

a[0], a[1] = a[1], a[0]

kouho = [a[0]]

for i in range(n - 1):
    hoge = []
    for j in range(len(kouho)):
        hoge.append(fractions.gcd(a[i + 1], kouho[j]))
    if i == 0:
        kouho = [hoge[0]]
    elif min(kouho) == max(hoge):
        kouho = [max(hoge)]
    elif len(hoge) == 1:
        kouho = [hoge[0], kouho[0]]
    else:
        kouho = hoge

a2 = max(kouho)

print(max(a1, a2))

Submission status: Wrong Answer

Solution code:
from functools import reduce
from fractions import gcd as gcd_
I=lambda:list(map(int,input().split()))
def gcd(numbers):
  return reduce(gcd_, numbers)

N=int(input())
A=I()

g=gcd(A[:2])
gL=[g]
for i in range(2,N):
  gL+=[gcd_(g,A[i])]
c=0
for i in range(1,N)[::-1]:
  if gL[i-1]>gL[-1]:c=i+1;break
#print(gL,c)
if c==0:
  B=[A[i] for i in range(N) if i!=0]
  C=[A[i] for i in range(N) if i!=1]
  #print(gcd(B),gcd(C),B,C)
  print(max(gcd(B),gcd(C)))
else:
  B=[A[i] for i in range(N) if i!=c]
  print(gcd(B))
#print(max([gcd([A[j] for j in range(N) if j!=i]) for i in range(N)]))

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

N = int(input())
A = list(map(int, input().split()))
num_list = []
divisor_list = []
for i in range(N):
    div_list = make_divisors(A[i])
    for j in range(len(div_list)):
        if div_list[j] in divisor_list:
            num_list[divisor_list.index(div_list[j])] += 1
        elif i <= 2 :
            divisor_list.append(div_list[j])
            num_list.append(1)
        else :
            c = 1
M = len(num_list)
for i in range(len(num_list)):
    if num_list[M-i-1] == N or num_list[M-i-1] == (N-1):
        print(divisor_list[M-i-1])
        break

Submission status: Wrong Answer

Solution code:

# from web
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(int(n))
  return table

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

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    b = sorted(make_divisors(a[0]), reverse = True)
    res = b[0]
    for i in b:
        c = [_ % i != 0 for _ in a]
        if sum(c) == 1:
            res = i
            break
    a2 = a[1:]
    b2 = sorted(make_divisors(a2[0]), reverse = True)
    res2 = b2[0]
    for i in b2:
        c = [_ % i != 0 for _ in a2]
        if sum(c) == 0:
            res2 = i
            break
    print(max(res,res2))

Submission status: Wrong Answer

Solution code:
import fractions;

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

max_gcd = 0;


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

else:
	for i in range(N):
		for j in range(i+1,N):

			g=fractions.gcd(A[i],A[j])			

			if max_gcd < g:
				max_gcd = g;

	print(max_gcd);

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[0][1]])
        tmp2 = fractions.gcd(A[g[0][1]+1], A[g[0][1]+1])
        #print(tmp1, tmp2)
        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:
def gcd(b,c):
    if c==0:
        return b
    return gcd(c,b%c)

n=int(input())
a=sorted([int(i) for i in input().split()])
gcd1=[0]*n
gcd2=[0]*n
for i in range(n):
    if i!=0:
        gcd1[i]=gcd(a[0],a[i])
    if i!=1:
        gcd2[i]=gcd(a[1],a[i])
m=sorted([max(gcd1),max(gcd2)])
cnt1=0
cnt2=0
for i in range(n):
    if a[i]%m[0]!=0:
        cnt1+=1
    if a[i]%m[1]!=0:
        cnt2+=1
if cnt2<=1:
    print(m[1])
elif cnt1<=1:
    print(m[0])
else:
    print(1)

Submission status: Wrong Answer

Solution code:
from functools import reduce

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

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

num=0
for i in range(N-1):
    B=A[:i]+A[i+1:]
    num=max(num,reduce(gcd,B))
print(num)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
i = 0
X = 0
Y = 0
for x in input().split():
  x = int(x)
  A.append(x)

A[0] = 4

if len(A) == 1:
  print(A[0])
else:
  S = A[0]
  for i in range(1,len(A)):
    if A[i] >= S:
      X = A[i]
      Y = S
    else:
      X = S
      Y = A[i]
    while True:
      amari = X % Y
      X = Y
      Y = amari
      if amari <= 0:
        break
    S = X
  print(S)

Submission status: Wrong Answer

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

def gcd_check(data, ans):
    p = list(itertools.permutations(data, 2))
    for i in range(len(p)):
        ans.append(reduce(gcd, p[i]))
 
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:
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
vmin = 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]:
        vmin = bn[i]
        break
print(vmin)

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(max(k))
    exit()

l.append([cost, g[len(g)-1]])

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:
from fractions import gcd
# from math import gcd

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


def f(a, l):
    ba = a
    ai = 0
    for i, v in enumerate(l):
        a_ = gcd(ba, v)
        if ba > a_:
            ba = a_
            ai = i + 2
    return (ba, ai)

a = gcd(A[0], A[1])
if a == 1:
    ba1, _ = f(A[0], A[2:])
    ba2, _ = f(A[1], A[2:])
    print(max(ba1, ba2))
else:
    ba, ai = f(a, A[2:])
    if ai == 0:
        ba1, _ = f(A[0], A[2:])
        ba2, _ = f(A[1], A[2:])
        print(max(ba1, ba2))
    else:
        A[ai] = A[0]
        ba, _ = f(a, A[2:])
        print(ba)

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(max(a) + 1)]

    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:

n = int(input())

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


from functools import lru_cache
import sys
if n == 2:
    print(max(a))
    sys.exit()


@lru_cache(maxsize=100000)
def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


def gcd_list(li):
    if len(li) == 1:
        return li[0]
    g = gcd(li[0], li[1])
    for i in range(2, len(li)):
        g = gcd(g, li[i])
    return g


def gcd_list_2(li):
    g = gcd(li[0], li[1])
    if g == 1:
        return max(gcd_list([li[0]] + li[2:]), gcd_list(li[1:]))
    for i in range(2, n):
        if gcd(g, li[i]) == 1:
            if i == n - 1:
                return gcd_list(li[:i-1])
            gg = gcd_list(li[i+1:])
            print(gg)
            bb = [gcd(li[x], gg) for x in range(i+1)]
            bb.sort()
            return bb[1]
        else:
            g = gcd(g, li[i])
    return g


def max_gcd(a):
    g = gcd_list(a)
    a = [a[i]//g for i in range(n)]
    m = gcd_list_2(a)
    print(g * m)


max_gcd(a)

Submission status: Wrong Answer

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

def gcd(A, B):
    if A < B:
        A, B = B, A
    if B == 0:
        return A
    else:
        return gcd(B, A%B)



if N == 2:
    print(max(a))
else:

    small = 1
    x = a[0]
    x2 = a[1]
    big = N - 2
    y = a[-1]
    i, j, k = gcd(x, x2), gcd(x, y), gcd(x2, y)
    if i < k and j < k:
        x = a[1]
        a[1], a[0] = a[0], a[1]
    elif k < i and j < i:
        y = a[1]
        a[1], a[-1] = a[-1], a[1]

    while small < big:
        if gcd(a[small], x) < gcd(a[big], y):
            y = gcd(a[big], y)
            big -= 1
        else:
            x = gcd(a[small], x)
            small += 1

    print(gcd(x, y))

Submission status: Wrong Answer

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

N=int(input())
A=[int(s)for s in input().split()]
A=sorted(A)
if N==2:
  print(A[1])
  exit()
r=[]
for i in range(1,N):
  r.append(gcdf(A[i],A[0]))
gcd=r[1]
gcd2=A[1]
for i in range(2,N):
  gcd2=gcdf(A[i],gcd2)
print(max((gcd,gcd2)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
count = 0
temp = []
for i in a:
  if i%2==1:
    count+=1
    temp.append(i)
def fanc(i,j):
  if j ==0:
    return i
  return fanc(j,i%j)

if len(a) ==2:
  print(max(a[0],a[1]))
  exit()
elif count == 1:
  a.remove(temp[0])
s = fanc(a[0],a[1])
for i in range(2,len(a)):
  s = fanc(s,a[i])
print(s)

Submission status: Wrong Answer

Solution code:
import copy

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

if N % 2 > 0:
    N += 1
    A.append(A[-1])

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

gcd_array = []
min_gcd = 10**9 + 1
min_idx = -1
min_val1 = 0
min_val2 = 0
for i in range(0, int(0.5*N)):
    val = gcd(A[2*i], A[2*i+1])
    gcd_array.append(val)
    if val < min_gcd:
        min_gcd = val
        min_idx = len(gcd_array)-1
        min_val1 = A[2*i]
        min_val2 = A[2*i+1]

gcd_array2 = copy.deepcopy(gcd_array)

gcd_array[min_idx] = min_val1
gcd_array2[min_idx] = min_val2

val1 = gcd_array[0]
val2 = gcd_array2[0]
for i in xrange(1, len(gcd_array)):
    val1 = gcd(gcd_array[i], val1)
    val2 = gcd(gcd_array[i], val2)

if val1 > val2: print val1
else: print val2

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] == 1: delete.append(x)
    delete.sort(reverse = True)
    for d in delete: ans.pop(d)
print(max(ans))

Submission status: Wrong Answer

Solution code:
import functools
import fractions


N = int(input())
A = list(map(int, input().split()))
gcd = functools.reduce(fractions.gcd, A)
for i in range(N):
    A_sub = (A[0:i]+A[i+1:]).copy()
    gcd_new = functools.reduce(fractions.gcd, A_sub)
    if gcd_new >= gcd:
        gcd = gcd_new
        break
print(gcd)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
from fractions import gcd
def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a
ans=1
lst=[]
for i in range(n):
    lst.append((gcd(l[0],l[i]),i))
lst.sort()

num=lst[0][1]
q=l[0]
r=l[num]
l.remove(q)
x=gcdlist(l)
l.append(q)
l.remove(r)
y=gcdlist(l)
print(max(x,y))

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):
        for j in range(N):
            if i == j:
                continue
            g = gcd(A[i], A[j])
            if g > m:
                m = g


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


if __name__ == '__main__':
    main()