Count pairs (i, j) from an array such that i < j and arr[j] – arr[i] = X * (j – i)
Last Updated :
01 Jul, 2021
Given an array arr[] consisting of N integers and an integer X, the task is count the number of pairs (i, j) such that i < j and arr[i] – arr[j] = X * ( j – i ).
Examples:
Input: N = 5, X = 2, arr[] = {1, 5, 5, 7, 11}
Output: 4
Explanation: All the pairs (i, j) such that i < j and arr[j] – arr[i] = X * (j – i) are (0, 2), (0, 3), (1, 4) and (2, 3).
For (0, 2), arr[2] – arr[0] = 5 – 1 = 4 and x * (j – i) = 2 * (2 – 0) = 4, which satisfies the condition arr[j] – arr[i] = x * (j – i), where j = 2, i = 1 and x = 2.
Similarly, the pairs (0, 3), (1, 4) and (2, 3) also satisfy the given condition.
Input: N = 6, X = 3, arr[] = {5, 4, 8, 11, 13, 16}
Output: 4
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs from the given array and for each pair, check if the given equation is satisfied or not.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: This problem can be solved using HashMap. Follow the steps below to solve this problem:
- The given condition is arr[j] – arr[i] = X * ( j – i ). This equation can be re-written as arr[j] – arr[i] = x * j – x * i, which can be written as arr[j] – x * j = arr[i] – x * i.
- So now the condition changed to find pairs (i, j) such that i < j and arr[j] – x * j = arr[i] – x * i.
- So all those indexes for which arr[index] – x * index are same, they can be paired. Let us say after going through all iterations the count of arr[index] – x * index is y. So now choose two different indices from y indexes. Which is nothing but yC2, that is equal to (y*(y-1))/2.
- Now, Iterate in the range [0, N-1] using the variable i:
- During each iteration perform arr[index] – x * index. Simultaneously increment the count of arr[index] – x * index.
- Iterate through the map:
- During each iteration perform count = count + (y * (y – 1)) / 2., where y is the second value of map. Which is nothing but count of arr[index] – x * index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int arr[], int n, int x)
{
int count = 0;
map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[arr[i] - x * i]++;
}
for ( auto x : mp) {
int n = x.second;
count += (n * (n - 1)) / 2;
}
cout << count;
}
int main()
{
int n = 6, x = 3;
int arr[] = { 5, 4, 8, 11, 13, 16 };
countPairs(arr, n, x);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void countPairs( int arr[], int n, int x)
{
int count = 0 ;
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
mp.put(arr[i] - x * i,
mp.getOrDefault(arr[i] - x * i, 0 ) + 1 );
}
for ( int v : mp.values())
{
count += (v * (v - 1 )) / 2 ;
}
System.out.println(count);
}
public static void main(String[] args)
{
int n = 6 , x = 3 ;
int arr[] = { 5 , 4 , 8 , 11 , 13 , 16 };
countPairs(arr, n, x);
}
}
|
Python3
def countPairs(arr, n, x):
count = 0
mp = {}
for i in range (n):
if ((arr[i] - x * i) in mp):
mp[arr[i] - x * i] + = 1
else :
mp[arr[i] - x * i] = 1
for key, value in mp.items():
n = value
count + = (n * (n - 1 )) / / 2
print (count)
if __name__ = = '__main__' :
n = 6
x = 3
arr = [ 5 , 4 , 8 , 11 , 13 , 16 ]
countPairs(arr, n, x)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countPairs( int [] arr, int n, int x)
{
int count = 0;
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (!mp.ContainsKey(arr[i] - x * i))
mp[arr[i] - x * i] = 0;
mp[arr[i] - x * i] = mp[arr[i] - x * i] + 1;
}
foreach (KeyValuePair< int , int > v in mp)
{
count += (v.Value * (v.Value - 1)) / 2;
}
Console.WriteLine(count);
}
public static void Main( string [] args)
{
int n = 6, x = 3;
int [] arr = { 5, 4, 8, 11, 13, 16 };
countPairs(arr, n, x);
}
}
|
Javascript
<script>
function countPairs(arr, n, x)
{
let count = 0;
let mp = new Map();
for (let i = 0; i < n; i++) {
if (mp.has(arr[i] - x * i)) {
mp.set(arr[i] - x * i, mp.get(arr[i] - x * i) + 1)
} else {
mp.set(arr[i] - x * i, 1)
}
}
for (let x of mp) {
let n = x[1];
count += Math.floor((n * (n - 1)) / 2);
}
document.write(count);
}
let n = 6, x = 3;
let arr = [5, 4, 8, 11, 13, 16];
countPairs(arr, n, x);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...