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:
# vim: set fileencoding=utf-8:
import fractions
import collections
from functools import reduce


def main():
    N = int(input())
    A = [int(ele) for ele in input().split()]

    # ans = tle_solver(A)

    ans = ac_solver(A)
    print(ans)


def ac_solver(A):
    C = collections.Counter(A)
    d = -1
    if len(C) == 2:
        if len(A) == 2:
            d = max(C.keys())
        else:
            for eles in C.most_common():
                if eles[1] != 1:
                    d = eles[0]
                    return d

    if len(C) == 3:
        tmp = C.most_common()[0][0]
        ct = 0
        for ele in C.keys():
            if ele % tmp != 0:
                ct += 1

        if ct == 1:
            return tmp

    else:
        a_min = min(A)
        a_min_divisors = sorted(make_divisors(a_min), reverse=True)
        for d in a_min_divisors:
            ct = 0
            for a in A:
                if a % d != 0:
                    ct += 1

                if 2 < ct:
                    break

            if ct == 1 or ct == 0:
                break

    return d


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 tle_solver(A):
    gcds = [-1] * len(A)
    for idx in range(len(A)):
        tmp = A[:idx] + A[idx+1:]
        gcds[idx] = gcd(*tmp)

    return max(gcds)


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

data = {}


def gcv(a, b):
    global data
    # if a >= b:
    #     a, b = b, a
    if data.get('{}:{}'.format(a, b), None):
        return data['{}:{}'.format(a, b)]
    elif data.get('{}:{}'.format(b, a), None):
        return data['{}:{}'.format(b, a)]

    if b == 0:
        data['{}:{}'.format(a, b)] = a
        return a
    else:
        # print(a, b)
        data['{}:{}'.format(a, b)] = gcv(b, a % b)

        return data['{}:{}'.format(a, b)]


def solve(n, a):
    if n == 2 and a[0] == a[1]:
        return a[0]
    n_dict = defaultdict(int)
    for i in range(len(a) - 1):
        for j in range(len(a) - i -  1):
            # print(i, j + i + 1)
            n_dict[gcv(a[i], a[j + i + 1])] += 1
    n_new = sorted(n_dict.items(), key=lambda x: x[1], reverse=True)
    # print(n_new)
    if n_new[0][0] == 1:
        try:
            return n_new[1][0]
        except:
            return n_new[0][0]
    return n_new[0][0]



def input_from_console():
    n = int(input())
    a = list(map(int, input().split()))
    return n, a


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a % b)
 
N = input()
As =  list(map(int, input().split()))
sorted(As)
r = []
r.append(gcd(As[0], As[-1]))
r.append(gcd(As[1], As[-1]))
r.append(gcd(As[0], As[-2]))
ans = max(r)
reigai = []
for a in As:
  c = 0
  if(a%ans != 0):
    c+=1
    if a%2 !=0:
      print(1)
      quit()
  if c!= 0:
    print(2)
    quit()
print(max(r))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

from fractions import gcd

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def slv_ref(N, A):
  
    ans = 0
    for i in range(N):
        cand = A[0] if i != 0 else A[1]
        for j, a in enumerate(A):
            if i == j:
                continue
            cand = gcd(cand, a)
            if cand <= ans:
                break
        ans = max(ans, cand)
        
    return ans


def eratosthenes(n):
    p = []
    t = [True] * n

    t[1] = True
    for i in range(2, int(math.ceil(math.sqrt(n)))):
        if t[i]:
            p.append(i)
            for j in range(2*i, n, i):
                t[j] = False

    for j in range(i+1, n):
        if t[j]:
            p.append(j)

    return p

# P = eratosthenes(int(math.sqrt(10**9))+1)

@mt
def slv(N, A):
    
    a0 = A[0]
    g0 = []
    for a in A:
        g0.append(gcd(a0, a))
    ans = 0
    for g in g0:
        c = 0
        for a in A:
            if gcd(a, g) == g:
                c += 1
        if c >= N-1:
            ans = max(ans, g)
    return ans
    


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))

    # N = 10
    # A = [72, 96, 44, 48, 34, 58, 38, 34, 6, 81]

    # print(slv_ref(N, A))
    # print(slv(N, A))
    # # for _ in range(10000):
    # #     N = 10
    # #     A = [random.randint(1, 100) for _ in range(N)]
    # #     if slv_ref(N, A) != slv(N, A):
    # #         print(N)
    # #         print(A)
    # #         print(slv_ref(N, A))
    # #         print(slv(N, A))
    # #         break

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

from fractions import gcd

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def slv_ref(N, A):
  
    ans = 0
    for i in range(N):
        cand = A[0] if i != 0 else A[1]
        for j, a in enumerate(A):
            if i == j:
                continue
            cand = gcd(cand, a)
            if cand <= ans:
                break
        ans = max(ans, cand)
        
    return ans


def eratosthenes(n):
    p = []
    t = [True] * n

    t[1] = True
    for i in range(2, int(math.ceil(math.sqrt(n)))):
        if t[i]:
            p.append(i)
            for j in range(2*i, n, i):
                t[j] = False

    for j in range(i+1, n):
        if t[j]:
            p.append(j)

    return p

# P = eratosthenes(int(math.sqrt(10**9))+1)

@mt
def slv(N, A):
    
    a0 = A[0]
    g0 = []
    for a in A:
        g0.append(gcd(a0, a))
    ans = 0
    for g in g0:
        c = 0
        for a in A:
            if gcd(a, g) == g:
                c += 1
        if c >= N-1:
            ans = max(ans, g)
    return ans
    


def main():
    # N = read_int()
    # A = read_int_n()
    # print(slv(N, A))

    N = 10
    A = [72, 96, 44, 48, 34, 58, 38, 34, 6, 81]

    print(slv_ref(N, A))
    print(slv(N, A))
    # for _ in range(10000):
    #     N = 10
    #     A = [random.randint(1, 100) for _ in range(N)]
    #     if slv_ref(N, A) != slv(N, A):
    #         print(N)
    #         print(A)
    #         print(slv_ref(N, A))
    #         print(slv(N, A))
    #         break

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce

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

n = int(input())
a = [ int(x) for x in input().split() ]
_gcd = reduce(gcd, a)

b = [ int(x)//_gcd for x in a ]

t = b[0]
tt = b[0]
cnt = 0
for i in b[1:]:
    tt = min(tt, gcd(t, i))

for i in b[1:]:
    if (gcd(t, i) == tt):
        if cnt == 0:
            cnt += 1
        else:
            t = gcd(t, i)
    else:
        t = gcd(t, i)

ans = (_gcd * t)

t = b[1]
cnt = 0
tt = b[1]
for i in [b[0]] + b[2:]:
    tt = min(tt, gcd(t, i))

for i in [b[0]] + b[2:]:
    if (gcd(t, i) == tt):
        if cnt == 0:
            cnt += 1
        else:
            t = gcd(t, i)
    else:
        t = gcd(t, i)

ans = (_gcd * t)
ans = max(ans, (_gcd * t))

t = b[-1]
cnt = 0
tt = b[-1]
for i in  b[:-1]:
    tt = min(tt, gcd(t, i))

for i in b[:-1]:
    if (gcd(t, i) == tt):
        if cnt == 0:
            cnt += 1
        else:
            t = gcd(t, i)
    else:
        t = gcd(t, i)

ans = max(ans, (_gcd * t))

print(ans)

Submission status: Wrong Answer

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

## input num
#i = input()
#A, B, T = [int(x) for x in i.split()]

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

## debug
#print("debug:{}".format(gcd(max(A),min(A))))
#print(list(reversed(range(1, gcd(max(A),min(A))+1))))

N = max(A)//2 if max(A)//2 > gcd(max(A), min(A)) else gcd(max(A), min(A))

X = 0
for i in reversed(range(1, N+1)):
    count = 0
    for j in A:
        if (j % i) == 0:
            count += 1
    if count > len(A)-2:
        X = i
        break

print(X)

Submission status: Wrong Answer

Solution code:

def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a%b)
n = int(input())
A = input().split()
m = [int(a) for a in A]
ans = 0
if n == 2:
    ans = max(m)
else:
    B = m[:]
    B = sorted(B)
    B = B[:3]
    gcm = gcd(B[0], B[1])
    if gcm > ans:
            ans = gcm
    gcm = gcd(B[1], B[2])
    if gcm > ans:
            ans = gcm
    gcm = gcd(B[0], B[2])
    if gcm > ans:
            ans = gcm
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,fractions
from functools import reduce
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    def dfs(res, flag, depth):
        if depth == N:
            return res
        if flag:
            return dfs(fractions.gcd(res, A[depth]), True, depth + 1)
        else:
            return max( dfs(res, True, depth + 1), dfs(fractions.gcd(res, A[depth]), False, depth + 1))

    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))

    pre = 0
    candidates = 0
    res = 0
    for i in A:
        pre = fractions.gcd( pre, i )
        candidates = max( pre, candidates )
        res = fractions.gcd( res, i )
        pre = candidates
    print(candidates)




lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Wrong Answer

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

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)
                #print('i,inlist:',i , n//i)

    divisors.sort()
    return divisors



adic = {}
#kouho = []
g = 1

for a1 in a:
    diva = make_divisors(a1)
    for diva1 in diva:
        if diva1 in adic.keys():
            adic[diva1] += 1

            if adic[diva1] >= N-1:
                #kouho.append(diva1)
                g = max(g,diva1)
        else:
            adic[diva1] = 1
print(g)

Submission status: Wrong Answer

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

for i in range(max(a), 0, -1):
  cnt = 0
  for j in range(n):
    if a[j] % i != 0:
      cnt += 1
    if cnt >= 2:
      break  
  if cnt >= n - 1:
    print(i)
    break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
v_list = list(map(int, input().split()))
v_set = set(v_list)
if len(v_set) < 3:
    print(max(v_set))
else:
    v_list.sort()
    cnt = 0
    ans = 1
    for i in range(2, min(v_list)):
        flag = 0
        for j in v_list:
            if j % i == 0:
                flag += 1
        if flag > n-2:
            ans = i
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

for j in range(N):
    if len(np.where(A % np.min(A) != 0)) == 0:
        print(np.min(A))

for i in range(2, np.min(A)):
    flag = len(np.where(A%i != 0)[0])
    if flag <= 1:
        i_max = i
print(i_max)

Submission status: Wrong Answer

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

def euclid(a, b):
    if a < b:
        tmp = a;
        a = b;
        b = tmp;

    r = a % b;
    while r != 0:
        a = b
        b = r
        r = a % b

    return b

if n == 2:
    print(max(a[0], a[1]))
    exit()
elif n == 3:
    print(max(euclid(a[0], a[1]), max(euclid(a[1], a[2]), euclid(a[0], a[2]))))
    exit()
else:
    t1 = euclid(a[0], a[1])
    t2 = euclid(a[1], a[2])
    t3 = euclid(a[0], a[2])

    l = [t1, t2, t3]
    l.sort(reverse=True)

    tt = [0, 0, 0]

    z = t1

    for i in range(n):
        if a[i] % l[0] != 0:
            tt[0] += 1

        if a[i] % l[1] != 0:
            tt[1] += 1

        if a[i] % l[2] != 0:
            tt[2] += 1

        if a[i] % z != 0:
            z = euclid(a[i], z)

    # print(l[0], l[1], l[2])
    # print(tt[0], tt[1], tt[2])

    for i in range(3):
        if tt[i] <= 1:
            print(l[i])
            exit()

    print(z)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import gcd
from functools import reduce
from itertools import combinations

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    A = il()
    N = len(A)
    ans = 0
    for i, n in enumerate(combinations(A, N)):
        ans = max(ans, reduce(gcd, n))
    return ans


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

Solution code:
import functools
import operator

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

result = 0
for i, _ in enumerate(A):
    factors = set()
    for j, a in enumerate(A):
        if i == j:
            continue
        factors_a = set()
        for n in range(1, a+1):
            if a%n == 0:
                factors_a.add(n)

        if len(factors) == 0:
            factors = factors_a
        else:
            factors &= factors_a

    if len(factors) > 0:
        result = max(result, functools.reduce(operator.mul, factors))


print(result)

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    while (y != 0):
        x, y = y, x%y
    return (x)

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

result = 1000000000
a[0] = 1

for i in range(n):
    for j in range(i+1,n):
        if(a[i]>result):
            a[i]=result
        if(a[i] >= a[j]):
            result = gcd(a[i], a[j])
        elif(a[i] < a[j]):
           result = gcd(a[j], a[i])
        if (result == 1):
            break

print(result)

Submission status: Wrong Answer

Solution code:
import math
from sys import stdin
import numpy as np

n = int(stdin.readline().rstrip())
nums = np.sort(np.array(list(map(lambda x: int(x), stdin.readline().rstrip().split()))))

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

if len(nums) == 2:
    print(max(nums))
else:
    diff = min(np.diff(nums))
    n1, n2, n3 = np.sort(np.array([gcd(nums[-1],nums[-2]), gcd(nums[-2],nums[-3]), gcd(nums[-1],nums[-3])]))
    p = nums[3:]/n3
    p2 = list(map(lambda x: x.is_integer(), p))
    if False in p2:
        print(n3)
    else:
        print(n1)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from fractions import gcd
from functools import reduce
import sys
N = int(input())
A = list(set([int(i) for i in input().split()]))
 
def gcd_(*numbers):
    return reduce(gcd, numbers)
 
def prime(n):
    # 素数列挙
    # nまでの素数をリストで返す(nを含む)
    primes = set(range(2, n+1))
    for i in range(2, int(n**0.5+1)):
        primes.difference_update(range(i*2, n+1, i))
    primes=list(primes)
    return primes
 
num = gcd_(*A)
ls = prime(min(A)//num)
for x in ls[::-1]:
    a = 0
    for y in A:
        if y%(x*num) != 0:
            a += 1
        if a >= 2:
            break
    if a == 1:
        print(num*x)
        sys.exit()
print(num)

Submission status: Wrong Answer

Solution code:
import fractions

N = int(input())
A = [int(i) for i in input().split()]
gcd = [0] * N

max_A = max(A)
for i in range(N):
    gcd[i] = fractions.gcd(A[i], max_A)

min_idx = gcd.index(min(gcd))

del gcd[min_idx]

print(min(gcd))

Submission status: Wrong Answer

Solution code:
### C

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

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

tmp = []
for i in range(1, N):
    tmp.append(gcd(A[0], A[i]))
    if i+1 < N:
        tmp.append(gcd(A[2], A[i+1]))

print(max(tmp))

Submission status: Wrong Answer

Solution code:
import fractions as f

def calc_odd(n):
    if(n % 2 != 0):
        return True
    else:
        return False

N = int(input())
A = [int(i) for i in input().split()]
index_min = A.index(min(A))
index_max = A.index(max(A))
check_odd = list(map(calc_odd,A))
answer = 0

if(check_odd.count(True) == 1):
    index_odd = check_odd.index(True)
    del A[index_odd]
    answer = A[0]
    for i in range(1, N-1):
        answer = f.gcd(answer, A[i])
else:
    A[index_min] = A[index_max]
    answer = A[0]
    for i in range(1, N):
        answer = f.gcd(answer, A[i])
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = input().split()
A_list_i = [int(s, 0) for s in A_list]
max_gcd = 0
min_gcd = [10000000000, -1]
for i in range(N-1):
    for j in range(i+1, N):
        if A_list_i[i] == A_list_i[j]:
            temp = A_list_i[i]
        elif A_list_i[i] > A_list_i[j]:
            temp = A_list_i[i] % A_list_i[j]
        else:
            temp = A_list_i[j] % A_list_i[i]
        if temp > max_gcd:
            max_gcd = temp
        if temp < min_gcd[0]:
            min_gcd[0] = temp
            if A_list_i[i] > A_list_i[j]:
                min_gcd[1] = i
            else:
                min_gcd[1] = j
A_list_i[min_gcd[1]] = max_gcd
temp = 0
max_gcd = 0
for i in range(N-1):
    for j in range(i+1, N):
        if A_list_i[i] == A_list_i[j]:
            temp = A_list_i[i]
        elif A_list_i[i] > A_list_i[j]:
            temp = A_list_i[i] % A_list_i[j]
        else:
            temp = A_list_i[j] % A_list_i[i]
        if temp > max_gcd:
            max_gcd = temp            
print('%d' % max_gcd)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=list(map(int,input().split()))
count=0
for i in range(max(c),0,-1):
  if i in c:
    for j in range(n):
      if c[j]%i!=0:
        count=count+1
      if count>=2:
        break
  if count==1 or count==0:
    print(i)
    break
  count=0

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
def find(inte):
    re = []
    for i in range(1,int(inte**0.5)+1):
        if inte%i==0:
            re.append(i)
            if i != inte/i:
                re.append(int(inte/i))
    return set(re)
if n==2:
    intersection = find(A[0]) & find(A[1]) 
    print(max(intersection))
elif n == 3:
    a1 = find(A[0])
    a2 = find(A[1])
    a3 = find(A[2])
    max1 = max(a1&a2)
    max2 = max(a1&a3)
    max3 = max(a2&a3)
    print(max(max1,max2,max3))
else:
    a1 = find(A[0])
    a2 = find(A[1])
    a3 = find(A[2])
    a4 = find(A[3])
    max1 = max(a1&a2)
    max2 = max(a1&a3)
    max3 = max(a2&a3)
    max4 = max(a2&a4)
    print(max(max1,max2,max3,max4))

Submission status: Wrong Answer

Solution code:
def GCD(a, b):
    while a % b != 0:
        r = a % b
        a, b = b, r
    return b

ans = 0
N = int(input())
A = tuple(map(int,input().split()))

A2 = tuple(set(A))
"""
if len(A2) <= 2:
    A2 = (A2[0],A2[0])
"""
for S in range(len(A2)):
    Af = 0
    for i in range(len(A2)):
        if S != i:
            Af = GCD(Af,A[i])
    ans = max(ans,Af)

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

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

kouho = [a[0], a[0]]

for i in range(n - 1):
    hoge = []
    for j in range(len(kouho)):
        hoge.append(fractions.gcd(a[i + 1], kouho[j]))
    if len(hoge) == 1:
        kouho = [hoge[0], kouho[0]]
    else:
        kouho = hoge

a1 = max(kouho)

a[0], a[1] = a[1], a[0]

kouho = [a[0], a[0]]

for i in range(n - 1):
    hoge = []
    for j in range(len(kouho)):
        hoge.append(fractions.gcd(a[i + 1], kouho[j]))
    if len(hoge) == 1:
        kouho = [hoge[0], kouho[0]]
    else:
        kouho = hoge

a2 = max(kouho)

print(max(a1, a2))

Submission status: Wrong Answer

Solution code:
import fractions
N = int(input())
A = list(map(int, input().split()))
# まずはA[0]以外のgcdをansとしておく
ans = A[1]
for i in range(2, N):
	ans = fractions.gcd(ans, A[i])
# つぎにA[0]と他の要素の2数のgcdをリストアップし、それが一番小さくなるindexをkとする
G = [A[0]]
for j in range(1, N):
	G.append(fractions.gcd(A[0], A[j]))
k = G.index(min(G))
# A[k]以外のgcdをtmpとしておく
if k == N - 1:
	B = A[: N - 1]
elif k == 0:
	B = A[1:]
else:
	B = A[: k] + A[k + 1:]
tmp = B[0]
for l in range(1, N - 1):
	tmp = fractions.gcd(tmp, B[l])
# ansとtmpの大きい方を出力
print(max(ans, tmp))

Submission status: Wrong Answer

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

for val in A:
    if val%A[0] == 0:
        continue
    else:
        break
else:
    print(A[0])
    exit()

ngcnt = 0
bef_searchnum = A[0] // 2
searchnum = A[0] // 2
resultflg = False
initflg = True
while 1:
    bef_searchnum = searchnum
    if initflg:
        initflg = False
    else:
        if resultflg:
            searchnum = searchnum + (A[0] - searchnum) // 2
        else:
            searchnum = searchnum // 2
    for val in A:
        if val%searchnum == 0:
            continue
        elif ngcnt < 2:
            ngcnt += 1
            continue
        else:
            if resultflg and abs(searchnum - bef_searchnum) == 1:
                print(bef_searchnum)
                exit()
            resultflg = False
            break
    else:
        resultflg = True

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

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

div = defaultdict(int)
for a in A:
    divisor = make_divisors(a)
    for d in divisor:
        div[d] += 1

N = len(A)
for num, a in sorted(div.items(), reverse=True):
    if a > N - 2:
        print(num)
        exit()

Submission status: Wrong Answer

Solution code:
import functools
import operator

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

result = 0
for i, _ in enumerate(A):
    print(i, _)
    factors = set()
    for j, a in enumerate(A):
        if i == j:
            continue
        factors_a = set()
        for n in range(1, a+1):
            if a%n == 0:
                factors_a.add(n)
        print("factors_a", factors_a)
        if len(factors) == 0:
            factors = factors_a
        else:
            factors &= factors_a
    print("factors", factors)
    if len(factors) > 0:
        result = max(result, functools.reduce(operator.mul, factors))
    print("---")

print(result)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    a,b=sorted([a,b])
    while b%a!=0:
        a,b=b%a,a
    return a
n=int(input())
a=list(map(int, input().split()))
if(n==2):
    print(max(a))
    quit()
ans=0
gcd1=[0]*n
gcd2=[0]*n
tmp=a[0]
for i in range(n):
    tmp=gcd(a[i],tmp)
    gcd1[i]=tmp
tmp=a[n-1]
for i in range(n-1,-1,-1):
    tmp=gcd(a[i],tmp)
    gcd2[i]=tmp
for i in range(0,n-2):
    ans=max(ans,gcd(gcd1[i],gcd2[i+2]))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
point = a[0]
b = []
save = 0
for i in range(1,point+1):
    if point % i == 0:
        b.append(i)
b.sort()
b.reverse()
for j in b:
    for i in range(n):
        if a[i] % j != 0:
            save += 1
        if save == 2:
            save = 0
            break
        if i == n-1:
            print(j)
            quit()

Submission status: Wrong Answer

Solution code:
import sys
def gcd(a, b):
    while a>0 and b >0:
        if a > b:
            a %= b
        else:
            b %= a
    return a + b

N = int(input())
A = list(map(int, input().split()))
if N == 2:
    print(max(A))
    sys.exit(0)
forwards = [A[0]]
backwards = [A[-1]]
for i in range(N):
    forwards.append(gcd(forwards[-1], A[i]))
    backwards.append(gcd(backwards[-1], A[-(i+1)]))
backwards = list(reversed(backwards))

ans = 0
for i in range(1, N-1):
    a = gcd(forwards[i-1], backwards[i+1])
    if a > ans:
        ans = a

if N > 2:
    ans = max([ans, forwards[-2], backwards[1]])
elif N == 2:
    ans = max(A)
else:
    ans = A[0]

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m


N = int(input())
A = list(map(int, input().split()))
# gcd_list = []
# hoge = gcd(A[0], A[1])
# for i in range(2, N):
#     gcd_list.append(gcd(hoge, A[i]))
# gcd_list.sort()
# print(gcd_list[len(gcd_list)-2])
A.sort()
max_A = max(A)
gcd_list = []
for i in range(int(N/2)+1):
    tmp = A[i]
    A[i] = max_A
    hoge = gcd(A[0], A[1])
    for j in range(2, int(N/2)+1):
        hoge = gcd(A[j], hoge)
    gcd_list.append(hoge)
    A[i] = tmp

print(max(gcd_list))
# 最大の値と変えてもいい

Submission status: Wrong Answer

Solution code:
import sys,math
input = sys.stdin.readline

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

flag=False
pre=min(A)
while True:
    MIN=min(A)
    B=[]
    for i,a in enumerate(A):
        if a==0:
            pass
        elif a!=MIN:
            B.append(a%MIN)
        elif a==MIN:
            B.append(a)
    B=list(set(B))
    B=[x for x in B if x != 0]
    if len(B)==1 and flag==False:
        A[A.index(MIN)]=max(A)
        B=A
        flag=True
    if len(B)==1:
        if flag:
            pre=min(B)
        break
    else:
        pre=MIN
        A=B

print(pre)

Submission status: Wrong Answer

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

from fractions import gcd
from functools import reduce
import functools

def gcd(a,b):
    if a==0:
        return b 
    else:
        return gcd(b%a,a)
  
max_gcd = 0
loop_range = min(20,len(A))
g = lambda a,b:a if b==0 else g(b,a%b)

# 小さい方から確認
for i in range(loop_range):
  if i < N:
    values = A[i+1:] + A[:i] 
  else:
    values = A[:-1]
  
  gcd = functools.reduce(lambda x,y:g(x,y),values)
  max_gcd = max(max_gcd,gcd)

# 大きい方からも確認
A.sort(reverse=True)
for i in range(loop_range):
  if i < N:
    values = A[i+1:] + A[:i] 
  else:
    values = A[:-1]
  
  gcd = functools.reduce(lambda x,y:g(x,y),values)
  max_gcd = max(max_gcd,gcd)

print(max_gcd)

Submission status: Wrong Answer

Solution code:
import fractions
from functools import reduce

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()

    tmp = gcd_list(a)
    tmp2 = tmp
    ans = 0
    for i in range(n):
        a_ = a.copy()
        a_.pop(i)
        res = gcd_list(a_)
        if tmp != res:
            print(res)
            break
    if tmp2 == tmp:
        print(tmp)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

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

not_replaced_max = A[0]

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

# def divisor(n):
#     limit = int(math.sqrt(n))
#     for i in range(2, limit):
#         if n % i == 0:
#             result.append(i)
#     return result

# def merge(A, B):

# divisor_list = divisor(A[1])
# for i in range(1, N):
#     not_replaced_max = gcd(not_replaced_max, A[i])
# print(replaced_max)
#
A.sort()

def func(n):
    limit = int(math.sqrt(n))
    divisor = []
    for i in range(1, limit):
        if n % i == 0:
            divisor.append(i)
    return divisor

def merge(A, B):
    divisor_list = [a for a in A]
    for b in B:
        if b not in A:
            divisor_list.append(b)
    return divisor_list

divisor_list = merge(func(A[0]), func(A[1]))
divisor_list.sort(reverse=True)
is_end = False
for d in divisor_list:
    count = 0
    for a in A:
        if a % d != 0:
            count += 1
            if count > 1:
                break
    if count == 0 or is_end:
        print(d)
    else:
        is_end = True

Submission status: Wrong Answer

Solution code:
import copy
from functools import reduce
from fractions import gcd

N = int(input())
nums = [int(i) for i in input().split()]

fort = [nums[0], gcd(nums[0], nums[1])]
back = [nums[N-1], gcd(nums[N-1], nums[N-2])]
ans = []

for i in range(N-2):
    fort.append(gcd(fort[i+1], nums[i+2]))
                
for i in range(N-2):
    back.append(gcd(back[i+1], nums[i+2]))
    
for i in range(1, N-1):
    ans.append(gcd(fort[i-1], back[N-1-i]))

ans.append(fort[N-1])
ans.append(back[N-1])
ans.append(gcd(fort[0],back[N-1]))

print(max(ans))