Advertisement
Plabon_dutta

B. Permutation Swap

May 14th, 2023
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  7.  
  8.     int testcase;
  9.     cin >> testcase;
  10.     while(testcase--) {
  11.         int n;
  12.         cin >> n;
  13.  
  14.         set <int> s;
  15.         for (int i = 1; i <= n; i++) {
  16.             int x;
  17.             cin >> x;
  18.             if (abs(x - i) != 0) s.insert(abs(x - i));
  19.         }
  20.         int ans = 0;
  21.  
  22.         for (auto i : s) {
  23.             ans = __gcd(ans, i);
  24.         }
  25.  
  26.         cout << ans << '\n';
  27.     }  
  28.  
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement