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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
        if ans == 1:
            return ans
    return ans
n = int(input())
a = list(map(int, input().split()))
a.sort()
max = 0
b_int = 0
min = 1
if n < 5:
  for i in range(0,n):
    if b_int == a[0]:
      p = a.pop(0)
      # a.append(p)
    else:
      p = a.pop(0)
      b_int = p
      x = list_euclid(a)
      if max < x:
        max = x
      a.append(p)
else:
  for i in range(0,n):
    if b_int == a[0]:
      p = a.pop(0)
      # a.append(p)
    else:
      if max == 0:
        p = a.pop(0)
        b_int = p
        k = len(a) // 2
        x = 0
        y = list_euclid(a[0:k])
        if y == 1:
          x = 1
        else:
          z = list_euclid(a[k:len(a)-1])
          x = euclid(y, z)
        if max < x:
          max = x
        a.append(p)
        min = p
      elif min <= max:
        break
      else:
        p = a.pop(0)
        b_int = p
        k = len(a) // 2
        x = 0
        y = list_euclid(a[0:k])
        if y == 1:
          x = 1
        else:
          z = list_euclid(a[k:len(a)-1])
          x = euclid(y, z)
        if max < x:
          max = x
        a.append(p)
print(max)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
    return ans
n = int(input())
a = list(map(int, input().split()))
max = 0
if n < 5:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        x = list_euclid(new_list)
        if max < x:
            max = x
else:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        k = n // 2
        print(k)
        y = list_euclid(new_list[0:k])
        z = list_euclid(new_list[k:n-1])
        x = euclid(y, z)
        if max < x:
            max = x
print(max)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
    return ans
n = int(input())
a = list(map(int, input().split()))
max = 0
if n < 5:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        x = list_euclid(new_list)
        if max < x:
            max = x
else:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        k = n // 2
        y = list_euclid(new_list[0:k-1])
        z = list_euclid(new_list[k:n-1])
        x = euclid(y, z)
        if max < x:
            max = x
print(max)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def euclid(a, b):
    if b == 0:
        return a
    else:
        return euclid(b, a%b)
def list_euclid(int_list):
    ans = int_list[0]
    for i in range(1,len(int_list)):
        ans = euclid(ans, int_list[i])
    return ans

n = int(input())
a = list(map(int, input().split()))
max = 0
if n < 4:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        x = list_euclid(new_list)
        if max < x:
            max = x
else:
    for i in range(0,n):
        new_list = a[:]
        del new_list[i]
        k = n // 2
        y = list_euclid(new_list[0:k-1])
        z = list_euclid(new_list[k:n-1])
        x = euclid(y, z)
        if max < x:
            max = x
print(max)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int,input().split()))
minflg = 0
mingcd = A[0]*A[1]
mingcd1 = A[0]*A[1]
if N == 2:
    print(max(A))
else:
    for i in range(N):
        gcd = fractions.gcd(mingcd,A[i])
        if gcd < mingcd:
            mingcd = gcd
            minflg = i

    for i in range(1,N):
        gcd1 = fractions.gcd(mingcd1, A[i])
        mingcd1 = gcd1
        minflg1 = i
    if mingcd < mingcd1:
        minflg = 0
    mingcd = A[0]*A[1]
    for i in range(N):
        if i != minflg:
            gcd = fractions.gcd(mingcd,A[i])
            if gcd < mingcd:
                mingcd = gcd
    print(mingcd)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int,input().split()))
minflg = 0
mingcd = A[1]
if N == 2:
    print(max(A))
else:
    for i in range(N):
        gcd = fractions.gcd(mingcd,A[i])
        if gcd < mingcd:
            mingcd = gcd
            minflg = i
    if minflg == 0 and N > 2:
        if fractions.gcd(A[0],A[2]) > fractions.gcd(A[1],A[2]):
            minflg = 1
    if minflg > 0:
        mingcd = A[minflg-1]
    else: mingcd = A[minflg+1]

    for i in range(N):
        if i != minflg:
            gcd = fractions.gcd(mingcd,A[i])
            if gcd < mingcd:
                mingcd = gcd
    print(mingcd)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int,input().split()))
minflg = 0
mingcd = A[1]
for i in range(N):
    gcd = fractions.gcd(mingcd,A[i])
    if gcd < mingcd:
        mingcd = gcd
        minflg = i
if minflg == 0 and N > 2:
    if fractions.gcd(A[0],A[2]) > fractions.gcd(A[1],A[2]):
        minflg = 1
if minflg > 0:
    mingcd = A[minflg-1]
else: mingcd = A[minflg+1]
    
for i in range(N):
    if i != minflg:
        gcd = fractions.gcd(mingcd,A[i])
        if gcd < mingcd:
            mingcd = gcd
print(mingcd)

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int,input().split()))
minflg = 0
mingcd = max(A)
for i in range(N-1):
    gcd = fractions.gcd(mingcd,A[i])
    if gcd < mingcd:
        mingcd = gcd
        minflg = i

mingcd = max(A)

for i in range(N-1):
    if i != minflg:
        gcd = fractions.gcd(mingcd,A[i])
        if gcd < mingcd:
            mingcd = gcd
print(mingcd)

Submission status: Wrong Answer

Solution code:
import numpy as np

def gcd(v1, v2):
    if v1 == 0 or v2 == 0:
        return max(v1, v2)
#     print(v1, v2, v1%v2)
    if v1 % v2 == 0:
        return v2
    return gcd(v2, v1%v2)

N = int(input())

A = list(map(int, input().split()))
L = np.zeros(N+1)
R = np.zeros(N+1)
for i in range(N):
    L[i+1] = gcd(L[i], A[i])
#     print(N-i-1, N-i, N-i-1)
    R[N-i-1] = gcd(R[N-i], A[N-i-1])
M = []
print(L)
print(R)
for j in range(N):
    M.append(gcd(L[j], R[j+1]))

print(int(max(M)))

Submission status: Wrong Answer

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

def break_primes(n):
    pn = n
    cnt = 0
    d = set()
    while pn % 2 == 0:
        pn //= 2
        d.add(2)
    p = 3
    while(p*p <= n):
        if pn % p == 0:
            d.add(p)
        while(pn % p == 0):
            pn //= p
        p += 2
    if pn != 1:
        d.add(pn)
    return d

def vectorize(n,primes,a):
    pn = n
    for p in range(len(primes)):
        a[p] = 0
        while pn % primes[p] == 0:
            a[p] += 1
            pn //= primes[p]
N = int(input())
A = list(map(int,input().split()))
if N == 2:
    print(max(A[0],A[1]))
if N > 2:
    primes = list(break_primes(gcd(A[0],A[1]))|break_primes(gcd(A[1],A[2]))|break_primes(gcd(A[2],A[0])))
    P = len(primes)
    vec_0 = [0 for _ in range(P)]
    vec_1 = [0 for _ in range(P)]
    vectorize(A[0],primes,vec_0)
    vectorize(A[1],primes,vec_1)
    perfect = [0 for _ in range(P)]
    onemiss = [0 for _ in range(P)]
    for p in range(P):
        perfect[p] = min(vec_0[p],vec_1[p])
        onemiss[p] = max(vec_0[p],vec_1[p])
    
    vec_n = [0 for _ in range(P)]
    for n in range(2,N):
        vectorize(A[n],primes,vec_n)
        for p in range(P):
            if vec_n[p] < perfect[p]:
                onemiss[p] = perfect[p]
                perfect[p] = vec_n[p]
            elif vec_n[p] < onemiss[p]:
                onemiss[p] = vec_n[p]
    ans = 1
    for p in range(P):
        ans *= primes[p] ** onemiss[p]
    print(ans)

Submission status: Wrong Answer

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

def break_primes(n):
    pn = n
    cnt = 0
    d = set()
    while pn % 2 == 0:
        pn //= 2
        d.add(2)
    p = 3
    while(p*p <= pn):
        if pn % p == 0:
            d.add(p)
        while(pn % p == 0):
            pn //= p
        p += 2
    if pn != 1:
        d.add(pn)
    return d

def vectorize(n,primes,a):
    pn = n
    for p in range(len(primes)):
        a[p] = 0
        while pn % primes[p] == 0:
            a[p] += 1
            pn //= primes[p]
N = int(input())
A = list(map(int,input().split()))
if N == 2:
    print(max(A[0],A[1]))
if N > 2:
    primes = list(break_primes(gcd(A[0],A[1]))|break_primes(gcd(A[1],A[2]))|break_primes(gcd(A[2],A[0])))
    P = len(primes)
    vec_0 = [0 for _ in range(P)]
    vec_1 = [0 for _ in range(P)]
    vectorize(A[0],primes,vec_0)
    vectorize(A[1],primes,vec_1)
    perfect = [0 for _ in range(P)]
    onemiss = [0 for _ in range(P)]
    for p in range(P):
        perfect[p] = min(vec_0[p],vec_1[p])
        onemiss[p] = max(vec_0[p],vec_1[p])
    
    vec_n = [0 for _ in range(P)]
    for n in range(2,N):
        vectorize(A[n],primes,vec_n)
        for p in range(P):
            if vec_n[p] < perfect[p]:
                onemiss[p] = perfect[p]
                perfect[p] = vec_n[p]
            elif vec_n[p] < onemiss[p]:
                onemiss[p] = vec_n[p]
    ans = 1
    for p in range(P):
        ans *= primes[p] ** onemiss[p]
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import fractions

def solve(N: int, A: "List[int]"):
    A.append(A[0])
    answer = 1
    for i in range(N):
        gcd = fractions.gcd(A[i],A[i+1])
        answer = max(gcd,answer)
    print(answer)
    return


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:
import fractions
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 0
for i in range(1,n):
  ans = max(ans,fractions.gcd(a[0],a[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(1,n):
  ans = max(ans,fractions.gcd(a[0],a[i]))
print(ans)

Submission status: Wrong Answer

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

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

L=[]
for n in range(N):
    #L=[]
    for i in range(n+1,N):
        l=euclid(max(A[n],A[i]), min(A[n],A[i]))
        L.append(l)
    #print(L)


from collections import Counter
# total: N*(N-1)/2 pattern
D=Counter(L)
D=sorted(D.items(), key=lambda x:x[0], reverse=True)
#print(D)
#D=sorted(D.items(), key=lambda x:x[1])
#print(D[-1][0])
for d in D:
    if d[1]>=(N-1)*(N-2)//2:
        print(d[0])
        exit()

Submission status: Wrong Answer

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

def euclid(a,b):
    #print(a,b)
    if b==0:
        return a
    else:
        return euclid(b, a%b)

m=1
for n in range(N):
# 2重のforループ？
# 最大公約数の最大を出力?
    if n==N-1:
        l=euclid(max(A[n],A[0]), min(A[n],A[0]))
    else:
        l=euclid(max(A[n],A[n+1]), min(A[n],A[n+1]))
    if m<l:
        m=l
    #print(l)

print(m)

Submission status: Wrong Answer

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

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

m=1
for n in range(N):
# 2重のforループ？
# 最大公約数の最大を出力?
    if n==N-1:
        l=euclid(max(A[n],A[0]), min(A[n],A[0]))
    else:
        l=euclid(max(A[n],A[n+1]), min(A[n],A[n+1]))
    if m<l:
        m=l
    #print(l)

print(m)

Submission status: Wrong Answer

Solution code:
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))

a = list1[0]
b = 0

for i in range(n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])

keep = list1.pop(b)
a = list1[0]

for i in range(len(list1)):
	a = min(a,math.gcd(a,list1[i]))
if b == 1:
	list1.insert(1,keep)
	del list1[0]
	b = list1[0]
	for i in range(len(list1)):
		s = min(b,math.gcd(b,list1[i]))
	print(max(a,s))
	exit()


print(a)

Submission status: Wrong Answer

Solution code:
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))

a = list1[0]
b = 0

for i in range(n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])

keep = list1.pop(b)
a = list1[0]

for i in range(len(list1)):
	a = min(a,math.gcd(a,list1[i]))
if b == 1:
	list1.insert(1,keep)
	del list1[0]
	a = list1[0]
	for i in range(len(list1)):
		s = min(a,math.gcd(a,list1[i]))
	print(max(a,s))


print(a)

Submission status: Wrong Answer

Solution code:

import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
if math.gcd(list1[0],list1[1]) <= math.gcd(list1[0],list1[-1]):
	a = math.gcd(list1[0],list1[-1])
	b = 0
else:
	a = math.gcd(list1[0],list1[1])
	b = -1

for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Wrong Answer

Solution code:

import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
if math.gcd(list1[0],list1[1]) < math.gcd(list1[0],list1[-1]):
	a = math.gcd(list1[0],list1[-1])
	b = 0
else:
	a = math.gcd(list1[0],list1[1])
	b = -1

for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Wrong Answer

Solution code:
#import math
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))

a = max(math.gcd(list1[0],list1[1]),math.gcd(list1[0],list1[-1]))
b = 0
for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Wrong Answer

Solution code:
import fractions as math
#import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
list1.sort()
a = math.gcd(list1[0],list1[-1])
b = 0
for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Wrong Answer

Solution code:
import fractions as math
#import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
a = math.gcd(list1[0],list1[-1])
b = 0
for i in range(0,n):
	if a > math.gcd(a,list1[i]):
		a = math.gcd(a,list1[i])
		b = list1.index(list1[i])
del list1[b]
a = list1[0]
for i in range(0,len(list1)):
	a = min(a,math.gcd(a,list1[i]))
print(a)

Submission status: Wrong Answer

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

def gcd(n, m):
    if m == 0:
        return n
    return gcd(m, n % m)

leftGcd = [-1] * (N + 1)
leftGcd[0] = A[0]

for i, a in enumerate(A, start=1):
    leftGcd[i] = gcd(leftGcd[i - 1], a)

rightGcd = [-1] * (N + 1)
rightGcd[-1] = A[-1]

for i, a in enumerate(reversed(A), start=1):
    rightGcd[-(i + 1)] = gcd(rightGcd[-i], a)

ans = max(rightGcd[1], leftGcd[-1])
for mid in range(1, N):
    ans = max(ans, gcd(leftGcd[mid], rightGcd[mid + 1]))

print(ans)

Submission status: Wrong Answer

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

def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

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

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

toLeft = [0]*(N+1)
toLeft[N] = A[N]
for i in reversed(range(1, N)):
    toLeft[i] = gcd(toLeft[i+1], A[i])

# print(toRight)
# print(toLeft)
    
ans = max(toRight[-2], toLeft[2])
for i in range(2, N-1 + 1):
    now = gcd(toRight[i-1], toLeft[-i+1])
    ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

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

def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

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

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

toLeft = [0]*(N+1)
toLeft[N] = A[N]
for i in reversed(range(1, N)):
    toLeft[i] = gcd(toLeft[i+1], A[i])
    
ans = max(toRight[-2], toLeft[2])
for i in range(3, N-3 + 1):
    now = gcd(toRight[i-1], toLeft[-i-1])
    ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
n=int(input())
a= list(map(int, input().split()))
if n==2:
    print(max(a))
elif n==3:
    b=[1]*n
    for i in range(-1,n-1):
        b[i]=gcd(a[i],a[i+1])
    print(max(b))
else:
    b=[1]*n
    for i in range(-1,n-1):
        b[i]=gcd(a[i],a[i+1])
    c=collections.Counter(b)
    d=c.most_common()
    x=0
    cnt=0
    e=0
    for i in range(len(d)):
        if d[i][1]==2:
            x=max(x,d[i][0])
            cnt+=1
        else:
            e=min(e,d[i][0])
    if cnt>=2:
        print(x)
    else:
        print(e)

Submission status: Wrong Answer

Solution code:
import collections
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
n=int(input())
a= list(map(int, input().split()))
if n==2:
    print(max(a))
if n==3:
    b=[1]*n
    for i in range(-1,n-1):
        b[i]=gcd(a[i],a[i+1])
    print(max(b))
else:
    b=[1]*n
    for i in range(-1,n-1):
        b[i]=gcd(a[i],a[i+1])
    c=collections.Counter(b)
    d=c.most_common()
    x=0
    cnt=0
    e=0
    for i in range(len(d)):
        if d[i][1]==2:
            x=max(x,d[i][0])
            cnt+=1
        else:
            e=min(e,d[i][0])
    if cnt>=2:
        print(x)
    else:
        print(e)

Submission status: Wrong Answer

Solution code:
a=int(input())
lis=list(map(int,input().split()))
lis=sorted(lis)
i=0
while lis[i]<0 and i<a-2:
  if lis[i]<=0 and lis[i+1]>=0:
    if -1*lis[i]>lis[i+1]:
      lis[i],lis[i+1]=-lis[i],-lis[i+1]
  elif lis[i]<=0 and lis[i+1]<=0:
    lis[i],lis[i+1]=-lis[i],-lis[i+1]
  i+=2
print(sum(lis))

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    if not a:
        # a が ゼロ ならば b
        return b
    if not b:
        # b が ゼロ ならば a
        return a
    while b:
        a, b = b, a % b
    return a


def main():
    N, *A = map(int, open(0).read().split())
    L = [0] * N
    R = L[:]
    R[N - 1] = A[N - 1]
    for i in range(N - 1):
        L[i + 1] = gcd(L[i], A[i])
    for i in range(N - 2, -1, -1):
        R[i] = gcd(R[i + 1], A[i])
    print(max(gcd(L[i], R[min(i + 1, N - 1)]) for i in range(N)))
    return


main()

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
def gcds(*i):
    return reduce(fractions.gcd, i)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = 0
    if gcd(A) == max(A):
        print(gcd(A))
        exit()
    else:
        for i in range(N):
            if i == 0 :
                tmp=max(tmp,gcd(A[1:]))
            elif i == N-1:
                tmp=max(tmp,gcd(A[:N-1]))
            else:
                tmp=max(tmp,gcds(gcds(gcd(A[:i]),A[i]),gcds(gcd(A[i+1:]),A[i])))
    print(tmp) #TLE

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import fractions
n = int(input())
l = list(map(int, input().split()))
ans = 0
front = [0] * len(l)
back = [0] * len(l)
front[0] = l[0]
back[0] = l[-1]
if len(l) == 2:
    ans = max(l[0], l[1])
else:
    for i in range(len(l) - 1):
        front[i + 1] = fractions.gcd(front[i], l[i+1])
        back[i + 1] = fractions.gcd(back[i], l[-i - 2])
    for i in range(len(l)):
        if i == 0:
            ans_i = back[-len(l) + 1]
        elif i == len(l) - 1:
            ans_i = front[len(l) - 1]
        else:
            ans_i = fractions.gcd(front[i - 1], back[len(l) - (i + 2)])
        if ans < ans_i:
            ans = ans_i
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

n = int(input())
l = list(map(int, input().split()))
ans = 0
front = [0] * len(l)
back = [0] * len(l)
front[0] = l[0]
back[0] = l[-1]
if len(l) == 2:
    ans = max(l[0], l[1])
else:
    for i in range(len(l) - 1):
        front[i + 1] = fractions.gcd(l[i], l[i+1])
        back[i + 1] = fractions.gcd(l[-i - 1], l[-i - 2])
    for i in range(len(l)):
        if i == 0:
            ans_i = back[-len(l) + 1]
        elif i == len(l) - 1:
            ans_i = front[len(l) - 1]
        else:
            ans_i = fractions.gcd(front[i - 1], back[len(l) - (i + 2)])
        if ans < ans_i:
            ans = ans_i
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
def gcds(*i):
    return reduce(fractions.gcd, i)
N = int(input())
A = list(map(int, input().split()))
tmp = []
for i in range(N):
    if i ==0:
        l = 0
        r = gcd(A[i+1:])
    elif i == N-1:
        l = gcd(A[:i])
        r = 0
    else:
        l = gcd(A[:i])
        r = gcd(A[i+1:])
    L = gcds(l,A[i])
    R = gcds(r,A[i])
    tmp.append(gcds(L,R))
print(max(tmp))

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce
def gcd(i):
    return reduce(fractions.gcd, i)
def gcds(*i):
    return reduce(fractions.gcd, i)
N = int(input())
A = list(map(int, input().split()))
tmp = 0
if gcd(A) == max(A):
    print(gcd(A))
    exit()
else:
    for i in range(N):
        if i == 0 :
            tmp=max(tmp,gcd(A[1:]))
        elif i == N-1:
            tmp=max(tmp,gcd(A[:N-1]))
        else:
            tmp=max(tmp,gcds(gcds(gcd(A[:i]),A[i]),gcds(gcd(A[i+1:]),A[i])))
print(tmp)

Submission status: Wrong Answer

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

import fractions
import copy

listb=[]
listc=[]
ansb= lista[0]

for i in range(1,n-1):
	ansb=fractions.gcd(ansb,lista[i])
#	print(ansb)
	listb.append(ansb)

#print(int(max(list_ans)))
ansc= lista[n-1]

for i in range(n-2,0,-1):
	ansc=fractions.gcd(ansc,lista[i])
#	print(ansc)
	listc.append(ansc)


list_b=[lista[0]]
listb=[1]+list_b+listb
list_c=[lista[n-1]]
listc=list(reversed(listc))+list_c+[1]
#print(listb)
#print(listc)

ans=0
list_ans=[listc[0],listb[n-1]]
for i in range(1,n-2):
	ans=fractions.gcd(listb[i],listc[i])
	list_ans.append(ans)
	
ansans=max(list_ans)

if n==2:
	ansans=max(lista[0],lista[1])
	
print(ansans)

Submission status: Wrong Answer

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

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

ans = 0
L = [0]*(N+1)
for i in range(N):
  L[i+1] = gcd(L[i],A[i])
  
R = [0]*(N+1)
for i in range(N-1,-1,-1):
  R[i] = gcd(R[i+1],A[i])
  
for i in range(N-1):
  M = gcd(L[i],R[i+1])
  if ans < M:
    ans = M
    
print(ans)

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:
    count = 0
    ans = gcd_list(tmp)
    for j in range(N):
      if A[j]%ans != 0:
        count+=1
    if count == 1:
      print(ans)
    else:
      for i in range(len(tmp)):
        tmp_ = tmp[:]
        tmp_.pop(i)
        ans_ = gcd_list(tmp_)
        if ans < ans_:
          ans = ans_
      print(ans)

Submission status: Wrong Answer

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

import fractions
import copy

listb=[]
listc=[]
ansb= lista[0]

for i in range(1,n-1):
	ansb=fractions.gcd(ansb,lista[i])
#	print(ansb)
	listb.append(ansb)

#print(int(max(list_ans)))
ansc= lista[n-1]

for i in range(n-2,0,-1):
	ansc=fractions.gcd(ansc,lista[i])
#	print(ansc)
	listc.append(ansc)


list_b=[lista[0]]
listb=[1]+list_b+listb
list_c=[lista[n-1]]
listc=list(reversed(listc))+list_c+[1]
#print(listb)
#print(listc)

ans=0
list_ans=[]
for i in range(n):
	ans=fractions.gcd(listb[i],listc[i])
	list_ans.append(ans)
	
ansans=max(list_ans)

if n==2:
	ansans=max(lista[0],lista[1])
	
print(ansans)

Submission status: Wrong Answer

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

import fractions
import copy

listb=[]
listc=[]
ansb= lista[0]

for i in range(1,n-1):
	ansb=fractions.gcd(ansb,lista[i])
#	print(ansb)
	listb.append(ansb)

#print(int(max(list_ans)))
ansc= lista[n-1]

for i in range(n-2,0,-1):
	ansc=fractions.gcd(ansc,lista[i])
#	print(ansc)
	listc.append(ansc)


list_b=[lista[0]]
listb=[1]+list_b+listb
list_c=[lista[n-1]]
listc=list(reversed(listc))+list_c+[1]
#print(listb)
#print(listc)

ans=0
list_ans=[]
for i in range(n):
	ans=fractions.gcd(listb[i],listc[i])
	list_ans.append(ans)
	
print(max(list_ans))

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
  div=[]
  for i in range(1,int(x**(1/2))+1):
    if x%i==0:
      div.append(i)
      if i!=x//i:
        div.append(x//i)
  div=sorted(div)[::-1]
  j=0
  while True:
    if y%div[j]==0:
      return div[j]
      break
    else:
      j+=1
n=int(input())
A=[]*n
A[:]=map(int,input().split())
GCD=[]
for i in range(n-1):
  for j in range(i+1,n):
    GCD.append(gcd(A[i],A[j]))
print(max(GCD))

Submission status: Wrong Answer

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

#print(num)

dict = {}

j = 2
a = 0
for i in range(n):
    a = num[i]
    j = 2
    while(a != 1):
       # print(a)
        if a % j == 0:
            if j not in dict:
                dict[j] = 1
                a = a // j
            else:
                dict[j] += 1
                a = a // j
        else:
            j += 1



sum = 1
for k in dict:
    if dict[k] >= n:
        sum *= k ** (dict[k] // n)

print(sum)

Submission status: Wrong Answer

Solution code:
import itertools


def get_prime_factorized(N):
    result    = []
    base, exp = 2, 0
    while base ** 2 <= N:
        while N % base == 0:
            N    = N // base
            exp += 1
        if exp > 0:
            result.append([base, exp])
        base, exp = base + 1, 0
    if N > 1:
        result.append([N, 1])
    return result


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

L = [pf for a in A for pf in  get_prime_factorized(a)]

print(0) # check TLE

Submission status: Wrong Answer

Solution code:
import itertools


def get_prime_factorized(N):
    result    = []
    base, exp = 2, 0
    while base ** 2 <= N:
        while N % base == 0:
            N    = N // base
            exp += 1
        if exp > 0:
            result.append([base, exp])
        base, exp = base + 1, 0
    if N > 1:
        result.append([N, 1])
    return result

def re_func_divisior(prime_factorized):
    base, exp = prime_factorized.pop()
    P = re_func_divisior(prime_factorized) if prime_factorized else [[]]
    Q = [[base ** k] for k in range(exp + 1)]
    return [p + q for p in P for q in Q]

def get_list_divisor(N, reverse=True):
    try:
        prime_factorized = get_prime_factorized(N)
        res    = re_func_divisior(prime_factorized)
        result = []
        for k in res:
            for i, x in enumerate(k):
                if i == 0:
                    r = x
                else:
                    r *= x
            result.append(r)
        result = sorted(result, reverse=reverse)
    except:
        result = [N]
    return result


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

L = [set(get_list_divisor(a)) for a in A]

print(max([max(l[0] & l[1]) for l in itertools.combinations(L, 2)]))

Submission status: Wrong Answer

Solution code:
import sys
import fractions
import functools
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
a = LIST()

L = [0]*n
R = [0]*n
L[0] = a[0] 
R[n-1] = a[n-1]

GCDs = [0]*n

for i in range(1, n):
    L[i] = fractions.gcd(L[i-1], a[i-1])

for i in range(n-2,-1,-1):
    R[i] = fractions.gcd(R[i+1], a[i+1])

for i in range(n):
    GCDs[i] = fractions.gcd(L[i], R[i])

res = max(GCDs)

print(res)

Submission status: Wrong Answer

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

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

ans=0
for i in range(N):
  ans=gcd(ans,A[i])
#print(ans)

for i in range(N):
  A[i]//=ans
#print(A) 

max_gcd=A[0]
sw=0
for i in range(1,N):
  max_gcd=gcd(max_gcd,A[i])
  if max_gcd==1:
    break
ii=i
#print(ii)

max_gcd1=0
max_gcd2=0
for i in range(N):
  max_gcd1=gcd(max_gcd1,A[i]) if i!=ii else gcd(max_gcd1,A[ii-1])
  max_gcd2=gcd(max_gcd2,A[i]) if i!=ii-1 else gcd(max_gcd2,A[ii])
#print(max_gcd1,max_gcd2)
print(ans*max(max_gcd1,max_gcd2))

Submission status: Wrong Answer

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

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

ans=0
for i in range(N):
  ans=gcd(ans,A[i])
#print(ans)

for i in range(N):
  A[i]//=ans
#print(A) 

max_gcd=A[0]
for i in range(1,N):
  max_gcd=gcd(max_gcd,A[i])
  if max_gcd==1:
    break
ii=i
#print(ii)

max_gcd1=0
max_gcd2=0
for i in range(N):
  max_gcd1=gcd(max_gcd1,A[i]) if i!=ii else gcd(max_gcd1,A[ii-1])
  max_gcd2=gcd(max_gcd2,A[i]) if i!=ii-1 else gcd(max_gcd2,A[ii])
#print(max(max_gcd1,max_gcd2))
print(ans*max(max_gcd1,max_gcd2))