**Link to the problem:** https://cses.fi/problemset/task/1081/

**My approach:** I came up with an O(n\sqrt{x}) algorithm. I go through each of the numbers and then find their factors using the O(\sqrt{x}) algorithm, and then update the frequency array of the possible factors (with size being equal to the maximum possible number), and then once I have built the frequency array, I go in the reverse order from the largest possible number to 1, and the moment I find that for a factor i, frequency[i] > 1, I stop the traversal. And finally I report the answer as i.

**My code:**

```
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int MAXN = 1e6+5;
vector<int> freq(MAXN,0);
void divisors(int x)
{
for(int i=1; i*i<=x; i++)
{
if(x%i == 0)
{
if(x/i != i)
{
freq[x/i]++;
freq[i]++;
}
else freq[i]++;
}
}
}
int main()
{
FASTIO
int n, maxVal = INT_MIN;
cin >> n;
vector<int> arr(n);
for(int& x : arr) cin >> x, maxVal = max(maxVal, x);
for(int x : arr) divisors(x);
for(int i=maxVal; i>=1; i--)
{
if(freq[i] > 1)
{
cout << i << "\n";
break;
}
}
return 0;
}
```

**My question:** Even though this approach gets an AC, but its still quite slow. Can we do better? If yes (most probably), how can we do it? Are there any other concepts that are needed over here?

Any help is highly appreciated.