Maximum GCD
Practice
1 (1 votes)
Gcd
Number theory
C++
Math
Problem
76% Success 125 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) of length \(N\). You can perform the following operation at most once.

  • Choose any element of the array and replace it with \(X\) where \(1 \leq X \leq M\).

Find the maximum possible GCD of the array after performing the operation.

Input Format:

  • The first line contains an integer \(T\), which denotes the number of test cases. 
  • The first line of each test case contains two integers \(N\) and \(M\).
  • The second line of each test case contains \(N\) space-separated integers, elements of array \(A\).

Output Format:

For each test case, print the maximum possible GCD of the array after performing the operation at most once.

Constraints:

\(1 \leq T \leq 10 \\ 1 \leq N,M \leq 10^5 \\ 1 \leq A[i] \leq 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
Tags:
Medium
Points:30
11 votes
Tags:
MathematicsMediumGreatest common divisorNumber Theory
Points:30
4 votes
Tags:
MathematicsMediumGreatest common divisorGreatest common divisor