fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(false);
  6. cin.tie(NULL);
  7.  
  8. int t;
  9. cin >> t;
  10.  
  11. while (t--) {
  12. int n;
  13. cin >> n;
  14. vector<int> a(n);
  15. for (int &x : a) cin >> x;
  16.  
  17. int res = 1;
  18.  
  19. // Try all pairs and compute GCD
  20. for (int i = 0; i < n; ++i) {
  21. for (int j = i + 1; j < n; ++j) {
  22. int g = __gcd(a[i], a[j]);
  23.  
  24. // Count frequencies of numbers modulo gcd
  25. unordered_map<int, int> freq;
  26. for (int x : a) freq[x % g]++;
  27.  
  28. // Find maximum frequency of same remainders
  29. for (auto p : freq) {
  30. if (p.second > 1) {
  31. res = max(res, g);
  32. }
  33. }
  34. }
  35. }
  36.  
  37. cout << res << endl;
  38. }
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5324KB
stdin
4
2
1 3
5
5 4 3 2 1
3
5 6 7
3
1 11 10
stdout
1
2
1
1