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



import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_map = {}
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : return gcd_sub(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[1:])
        return value

def gcd_sub(a , b) : 
    if (a , b) in gcd_map : return gcd_map[(a,b)]
    else : 
        rst = gcd(a , b)
        gcd_map[(a,b)] = rst
        return rst

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in reversed(range(N)) : 
        if i == 0 : 
            prev_list = a_list[1:]
            after_list = []
        elif i == N - 1 : 
            prev_list = []
            after_list = a_list[:N-1]
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
        rst = gcd(GCD(prev_list) , GCD(after_list))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





from math import gcd
from fractions import gcd  # for Ver.3.4 or lower.
#from functools import reduce
gcd_map = {}
def GCD(numbers , startIndex , endIndex):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        set_index = (startIndex , endIndex)
        if set_index in gcd_map : 
            return gcd_map[set_index]
        value = GCD(numbers[1:] , startIndex + 1 , endIndex)
        result = gcd(value , numbers[0])
        gcd_map[set_index] = result
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : 
            prev_list = a_list[1:]
            rst = GCD(prev_list , 1 , N - 1)
        elif i == N - 1 : 
            after_list = a_list[:N-1]
            rst = GCD(after_list , 0 , N - 2)
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
            rst = gcd(GCD(prev_list , 0 , i - 1) , GCD(after_list , i + 1 , N - 1))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





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)

from functools import reduce
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    R = [0 for i in range(N - 1)]
    L = [0 for i in range(N - 1)]
    R[0] = a_list[0]
    L[0] = a_list[-1]

    for i in range(1 , N-1) : 
        R[i] = gcd(R[i-1] , a_list[i])
    
    for j in range(1 , N-1) : 
        L[j] = gcd(L[0] , a_list[N - j - 1])
    
    maxValue = 0
    for i in range(N - 3) : 
        value = gcd(R[i] , L[N - i - 3])
        if value > maxValue : maxValue = value
    print(maxValue)
main()





import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce

gcd_map = {}
def GCD(numbers):
    x = numbers[0]
    for i in range(len(numbers) - 1) : 
        x = gcd_sub(x , numbers[i + 1])
    return x

def gcd_sub(a , b) : 
    if (a , b) in gcd_map : return gcd_map[(a,b)]
    else : 
        rst = gcd(a , b)
        gcd_map[(a,b)] = rst
        return rst



def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : sub_a_list = a_list[1:]
        elif i == N - 1 : sub_a_list = a_list[:N-1]
        else : sub_a_list = a_list[0 : i ] + a_list[i + 1 :] 
        rst = GCD(sub_a_list)
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_map = {}
def GCD(numbers , startIndex , endIndex):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : return gcd_sub(numbers[0] , numbers[1])
    else : 
        set_index = (startIndex , endIndex)
        if set_index in gcd_map : 
            return gcd_map[set_index]
        value = GCD(numbers[1:] , startIndex + 1 , endIndex)
        gcd_map[set_index] = value
        return value

def gcd_sub(a , b) : 
    rst = gcd(a , b)
    return rst

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in reversed(range(N)) : 
        if i == 0 : 
            prev_list = a_list[1:]
            after_list = []
        elif i == N - 1 : 
            prev_list = []
            after_list = a_list[:N-1]
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
        rst = gcd(GCD(prev_list , 0 , i - 1) , GCD(after_list , i + 1 , N - 1))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





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)

from functools import reduce
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    R = [0 for i in range(N - 1)]
    L = [0 for i in range(N - 1)]
    R[0] = a_list[0]
    L[0] = a_list[-1]

    for i in range(1 , N-1) : 
        R[i] = gcd(R[i - 1] , a_list[i])
    
    for j in range(1 , N-1) : 
        L[j] = gcd(L[j - 1] , a_list[- j - 1])
    
    maxValue = 0
    for i in range(N - 2) : 
        print(i , R[i] , L[N - i - 3])
        value = gcd(R[i] , L[N - i - 3])
        if value > maxValue : maxValue = value
    print(max(maxValue , R[-1] , L[-1]))
main()





#from math import gcd
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_L_map = {}
def GCD_L(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        if len(numbers) in gcd_L_map : 
            return gcd_L_map[len(numbers)]
        value = GCD_L(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        gcd_L_map[len(numbers)] = result
        return result

gcd_R_map = {}
def GCD_R(numbers):
    numbers = list(reversed(numbers))
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        if len(numbers) in gcd_L_map : 
            return gcd_R_map[len(numbers)]
        value = GCD_L(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        gcd_R_map[len(numbers)] = result
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : 
            prev_list = a_list[1:]
            rst = GCD_R(prev_list )
        elif i == N - 1 : 
            after_list = a_list[:N-1]
            rst = GCD_L(after_list)
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
            rst = gcd(GCD_L(prev_list ) , GCD_R(after_list ))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





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)

from functools import reduce
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    R = [0 for i in range(N - 1)]
    L = [0 for i in range(N - 1)]
    R[0] = a_list[0]
    L[0] = a_list[-1]

    for i in range(1 , N-1) : 
        R[i] = gcd(R[i - 1] , a_list[i])
    
    for j in range(1 , N-1) : 
        L[j] = gcd(L[j - 1] , a_list[- j - 1])
    
    maxValue = 0
    for i in range(N - 2) : 
        print(i , R[i] , L[N - i - 3])
        value = gcd(R[i] , L[N - i - 3])
        if value > maxValue : maxValue = value
    print(max(maxValue , R[-1] , L[-1])
main()





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)

from functools import reduce
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    R = [0 for i in range(N - 1)]
    L = [0 for i in range(N - 1)]
    R[0] = a_list[0]
    L[0] = a_list[-1]

    for i in range(1 , N-1) : 
        R[i] = gcd(R[i - 1] , a_list[i])
    
    for j in range(1 , N-1) : 
        L[j] = gcd(L[j - 1] , a_list[- j - 1])
    
    maxValue = 0
    for i in range(N - 2) : 
        value = gcd(R[i] , L[N - i - 3])
        if value > maxValue : maxValue = value
    print(maxValue)
main()





import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
def GCD(*numbers):
    return reduce(gcd, numbers)
    #return reduce(math.gcd, numbers)

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : sub_a_list = a_list[1:]
        elif i == N - 1 : sub_a_list = a_list[:N-1]
        else : sub_a_list = a_list[0 : i ] + a_list[i + 1 :] 
        rst = GCD(*sub_a_list)
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





import math
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_map = {}
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : return gcd_sub(numbers[0] , numbers[1])
    else : 
        set_index = set(numbers[1:])
        if set_index in gcd_map : return gcd_map[set_index]
        value = GCD(numbers[1:])
        gcd_map[set_index] = value
        return value

def gcd_sub(a , b) : 
    if (a , b) in gcd_map : return gcd_map[(a,b)]
    else : 
        rst = gcd(a , b)
        gcd_map[(a,b)] = rst
        return rst

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in reversed(range(N)) : 
        if i == 0 : 
            prev_list = a_list[1:]
            after_list = []
        elif i == N - 1 : 
            prev_list = []
            after_list = a_list[:N-1]
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
        rst = gcd(GCD(prev_list) , GCD(after_list))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





#from math import gcd
from fractions import gcd  # for Ver.3.4 or lower.
from functools import reduce
gcd_map = {}
def GCD(numbers , startIndex , endIndex):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        set_index = (startIndex , endIndex)
        if set_index in gcd_map : 
            return gcd_map[set_index]
        value = GCD(numbers[1:] , startIndex + 1 , endIndex)
        result = gcd(value , numbers[0])
        gcd_map[set_index] = result
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    maxValue = 0
    for i in range(N) : 
        if i == 0 : 
            prev_list = a_list[1:]
            rst = GCD(prev_list , 1 , N - 1)
        elif i == N - 1 : 
            after_list = a_list[:N-1]
            rst = GCD(after_list , 0 , N - 2)
        else : 
            prev_list = a_list[0 : i ]
            after_list = a_list[i + 1 :] 
            rst = gcd(GCD(prev_list , 0 , i - 1) , GCD(after_list , i + 1 , N - 1))
        if maxValue < rst : maxValue = rst
    print(maxValue)
main()





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)

from functools import reduce
def GCD(numbers):
    if len(numbers) == 0 : return 0
    if len(numbers) == 1 : return numbers[0]
    if len(numbers) == 2 : 
        return gcd(numbers[0] , numbers[1])
    else : 
        value = GCD(numbers[:len(numbers)-1])
        result = gcd(value , numbers[len(numbers) - 1])
        return result

def main() : 
    N = int(input())
    a_list = input().split(" ")
    a_list = [int(i) for i in a_list]
    R = [0 for i in range(N - 1)]
    L = [0 for i in range(N - 1)]
    R[0] = a_list[0]
    L[0] = a_list[-1]

    for i in range(1 , N-1) : 
        R[i] = gcd(R[i - 1] , a_list[i])
    
    for j in range(1 , N-1) : 
        L[j] = gcd(L[j - 1] , a_list[- j - 1])
    
    maxValue = 0
    for i in range(N - 2) : 
        value = gcd(R[i] , L[N - i - 3])
        if value > maxValue : maxValue = value
    print(max(maxValue , R[-1] , L[-1]))
main()


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Time Limit Exceeded

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Time Limit Exceeded

Runtime Error

Runtime Error

Accepted