BarclaysOA #1
Find Maximum Possible GCD
估计中等可在线判题
You are given an integer R and an array A of positive integers. You may perform the following operation at most once (or not at all): choose any element of the array and replace it with any integer X satisfying 1≤X≤R. Find the maximum possible GCD of the whole array.
输入格式
The first line contains two integers N and R; the second line contains N integers A[0..N−1]. 2≤N≤10^5, 1≤R≤10^5, 1≤A[i]≤10^5.
输出格式
Output a single integer, the maximum possible GCD.
样例 1
输入:
3 10
2 3 4
输出:
2
样例 2
输入:
2 5
100 100
输出:
100
样例 3
输入:
3 7
12 18 7
输出:
6
样例说明
Example 1: A=[2,3,4], R=10. Changing 3 to 2 gives gcd(2,2,4)=2, which is the maximum possible. Example 2: A=[100,100], R=5. Without an operation the gcd is 100, while any replacement makes the gcd at most 5, so the answer is 100. Example 3: A=[12,18,7], R=7. After replacing 7, the gcd of the other elements is 6≤7; take X=6, and the answer is 6.