# Count the pairs in an array such that the difference between them and their indices is equal

Given an array **arr[]** of size **N**, the task is to count the number of pairs **(arr[i], arr[j])** such that **arr[j] – arr[i] = j – i**.

**Examples:**

Input:arr[] = {5, 2, 7}

Output:1

The only valid pair is (arr[0], arr[2]) as 7 – 5 = 2 – 0 = 2.

Input:arr[] = {1, 2, 3, 4}

Output:6

**Approach:** A pair **(arr[i], arr[j])** is said to be valid if **(arr[j] – arr[i]) = (j – i)**, it can also be written as **(arr[j] – j) = (arr[i] – i)** which is the difference of the element with its index. Now, the task is to divide the array into groups such that every group has equal difference of the element with its index then for every group if it has **N** elements, the count of possible pairs will be **(N * (N – 1)) / 2**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of all valid pairs ` `int` `countPairs(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the frequencies ` ` ` `// of (arr[i] - i) ` ` ` `unordered_map<` `int` `, ` `int` `> map; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `map[arr[i] - i]++; ` ` ` ` ` `// To store the required count ` ` ` `int` `res = 0; ` ` ` `for` `(` `auto` `x : map) { ` ` ` `int` `cnt = x.second; ` ` ` ` ` `// If cnt is the number of elements ` ` ` `// whose differecne with their index ` ` ` `// is same then ((cnt * (cnt - 1)) / 2) ` ` ` `// such pairs are possible ` ` ` `res += ((cnt * (cnt - 1)) / 2); ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 5, 6, 7, 9 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << countPairs(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of all valid pairs ` `def` `countPairs(arr, n): ` ` ` ` ` `# To store the frequencies ` ` ` `# of (arr[i] - i) ` ` ` `map` `=` `dict` `() ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `map` `[arr[i] ` `-` `i] ` `=` `map` `.get(arr[i] ` `-` `i, ` `0` `) ` `+` `1` ` ` ` ` `# To store the required count ` ` ` `res ` `=` `0` ` ` `for` `x ` `in` `map` `: ` ` ` `cnt ` `=` `map` `[x] ` ` ` ` ` `# If cnt is the number of elements ` ` ` `# whose differecne with their index ` ` ` `# is same then ((cnt * (cnt - 1)) / 2) ` ` ` `# such pairs are possible ` ` ` `res ` `+` `=` `((cnt ` `*` `(cnt ` `-` `1` `)) ` `/` `/` `2` `) ` ` ` ` ` `return` `res ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `5` `, ` `6` `, ` `7` `, ` `9` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(countPairs(arr, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Count all distinct pairs with difference equal to k
- Count equal element pairs in the given array
- Count pairs in an array such that both elements has equal set bits
- Count of index pairs with equal elements in an array
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count pairs from two arrays having sum equal to K
- Count pairs with set bits sum equal to K
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count equal pairs from given string arrays
- Count pairs of natural numbers with GCD equal to given number
- Count pairs of characters in a string whose ASCII value difference is K
- Count numbers whose difference with N is equal to XOR with N
- Longest subarray having difference in the count of 1's and 0's equal to k
- Find two non-overlapping pairs having equal sum in an Array
- Print all pairs in an unsorted array with equal sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.