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:
import math
from functools import reduce
import random
import sys


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

tmpgcd = list()
for i in range(2):
    sample = random.choices(a, k=2)
    tmpgcd.append(math.gcd(sample[0], sample[1]))

# print('hey')

tmpgcd = sorted(tmpgcd)
mini = tmpgcd[1]
# print(mini, tmpgcd)
result = 0
for i in range(mini, -1, -1):
    tmp = [x % i for x in a]
    tmp = [x for x in tmp if x != 0]
    if len(tmp) <= 1:
        result = i
        break

print(result)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math

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


def gcd(a):
    r = a[0]
    for i in range(len(a) - 1):
        r = math.gcd(r, a[i + 1])
    return r


def main():
    m = 1
    for i in range(N):
        a = A[0:i] + A[(i + 1):N]
        g = gcd(a)
        if m < g:
            m = g

    print(m)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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)


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)

    divisors.sort(reverse=True)
    return divisors

n = int(input())
a = list(map(int, input().split()))
a_div = [make_divisors(a[i]) for i in range(n)]

flag = False
i = 0


while True:
    i += 1
    out = 0
    tmp = a_div[0][i]
    for j in range(n):
        if tmp not in a_div[j]:
            out += 1
    if out <= 1:
        break

a_tmp = a[:]
del a_tmp[0]
temp = gcd_list(a_tmp)

print(max(temp,tmp))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
A = np.unique(A)
if len(A) == 1:
	print(A[0])
else:
	ans = 1	
	for i in range(1, A[-1]-A[0]+1):
		count = 0
		for j in range(N):
			if A[j] % i != 0:
				count += 1
			if count == 2:
				break
		else:
			ans = i
	print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
N = int(input())
A = input().split()
A = [int(a) for a in A]
B = []
for n in range(N):
  for i in range(1:(10^9)+1):
    A[n] = i
    b = reduce(math.gcd, A)
    B.append(b)
B_max = max(b)
print(B_max)

Submission status: Runtime Error

Solution code:
"""
問題文

N個の整数 A1,A2,...,ANが黒板に書かれています。
あなたはこの中から整数を 1つ選んで、1 以上 109以下の好きな整数に書き換えます。
元の整数と同じ整数に書き換えても構いません。書き換えた後のN個の整数の最大公約数の最大値を求めてください。

制約
入力は全て整数である。
2≤N≤105
1≤Ai≤109
"""

"""
3 5 7
最大公約数: 1
最大の最大公約数: 1

8 4 6
最大公約数: 2
最大の最大公約数: 4
『aとbとcの最大公約数』　＝　『aとbの最大公約数』と『c』　の最大公約数

A1 , A2 , A3 ... An

gcd(A1 , A2 , A3 ... An-1) * N
= gcd * N * N
= gcd * N^2

gcd(A1 , A2 , A3 ... An-1)
gcd(A1 , A2 , A3 ... An-2 , An) = gcd(A1 , A2 , A3 ... An-2) , An
"""
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()

Submission status: Runtime Error

Solution code:
from math import gcd
from functools import reduce

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

n = int(input())
a = [int(_) for _ in input().split()]
a.sort()
ir = [a[0]]
for i in range(1,n):
    ir.append(gcd(a[0], a[i]))
mi = ir.index(min(ir))
mr = []
for i in range(n):
    mr.append(gcd(a[mi], a[i]))
if ir.count(min(ir)) > mr.count(min(mr)):
    print(gcd_list(a[1::]))
elif ir.count(min(ir)) == mr.count(min(mr)):
    print(a[0])
elif ir.count(min(ir)) < mr.count(min(mr)):
    b = a[:mi] + a[mi+1:]
    print(gcd_list(b))

Submission status: Runtime Error

Solution code:
N = map(int, input().split())
A = map(int, input().split())
A = list(A)
A.append(A[-1])
each_GCD = []
for idx in range(len(A)-1):
    each_GCD.append(computeGCD(A[idx], A[idx+1]))
min_idx = each_GCD.index(min(each_GCD))
if min_idx < len(each_GCD)-1:
    pre = each_GCD[min_idx-1]
    nex = each_GCD[min_idx+1]
    if pre - nex >= 0:
        c = computeGCD(A[min_idx], A[min_idx+2])
        if nex > c:
            print(nex)
        else:
            print(c)
    else:
        c = computeGCD(A[min_idx+1], A[min_idx-2])
        if pre > c:
            print(pre)
        else:
            print(c)

Submission status: Runtime Error

Solution code:
def c_gcd_on_blackboard(N, A):
    from functools import reduce
    from fractions import gcd
    odd, even = set(), set()
    for a in A:
        if a % 2 == 0:
            even.add(a)
        else:
            odd.add(a)
    if len(odd) >= 3:
        return 1
    if len(odd) == 0:
        even = sorted(even)
        even[0] = even[-1]
        return reduce(gcd, even)
    ans = 1
    for o in odd:
        ans = max(ans, reduce(gcd, even))
    return ans

N = int(input())
A = [int(i) for i in input().split()]
print(d_flipping_signs(N, A))

Submission status: Runtime Error

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

def maxgcd(A,N):
    B=[]
    temp=[]
    mmax = 1
    for i in range(N):
        if len(A) == 2:
            if A[0] > A[1]:
                con = gcd(A[0],A[1])
            else:
                con = gcd(A[1],A[0])
            if mmax < con:
                mmax = con
            break
        else:
            B = copy.copy(A)
            B.pop(i)
            temp.append(maxgcd(B,N-1))
        if len(temp) == N:
            mmax = min(temp)
    return mmax
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
B=[]
temp=[]
mmax = 1
if len(A) == 2:
    if A[0] > A[1]:
        con = gcd(A[0],A[1])
    else:
        con = gcd(A[1],A[0])
    if mmax < con:
        mmax = con
    print(mmax)
else:
    for i in range(N):
        B = copy.copy(A)
        B.pop(i)
        temp.append(maxgcd(B,N-1))
    print(max(temp))

Submission status: Runtime Error

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

from math import gcd

b = [0] * n
m = 0

if n == 2:
    print(gcd(a[0], a[1]))
else:
    for k in range(n):
        for i in range(n-1):
            if k != i:
                if i+1 != k:
                    if b[k] == 0:
                        b[k] = gcd(a[i], a[i+1])
                    else:
                        b[k] = gcd(b[k], a[i+1])
                else:
                    if i+2 < n:
                        if b[k] == 0:
                            b[k] = gcd(a[i], a[i+2])
                        else:
                            b[k] = gcd(b[k], a[i+2])
        m = max(m, b[k])
    print(m)

Submission status: Runtime Error

Solution code:
import copy

#最大公約数
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a

#３以上の配列の最大公約数
def gcd3up(dataSet):
    i = 0
    for data in dataSet:
        if i == 0:
            a = data
        else:
            a = gcd(a,data)
        i += 1
    return a

numNum = int(input())
setOfNumOrg = input().split()
setOfNum = [int(i) for i in setOfNumOrg]

ans = 1
#二つの場合
if numNum == 2:
    ans = max(setOfNum)
else:
#一つづつ順に欠損させていきそのなかの最大に合わせればいい
    for i in range(numNum):
        copyData = copy.copy(setOfNum)
        del copyData[i]
        if gcd3up(copyData) > ans:
            ans = gcd(*copyData)
print(ans)

Submission status: Runtime Error

Solution code:
def saidai(a,b):
    amari = a % b
    if amari != 0:
        return saidai(b,amari)
    else:
        return b
def saidai_f(a):
    a.sort(reverse=True)
    if len(a) == 1:
        return a[0]
    a.insert(0,saidai(a.pop(0),a.pop(0)))
    return saidai_f(a)

ans=[]
hoge=[]
n=int(input())
a=list(map(int,input().split()))
if 0 in a:
    print(0)
else:
    a=[i for i in a if i != 0]
    for i in range(len(a)):
        for ii in range(len(a)):
            if i!=ii:
                hoge.append(a[ii])
        hoge.sort(reverse=True)
        ans.append(saidai_f(hoge))
        hoge=[]
    ans.sort(reverse=True)
    print(ans[0])

Submission status: Runtime Error

Solution code:
import math
from functools import reduce
N = int(input())
A = input().split()
A = [int(a) for a in A]
B = []
for n in range(N):
  for i in range(1:(10^9)-1):
    A[n] = i
    b = reduce(math.gcd, A)
    B.append(b)
B_max = max(b)
print(B_max)

Submission status: Runtime Error

Solution code:
import math


N = int(input())
A = input().split()
for i in range(N):
    A[i] = int(A[i])
    
if N == 2:
    print(max(A[0],A[1]))
if N >= 3:
    gcd_all = math.gcd(A[0], A[1])
    gcd_without_1 = A[0]
    gcd_without_2 = A[1]
    gcd_without_1 = max(gcd_all, max( math.gcd(gcd_without_1, A[i]),math.gcd(gcd_without_2, A[i])))
    gcd_all = math.gcd(gcd_all,A[2])
    for i in range(3,N):
        gcd_without_1 = max(gcd_all, math.gcd(gcd_without_1, A[i]))
        gcd_all = math.gcd(gcd_all,A[i])
    print(max(gcd_all, gcd_without_1))

Submission status: Runtime Error

Solution code:
N = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[-1])
each_GCD = []
for idx in range(len(A)-1):
    each_GCD.append(computeGCD(A[idx], A[idx+1]))
min_idx = each_GCD.index(min(each_GCD))
if min_idx < len(each_GCD)-1:
    pre = each_GCD[min_idx-1]
    nex = each_GCD[min_idx+1]
    print(pre, min(each_GCD), nex)
    if pre - nex >= 0:
        c = computeGCD(A[min_idx], A[min_idx+2])
        if nex > c:
            print(nex)
        else:
            print(c)
    else:
        c = computeGCD(A[min_idx+1], A[min_idx-2])
        if pre > c:
            print(pre)
        else:
            print(c)

Submission status: Runtime Error

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

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

r = 0
if N==1:
    print(A[0])
    exit()
else:
    r = gcd(A[0], A[1])
    if N==2:
        print(r)
        exit()
    else:
        for i in range(2, N):
            r = gcd(r,A[i])
B=[]
for i in A:
    B.append(i//r)
#print(B)
D={}
for i in range(N):
    p = B.pop(i)
    rr = gcd(B[0],B[1])
    if len(B)>2:
        for j in range(2,N):
            rr = gcd(rr,B[j])
    if rr not in D:
        D[rr]=1
    else:
        D[rr]+=1
    B.insert(i,p)
#print (D)
for i in D:
    if i ==N-1:
        print(r*i)
        exit()
print(r)

Submission status: Runtime Error

Solution code:
# GCD on Blackboard
"""
リストのどの要素を抜いた時に最大公約数が最大値をとるか調べて、
その時の最大公約数を出力する。
"""

from functools import reduce

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


def get_gcd(a, b):
    """
    ユークリッド互除法により最大公約数を求める。
    """
    if a < b:
        a, b = b, a

    elif a % b == 0:
        return b

    return get_gcd(b, a % b)


def get_gcd_l(l):
    """
    リストの要素の最大公約数を求める。
    """
    return reduce(get_gcd, l)


def answer(l):
    """
    どの要素を抜いた時に最大公約数が最大値をとるか調べる。
    """
    len_l = len(l)
    answers = []  # 答えの案

    for i in range(len_l): # 要素を1個ずつ消してみる。
        temp_l = []
        temp_l += l[0 : i]
        temp_l += l[i+1 : len_l]
        answers.append(get_gcd_l(temp_l))
    ans = max(answers)
    return ans


def main():
    """
    答えを出力する。
    """
    print(answer(A))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import fractions
import sys
import copy

def gcd2(array):
    gcd = fractions.gcd(array[0], array[1])
    for i in range(2, len(array)-1):
        gcd = fractions.gcd(gcd, array[i])
    return gcd


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

gs = []
for i in range(n-1):
    gs.append((fractions.gcd(a[i], a[i+1]), i))
gs.append((fractions.gcd(a[n-1], a[0]), n-1))

_, l = min(gs)
b = copy.copy(a)
c = copy.copy(a)

del b[l]
del c[l+1]
bd = gcd2(b)
cd = gcd2(c)
print(max(bd, cd))

Submission status: Runtime Error

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

if len(A) == 1:
    print(A[0])
    exit()

ls = []
for i in range(N):
    for j in range(N):
        tmp = fractions.gcd(A[i], A[j])
        if tmp not in A:
            ls.append(tmp)

print(max(ls))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define ALL(A) A.begin(),A.end()
const long long mod=1000000007;
int gcd(int a,int b){
    if(a<b) gcd(b,a);
    while(a%b){
        int r=a%b;
        a=b;b=r;
    }
    return b;
}
int a[300000],n;
//true すでにhauita
int dfs(bool f,int g,int i)
{
    if(i<n)
    {
        if(f)
        return dfs(f,gcd(g,a[i]),i+1);
        else
        return (max(dfs(true,g,i+1),dfs(f,gcd(g,a[i]),i+1)));
    }
    else
    return g;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    int k=gcd(a[0],a[1]);
    for(int i=2;i<n;i++)
    {
        k=gcd(k,a[i]);
    }
    for(int i=0;i<n;i++)
    a[i]/=k;
    cout<<k*max(dfs(false,a[0],1),dfs(true,a[1],2))<<endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
"""
python3 main.py < input.txt
"""
import math
import itertools as it
import heapq


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

if len(A) == 2:
        print(max(A[0][1], A[1][1]))
        exit(0)

maxX=-1
for i, ai in enumerate(A):
        x=10**20
        l=list(A)
        l0, l1=(l[1][1], l[2][1]) if i == 0 else (l[0][1], l[2][1]) if i == 1 else (l[0][1], l[1][1]) 
        if (3 if i <= 2 else 2) >= len(A):
                maxX=maxX
        else:
                x=math.gcd(l0, l1)
                for j, lj in l[(3 if i <= 2 else 2):]:
                        if i != j:
                                x=math.gcd(x, lj)
        maxX=max(x, maxX)
print(maxX)

Submission status: Runtime Error

Solution code:
import functools
import math
import sys

sys.setrecursionlimit(10^6)
n=int(input())
a=list((map(int,input().split())))
ans=[]

for i in range(n):
    tmp=a[i]
    a.pop(i)
    ans.append(functools.reduce(math.gcd,a))
    a.insert(i,tmp)
print(max(ans))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

a = list(map(int, input().split()))
box = [0]*10**9

def make_divisors(k):
    divisors = []
    for i in range(1, int(k**0.5)+1):
        if k % i == 0:
            divisors.append(i)
            if i != k // i:
                divisors.append(k//i)
    return divisors
for i in a:
    for j in make_divisors(i):
        box[10**9-j]+=1 
if n-1 in box:
    print(10**9 - box.index(n-1))
else:
    print(100000 - box.index(n))

Submission status: Runtime Error

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


import numpy as np

N = read_a_int()
A = np.array(read_ints())


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

    return a


import math


def is_prime(n):
    if n == 1:
        return True

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


gcd_tmp = A[0]
for a in A[1:]:
    gcd_tmp = gcd(gcd_tmp, a)

A_gcd = A//gcd_tmp

# delidx = None
for i, a in enumerate(A_gcd):
    if is_prime(a):
        delidx = i
        break


ans = A[delidx-1]
for i, a in enumerate(A[:delidx].tolist()+A[delidx+1:].tolist()):
    ans = gcd(ans, a)

print(ans)

Submission status: Runtime Error

Solution code:
import fractions
import functools
import sys
sys.setrecursionlimit(100000)

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

if len(a) == 2:
  print(max(a))
  sys.exit()
  
gcd_a = functools.reduce(fractions.gcd, a)
a=[i//gcd_a for i in a]

cnt = 0
gcd_0 = functools.reduce(fractions.gcd, a[1:])
for i in range(1,len(a)):
  if fractions.gcd(a[0],a[i]) == 1:
    so = i
    cnt+=1
    if cnt == 2:
      print(max(gcd_a,gcd_0)*gcd_a)
      sys.exit()

a_i = [i for i in a if i != a[so]]
gcd_i = functools.reduce(fractions.gcd, a_i)

print(max(gcd_0,gcd_i)*gcd_a)

Submission status: Runtime Error

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)


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)

    divisors.sort(reverse=True)
    return divisors

n = int(input())
a = list(map(int, input().split()))
a_div = [make_divisors(a[i]) for i in range(n)]

flag=False

while True:
    out = 0
    tmp = a_div[0][i]
    for j in range(n):
        if tmp not in a_div[j]:
            out += 1
    if out <= 1:
        break

a_tmp = a[:]
del a_tmp[0]
temp = gcd_list(a_tmp)

print(max(temp,tmp))

Submission status: Runtime Error

Solution code:
import math
import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
#a=list(set(a))
#n=len(a)

ans=0
if n==2: print(max(a))
else:
    for i in range(n):
        b=np.array([a[j] for j in np.delete(np.array(range(n)),i)]) 
        while len(b)>1:
            d=math.gcd(b[0],b[1])
            b=np.delete(b,0)
            b[0]=d
        c=b[0]
        if ans<c: ans=c
    print(ans)

Submission status: Runtime Error

Solution code:

from collections import defaultdict


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

    return divisors


n = int(input())
a = list(map(int, input().split()))
divs = defaultdict(int)
divs_ = defaultdict(int)
la = len(a)
maxdiv = 1
for y in make_divisors(a[0]):
    divs[y] += 1
for y in make_divisors(a[1]):
    divs[y] += 1
    if divs[y] == 2:
        maxdiv = max(maxdiv, y)
if la == 2:
    print(maxdiv)
    exit()
for y in make_divisors(a[2]):
    divs[y] += 1
divs_ = {k: v for k, v in divs.items() if v >= 2}
c = 4
for x in a[3:]:
    d = make_divisors(x)
    for y in d:
        if divs_[y] >= c - 2:
            divs_[y] += 1
    c += 1
for k, v in divs_.items():
    if v >= la - 1:
        maxdiv = max(maxdiv, k)
print(maxdiv)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce

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

n=int(input())
array = list(map(int, input().split())) 
# array=list(set(array))
array.sort(reverse=True)


max=-100

for i in range(n):
    if(max>array[i]):
        break
    pop=array.pop(i)
    tmp_gcd=gcd(*array)
    # print(copy_array)
    if(max<tmp_gcd):
        max=tmp_gcd
    array.insert(i,pop)
    
print(max)





############################################################################


import fractions
from functools import reduce
 
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
 
n=int(input())
array = list(map(int, input().split())) 
array=list(set(array))
array.sort(reverse=True)
 
 
max=-100
 
for i in range(n):
    copy_array=array.copy()
    if(max>array[i]):
        break
    copy_array.pop(i)
    tmp_gcd=gcd(*copy_array)
    # print(copy_array)
    if(max<tmp_gcd):
        max=tmp_gcd
    
print(max)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
w=sorted(a)
ans=1
end=False

flag=False
for h in range(n):
    if a[h]%w[0]!=0 and flag==True:
       break
    if a[h]%w[0]!=0:
        flag=True
    if h==n-1:
        ans=w[0]
        end=True
"""
y=10000000001
for i in range(n):
    y=min(y,a[i]//2)
"""
    

for i in reversed(range(2,-(-x[0]//2)+2)):
    flag=False
    if end==True:
        break
    for h in range(n):
        if a[h]%i!=0 and flag==True:
            break
        if a[h]%i!=0:
            flag=True
        if h==n-1:
            ans=i
            end=True
            break
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

n=int(input())
a = np.array([int(x) for x in input().split()])
a = np.sort(a)

b = np.diag(a)

for i in range(0, len(a)):
    for j in range(0, i):
        b[i,j] = a[i] - a[j]
        b[j,i] = a[i] - a[j]

c = np.delete(b, obj=max(np.unravel_index(np.argmin(b), b.shape)[0],np.unravel_index(np.argmin(b), b.shape)[1]), axis=0)
c = np.delete(c, obj=max(np.unravel_index(np.argmin(b), b.shape)[0],np.unravel_index(np.argmin(b), b.shape)[1]), axis=1)

d=b
d[np.unravel_index(np.argmin(b), b.shape)[0], np.unravel_index(np.argmin(b), b.shape)[1]] = c.min()
d[np.unravel_index(np.argmin(b), b.shape)[1], np.unravel_index(np.argmin(b), b.shape)[0]] = c.min()

d[np.unravel_index(np.argmin(b), b.shape)[1], np.unravel_index(np.argmin(b), b.shape)[1]] = b[np.unravel_index(np.argmin(b), b.shape)[0], np.unravel_index(np.argmin(b), b.shape)[0]] + c.min()

print(math.gcd(d[0,0], d.min()))

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce

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

n=int(input())
array = list(map(int, input().split())) 
array=list(set(array))
array.sort(reverse=True)


max=-100

for i in range(n):
    if(max>array[i]):
        break
    pop=array.pop(i)
    tmp_gcd=gcd(*array)
    # print(copy_array)
    if(max<tmp_gcd):
        max=tmp_gcd
    array.insert(i,pop)
    
print(max)

Submission status: Runtime Error

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

def gcd_list(a, n):
    b = np.array([1]*(n-1))
    for i in range(n-1):
        b[i] = math.gcd(a[n-1], a[i])
    b[b.argmin()] = b[b.argmax()]
    return reduce(math.gcd, b)

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

Submission status: Runtime Error

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

N = int(input())
As = [int(elm) for elm in input().split(" ")] 


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

if N >= 3:
    gs = []
    for i in range(0, (N//2)*2 , 2):
        g = math.gcd(As[i], As[i+1])
        gs.append(g)
    if N % 2 == 1:
        gs.append(As[N-1])

    while len(gs) != 2:
        #print(gs)
        new_gs = []
        N1 = len(gs)
        #print("len",len(gs), (N1//2)*2)
        for i in range(0, (N1//2)*2, 2):
            g = math.gcd(gs[i], gs[i+1])
            new_gs.append(g)
        if N1 % 2 == 1:
            new_gs.append(gs[-1])
        gs = new_gs

    g1, g2 = gs
    d1 = divisor(g1) 
    d2 = divisor(g2) 
    ds = sorted(d1 + d2, reverse=True)

    #print(gs)
    #print(ds)
    for d in ds:
        count = sum([int(A % d == 0) for A in As])
        if count >= N-1:
            out = d
            break

else:
    out = max(As)

print(out)

Submission status: Runtime Error

Solution code:
import copy
import math
from functools import reduce

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

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

def is_prime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True

n = int(input())
a = list(map(int, input().split()))
a.sort()
rtn = gcd_list(a)
rtn_2 = 1
a_2 = [x / rtn for x in a]
flg = True

for x in a_2:
    if is_prime(x):
        a_2.remove(x)
        flg = False
        break

if flg:
    for i in range(a_2):
        tmp = copy.copy(a_2)
        del tmp[i]
        tmp_rtn = gcd_list(tmp)
        if tmp_rtn > rtn_2:
            rtn_2 = tmp_rtn

rtn_2 = gcd_list(a_2) if len(a_2) > 1 else 1

print(int(rtn * rtn_2))

Submission status: Runtime Error

Solution code:
def fermat(n):
    if n % 2 == 0:
        return 0        
    if pow(2, n-1, n) == 1:
        return 1


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


def get_gcd(x,n):
    element = gcd(x[0], x[1])
    for i in range(2,n):
        element = gcd(element,x[i])
    return element


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

if n < 3:
    exit(print(max(a)))
prime_n = 0
interim_max = 0
for i in range(n-1):
    prime_n += fermat(a[i])
    if prime_n > 2:
        exit(print(1))
    max_gcd = get_gcd(a[:i]+a[i+1:], n-1)
    if interim_max < max_gcd:
        interim_max = max_gcd
print(interim_max)

Submission status: Runtime Error

Solution code:
import numpy as np
N =int(raw_input())
A =map(int ,raw_input().split())

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)
def gcd2(A,S):
    r = gcd(A[0],A[1])
    for i in range(len(A)-2):
        while(r>S):
            r = gcd(A[i+2],r)
    return r
S = 0
if len(A) ==2:
    print int(max(A))
else:
    L = np.split(A,2)
    a = gcd2(L[0],max(A))
    b = gcd2(L[1],max(A))
    for i in range(L[0]):
        k = L[0].pop(i)
        S =max(S,gcd2(L[0],S))
        if b == S:
            break
        A.insert(i,k)
    print int(S)

Submission status: Runtime Error

Solution code:
import math

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

ans=[]

for i in range(N):
    if i<N-1:
        if math.gcd(A[i], A[i+1]) != 1:
            ans.append(math.gcd(A[i], A[i+1]))

minimum=min(ans)

if minimum % 2 != 0:
    print(minimum*2)
else:
    print(minimum)

Submission status: Runtime Error

Solution code:
import numpy as np
def GCD(num, kaisu):
    root_num = int(np.ceil(np.sqrt(num)))
    for r in range(root_num):
        if num % (r+1) == 0:
            kaisu[r+1] += 1
            if num / (r+1) != (r+1):
                kaisu[int(num/(r+1))] += 1
    return kaisu

N = int(input())
A = list(map(int, input().split()))
kaisu = np.zeros(max(A)+1, int)
for n in range(N):
    kaisu = GCD(A[n], kaisu)
print(max(np.where(kaisu >= N - 1)[0]))

Submission status: Runtime Error

Solution code:

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


N=int(input())
a=sorted(spaceinput(),reverse=True)


notrewrite=[-1]*N
rewrite=[-1]*N
best=[-1]*N

notrewrite[0]=rewrite[0]=best[0]=a[0]
for i in range(1,N):

    notrewrite[i]=gcd(notrewrite[i-1],a[i])
    rewrite[i]=notrewrite[i-1]
    best[i]=max([rewrite[i],gcd(a[i],best[i-1])])
    #print([notrewrite[i],rewrite[i]])


if N==2:
    print(max(a))
else:
    print(best[N-1])

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

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

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

gcd = gcd_list(A)

for i in range(N):
    for j in range(10**9):
        A[i] = j
        print(gcd, j, A[i])
        if gcd_list(A) > gcd:
            gcd = gcd_list(A)
        else :
            pass

print(gcd)

Submission status: Runtime Error

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:
    c = 0
    for b in As:
        if b % 2 == 1:
            c = b
            break
    for a in As:
        if a % 2 == 1:
            c = fractions.gcd(c, a)
    print(c)
elif odd == 1:
     c = 0
    for b in As:
        if b % 2 == 0:
            c = b
            break
    for a in As:
        if a % 2 == 0:
            c = fractions.gcd(c, a)
    print(c)
    
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: Runtime Error

Solution code:
import fractions as f
import heapq
N = int(input())
As = list(map(int, input().split()))
ans = 1

for i in range(N):
    new_As = As.copy()
    new_As.pop(i)
    # new_As = sorted(new_As)
    mins = heapq.nsmallest(2, new_As)
    if len(new_As) == 1:
        ans = max(ans, mins[1])
    else:
        ans = max(ans, f.gcd(mins[0], mins[1]))

print(ans)

Submission status: Runtime Error

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

def solve():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse=False)
    value=1
    before=1
    for i in range(N):
      for j in range(i+1,N):
        _value = euclid(A[j],A[i])
        if value < _value:
          before=value
          value=_value      
    print(max(before,value))


solve()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
def Gcd(x,y):
    if y == 0:
        return x
    else:
        return Gcd(y, x % y) #再帰定義
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 get_gcd(nums):
    gcd = nums[1]
    for i in nums:
        gcd = Gcd(gcd,i)
    return gcd
max_gcd = 1

for i in A:
    nums = [j for j in A if j != i]
    if len(nums) == 0:
        max_gcd = i
    else:
        gcd = get_gcd(nums)
        if gcd > max_gcd:
            max_gcd = gcd


print(max_gcd)