
array ‘A’ of size ‘N’ and asked him to find the number of twin pairs in that array.
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two spaced integers, ‘N’ denoting the size of the array.
The following line contains an array ‘A’ of ‘N’ spaced integers.
For each test case, print a single integer denoting the number of twin pairs in the array.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= N <= 10^5
1 <= A[i] <= N
Time Limit: 1 sec
We can check all the pairs of indexes using two nested loops and if they satisfy the given constraints, then increase the count of twin pairs by 1.
We are given that two indexes, ‘x’ and ‘y’ are twin pairs when:
If we rearrange the second condition we get:
‘A[y]’ - y = ‘A[x]’ - ‘x’
Now let ‘B[i]’ be equal to ‘A[i]’ - ‘i’, so the new condition for ‘x’ and ‘y’ to be a twin pair becomes:
We can use the map to store the frequency of all the values of ‘B[x]’ till ‘y’ and the frequency of ‘B[y]’ is in the map will give us the count of twin pairs index ‘y’ with all the indexes less than ‘y’.