Count triplets from a sorted array having difference between adjacent elements equal to D
Last Updated :
25 May, 2021
Given a sorted array arr[] consisting of N positive integers and an integer D, the task is to find the number of triplets (i, j, k) such that arr[j] – arr[i] = D and arr[k] – arr[j] = D and 0 ? i < j < k < N.
Examples:
Input: arr[] = {1, 2, 4, 5, 7, 8, 10}, D = 3
Output: 3
Explanation:
Following are the triplets having the difference between the adjacent elements is D(= 3) are:
- {1, 4, 7}
- {4, 7, 10}
- {2, 5, 8}
Therefore, the total count of triplets is 3.
Input: arr[] = {1, 2, 4, 5, 7, 8, 10}, D = 1
Output: 0
Naive Approach: The simplest approach to solve this problem is to generate all the triplets of the given array and count those triplets having the difference between the adjacent elements is D(= 3).
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by considering every element of the array as the last element of the triplet and check for the previous two elements i.e., (arr[i] – D) and (arr[i] – 2 * D) exists in the array or not. Follow the steps below to solve the problem.
- Initialize a HashMap, say M that stores the frequency of the array elements.
- Initialize a variable, say ans as 0.
- Traverse the given array arr[] and perform the following steps:
- Increment the frequency of arr[i] by 1 in the HashMap M.
- Now, check if the element (arr[i] – D) and (arr[i] – 2 * D) are present in the HashMap or not. If found to be true, then increment the value of ans by freq[arr[i] – D] * freq[arr[i] – 2 * D].
- After completing the above steps, print the value of ans as the resultant count of triplets in the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int D, vector< int >& arr)
{
unordered_map< int , int > freq;
int ans = 0;
for ( int i = 0; i < arr.size(); i++) {
if (freq.find(arr[i] - D)
!= freq.end()
&& freq.find(arr[i] - 2 * D)
!= freq.end()) {
ans += freq[arr[i] - D]
* freq[arr[i] - 2 * D];
}
freq[arr[i]]++;
}
return ans;
}
int main()
{
vector< int > arr{ 1, 2, 4, 5, 7, 8, 10 };
int D = 1;
cout << countTriplets(D, arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countTriplets( int D, int []arr)
{
HashMap<Integer,
Integer> freq = new HashMap<Integer,
Integer>();
int ans = 0 ;
for ( int i = 0 ; i < arr.length; i++)
{
if (freq.containsKey(arr[i] - D) &&
freq.containsKey(arr[i] - 2 * D))
{
ans += freq.get(arr[i] - D) *
freq.get(arr[i] - 2 * D);
}
if (freq.containsKey(arr[i]))
{
freq.put(arr[i], freq.get(arr[i]) + 1 );
}
else
{
freq.put(arr[i], 1 );
}
}
return ans;
}
public static void main(String[] args)
{
int []arr = { 1 , 2 , 4 , 5 , 7 , 8 , 10 };
int D = 1 ;
System.out.print(countTriplets(D, arr));
}
}
|
Python3
def countTriplets(D, arr):
freq = {}
ans = 0
for i in range ( len (arr)):
if (((arr[i] - D) in freq) and
(arr[i] - 2 * D) in freq):
ans + = (freq[arr[i] - D] *
freq[arr[i] - 2 * D])
freq[arr[i]] = freq.get(arr[i], 0 ) + 1
return ans
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 5 , 7 , 8 , 10 ]
D = 1
print (countTriplets(D, arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countTriplets( int D, int [] arr)
{
Dictionary< int ,
int > freq = new Dictionary< int ,
int >();
int ans = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (freq.ContainsKey(arr[i] - D) &&
freq.ContainsKey(arr[i] - 2 * D))
{
ans += freq[arr[i] - D] *
freq[arr[i] - 2 * D];
}
if (!freq.ContainsKey(arr[i]))
freq[arr[i]] = 0;
freq[arr[i]]++;
}
return ans;
}
public static void Main()
{
int [] arr = { 1, 2, 4, 5, 7, 8, 10 };
int D = 1;
Console.WriteLine(countTriplets(D, arr));
}
}
|
Javascript
<script>
function countTriplets(D, arr)
{
var freq = new Map();
var ans = 0;
var i;
for (i = 0; i < arr.length; i++) {
if (freq.has(arr[i] - D) && freq.has(arr[i] - 2 * D)) {
ans += freq.get(arr[i] - D) * freq.get(arr[i] - 2 * D);
}
freq.set(arr[i],freq.get(arr[i])+1);
}
return ans;
}
var arr = [1, 2, 4, 5, 7, 8, 10];
var D = 1;
document.write(countTriplets(D, arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...