fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. void solve(){
  6. int n;
  7. cin>>n;
  8. vector<int> v(n);
  9. map<int,int> mp;
  10. for(int i=0;i<n;i++){
  11. cin>>v[i];
  12. mp[v[i]]=i;
  13. }
  14. int ans=-1;
  15. for(int i=1;i<=1000;i++){
  16. if(mp.find(i)==mp.end()){
  17. continue;
  18. }
  19. for(int j=1;j<=1000;j++){
  20. if(mp.find(j)!=mp.end()){
  21. int x=__gcd(i,j);
  22. if(x==1){
  23. ans=max(ans,mp[i]+mp[j]+2);
  24. }
  25. }
  26. }
  27.  
  28. }
  29.  
  30. cout<<ans<<endl;
  31. }
  32. signed main() {
  33. // your code goes here
  34. int t;
  35. cin>>t;
  36. while(t--)solve();
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5288KB
stdin
6
3
3 2 1
7
1 3 5 2 4 7 7
5
1 2 3 4 5
3
2 2 4
6
5 4 3 15 12 16
5
1 2 2 3 6
stdout
6
12
9
-1
10
7