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

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

tmp = A[0]
for i in range(N):
    tmp = gcd(tmp, A[i])
    
    
for i in range(N):
    A[i] //= tmp
    
v1 = A[0]
count1 = 0
for i in range(N):
    tmp1 = v1
    v1 = gcd(v1, A[i])
    if  v1 == 1 and count1 == 0:
        count1 += 1
        v1 = tmp1
        
        
v2 = A[1]
count2 = 0
for i in range(N):
    tmp2 = v2
    v2 = gcd(v2, A[i])
    if v2 == 1 and count2 == 0:
        count2 += 1
        v2 = tmp2

if N != 2:
    print(tmp * max(v1, v2))
else:
     print(tmp * min(v1, v2))

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
A = input().split()

max_gcd = 0
for i in range(N-1):
  for j in range(i+1, N):
    tmp = fractions.gcd(int(A[i]), int(A[j]))
    if max_gcd < tmp:
      max_gcd = tmp

print(max_gcd)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

num = 1

for i in range(np.max(A)):
  B = [m for m in A if m // (i + 1) != 0]
  if len(B) == 0 or len(B) == 1:
    num = (i + 1)
    
print(num)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from functools import reduce


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

def gcd_group(A):
    return reduce(gcd, A)


def solve(N: int, A: "List[int]"):
    left = [A[0]]
    right = [A[-1]]
    for i in range(1, N):
        left.append(gcd(left[i-1], A[i]))
        right.append(gcd(right[-i], A[-i-1]))
    right.reverse()

    ret = 0
    for i in range(N):
        if i == 0:
            temp = right[1]
        elif i == N-1:
            temp = left[-2]
        else:
            temp = gcd(left[i-1], right[i+1])

        if temp > ret:
            ret = temp
    print(ret)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

n = int(input())
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)):
        for j in range(i+1, len(an)):
            tmo = gcd(an[i], an[j])
            sample.append(gcd(an[i], an[j]))

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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


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


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

    b = a[i+1:]
    if len(b) > 0:
        b_ans = b[0]
        for k in b:
            b_ans = gcd(b_ans, k)

    if len(f) == 0:
        ans = b_ans
    elif len(b) == 0:
        ans = f_ans
    else:
        ans = gcd(f_ans, b_ans)

    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 = [int(_) for _ in input().split()]
A = sorted(A)
GCD = []

if N == 2:
    print(max(A[0], A[1]))
else:
    for i in range(1, N):
        GCD.append(gcd(A[0], A[i]))
    
    GCD = sorted(GCD)
    
    print(GCD[1])

Submission status: Wrong Answer

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

import fractions

for i in range(N):
    for j in range(N):
        if i == j:
            pass
        else:
            if fractions.gcd(A[i], A[j]) > max_gcd:
                max_gcd = fractions.gcd(A[i], A[j])

print(max_gcd)

Submission status: Wrong Answer

Solution code:
from functools import reduce


def gcd(a, b):
    if not b:
        return a
    return gcd(b, a % b)


def main():
    input()
    nums = set(map(int, input().split()))

    if len(nums) == 1:
        return nums.pop()

    max_c = reduce(gcd, nums)
    if len(nums) == 2:
        return max_c

    for i in range(len(nums)):
        poped_nums = list(nums)
        poped_nums.pop(i)
        c = reduce(gcd, poped_nums)
        if c > max_c:
            max_c = c
    return max_c


print(main())

Submission status: Wrong Answer

Solution code:
from fractions import gcd

n = int(input())
A = list(map(int, input().split(" ")))

if n == 2:
    print(max(A))
else:
    min0, min1, min2 = 10 ** 9 + 1, 10 ** 9 + 1, 10 ** 9 + 1
    for i in range(n):
        for j in range(i + 1, n):
            g = gcd(A[i], A[j])
            if g < min0:
                min0, min1, min2 = g, min0, min1
            elif g < min1:
                min1, min2 = g, min1
            elif g < min2:
                min2 = g

    print(min2)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
import fractions

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


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


def main():
    n = II()
    A = LI()
    A.sort()
    A.reverse()

    pre = [0] * n
    pre[0] = A[0]
    for i in range(1, n):
        pre[i] = gcd(pre[i - 1], A[i])
    res = pre[-1]
    for i in range(n):
        t = pre[i]
        for j in range(i + 2, n):
            t = gcd(t, A[j])
            if t == pre[j - 1]:
                t = -1
                break
        res = max(res, t)

    return res


print(main())

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

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

class Prime():
    def __init__(self, n):
        self.M = m = int(math.sqrt(n)) + 10
        self.A = a = [True] * m
        a[0] = a[1] = False
        self.T = t = []
        for i in range(2, int(math.sqrt(m)) + 1):
            if not a[i]:
                continue
            t.append(i)
            for j in range(i*i,m,i):
                a[j] = False

    def is_prime(self, n):
        return self.A[n]

    def division(self, n):
        d = collections.defaultdict(int)
        for c in self.T:
            while n % c == 0:
                d[c] += 1
                n //= c
            if n < 2:
                break
        if n > 1:
            d[n] += 1
        return d.items()

    def sowa(self, n):
        r = 1
        for k,v in self.division(n):
            t = 1
            for i in range(1,v+1):
                t += math.pow(k, i)
            r *= t
        return r


def main():
    n = I()
    ac = collections.Counter(LI())
    a = []
    aa = []
    for k,v in ac.items():
        if v > 1:
            aa.append(k)
        else:
            a.append(k)
    tr = -1
    if len(aa) > 0:
        tr = aa[0]
        for k in aa[1:]:
            tr = fractions.gcd(k,tr)

    l = len(a)
    if l < 1:
        return tr
    if l < 3:
        r = 0
        for i in a:
            kr = fractions.gcd(i,tr)
            if r < kr:
                r = kr
        return r
    m = max(a)
    lm = m // (l-1)
    pr = Prime(10**9)
    p = collections.defaultdict(int)

    for b in a:
        r = [1]
        for k, v in pr.division(b):
            for c in r[:]:
                for i in range(1,v+1):
                    t = c*(k**i)
                    if t > lm:
                        break
                    r.append(t)
        for k in r:
            p[k] += 1
    r = 1
    for k,v in p.items():
        if v >= n-1 and r < k:
            r = k

    if tr == -1:
        return r
    return fractions.gcd(r, tr)


print(main())

Submission status: Wrong Answer

Solution code:
import copy

N = int(input())
A = list(map(int, input().split()))
ans = 0
tmp = []

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()
tmp.append(gcd(A[0], A[-1]))
tmp.append(gcd(A[1], A[-1]))
tmp.append(gcd(A[0], A[-2]))
ans = max(tmp)
print(ans)

Submission status: Wrong Answer

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

n=int(input())
a=list(map(int, input().split()))
a.sort(reverse=True)
crt_gcd=a[0]
cng=-1
for i, ai in enumerate(a):
  gcdi = gcd(crt_gcd, ai)
  if gcdi<crt_gcd:
    cng=i-1
    crt_gcd=gcdi

if cng==0:
  crt_gcd=a[min(len(a)-1, 2)]
else:
  crt_gcd=a[0]
  
for i, ai in enumerate(a):
  if i!=cng and i!=cng+1:
    gcdi = gcd(crt_gcd, ai)
    if gcdi<crt_gcd:
      crt_gcd=gcdi

print(max(gcd(crt_gcd, a[cng]), gcd(crt_gcd, a[cng+1]), gcd(a[cng], a[cng+1])))

Submission status: Wrong Answer

Solution code:
import collections


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


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    # divisors.sort()
    return divisors
tmp = [''] * N
for i in range(N):
    tmp[i] = make_divisors(A[i])
tmp = [flatten for inner in tmp for flatten in inner]

c = collections.Counter(tmp)

values, counts = zip(*c.most_common())

print(max([list(values)[i] for i , x in enumerate(list(counts)) if x > 1]))

Submission status: Wrong Answer

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

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

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

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

gc = [0] * (N-1)
for i in range(0,N-1):
    gc[i] = gcd(A[i],A[i+1])
#print(gc)
# このgcで最小のものが算出されるところを書き換えればよい？
# 連続してれば共通部、連続してないなら端とか？

mnd = -1
mn = 1e10
for i in range(0, N-1):
    if mn >= gc[i]:
        mnd = i
        mn = gc[i]

if mnd == N-1 and gc[-1] < gc[-2]:
    mnd = N-1

# 連続してれば後者になるのでmndはA[i]に対応することになる
# gcの長さはN-1

B = [a for a in A if a != A[mnd]]
#print(B)

lb = len(B)
if lb == 0:
    g = A[mnd]
elif lb >= 2:
    g = B[0]
    for i in range(1, len(B)):
        g = gcd(g, B[i])
else:
    g = B[0]
print(g)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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

n = int(input())
li = list(map(int, input().split()))
li.sort(reverse=True)
max_gcd = 0
for i in range(len(li)):
    if li[i] <= max_gcd:
        continue
    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:
N = int(input())
A = list(map(int,input().split()))
ans = 0
A = sorted(A)
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
ans = []
if N-1 != 0:
  ans.append(gcd(A[N-1],A[0]))
if N-1 != 1:
  ans.append(gcd(A[N-1],A[1]))
if N-2 != 0:
  ans.append(gcd(A[N-2],A[0]))
if N-2 != 1:
  ans.append(gcd(A[N-2],A[1]))
print(max(ans))

Submission status: Wrong Answer

Solution code:
from functools import reduce

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

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

def gcd_list(numbers):
    return reduce(hcfnaive, numbers)
ans = []
  
for i in range(N//2):
  ans.append(gcd_list(A[i+1:]+A[:i]))
  
ans.sort()
print(ans[-1])

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = sorted(list(map(int,input().split())), reverse = True)

dp = [[0]*2 for i in range(n)]
dp[0][0] = arr[0]
dp[0][1] = arr[1]
def gcd(a,b):
    x = max(a,b)
    y = min(a,b)
    if x%y:
        return gcd(y,x%y)
    else:
        return y
for i in range(n-1):
    dp[i+1][0] = gcd(dp[i][0],arr[i+1])
    dp[i+1][1] = max(dp[i][0],gcd(dp[i][1],arr[i+1]))
print(max(dp[-1]))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n = int(input())
a = list(map(int,input().split()))
mae = [a[0]]
for i in range(n-1):
    mae.append(gcd(mae[i],a[i+1]))
ushiro = [a[n-1]]
for i in range(n-1):
    ushiro.append(gcd(ushiro[i],a[n-i-1]))

ans = ushiro[n-1]
for i in range(n-1):
    if i != n-2:
        ans = max(ans,gcd(mae[i],ushiro[n-i-2]))
    else:
        ans = max(ans,mae[n-1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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


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

# 素因数のリストを作る
#print(A[1])

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

print(ans)

Submission status: Wrong Answer

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

n = int(input())
vals = [int(x) for x in input().split(" ")]
vals.sort()
vals.reverse()

rewrite = False

minindex = 0

minval = vals[0]
for b in vals[1:]:
    temp = b
    a = minval
    tempmin = minval
    
    minval = gcd(a, b)
    if minval < tempmin:
        minindex = temp

if len(vals) > 2 and vals[1] == minindex:
    # 調整
    v1 = gcd(vals[0], vals[2])
    v2 = gcd(vals[1], vals[2])
    if v1 < v2:
        minindex = vals[0]

minval = vals[0]
for b in vals[1:]:
    if b == minindex:
        continue
    a = minval
    minval = gcd(a, b)

print(minval)

Submission status: Wrong Answer

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

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

sort_a = sorted(a)
max_a = sort_a.pop()

max_gcd=0
for i in sort_a:
  temp = gcd(max_a, i)
  if temp>max_gcd:
    max_gcd = temp
print(max_gcd)

Submission status: Wrong Answer

Solution code:
import fractions
import math


def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    minNum = min(A)

    gcd_list = []
    for i in range(N):
        gcd_list.append(fractions.gcd(minNum, A[i]))

    gcd_list = sorted(gcd_list)
    print(gcd_list[1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

gcd = 0;

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

		print(x);
		print(y);
		print("----");

		while y != 0:
			x %= y;
			x, y = y, x;

		if gcd < x:
			gcd = x;

print(gcd);

Submission status: Wrong Answer

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

def gcd_lis(l):
    res = 0
    for i in range(len(l)-1):
        if i == 0:
            res = gcd(l[i],l[i+1])
        else:
            res = gcd(res,l[i+1])
    return res

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

reslis = []
reslis.append(gcd_lis(lis))
for i in range(n):
    tmp = copy.deepcopy(lis)
    del tmp[i]
    reslis.append(gcd_lis(tmp))
print(max(reslis))

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

n = int(input())

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


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


gcd_l = [a[0]]
gcd_r = [a[-1]]
for i in range(1,n):
    gcd_l.append(fractions.gcd(gcd_l[i-1],a[i]))
    gcd_r.append(fractions.gcd(gcd_l[i-1],a[-i-1]))

gcd_r.reverse()

ans = max(gcd_l[1],gcd_r[-2])
for i in range(1,n-1):
    gcd = fractions.gcd(gcd_l[i-1],gcd_r[i+1])
    ans = max(ans,gcd)

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()]
if(N==2):
    print(max(A))
else:
    gc = gcd_list(A)
    current = A[0]
    for i in range(N):
        current = fractions.gcd(current,A[i])
        if(gc==current):
            break
    A.pop(i)
    print(gcd_list(A))

Submission status: Wrong Answer

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

A.sort()

l = []
m = A[0]
for i in range(1, N):
    tmp = gcd(m, A[i])
    if tmp < m:
        l.append(i)
    m = tmp

max_num = m
for l_i in l:
    x = A[0]
    for i in range(1, N):
        if i == l_i:
            continue
        x = gcd(x, A[i])
    max_num = max(max_num, x)

print(max_num)

Submission status: Wrong Answer

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

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

tmp = A[0]
for i in range(N):
    tmp = gcd(tmp, A[i])
    
    
for i in range(N):
    A[i] //= tmp
    
v1 = A[0]
count1 = 0
for i in range(N):
    tmp1 = v1
    v1 = gcd(v1, A[i])
    if  v1 == 1 and count1 == 0:
        count1 += 1
        v1 = tmp1
        
        
v2 = A[1]
count2 = 0
for i in range(N):
    tmp2 = v2
    v2 = gcd(v2, A[i])
    if v2 == 1 and count2 == 0:
        count2 += 1
        v2 = tmp2


print(tmp * max(v1, v2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
if N == 2:
    print(max(A[0], A[1]))
    quit()


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


ans = max(gcd(A[0], A[1]), gcd(A[0], A[2]), gcd(A[1], A[2]))
flag = 0
index = 0
for i in range(N-1):
    if gcd(A[i], A[i+1]) < ans and flag == 0:
        flag = 1
        index = i
        if 0 <= i <= N-3:
            ans = max(gcd(A[i], A[i+2]), gcd(A[i+1], A[i+2]))
            #print('pa1', ans)
        else:
            ans = max(gcd(A[i-1], A[i+1]), gcd(A[i], A[i+1]))
            #print('pa2', ans)
    elif flag == 1:
        ans = gcd(ans, A[i+1])
       # print('pa3', ans)
    else:
        ans = gcd(A[i], A[i+1])
        #print('pa4', ans)

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(p,q):
  while q!=0:
    p,q=q,p%q
  return p

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

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

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 gcd_list(n, a):
    A = a[0:n] + a[n+1:]
    gcd0 = gcd(A[0], A[1])
    for i in range(2, n-1):
        gcd0 = gcd(gcd0, A[i])

    return gcd0


def solve(n, a):
	
    if n == 2:
        return gcd(a[0], a[1])
    gcd0 = gcd_list(0, a)
    for i in range(1,n):
        temp = gcd_list(i, a)
        if temp < gcd0:
            return gcd0
        elif temp > gcd0:
            return temp


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

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a % b)
N = input()
As =  list(map(int, input().split()))
ar = set(sorted(As))
if(len(ar) < 3):
  print(list(ar)[-1])
  quit()
r = [1]
for i, a1 in enumerate(As):
  for a2 in As[i+1:]:
    r.append(gcd(a1, a2))
r = set(sorted(r))
if len(r) == 1:
  print(1)
elif len(r) <= 2 :
  print(list(r)[1])
else:
  print(list(r)[2])

Submission status: Wrong Answer

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

import itertools
def eratosthenes_sieve(n):
    table = [0] * (n + 1)
    prime_list = []
    
    for i in range(2, n + 1):
        if table[i] == 0:
            prime_list.append(i)
            for j in range(i + i, n + 1, i):
                table[j] = 1
    
    return prime_list

if N == 2:
    if a[0] > a[1]:
        print(a[0])
    else:
        print(a[1])
        
else:
    prime =  eratosthenes_sieve(int(10**(4.5)))
    num   =  1
    for i in prime:
        flag = 0
        for cnt, j in enumerate(a):
            if j % i == 0:
                a[cnt] = a[cnt]/i
            elif flag == 0:
                flag = 1
            else:
                print(num)
                exit()
        num = i

Submission status: Wrong Answer

Solution code:
from fractions import gcd

n = int(input())
a = list(map(int, (input().split())))
noskip = gcd(a[0],a[1])

if n == 2:
    print(noskip)
    exit()

skipped = max(gcd(a[0], a[2]), gcd(a[1], a[2]))

for i in range(2, n):
    noskip = gcd(noskip, a[i])
    skipped = max(gcd(skipped, a[i]), noskip)

print(skipped)

Submission status: Wrong Answer

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

wari_num = []
for i in range(1, min(a)+1):
    if min(a) % i == 0:
        wari_num.append(i)

one = 0
for i in wari_num[::-1]:
    for j in a:
        if j % i != 0:
            one += 1
            if one == 2:
                one = 0
            continue

    if one == 1:
        print(i)
        exit()

print(a[0])

Submission status: Wrong Answer

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


# ユークリッド互除法の関数の定義
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


max_A = max(A)
gcd_list = []
if N == 1:
    print(A[0])
else:
    element = gcd(A[0], A[1])
    if N == 2:
        print(element)
        exit()
    else:
        for i in range(N):
            tmp = A[i]
            A[i] = max_A
            for j in range(2, N):
                element = gcd(element, A[i])
            gcd_list.append(element)
            A[i] = tmp
print(max(gcd_list))
# 最大の値と変えてもいい

Submission status: Wrong Answer

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

for i in range(1,n):
    ans[i-1] = fractions.gcd(a[0], a[i])
for i in range(2,n):
    ans2[i-2] = fractions.gcd(a[1], a[i])
sorted(ans)
sorted(ans2)

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

Submission status: Wrong Answer

Solution code:
import sys
import fractions

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

kouho = []
n = ni()
li = na()
if len(li) == 2:
    print(max(li[0], li[1]))
else:
    kouho.append(fractions.gcd(li[0], li[1]))
    kouho.append(fractions.gcd(li[1], li[2]))
    kouho.append(fractions.gcd(li[2], li[0]))
    for i in range(3,n):
        if len(kouho) == 0:
            break
        for num in kouho:
            if li[i] % num != 0:
                kouho.remove(num)
    if len(kouho) == 0:
        print(1)
    else:
        print(max(kouho))

Submission status: Wrong Answer

Solution code:
N = int(input())
L=[]
A=list(map(int,input().split()))
f=min(A)
F=min(A)
for i in range(f):
 L=list(map(lambda n:n/F-i , A))
 if max(L) == 0:
  break
 F-=1
print(F)

Submission status: Wrong Answer

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


N = int(input())
A = list(map(int, input().split()))
a = float('INF')
t = A[0]
x, y = 0, 0
c = 0
for i in range(1, N):
    x = gcd(t, A[i])
    if t != x:
        c = i
        t = x
if c == 0:
    print(t)
elif c == 1:
    B = A
    #del A[0]
    x = A[1]
    for i in range(2, N):
        x = gcd(x, A[i])
    del A[1]
    y = B[0]
    #print(B)
    for i in range(1, N - 1):
        y = gcd(y, B[i])
    #print(y)
    print(max(x, y))
else:
    del A[c]
    t = A[0]
    for i in range(1, N - 1):
        x = gcd(t, A[i])
    print(x)

Submission status: Wrong Answer

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




N = int(input())
A = [int(x) for x in input().split()]
a2 = list(A)
max = 0
for i,_ in enumerate(A):
    A.pop(i)
    res = A[0]
    for i in A[1:]:
        res = gcd(res,i)
    max = res if res >= max else max
    A = list(a2)


print(max)

Submission status: Wrong Answer

Solution code:
import fractions
N=int(input())
a = list(map(int, input().split()))

ans=0
if N==2:
    print(max(a))
else:
    for i in range(N):
        b=a[:]
        b.pop(i)
        for j in range(1, N-1):
            mid=b[0]
            mid = fractions.gcd(mid, b[j])
            if ans<mid:
                ans=mid
    print(ans)

Submission status: Wrong Answer

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

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

mina = min(A)
import fractions
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 math
N =int(input())
a = input().split()
b=0
num=[]
test=[]
res=[]
for i in range(N):
  num.append(int(a[i]))
#a,bの最大公約数を求める
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
for j in range(N):
  test=num[:]
  if len(test) > 2:
    del test[j]
  for i in range(N-1):
	  b = gcd(test[0],test[1])
	  b = gcd(b,test[i])
  res.append(b)

print(max(res))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


# globals
inf = float('inf')
N = 0
A = []
memo = dict()


def set_inputs():
    global N, A
    N = get_int()
    A = get_li()
    return


def gcd_(i, j):
    if j - i < 1:
        return 1
    ret = memo.get((i, j), None)
    if ret:
        return ret
    if i + 1 == j:
        ret = gcd(A[i], A[j])
        memo[(i, j)] = ret
        return ret
    ret = gcd(gcd(i, j-1), A[j])
    memo[(i, j)] = ret
    return ret


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    forward = [A[0]]
    for a in A[1:]:
        forward.append(gcd(forward[-1], a))
    backward = [A[-1]]
    for a in reversed(A[:-1]):
        backward.append(gcd(backward[-1], a))
    backward.reverse()
    ans = max(forward[-2], backward[-2])
    for i in range(1, len(A)-1):
        ans = max(ans, gcd(forward[i-1], backward[i+1]))
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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)
    if cnt.count(tmp)>=len(cnt)-2:
        print(tmp)
        break
    else:
        cnt = [i for i in cnt if i != tmp]

Submission status: Wrong Answer

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

A = list(set(A))
for i,a in enumerate(A):
    if len(A)==1:
        tmpA=A
        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

print(list(tmpA)[0])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 27 20:57:32 2019

@author: Yamazaki Kenichi
"""

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

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

T = [[1,1] for i in range(N)]
T[0][0],T[0][1], = A[0],A[0]
for i in range(1,N):
    T[i][0] = eg(T[i-1][0],A[i])
    T[i][1] = max(eg(T[i-1][1],A[i]),T[i-1][0])
print(max(T[N-1]))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
from fractions import gcd
#from math import gcd
N = int(input())
A = list(map(int, input().split()))
l = []
for i in range(N):
    a = A[i]
    for j in range(N):
        l.append(gcd(a, A[j]))
l = list(set(l))
l.sort()
print(l[1] if len(l) > 1 else l[0])

Submission status: Wrong Answer

Solution code:
import math
from sys import stdin
import numpy as np

n = int(stdin.readline().rstrip())
nums = np.sort(np.array(list(map(lambda x: int(x), stdin.readline().rstrip().split()))))

n1 = nums[1]

flag1 = list(map(lambda x: x.is_integer(), nums[1:]/nums[1]))
if not False in flag1:
    print(n1)
else:
    print(np.min(np.diff(np.delete(nums, 1))))