```
/*
Time Complexity: O(N*sqrt(MAXA))
Space Complexity: O(N)
where 'N' is the length of the array 'A' and MAXA is the largest element in the array.
*/
int maximumGCD(int m, vector<int> &a)
{
int n = a.size();
// Initializing integer array 'freq' and a integer variable 'ans'.
vector<int> freq(1e5 + 5);
int ans = 0;
// Calculating frequency of factors of elements.
for (int i = 0; i < n; i++)
{
ans = __gcd(ans, a[i]);
for (int j = 1; j * j <= a[i]; j++)
{
if (a[i] % j == 0)
{
// Updating frequency.
freq[j]++;
if (j != a[i] / j)
{
freq[a[i] / j]++;
}
}
}
}
// Finding maximum GCD.
for (int i = 1; i <= 100000; i++)
{
if (freq[i] >= n - 1)
{
// 'i' or its factor can be the overall GCD.
for (int j = 1; j * j <= i; j++)
{
if (i % j == 0 && j <= m)
{
// Updating answer.
ans = max(ans, j);
if (i / j <= m)
{
ans = max(ans, i / j);
}
}
}
}
}
// We are returning the answer here.
return ans;
}
```