Problem:


Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Wrong Answer

Solution code:
import fractions

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

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])
print(a, b)
for i, j in zip(a[:-2], b[:-2:]):
  ans = max(ans, gcd(i,j))
print(ans)

Submission status: Wrong Answer

Solution code:
org_N = int(input())
org_a = list(map(int,input().split()))
set_a = set(org_a)
a = list(set_a)
N = len(a)
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)
                #print('i,inlist:',i , n//i)

    divisors.sort()
    return divisors
if N == 1:
    print(a[0])
elif N == 2:
    y = 1
    c1 = org_a.count(a[0])
    c2 = org_a.count(a[1])
    if c1 >= 2 and c2 >= 2:
        print(1)
    else:
        if c1 == 1:
            y = max(y,a[1])
        if c2 == 1:
            y = max(y,a[0])
        #print('2pat:',c1,c2,y)
        print(y)
else:
    adic = {}
    g = 1
    for a1 in a:
        diva = make_divisors(a1)
        for diva1 in diva:
            if diva1 in adic.keys():
                adic[diva1] += 1
                #l = adic[diva1] >= N-1: 
                #if adic[diva1] >= N-1:
                    #kouho.append(diva1)
                #    g = max(g,diva1)
            else:
                adic[diva1] = 1
            if adic[diva1] >= N-1:
                    #kouho.append(diva1)
                    g = max(g,diva1)
    #print(adic)
    #print(kouho,max(kouho))
    print(g)

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])
            gg = gcd_list(li[i+1:])
            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:
# -*- 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] and 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:
from fractions import gcd
 
n = int(input())
a = sorted(list(map(int, input().split())))
if n == 2:
    print(max(a))
else:
    g1 = sorted([gcd(d, a[0]) for d in a[1:]])
    g2 = sorted([gcd(d, a[1]) for d in [a[0]] + a[1:]])
    print(max([g1[1], g2[1]]))

Submission status: Wrong Answer

Solution code:
n = int(input())
num = list(map(int,input().split()))
c = 0
for i in range(n-1) :
    if num[i] < 0 and num[i+1] < 0 :
        num[i] *= -1
        num[i+1] *= -1
        
    elif num[i] > 0 and num[i+1] > 0 :
        num[i] *= 1
        num[i+1] *= 1
        
    elif num[i] < 0 and num[i+1] > 0 :
        if -num[i] >= num[i+1] :
            num[i] *= -1
            num[i+1] *= -1
            
    elif num[i] == 0 and num[i+1] < 0 :
        num[i] *= -1
        num[i+1] *= -1
            
    elif num[i] < 0 and num[i+1] == 0 :
        num[i] *= -1
        num[i+1] *= -1
            
print(sum(num))

Submission status: Wrong Answer

Solution code:
import numpy as np

def gcd_(a, b):
    a_ = max(a, b)
    b_ = min(a, b)

    while True:
        if a_ % b_ == 0:
            return b_
        a_, b_ = b_, a_ % b_

_ = input()
a = np.array([int(x) for x in input().split(" ")])

if len(a) == 2:
    print(gcd_(*a))
    exit()

list = [gcd_(a[2], a[1]), gcd_(a[0], a[2]), gcd_(a[0], a[1])]
gcd = np.max(list)
bad_num = a[np.argmax(list)]

for i in range(3, len(a)):
    if a[i] % gcd == 0:
        continue
    else:
        newgcd = gcd_(a[i], gcd)
        oldgcd = gcd_(bad_num, gcd)

        if newgcd > oldgcd:
            gcd = newgcd
        else:
            gcd = oldgcd
            bad_num = a[i]

print(int(gcd))

Submission status: Wrong Answer

Solution code:
import functools
import copy

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

def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if x % y ==0:
        return y
    else:
        while x % y != 0:
            z = x % y
            x = y
            y = z
        else:
            return z


def gcd_list(numbers):
    if len(numbers) == 1:
        return numbers[0]
    else:
        return functools.reduce(gcd, numbers)

n_gcd = gcd_list(A)

def a_gcd_list(nums, aaa):
    bbb = 0
    for i in range(len(nums)-1):
        bbb = gcd(nums[i], nums[i + 1])
        if aaa >= bbb:
            return aaa
    return bbb

ans = []
B = []
for i in range(len(A)):
    B = copy.copy(A)
    B.pop(i)
    ans.append(a_gcd_list(B, n_gcd))

print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline

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

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


if len(t) == 2:
	print(gcd(t[1], t[0]))
	exit()

a1 = gcd(t[1], t[0])
a2 = gcd(t[2], t[1])
a3 = gcd(t[2], t[0])


print(max(a1, a2, a3))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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])
            gg = gcd_list(li[i+1:])
            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:
import copy

N = int(input())
As = [int(i) for i in input().split()]
A = copy.copy(As)
max_gcd = 0

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

for i in range (N):
    del (A[i])
    for k in range (N-2):
        A[k+1] = gcd(A[k], A[k+1])
        if A[N-2] > max_gcd:
            max_gcd = A[N-2]
    A = copy.copy(As) 
        
print (max_gcd)

Submission status: Wrong Answer

Solution code:
import collections

def cal_divs(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors
  
def check(num, kouho, N, i):
  for val in kouho.keys():
    if num%val == 0:
      kouho[val] += 1
      
  return {k: v for k, v in kouho.items() if v > i-1}
  
  
N = int(input())
A= list(map(int, input().split()))
pri_flag = True

if N > 4:
  divs = []
  for i in range(3):
      divs.extend(cal_divs(A[i]))
  kouho = {}
  c = collections.Counter(divs)
  for div, n in c.most_common():
      kouho[div] = n

  kouho = {k: v for k, v in kouho.items() if v > 1}

  for i,num in enumerate(A[3:]):
    if len(kouho) == 1:
      print(1)
      pri_flag = False
      break
      kouho = check(num, kouho, N, i+3)

  if pri_flag:
      print(max(kouho))
else:
    divs = []
    kouho = {}
    for i in range(N):
        divs.extend(cal_divs(A[i]))
        c = collections.Counter(divs)
    for div, n in c.most_common():
        kouho[div] = n
    kouho = {k: v for k, v in kouho.items() if v > N-2}
    print(max(kouho))

Submission status: Wrong Answer

Solution code:
import fractions


def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    sortedA = sorted(A)
    minNum = sortedA[0]
    scdNum = sortedA[1]

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

    gcd_list = sorted(gcd_list)
    print(max(gcd_list[1], scdNum))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

def gcd(a, b):
    maxV = max(a, b)
    minV = min(a, b)
    if minV == 0:
        return maxV
    else:
        return gcd(minV, maxV%minV)

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

maxg = 0
if N == 2:
    print(max(Alist))
else:
    gcalc = Alist[0]
    for i in range(0, N):
        tmp = np.array(Alist)
        g = 0
        if i != N - 1:
            tmp[i] = tmp[i + 1]
        else:
            tmp[i] = tmp[i - 1]
        if i == 0:
            for j in range(1, N):
                if j == 1:
                    g = gcd(tmp[0], tmp[1])
            else:
                g = gcd(g, tmp[j])
        else:
            for j in range(i, N):
                g = gcd(gcalc, tmp[j])
                if j == i:
                    gcalc = g
        if maxg < g:
            maxg = g
    print(maxg)

Submission status: Wrong Answer

Solution code:
from functools import reduce

d = {}


def gcd(a, b):
    if (a, b) in d:
        return d[(a, b)]
    else:
        while b != 0:
            a, b = b, a % b
        d[(a, b)] = a
    return a


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


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

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

print(max_)

Submission status: Wrong Answer

Solution code:
import math
import copy 

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

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

	ans=0
	for i in range(N):
		l=copy.deepcopy(A)
		l.pop(int(i))
		if(N>2):
			tmp_gcd=gcd(l[0],l[1])
			for j in range(2,len(l)):
				tmp_gcd=gcd(l[j],tmp_gcd)
			if(tmp_gcd > ans):
				ans=tmp_gcd
		else:
			ans=gcd(A[0],A[1])
	print(ans)

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)

#in
n=int(input())
v=list(map(int, input().split()))

maxi = 0

# おなじか？
if len(list(set(v))) == 1:
    maxi = (v[0])
elif len(v) == 2:
    maxi = (max(v))
# 書き換え 1
else:
    for i in range(n):
        listv = v
        listv[i] = min(v) 
        listv[i] = max(listv)
        
        if (gcd_list(listv)) > maxi:
            maxi = (gcd_list(listv))
#最大公約数


print(maxi)

# 書き換え 2

Submission status: Wrong Answer

Solution code:
from functools import reduce


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


N = int(input())
a_list = list(map(int, input().split()))
gcd_list = []
for idx in range(1, N):
    gcd_list.append(reduce(gcd, a_list[1:idx], a_list[idx]))
max_gcd_a = gcd_list[-1]
gcd_list.append(a_list[-1])

for idx in range(1, N):
    if idx != 1:
        left_gcd = gcd(left_gcd, a_list[idx])
    else:
        left_gcd = a_list[0]
    right_gcd = gcd_list[idx]

    gcd_a = gcd(left_gcd, right_gcd)
    if max_gcd_a < gcd_a:
        max_gcd_a = gcd_a

print(max_gcd_a)

# どれか１つを除いた時の最大公約数で最大のもの

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
if n==2:
  print(gcdlist(l))
else:
  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:
import fractions


def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    sortedA = sorted(A)
    minNum = sortedA[0]
    scdNum = sortedA[1]

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

    gcd_list = sorted(gcd_list)
    print(min(gcd_list[1], scdNum))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

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

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

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

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

    if f > 1:
        print(1)
        break
else:
    l.sort()
    if len(l) >= 2:
        print(l[1])
    else:
        print(max(tmp,res1,res2,res3))

Submission status: Wrong Answer

Solution code:
# vim: set fileencoding=utf-8:
import fractions
import collections
from functools import reduce


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

    # ans = tle_solver(A)

    ans = ac_solver(A)
    print(ans)


def ac_solver(A):
    C = collections.Counter(A)
    d = -1
    if len(C) == 2:
        if len(A) == 2:
            d = max(C.keys())
        else:
            for eles in C.most_common():
                if eles[1] != 1:
                    d = eles[0]

    else:
        a_min = min(A)
        a_min_divisors = sorted(make_divisors(a_min), reverse=True)
        for d in a_min_divisors:
            ct = 0
            for a in A:
                if a % d != 0:
                    ct += 1

                if 2 < ct:
                    break

            if ct == 1 or ct == 0:
                break

    return d


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

    return divisors


def tle_solver(A):
    gcds = [-1] * len(A)
    for idx in range(len(A)):
        tmp = A[:idx] + A[idx+1:]
        gcds[idx] = gcd(*tmp)

    return max(gcds)


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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

ans = 0
for i in range(n):
    cd = a[i]
    for j in range(n):
        if i != j and i + 1 != j and not(i == n - 1 and j == 0):
            cd = fractions.gcd(cd, a[j])
            if cd > ans:
                ans = cd
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce
N = int(input())
Alist = list(map(int, input().split()))
def gcd_list(numbers):
  return reduce(gcd, numbers)
if len(Alist) == 2:
    print(max(Alist))
else:
    gcdlist=[gcd_list(Alist[i:i+2]) for i in range(N-1)]
    indlist = [i for i,x in enumerate(gcdlist) if x ==min(gcdlist)]
    print(max([gcd_list(Alist[:ind]+Alist[ind+1:]) for ind in indlist]))

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(1,N):
    check2 = gcd(check2, A[i])

ans = check2

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = 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.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:
N = int(input())
A = list()
common_value = list()
tem_value = list()
tem_values = list()

for i, j in zip(range(N), input().split()):
    A.append(int(j))
A = sorted(A)
for i, j in zip(range(3), A):
    for p in range(1, j+1):
        tem_values.append(p)
    tem_value.append(j)
for i in tem_values:
    if i>tem_value[0]:
        tem_values.remove(i)

for i in tem_values:
    check = 0
    for j in tem_value:
        if not(j%i == 0):
            check += 1
    if check <= 1:
        common_value.append(i)
common_value = sorted(common_value)
print(common_value[-1])

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_array2[i], val2)

if val1 > val2: print val1
else: print val2

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])
            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:
from copy 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()))

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

gcds = []
for i in range(n):
	temp = copy(a)
	temp.pop(i)

	num1 = temp[0]
	num2 = temp[1]
	result = gcd(num1, num2)
	for j in range(2, len(temp)):
		if result in gcds:
			break
		result = gcd(result, temp[j])
	
	if result not in gcds:
		gcds.append(result)

if len(gcds)==1:
	print(gcds[0])
else:
	gcds = sorted(gcds)
	print(gcds[1])

Submission status: Wrong Answer

Solution code:
import sys
def plane_gcd(a,b):
	alp=max(a,b)
	bet=min(a,b)
	while(1):
		if alp%bet==0:
			return bet
		else:
			r=alp%bet
			alp=bet
			bet=r

def list_gcd(li):
	ans=plane_gcd(li[0],li[1])
	for i in range(2,len(li)):
		tmp=plane_gcd(li[i],ans)
		if tmp<ans:
			ans=tmp
	return ans

n=int(input())
a=list(map(int,input().split()))
if n==2:
	print(max(a))
	sys.exit()
gcd=list_gcd(a)
for i in range(n):
	if i==0:
		tmp=list_gcd(a[1:n])
		if tmp!=gcd:
			print(tmp)
			sys.exit()
	elif i==n-1:
		tmp=list_gcd(a[0:n-1])
		if tmp!=gcd:
			print(tmp)
			sys.exit()
	else:
		tmp=list_gcd(a[0:i]+a[i+1:n])
		if tmp!=gcd:
			print(tmp)
			sys.exit()
print(gcd)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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[:-2], b[:-2:-1]):
  ans = max(ans, gcd(i,j))
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=input().split()
a=[int(i) for i in a]
b=max(a)
c=max(a)-min(a)
for i in range(c,0,-1):
    count=0
    for j in range(n):
        if a[j]%c==0:
            count+=1
    if count>=n-1:
        print(c)
        exit()

print(1)

Submission status: Wrong Answer

Solution code:
org_N = int(input())
org_a = list(map(int,input().split()))
set_a = set(org_a)
a = list(set_a)
N = len(a)
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)
                #print('i,inlist:',i , n//i)

    divisors.sort()
    return divisors
if N == 1:
    print(a[0])
elif N == 2:
    y = 1
    c1 = org_a.count(a[0])
    c2 = org_a.count(a[1])
    if c1 >= 2 and c2 >= 2:
        print(1)
    else:
        if c1 == 1:
            y = max(y,a[1])
        if c2 == 1:
            y = max(y,a[0])
    #print('2pat:',c1,c2,y)
    print(y)
else:
    adic = {}
    g = 1
    for a1 in a:
        diva = make_divisors(a1)
        for diva1 in diva:
            if diva1 in adic.keys():
                adic[diva1] += 1
                #l = adic[diva1] >= N-1: 
                #if adic[diva1] >= N-1:
                    #kouho.append(diva1)
                #    g = max(g,diva1)
            else:
                adic[diva1] = 1
            if adic[diva1] >= N-1:
                    #kouho.append(diva1)
                    g = max(g,diva1)
    #print(adic)
    #print(kouho,max(kouho))
    print(g)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

g = gcd(A[0], A[1])
lmax = 1
num = 0
for i in range(2,N):
    cand = gcd(g, A[i])
    if cand < g:
        l = int(g / cand)
        g = cand
        if lmax < l:
            lmax = l
            num = A[i]
A = A[1:] + [A[0]]
g = gcd(A[0], A[1])
for i in range(2,N):
    cand = gcd(g, A[i])
    if cand < g:
        l = int(g / cand)
        g = cand
        if lmax < l:
            lmax = l
            num = A[i]
A = A[1:] + [A[0]]
g = gcd(A[0], A[1])
for i in range(2,N):
    cand = gcd(g, A[i])
    if cand < g:
        l = int(g / cand)
        g = cand
        if lmax < l:
            lmax = l
            num = A[i]
if num == A[0]:
    A = A[1:]
    flag = False
    N -= 1
elif num == A[1]:
    A = [A[0]] + A[2:]
    flag = False
    N -= 1
else:
    flag = True
g = gcd(A[0], A[1])
for i in range(2,N):
    if A[i] == num and flag:
        flag = False
    else:
        g = gcd(g, A[i])
print(g)

Submission status: Wrong Answer

Solution code:
import math
def yakusuu(a):
    yLst = []
    for i in range(1, int(pow(a, 1 / 2)) + 1):
        if a % i == 0:
            yLst.append(i)
            yLst.append(int(a / i))
    return yLst

N = int(input())
anslst = []
aLst = list(map(int, input().split()))
for i in range(N):
    anslst += yakusuu(aLst[i])

nCnt = 1
ansUnique = list(set(anslst))
for i in ansUnique:
    if anslst.count(i) == N - 1:
        nCnt = max(nCnt, i)
    elif anslst.count(i) == N:
        nCnt = max(nCnt, i)

print(nCnt)

Submission status: Wrong Answer

Solution code:
# 2019/04/27
# AtCoder Beginner Contest 125 - C

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

a.sort()
smincnt = 1
sminflg = True
for i in range(1, n):
    if a[i] == a[0]:
        smincnt += 1

    if a[i] % a[0] > 0:
        sminflg = False

ans = 1
if smincnt == n or sminflg == True:
    ans = a[0]

else:
    posl = list()
    savj = -1
    # Make list
    for j in range(1, a[0]):
        div = int(a[0] / j)
        if div == savj:
            break
        if a[0] % j == 0:
            posl.append(div)
            posl.append(j)
            savj = j
    
    posl.sort(reverse=True)
    
    for x in range(len(posl)):
        dcnt = 0
        for i in range(1, n):
            if a[i] % posl[x] > 0:
                dcnt += 1
                if dcnt > 1:
                    break
        if dcnt <= 1:
            ans = posl[x]
            break
    
# Output
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
As = list(map(int, input().split()))
As.sort()
ans = 1
odd = 0
even = 0
for a in As:
    if a%2 == 0:
        even += 1
    else:
        odd += 1
if even >= 2 and odd >= 2:
    print(1)
elif even == 1:
    for a in As:
        if a % 2 == 1:
            ans = fractions.gcd(ans, a)
    print(ans)
elif odd == 1:
    for a in As:
        if a % 2 == 0:
            ans = fractions.gcd(ans, a)
    print(ans)
    
else:
    
    for i in range(N):
        flag = True
        if i == 0:
            a = As[1]
        else:
            a = As[0]
        for j in range(N):
            if j != i:
                a = fractions.gcd(a, As[j])
                if a <= ans:
                    flag = False
                    break
        if flag:  
            ans = a
    print(ans)

Submission status: Wrong Answer

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

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

ans = 1
if n == 2:
    print(max(a))
else:
    for i in range(n // 10):
        b = a[:i] + a[i + 1:]
        element = gcd(b[0], b[1])
        if n == 3:
            ans = max(ans, element)
        else:
            for j in range(2, n - 1):
                element = gcd(element, b[j])
            ans = max(ans, element)
    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
N = int(input())
A = list(map(int,input().split()))

ans = 1
x = []
for i in range(N-1):
    tmp = gcd(A[i],A[i+1])
    x.append(tmp)
x.append(gcd(A[-1],A[0]))
x.sort()

if N == 2:
    print(max(A))
else:
    print(x[2])