forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1520D - Same Differences.cpp
66 lines (47 loc) · 1.02 KB
/
1520D - Same Differences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
You are given an array a of n integers. Count the number of pairs of indices (i,j) such that i<j and aj-ai=j-i.
Input
The first line contains one integer t (1=t=104). Then t test cases follow.
The first line of each test case contains one integer n (1=n=2·105).
The second line of each test case contains n integers a1,a2,…,an (1=ai=n) — array a.
It is guaranteed that the sum of n over all test cases does not exceed 2·105.
Output
For each test case output the number of pairs of indices (i,j) such that i<j and aj-ai=j-i.
Example
inputCopy
4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6
outputCopy
1
3
3
10
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
unordered_map <int, int> mp;
long long res = 0;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
res += mp[a - i]++;
}
cout << res << endl;
}
return 0;
}