Problem of the day
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', 1 <= 'X' <= 'M'.
Return the maximum possible GCD of the array after performing the operation.
For Example:-Let 'N' = 3, 'M' = 5, and 'A' = [4, 3, 2].
We can replace 3 with 2.
The maximum possible GCD is 2.
First-line contains an integer 'T', which denotes the number of test cases.
For every test case:-
First-line contains two integers, 'N', and 'M'.
Second-line contains 'N' space-separated integers, elements of array 'A'.
Output Format:-
For each test case, Return the maximum possible GCD of the array after performing the operation.
Note:-
You don’t need to print anything. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' , 'M' <= 10^5
1 <= 'A[i]' <= 10^5
The Sum of 'N' overall test cases does not exceed 10^5.
Time Limit: 1 sec
2
4 5
2 5 6 12
2 4
10 15
2
5
First test case:-
We can replace 5 with 4.
The maximum possible GCD is 2.
Second test case:-
We do not need to replace any element.
The maximum possible GCD is 5.
2
5 13
3 4 2 8 7
3 12
4 14 13
1
2
Does the frequency of the factors of the elements matter?
Approach:-
Algorithm:-
O(N*sqrt(MAXA)), where 'N' is the length of the array 'A' and ‘MAXA’ is the largest element in the array.
We are traversing from 1 to 'N' and finding factors, and the complexity associated with this is O(N*sqrt(MAXA)). Hence, the overall time complexity is of the order O(N*sqrt(MAXA)).
O(N), where 'N' is the length of the array 'A'.
We are creating 'freq' to store frequency, So the Space Complexity is O(N).