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()))
As = [[0]*N for _ in range(N-1)]
As[0] = A

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

agcd = A[0]
agcd2 = A[0]
agcd3 = A[0]
for i in range(1,N):
    agcd = gcd(agcd,A[i])


is_skipped = False
for i in range(1,N):
    if(gcd(agcd2,A[i]) == agcd and not is_skipped):
        is_skipped =True
        continue
    agcd2 = gcd(agcd2,A[i])

is_skipped = False
for i in range(1,N):
    if(gcd(agcd3,A[i]) == agcd and not is_skipped):
        is_skipped =True
        continue
    agcd3 = gcd(agcd3,A[-i])

agcd4 = A[1]    
is_skipped = False
for i in range(1,N):
    if(gcd(agcd4,A[i]) == agcd and not is_skipped):
        is_skipped =True
        continue
    agcd4 = gcd(agcd4,A[-i])



print(max(agcd2,agcd3,agcd4))

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)
        B = sorted(B)
        gcm = gcd(B[0], B[1])
        if gcm > ans:
            ans = gcm
print(ans)

Submission status: Wrong Answer

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

n=int(input())
v=[int(x) for x in input().split()]
b=0
c=int(min(v))
d=int(min(v))
e=0
f=0
for i in range(n):
    if v[i] !=c:
      l=fractions.gcd(c,v[i])
      if l==1:
        b +=1
      elif l>1 and l<d:
        e=d
        d=l
if b>1:
  print(1)
else:
  print(int(e))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc125/tasks/abc125_c

import itertools
from collections import Counter
from collections import defaultdict
import bisect

memo = {}


def gcd(a, b):
    if a in memo:
        if b in memo[a]:
            return memo[a][b]
    ans = gcd_core(a, b)
    if a in memo:
        memo[a][b] = ans
    else:
        memo[a] = {}
        memo[a][b] = ans
    return ans


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


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

    ming = 0
    pre_index = 0
    index = 0

    for i in range(N):
        temp = gcd(A[i], ming)
        # print(temp)
        if temp != ming:
            pre_index = index
            index = i
        ming = temp

    # print(index)

    ans1 = 0
    for i in range(N):
        if i != pre_index:
            ans1 = gcd(A[i], ans1)

    ans2 = 0
    for i in range(N):
        if i != index:
            ans2 = gcd(A[i], ans2)

    print(max(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce
import random

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

for i in range(1000):
    rand = random.randrange(n-1)
    t = b[rand]
    cnt = 0

    for i in b[:rand] + b[rand+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:
def GCD(a, b):
    while a % b != 0:
        r = a % b
        a, b = b, r
    return b

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

if len(A2) <= 2:
    A2 = (A2[0],A2[0])

S = 0
for j in range(len(A2)):
    for i in range(len(A2)):
        if S != i:
            Af = GCD(Af,A[i])
    ans = max(ans,Af)
    S += 1
    Af = 0


print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int, input().split()))
#まずはA[0]以外のgcdをansとしておく
ans = A[1]
for i in range(2, N):
	ans = fractions.gcd(ans, A[i])
#つぎにA[0]と他の要素の2数のgcdをリストアップし、それが一番小さくなるindexをkとする
G = [A[0]]
for j in range(1, N):
	G.append(fractions.gcd(A[0], A[j]))
k = G.index(min(G))
#A[k]以外のgcdをtmpとしておく
if k == N - 1:
	B = A[: N - 1]
else:
	B = A[: k] + A[k + 1:]
tmp = B[0]
for l in range(1, N - 1):
	tmp = fractions.gcd(tmp, B[l])
#ansとtmpの大きい方を出力
print(max(ans, tmp))

Submission status: Wrong Answer

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

import sys
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=LIST()

a=A[0]
idx=1
for i in range(1, N):
    b=gcd(a, A[i])
    if b<a:
        a=b
        idx=i

B=list(set(A))
if len(B)!=1:
    B.pop(B.index(A[idx]))
    ans1=B[0]
    for b in B:
        ans1=gcd(ans1, b)
    B=list(set(A))
    B.pop(B.index(A[idx-1]))
    ans2=B[0]
    for b in B:
        ans2=gcd(ans2, b)
    print(max(ans1, ans2))
else:
    print(B[0])

Submission status: Wrong Answer

Solution code:
import fractions


def solve(N, A):
    max_gcd = 0
    gcd1memo = [None] * N  # gcd [0, i]
    gcd2memo = [None] * N  # gcd [i, N - 1]
    gcd1memo[0] = A[0]
    gcd2memo[N - 1]= A[N - 1]
    for i in range(1, N):
        gcd1memo[i] = fractions.gcd(gcd1memo[i - 1], A[i])
    for i in reversed(range(N - 1)):
        gcd2memo[i] = fractions.gcd(gcd2memo[i + 1], A[i])
    for i in range(N):
        gcd = None
        if i == 0:
            gcd = gcd2memo[1]
        elif i == N - 1:
            gcd = gcd1memo[N - 1]
        else:
            gcd = fractions.gcd(gcd1memo[i - 1], gcd2memo[i + 1])
        max_gcd = max(max_gcd, gcd)
    return max_gcd


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

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

A = sorted(A)

import fractions
dmin = []
dmax = []
mina = min(A)
maxa = max(A)
for i in range(N):
    dmin.append(fractions.gcd(mina,A[i]))
    dmax.append(fractions.gcd(maxa,A[i]))

dmin = sorted(dmin)
dmax = sorted(dmax)
if len(dmax) == 1:
    print(dmax[0])
    exit()

if dmax[0] != dmax[1]:
    if dmin[0] != dmin[1]:
        print(max(dmax[1],dmin[1]))
    else:
        print(max(dmax[0],dmin[0]))
else:
    if dmin[0] != dmin[1]:
        print(max(dmax[0],dmin[1]))
    else:
        print(max(dmax[0],dmin[0]))

Submission status: Wrong Answer

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

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

valueList = []
maxList = []

for i in range(n):
  g = a[0]
  for j in range(i):
    g = gcd(g,a[j])
  valueList.append(g)
  g = valueList[i]
  for k in range(i+1, n):
    g = gcd(g, a[k])
  maxList.append(g)
    
print(max(maxList))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
divisors = []
for j in range(2):
    for i in range(1, int(a[j]**0.5)+1):
        if a[j] % i == 0:
            divisors.append(i)
            if i != a[j] // i:
                divisors.append(a[j]//i)
divisors = list(set(divisors))
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:
try:
    from math import gcd
except:
    from fractions import gcd

N = int(input())
A = [int(s) for s in input().split()]
ans0 = 0
ans1 = 0
for a in A:
    ans1 = max(gcd(ans1, a), ans0)
    ans0 = gcd(ans0, a)
print(max(ans0, ans1))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()

def primes(N):
    sieve = [1]*(N+1)
    sieve[:2] = [0, 0]
    P = []
    for i in range(2, N+1):
        if sieve[i]:
            for j in range(2*i, N, i):
                sieve[j] = 0
            P.append(i)
    return P

P = set(primes(32000))
D = defaultdict(list)
C = set()
for a in A:
    removes = []
    for p in P:
        cnt = 0
        while a%p==0:
            cnt += 1
            a //= p
        if cnt:
            D[p].append(cnt)
        else:
            if p in C:
                removes.append(p)
            else:
                C.add(p)
    P = P - set(removes)
    if a > 1:
        D[a].append(1)

D = {k:[0]*(len(v) == N-1) + sorted(v) for k, v in D.items() if len(v) >= N-1}

ori = 1
for k, v in D.items():
    ori *= k**v[0]

ans = max(ori, 1)
for a in A:
    tmp = 1 * ori
    for p in D.keys():
        cnt = 0
        while a%p==0:
            cnt += 1
            a //= p
        if cnt and cnt == D[p][0]:
            tmp *= p**(D[p][1] - D[p][0])
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input()) 
alist = list(map(int, input().split()))

def gcd2(a,b):
  while b!=0:
    a,b=b,a%b
  return a

ansList = []
if len(alist) == 2:
  ans = max(alist)
else:
  for i, v in enumerate(alist):
    if i == len(alist) - 2:
      ansList.append(gcd2(alist[i], alist[i+1]))
    elif i < len(alist) - 2:
      a1 = gcd2(alist[i], alist[i+1])
      a2 = gcd2(alist[i], alist[i+2])
      ansList.append(max(a1,a2))
  sortList = sorted(ansList)
  ans = sortList[1]      

print("{}".format(ans))

Submission status: Wrong Answer

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

n=int(input())
v=[int(x) for x in input().split()]
b=0
c=int(min(v))
d=int(min(v))
e=0
f=0
for i in range(n):
    if v[i] !=c:
      l=fractions.gcd(c,v[i])
      if l==1:
        b +=1
      elif l>1 and l<d:
        e=d
        d=l
if b>2:
  print(1)
else:
  print(int(e))

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)

maxg = s_g0[0] #一番大きいgcd
midg = s_g0[1] #二番目に大きいgcd
ming = s_g0[2]

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

print(max(maxg,midg,ming))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce

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

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

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

min_res = min(res1)
index = res1.index(min_res)

res1 = [A[i] for i in range(N) if i != index]
res2 = A[1:]

res1 = gcd_list(res1)
res2 = gcd_list(res2)

print(max(res1,res2))

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
A = list(map(int, input().split()))
gcd_list = [max([fractions.gcd(A[i], A[j]) for j in range(N) if i != j]) for i in range(N)]
gcd_list.sort()
print(gcd_list[1])

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(1,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:
import fractions
import functools
N = int(input())
A = [int(i) for i in input().split()] 
max_gcd = functools.reduce(fractions.gcd, A)
for i in range(N):
	tmp = A.copy()
	if A[i]%2==1:
		tmp.pop(i)
		gcd = functools.reduce(fractions.gcd, tmp)
	else:
		gcd = functools.reduce(fractions.gcd, tmp)
	if max_gcd < gcd:
		max_gcd = gcd
print(max_gcd)

Submission status: Wrong Answer

Solution code:

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

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


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


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

notrewrite[0]=rewrite[0]=best[0]=a[0]
for i in range(1,N):

    notrewrite[i]=gcd(notrewrite[i-1],a[i])
    rewrite[i]=notrewrite[i-1]
    best[i]=max([rewrite[i],gcd(a[i],best[i-1])])
    #print([notrewrite[i],rewrite[i]])

print(best[N-1])

Submission status: Wrong Answer

Solution code:
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

N = I()
A = IL()
A.sort()
ABS = [A[0]]
tmp = MAX_INT
num = 0
for i in range(1,N):
    if tmp > A[i]-A[i-1]:
        tmp = A[i]-A[i-1]
        num = i
    ABS.append(A[i]-A[i-1])

if num == 0:
    pass
elif num == N-1:
    i -= 1
elif A[num] - A[num-1] < A[num+1] - A[num]:
    i -= 1

f = 0
for i in range(N):
    if i == num:
        continue
    if f == 0:
        ans = A[i]
        f = 1
    else:
        if A[i]%ans==0:
            continue
        else:
            ans = gcd(ans,A[i])

print(ans)

Submission status: Wrong Answer

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

from fractions import gcd

b = [0] * n

if n == 2:
    print(gcd(a[0], a[1]))
else:
    for i in range(n-1):
        for k in range(n):
            if k != i:
                if i+1 != k:
                    if b[k] == 0:
                        b[k] = gcd(a[i], a[i+1])
                    else:
                        b[k] = gcd(b[k], a[i+1])
                else:
                    if i+2 < n:
                        if b[k] == 0:
                            b[k] = gcd(a[i], a[i+2])
                        else:
                            b[k] = gcd(b[k], a[i+2])

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

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 min(kouho) == max(hoge):
        kouho = [max(hoge)]
    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 min(kouho) == max(hoge):
        kouho = [max(hoge)]
    else:
        kouho = hoge

a2 = max(kouho)

print(max(a1, a2))

Submission status: Wrong Answer

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

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

r=[]
l=[]
resr = ai[0]
resl = ai[n-1]
r.append(ai[0])
l.append(ai[n-1])
for i in range(1,n):
	r.append(gcd(resr,ai[i]))
	l.append(gcd(resl,ai[n-1-i]))

#print(r)
#print(l)

resmax = 1
for i in range(n):
	if i == 0:
		res = l[n-1-1]
	elif i == n-1:
		res = r[n-1-1]
	else:
		res = gcd(r[i-1], l[n-1-i-1])
	#print(res)
	if res > resmax:
		resmax = res

print(resmax)

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

if N == 2:
  print(max(A))
else:
  l = []
  for i in range(0,N-1):
      l.append(gcd(A[i], A[i+1]))
  m = 1000000000
  for i in l:
    if l.count(i) > 1:
      m = min(m, i)
  print(m)

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()
if cost >= 1:
    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:
import heapq
N = int(input())
A = [int(i) for i in input().split()]

AS = min(A)
ASs =[]
i = 2
c = -1
while AS >= i*2:
    j = 2
    p = True
    while i >= j**2 and i > 2:
        if i%j == 0:
            p = False
            break
        j += 1
    if p:
        c += 1
        ASs.append([i])
        ASs[c].append([0 for k in range(N)])

    i += 1
for i in range(N):
    for j in ASs:
        while True:
            if A[i] % (j[0]) == 0:
                j[1][i] += 1
                A[i] /= j[0]
            else:
                break
x = []
for i in ASs:
    x.append([i[0],heapq.nsmallest(2,i[1])])
ans = 1
last = 1
for y in x:
    ans *= y[0] ** min(y[1])
    t = y[0] ** abs(y[1][0] - y[1][1])
    if t > last:
        last = t
print(ans*last)

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()]
AA = []
for i in A:
    if(not i in AA):
        AA+=[i]
AA.sort()
cand = -1
mingcd = gcd_list(AA)
if(len(AA)<=2):
    print(max(AA))
else:
    for i in range(len(AA)-1):
        if(mingcd==gcd(AA[i],AA[i+1])):
            if(i<len(AA)-2):
                if(mingcd==gcd(AA[i],AA[i+2])):
                    cand = i
                else:
                    cand = i+1
                break
            else:
                cand = i+1
    if(cand==-1):
        cand = len(AA)-1
    print(gcd_list(AA[:cand]+AA[cand+1:]))

Submission status: Wrong Answer

Solution code:
import copy
import math
from functools import reduce

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

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

def is_prime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True

n = int(input())
a = list(map(int, input().split()))
a.sort()
rtn = gcd_list(a)
a_2 = [x / rtn for x in a]
flg = True

for x in a_2:
    if is_prime(x):
        a_2.remove(x)
        flg = False
        break

if flg:
    del a_2[0]

rtn_2 = gcd_list(a_2) if len(a_2) > 1 else 1

print(int(rtn * rtn_2))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

lowest = 1
g1 = As[0]
for i in range(1, N):
    new_g = gcd(As[i], g1)
    if new_g != g1:
        lowest = i
    g1 = new_g

g2 = As[0]
for i in range(1, N):
    if i == lowest:
        continue
    g2 = gcd(As[i], g2)

g3 = As[1]
for i in range(2, N):
    g3 = gcd(As[i], g3)

print(max(g1, g2, g3))

Submission status: Wrong Answer

Solution code:
import fractions

N=int(input())
a_list = [int(i) for i in input().split()]
a_list.sort()
b_list = a_list[:]
del b_list[-1]
ans1 = b_list[0]
for i in range(N-1):
	ans1 = fractions.gcd(ans1, b_list[i])

max_number = a_list[-1]
min_number = a_list[-1]
min_index = 0
for i in range(N-1):
	gcd = fractions.gcd(max_number,a_list[i])
	if gcd < min_number:
		min_index = i
		min_number = gcd

del a_list[min_index]
ans2 = a_list[0]
for j in range(N-1):
	ans2 = fractions.gcd(ans2, a_list[j])
print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())

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


def gcd(x, y):
    while x % y != 0:
        x, y = y, x % y

    return y


max_ans = 1
f_gcd = a[0]
for i in range(n):
    if i > 0:
        f_gcd = gcd(f_gcd, a[i-1])

    if i < n-1:
        b_gcd = a[i+1]
        for j in a[i+1:]:
            b_gcd = gcd(b_gcd, j)

    if i == 0:
        ans = b_gcd
    elif i == n:
        ans = f_gcd
    else:
        ans = gcd(b_gcd, f_gcd)

    if ans > max_ans:
        max_ans = ans

print(max_ans)

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:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]

    li.sort()

    if (li[0] == mae1 and li[1] == mae3 and li[2] == mae4) or (li[0] == usiro1 and li[1] == usiro3 and li[2] == usiro4):
        print(li[3])
    elif (li[0] == mae2 and li[0] == mae1) or (li[0] == usiro2 and li[0] == usiro1):
        print(li[2])
    else:
        print(li[4])

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)

min2A = A[A.argsort()[1]]
minA = np.amin(A)
if np.sum((A%min2A)==0)>=N-1:
    print(min2A)
elif np.sum((A%minA)==0)>=N-1:
    print(minA)
else:
    #for i in range((min2A//2)+1)[::-1]:
    #    if np.sum((A%i)==0)>=N-1:
    #        print(i)
    #        break
    for i in range(min2A):
        if (min2A%i == 0) or (minA%i == 0):
            j = min2A/i
            if np.sum((A%j)==0)>=N-1:
                print(j)
                break

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
A = sorted(list(map(int,input().split())),key=lambda x:abs(x))

ans = []
for i in range(N - 1):
  ans.append(fractions.gcd(A[i],A[i + 1]))
  if i + 2 < N:
    ans.append(fractions.gcd(A[i],A[i + 2]))

ans = list(set(sorted(ans)))
print(max(A) if len(ans) == 1 else (ans[-1] if len(ans) == 2 else ans[2]))

Submission status: Wrong Answer

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

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

if A.count(max(A)) >= 2:
    print(gcd(max(A), max(A)))

elif sum(A) % 2 != 0:
    print(gcd(max(A), min(A)))

else:
    A.remove(min(A))
    print(gcd(max(A), min(A)))

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(max(gc1,gc2))

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

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

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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

n = int(input())
a = list(map(int,input().split()))
max = 0
for i in range(n):
  li = a[:i] + a[i + 1:]
  tmp = gcd_list(li)
  if tmp > max:
    max = tmp
    print(max)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
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 = map(int, input().split())

cnt_div = dict()
for i, _a in enumerate(a):
    if i < 2:
        for d in make_divisors(_a):
            if d in cnt_div:
                cnt_div[d] += 1
            else:
                cnt_div[d] = 1
    else:
        tmp = {}
        for d, c in cnt_div.items():
            if _a % d == 0:
                tmp[d] = c + 1
        cnt_div = tmp


max_div = 1
for r, c in cnt_div.items():
    if c >= n - 1:
        max_div = max(max_div, r)

print(max_div)

Submission status: Wrong Answer

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

dic = {}

for i in range(1,a[0]+1):
        if a[0]%i==0:
            print (i,"was inserted")
            dic[i]=1
            
for i in range(1,a[1]+1):
        if a[1]%i==0:
            print (i,"was inserted")
            if i in dic:
                dic[i]+=1
            else:
                dic[i]=1
                
for x in (a[2:]):
    for i in range(1,x+1):
        if x%i==0:
            print (i,"was inserted")
            if i in dic:
                dic[i]+=1
largest = 1
for id in dic:
    if (id>largest)and(dic[id]>=n-1):
        largest=id

print (largest)

Submission status: Wrong Answer

Solution code:
import fractions
import functools

N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
gcdA=functools.reduce(fractions.gcd,A)
ans=gcdA
B=A
for i in range(N):
    num=B[i]
    B.pop(i)
    gcdB=fractions.gcd(B[0],B[-1]) 
    gcd=max(gcdA,gcdB)
    if gcd>ans:
        ans=gcd
        B.insert(i,num)
    else:
        B.insert(i,num)
print(ans)

Submission status: Wrong Answer

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

def gcd_check(data, ans):
    p = list(itertools.combinations(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 C(n,arr, mid):
  cnt = 0
  #print(mid)
  for i in range(n):
    if arr[i]  >= mid and arr[i] % mid == 0:
    #if arr[i] / mid >= 1:
      cnt += 1
    else:
      return False
  return True 
  
n = int(input())
a = list(map(int,input().split()))
ans = 0

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

for i in range(n):
  lb = 0
  ub = 10 ** 9
  tmp_list = a[:i] + (a[i+1:])
  while ub-lb > 1:
    mid = (ub+lb) // 2 
    if C(n-1, tmp_list, mid):
      lb = mid
      break
    else:
      ub = mid
  tmp = 0
  for j in range(lb, ub+1):
        cnt = 0
        for k in tmp_list:
            #print(k % j)
            if k % j == 0:
              #break
              cnt += 1
        if cnt == n-1:    
          tmp = j
  
  #print(tmp_list)
  #print(lb,ans)
  if ans < tmp:
        ans = tmp

  
print(ans)

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=10000000001
for i in range(n):
    y=min(y,a[i])
    

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:
from fractions import gcd
from functools import reduce
def gcd_list(numbers):
    return reduce(gcd,numbers)
import sys
input=sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))

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

A.sort()
B=A+[A[0]]
min1,min2=float('inf'),float('inf')
min1_idx,min2_idx=0,1
for i in range(n):
    g=gcd(B[i],B[i+1])
    if g<min1:
        min2=min1
        min2_idx=min1_idx
        min1=g
        min1_idx=i
    elif g<min2:
        min2=g
        min2_idx=i
    #print(g,min1,min2)
    #print(i,min1_idx,min2_idx)
#print(min1_idx,min2_idx)
if abs(min1_idx-min2_idx)==1:
    idx=max(min1_idx,min2_idx)
else:
    idx=0
#print(idx)
del A[idx]
print(gcd_list(A))

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()]
al = sorted(al)[:50]

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