Problem: 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.

Submission attempts (in order):
# input
N = int(input())
A = [int(x) for x in input().split()]


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


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
else:
    CD = []
    CD_alt = []
    base_val = sorted(A)
    for index, array_val in enumerate(A):
        if index != 0:
            CD.append(gcd_wrapper(base_val[0], array_val))
        if index != 1:
            CD_alt.append(gcd_wrapper(base_val[1], array_val))

    sorted_CD = sorted(CD)
    sorted_CD_alt = sorted(CD_alt)
    print(sorted_CD)
    print(sorted_CD_alt)
    print(max([max(sorted_CD[0:2]), max(sorted_CD_alt[0:2])]))

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

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


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
else:
    CD = []
    for index1, array_val1 in enumerate(A):

        for index2, array_val2 in enumerate(A[index1 + 1:]):
            CD.append(gcd_wrapper(A[index1], array_val2))

    sorted_CD = sorted(CD)
    print(sorted_CD)
    print(sorted_CD[N-1:N][0])

import random
import heapq

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

N = 3
A = [12, 15, 18]

# process


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

CD = []
for i in A[1:]:
    if A[0] >= i:
        CD.append(gcd(A[0], i))
    else:
        CD.append(gcd(i, A[0]))


print(min(heapq.nlargest(2, CD)))






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


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


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
else:
    CD = []
    CD_alt = []
    base_val = sorted(A)
    for index, array_val in enumerate(A):
        if index != 0:
            CD.append(gcd_wrapper(base_val[0], array_val))
        if index != 1:
            CD_alt.append(gcd_wrapper(base_val[1], array_val))

    sorted_CD = sorted(CD)
    sorted_CD_alt = sorted(CD_alt)
    print(max([max(sorted_CD[0:2]), max(sorted_CD_alt[0:2])]))


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


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


def gcd_wrapper(x, y):
    if x >= y:
        return gcd(x, y)
    else:
        return gcd(y, x)

if N == 2:
    print(max(A))
    exit()
if N == 3:
    x1 = gcd_wrapper(A[0], A[1])
    x2 = gcd_wrapper(A[1], A[2])
    x3 = gcd_wrapper(A[0], A[2])
    print(max([x1, x2, x3]))
    exit()
A = sorted(A)
A.reverse()
init_results = []
for k, g in enumerate(A[0:3]):
    for h in A[k+1:4]:
        init_results.append(gcd_wrapper(g, h))
if max(init_results) == 1:
    print(1)
    exit()
stop_flag = 0
x = max(init_results)
final_result = set()
final_result.add(x)
for i in A:
    tmp = gcd_wrapper(x, i)
    if tmp == 1:
        stop_flag += 1
    else:
        x = tmp
        final_result.add(tmp)
if stop_flag >= 2:
    print(1)
else:
    sorted_final_result = sorted(list(final_result))
    if len(sorted_final_result) == 1:
        print(sorted_final_result[0])
        exit()
    divable = []
    for j in A:
        if j % sorted_final_result[1] != 0:
            divable.append(1)
    if sum(divable) > 1:
        print(sorted_final_result[0])
    else:
        print(sorted_final_result[1])


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted