Count the pairs in an array such that the difference between them and their indices is equal
Last Updated :
19 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
unordered_map< int , int > map;
for ( int i = 0; i < n; i++)
map[arr[i] - i]++;
int res = 0;
for ( auto x : map) {
int cnt = x.second;
res += ((cnt * (cnt - 1)) / 2);
}
return res;
}
int main()
{
int arr[] = { 1, 5, 6, 7, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
static int countPairs( int arr[], int n)
{
HashMap<Integer,
Integer> map = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
map.put(arr[i] - i, 0 );
for ( int i = 0 ; i < n; i++)
map.put(arr[i] - i, map.get(arr[i] - i) + 1 );
int res = 0 ;
for ( int x : map.values())
{
int cnt = x;
res += ((cnt * (cnt - 1 )) / 2 );
}
return res;
}
public static void main (String[] args)
{
int arr[] = { 1 , 5 , 6 , 7 , 9 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
map = dict ()
for i in range (n):
map [arr[i] - i] = map .get(arr[i] - i, 0 ) + 1
res = 0
for x in map :
cnt = map [x]
res + = ((cnt * (cnt - 1 )) / / 2 )
return res
arr = [ 1 , 5 , 6 , 7 , 9 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs( int []arr, int n)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
map[arr[i] - i] = 0;
for ( int i = 0; i < n; i++)
map[arr[i] - i]++;
int res = 0;
foreach (KeyValuePair< int , int > x in map)
{
int cnt = x.Value;
res += ((cnt * (cnt - 1)) / 2);
}
return res;
}
public static void Main (String []args)
{
int []arr = { 1, 5, 6, 7, 9 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
<script>
function countPairs(arr, n) {
var map = {};
for ( var i = 0; i < n; i++) map[arr[i] - i] = 0;
for ( var i = 0; i < n; i++) map[arr[i] - i]++;
var res = 0;
for (const [key, value] of Object.entries(map)) {
var cnt = value;
res += (cnt * (cnt - 1)) / 2;
}
return res;
}
var arr = [1, 5, 6, 7, 9];
var n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...