Count pairs such that difference between them and indices are different
Given an array arr[] of size N, the task is to count all pair of indices (i, j) such that i < j and j – i != arr[j] – arr[i].
Examples:
Input: arr[] = {4, 1, 3, 3}
Output: 5
Explanation:
The pair (0, 1) is a valid pair since 1 – 0 != 1 – 4.
The pair (0, 2) is a valid pair since 2 – 0 != 3 – 4, 2 != -1.
The pair (0, 3) is a valid pair since 3 – 0 != 3 – 4, 3 != -1.
The pair (1, 2) is a valid pair since 2 – 1 != 3 – 1, 1 != 2.
The pair (2, 3) is a valid pair since 3 – 2 != 3 – 3, 1 != 0.
There are a total of 5 valid pairs, so we return 5.
Input: arr = {1, 2, 3, 4, 5}
Output: 0
Explanation: There are no valid pairs.
An approach using Hashing:
As per the valid pair is concerned the value of j – i != arr[j] – arr[i], This can also be written as j – arr[j] != i – arr[i].
We’ll use map to keep the count of element that has value (i – arr[i]) occurred till ith index. If we are at ith index then the valid pair for ith element would be just (Total number of pairs till ith index – count of element that has value (i – arr[i]) till ith index).
Follow the steps below to implement the above idea:
- Initialize a map for storing the value of (i – arr[i])
- Initialize a variable result for storing the count of all valid pairs
- Iterate over the given array
- Store the count of all the previous element that has value (i – arr[i]) into a variable count, as this will act as all invalid pair with the element at the ith index.
- Add all valid pairs by removing the invalid pairs into the result (i.e, result += i – count).
- Increment the count of (i – arr[i]) into map for every ith index.
- Return the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
long long validPairs(vector< int >& arr)
{
unordered_map< int , int > unmap;
long long result = 0;
for ( int i = 0; i < arr.size(); i++) {
long long count = unmap[i - arr[i]];
result += i - count;
unmap[i - arr[i]]++;
}
return result;
}
int main()
{
vector< int > arr = { 4, 1, 3, 3 };
cout << validPairs(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static long validPairs( int [] arr)
{
HashMap<Integer, Integer> unmap = new HashMap<>();
long result = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
long count = 0 ;
if (unmap.containsKey(i - arr[i])) {
count = unmap.get(i - arr[i]);
}
result += i - count;
if (unmap.containsKey(i - arr[i])) {
unmap.put(i - arr[i],
unmap.get(i - arr[i]) + 1 );
}
else {
unmap.put(i - arr[i], 1 );
}
}
return result;
}
public static void main(String[] args)
{
int [] arr = { 4 , 1 , 3 , 3 };
System.out.print(validPairs(arr));
}
}
|
Python3
def validPairs(arr):
unmap = {}
result = 0
for i in range ( len (arr)):
count = 0
if (i - arr[i] in unmap):
count = unmap[i - arr[i]]
result = result + i - count
if (i - arr[i] in unmap):
unmap[i - arr[i]] = unmap[i - arr[i]] + 1
else :
unmap[i - arr[i]] = 1
return result
arr = [ 4 , 1 , 3 , 3 ]
print (validPairs(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static long validPairs( int [] arr)
{
Dictionary< int ,
int > unmap = new Dictionary< int , int >();
long result = 0;
for ( int i = 0; i < arr.Length; i++) {
long count = 0;
if (unmap.ContainsKey(i - arr[i])) {
count = unmap[(i - arr[i])];
}
result += i - count;
if (unmap.ContainsKey(i - arr[i])) {
unmap[i - arr[i]]++;
}
else {
unmap[i - arr[i]] = 1;
}
}
return result;
}
public static void Main( string [] args)
{
int [] arr = { 4, 1, 3, 3 };
Console.WriteLine(validPairs(arr));
}
}
|
Javascript
function validPairs(arr)
{
let unmap = new Map();
let result = 0;
for (let i = 0; i < arr.length; i++) {
let count = 0;
if (unmap.has(i - arr[i]))
count = unmap.get(i - arr[i]);
result += i - count;
if (unmap.has(i - arr[i]))
unmap.set(i - arr[i],
unmap.get(i - arr[i]) + 1);
else
unmap.set(i - arr[i], 1);
}
return result;
}
let arr = [ 4, 1, 3, 3 ];
console.log(validPairs(arr));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
02 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...