BarclaysOA #1
Find Maximum Possible GCD
Estimated MediumJudge-ready
Topics (AI-labelled)Bits & MathPrefix & Ranges
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.
Input
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
Output a single integer, the maximum possible GCD.
Example 1
Input:
3 10
2 3 4
Output:
2
Example 2
Input:
2 5
100 100
Output:
100
Example 3
Input:
3 7
12 18 7
Output:
6
Example notes
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.
Editorial
Loading solution…