Advertisement
matbensch

Same Differences Solution

Sep 13th, 2023
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     // read number of cases
  8.     int t; cin>>t;
  9.     while (t--){
  10.         // read n
  11.         int n; cin>>n;
  12.         // map a[i]-i to frequency
  13.         map<int, long long> freq;
  14.         long long ans = 0;
  15.         for (int i = 0; i < n; ++i)
  16.         {
  17.             // read next int
  18.             int x; cin>>x;
  19.             // we can pair x-i with each previous appearance of x-i
  20.             ans += freq[x-i];
  21.             // increment frequency of x-i
  22.             freq[x-i]++;
  23.         }
  24.         // print answer
  25.         cout<<ans<<"\n";
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement