Count of pairs (arr[i], arr[j]) such that arr[i] + j and arr[j] + i are equal
Last Updated :
15 Feb, 2023
Given an array arr[], the task is to count pairs i, j such that, i < j and arr[i] + j = arr[j] + i.
Examples:
Input: arr[] = {4, 1, 2, 3}
Output: 3
Explanation: In total three pairs are satisfying the given condition those are {1, 2}, {2, 3} and {1, 3}.
So, the final answer is 3.
Input: arr[] = {1, 5, 6}
Output: 1
Naive Approach: The naive approach for solving this problem is to check for each and every pair of the array for the given condition and count those pairs.
Time Complexity: O(N2), Where N is the size of arr[].
In order to get every pair we need to run two nested loops. Thus the time complexity will be O(N2).
Auxiliary Space: O(1).
As constant extra space is used.
Efficient approach: This problem can be solved by using hashmaps. At first, we can twist the condition that is given to us we can change arr[j] + i= arr[i]+ j it to arr[j] – j = arr[i] – i, which means two different numbers having the same difference in their value and index. That makes it easy, Now follow the steps below to solve the given problem.
- Create a map mp and a variable say, ans = 0, to store the answer.
- Traverse the whole array arr[] with say i.
- For each element, we will find out the difference in its value and index, simply a[i] – i.
- If there is some value present in the map that means there are other numbers with the same value so we will add those frequencies to the answer.
- Increase the value of mp[a[i] – i].
- Return ans as the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, int arr[])
{
map< int , int > mp;
int ans = 0;
for ( int i = 0; i < N; i++) {
ans += mp[arr[i] - i];
mp[arr[i] - i]++;
}
return ans;
}
int main()
{
int N = 4;
int arr[] = { 4, 1, 2, 3 };
cout << solve(N, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int solve( int N, int arr[])
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (mp.containsKey(arr[i]-i)){
ans+=mp.get(arr[i]-i);
mp.put(arr[i]-i, mp.get(arr[i]-i)+ 1 );
}
else {
mp.put(arr[i]-i, 1 );
}
}
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
int arr[] = { 4 , 1 , 2 , 3 };
System.out.print(solve(N, arr));
}
}
|
Python3
def solve(N, arr):
mp = dict ()
ans = 0
for i in range (N):
if ((arr[i] - i) not in mp):
mp[arr[i] - i] = 0
ans + = mp[arr[i] - i]
mp[arr[i] - i] = mp[arr[i] - i] + 1
return ans
N = 4
arr = [ 4 , 1 , 2 , 3 ]
print (solve(N, arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int solve( int N, int []arr)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
int ans = 0;
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(arr[i]-i)){
ans+=mp[arr[i]-i];
mp[arr[i]-i]= mp[arr[i]-i]+1;
}
else {
mp.Add(arr[i]-i, 1);
}
}
return ans;
}
public static void Main(String[] args)
{
int N = 4;
int []arr = { 4, 1, 2, 3 };
Console.Write(solve(N, arr));
}
}
|
Javascript
<script>
function solve(N, arr)
{
let mp = new Map();
let ans = 0;
for (let i = 0; i < N; i++) {
if (mp.has(arr[i] - i) == false ) {
mp.set(arr[i] - i, 0)
}
ans += mp.get(arr[i] - i);
mp.set(arr[i] - i, mp.get(arr[i] - i) + 1);
}
return ans;
}
let N = 4;
let arr = [4, 1, 2, 3];
document.write(solve(N, arr));
</script>
|
Time Complexity: , where N is the size of the array.
Auxiliary Space: O(N), where N is the size of the array.
Share your thoughts in the comments
Please Login to comment...