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: Runtime Error

Solution code:
n = int(input())

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

import math
from functools import reduce


def gcd(numbers):
    return reduce(math.gcd, numbers)


g = gcd(a)
a = [a[i]//g for i in range(n)]


m = 1
for i in range(n):
    b = a[:]
    del b[i]
    gg = gcd(b)
    m = max(m, gg)


print(g * m)

Submission status: Runtime Error

Solution code:
import itertools
import math

N = int(input()) 
a = list(int(_) for _ in input().split()) 

ma = list(itertools.combinations(a,N-1))
mal = []
for i in range(len(ma)):
    mal.append(math.gcd(*ma[i]))
print((max(mal)))

Submission status: Runtime Error

Solution code:
from functools import reduce

def gcdd(x, y):
    for i in range(min(x, y), 1, -1):
        if x % i == 0 and y % i == 0:
            return i

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

n = int(input())
a = [int(i) for i in input().split()]
b = []
for i in range(n):
    d = a[:i] + a[i+1:]
    b.append(gcd(d))
print(max(b))

Submission status: Runtime Error

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

import copy

import math
from functools import reduce

ans = 1

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

for i in range(N):
    A_sub = copy.deepcopy(A)
    del(A_sub[i])
    ans_sub = gcd_list(A_sub)
    if ans < ans_sub:
        ans = ans_sub

print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

N = int(input())

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

g = gcd_list(A)

for i in range(N):
    tmp = A.pop(0)
    if g < gcd_list(A):
        g = gcd_list(A)
    A.append(tmp)

print(g)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
import numpy as np

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

n = int(input())

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

ans = []
A = np.array(a)
for i in range(n):
    ind = np.ones(n, dtype=bool)
    ind[i] = False
    ans.append(gcd_list(A[ind]))

print(max(ans))

Submission status: Runtime Error

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

import fraction
from functools import reduce

ans = 0
for i in range(n):
  x = a[:i] + a[i+1:]
  ans = max(ans, reduce(fraction.gcd, x))
  
print(ans)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce
def gcd_list(numbers):
    return reduce(fractions._gcd, numbers)
n = int(input())
a = list(map(int,input().split()))
m = 1
for i in range(n):
    b = a[:i] + a[i+1:]
    x = gcd_list(b)
    if x > m:
        m = x
print(m)

Submission status: Runtime Error

Solution code:
import math
import copy
from functools import reduce

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

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

ans = 1
for i in range(n):
    tmp_list = copy.copy(a)
    tmp_list.pop(i)
    tmp = gcd_list(tmp_list)
    if ans < tmp:
        ans = tmp
print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def gcd(numbers):
    return reduce(math.gcd, numbers)

n = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(n):
    ans = max(ans, gcd(A[:i]+A[i+1:]))
print(ans)

Submission status: Runtime Error

Solution code:
from fraction import gcd

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

def main():
    n = int(input())
    a = input_ints()
    x = [a[0]]
    y = [a[-1]]
    for i in range(n-1):
        x.append(gcd(x[-1], a[i + 1]))
        y.append(gcd(y[-1], a[n - i - 2]))
    ans = max(x[-2],y[-2])
    for i in range(1, n-1):
        ans = max(ans,gcd(x[i-1],y[-i-1]))
    print(ans)
    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import functools
import math

N = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(N):
	a_new = a[0:i]+a[i+1:N]
	gcd = functools.reduce(math.gcd,a_new)
	ans = max(ans,gcd)

print(ans)

Submission status: Runtime Error

Solution code:
import math
import copy
from functools import reduce

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

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

n = int(input())
max_ = 0
lists = list(map(int,input().split()))

for i in range(len(lists)):
    cp = copy.copy(lists)
    cp.pop(i)
    k = gcd_list(cp)
    if k>= max_:
        max_ = k
print(max_)

Submission status: Runtime Error

Solution code:
from math import gcd
n = int(input())
nums = list(map(int, input().split()))

prefix_gcd = []

prefix_gcd.append(nums[0])

for i in nums[1:]:
    prefix_gcd.append(gcd(i,prefix_gcd[-1]))

postfix_gcd = []
postfix_gcd.append(nums[-1])

for j in range(n-2,-1,-1):
    postfix_gcd.append(gcd(nums[j],postfix_gcd[-1]))

postfix_gcd = postfix_gcd[::-1]

ans = 1

for i in range(n):
    # del i
    if i == 0:
        ans = max(ans,postfix_gcd[i+1])
    elif i == n-1:
        ans = max(ans,prefix_gcd[i-1])
    else:
        ans = max(ans, gcd(prefix_gcd[i-1], postfix_gcd[i+1]))
print(ans)

Submission status: Runtime Error

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

import functools
import math
gcd = 0
for i in range(0, N):
    gcd_part = functools.reduce(math.gcd, A[:i]+A[i+1:])
    if gcd < gcd_part:
        gcd = gcd_part
    
print(gcd)

Submission status: Runtime Error

Solution code:
import functools
import math
n = int(input())
values = list(map(int, input().split()))
m = 1

for i in range(n):
    a = values[:]
    a.pop(i)
    gcd = functools.reduce(math.gcd, a)
    if m < gcd:
        m = gcd


print(m)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def gcd(numbers):
    return reduce(math.gcd, numbers)

n = int(input())
a = [int(i) for i in input().split()]
b = []
for i in range(n):
    d = a[:i] + a[i+1:]
    b.append(gcd(d))
print(max(b))

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

index=0
max=0
while index < len(listA):
    if index == 0:
        gc = gcd_list(listA[1:])
    elif index == len(listA)-1:
        gc = gcd_list(listA[:len(listA)-1])
    else:
        listB = listA[:index]
        listB.extend(listA[index+1:len(listA)])
        gc = gcd_list(listB)
    if gc > max:
        max = gc
    index+=1
print(max)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

ans = 1
for i in range(N):
    x = A[:i] + A[i+1:]
    tmp = gcd(*x)
    ans = max(tmp,ans)
print(ans)

Submission status: Runtime Error

Solution code:
from functools import reduce
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
def gcd_list(numbers):
    return reduce(gcd, numbers)

n = int(input())
a = list(map(int, input().split()))
L = []
if n == 2:
  print(max(a))
else:
  for i in range(n):
    b = [_ for _ in a if _ != a[i]]
    L.append(gcd_list(b))

  print(max(L))

Submission status: Runtime Error

Solution code:
from math import gcd


def mygcd(enum):
    if len(enum) == 1:
        return enum[0]
    ret = 0
    for i in range(len(enum)-1):
        if i == 0:
            ret = gcd(enum[0], enum[1])
        else:
            ret = gcd(ret, enum[i])
    return ret


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

ans = 0
for i in range(n):
    li1 = a[0:i]
    li2 = a[i+1:]
    po = gcd(mygcd(li1), mygcd(li2))
    if ans < po:
        ans = po

print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

n = int(input())

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

ans = []
for i in range(n):
    B = A[:i] + A[i+1:]
    ans.append(gcd(*B))
print(max(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int,input().split())
mx = -1
for i in range(1,n):
  z = a[0:]
  for b in range(i+1):
    z[i] *= -1
    z[i+1] *= -1
  nz = sum(z)
  if mx < nz:
    mx = nz
print(mx)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

ans = 0
for i in range(n):
    a_reduce = a.copy()
    del(a_reduce[i])
    ans = max(ans, gcd(*a_reduce))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

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

import math
import copy
from functools import reduce

def gcd(numbers):
    return reduce(math.gcd, numbers)

max_gcd = 0
for idx in range(N):
    cleaned = copy.copy(A_list)
    del cleaned[idx]
    gcd_value = gcd(tuple(cleaned))
    if max_gcd < gcd_value:
        max_gcd = gcd_value
print(max_gcd)

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(N):
    ans=max(ans,gcd(a[:i]+a[i+1:]))
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd

n = int(input())
a = list(map(int, input().split()))
p = list(a)
s = list(a)
for i in range(1, n):
    p[i] = gcd(p[i - 1], p[i])
for i in range(n - 2, -1, -1):
    s[i] = gcd(s[i], s[i + 1])

ans = max(p[-2], s[1])
if n > 2:
    ans = max(ans, max(gcd(p[i - 1], s[i + 1]) for i in range(1, n - 1)))

print(ans)

Submission status: Runtime Error

Solution code:
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
p = list(a)
s = list(a)
for i in range(1, n):
    p[i] = gcd(p[i - 1], p[i])
for i in range(n - 2, -1, -1):
    s[i] = gcd(s[i], s[i + 1])

ans = max(p[-2], s[1])
ans = max(ans, max(gcd(p[i - 1], s[i + 1]) for i in range(1, n - 1)))
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd

n = int(input())
a = list(map(int, input().split()))
p = list(a)
s = list(a)
for i in range(1, n):
    p[i] = gcd(p[i - 1], p[i])
for i in range(n - 2, -1, -1):
    s[i] = gcd(s[i], s[i + 1])

ans = max(p[-2], s[1])
ans = max(ans, max(gcd(p[i - 1], s[i + 1]) for i in range(1, n - 1)))
print(ans)