Given an array arr and an integer D, the task is to count the number of triplets(arr[i], arr[j], arr[k]) in the array such that:
- i < j < k
- arr[j] – arr[i] = arr[k] – arr[j] = D
Examples:
Input: arr = {1, 6, 7, 7, 8, 10, 12, 13, 14}, D = 3
Output: 2
Explanation:
There are two triplets in the array that satisfies the given criteria.
-> 1st triplet(7, 10, 13) such that i = 3, j = 6 and k = 8, such that (i < j < k) and (10 – 7 = 13 – 10 = D (=3))
-> 2nd triplet(7, 10, 13) such that i = 4, j = 6 and k = 8, such that (i < j < k) and (10 – 7 = 13 – 10 = D (=3))Input: arr = {6, 3, 4, 5}, D = 1
Output: 0
Naive Approach: The simplest approach is using three nested for loops and find the triplets that satisfy the given criteria. Below is the implementation of this approach.
C++
// C++ program to count the triplets #include<bits/stdc++.h> using namespace std; // Function to count the triplets int CountTriplets(int arr[], int d, int n) { int count = 0; // Three nested for loops to count the // triplets that satisfy the given criteria for(int i = 0; i < n - 2; i++) { for(int j = i + 1; j < n - 1; j++) { for(int k = j + 1; k < n; k++) { if ((arr[j] - arr[i] == d) && (arr[k] - arr[j] == d)) { count++; } } } } return count; } // Driver Code int main() { int A[] = { 1, 6, 7, 7, 8, 10, 12, 13, 14 }; int D = 3; int n = sizeof(A) / sizeof(A[0]); cout << (CountTriplets(A, D, n)); } // This code is contributed by chitranayal |
Java
// Java program to count the triplets class GFG { // Function to count the triplets static int CountTriplets(int[] arr, int d) { int count = 0; int n = arr.length; // Three nested for loops to count the // triplets that satisfy the given criteria for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if ((arr[j] - arr[i] == d) && (arr[k] - arr[j] == d)) { count++; } } } } return count; } // Driver Code public static void main(String args[]) { int A[] = { 1, 6, 7, 7, 8, 10, 12, 13, 14 }; int D = 3; System.out.println(CountTriplets(A, D)); } } |
Python3
# Python3 program to count the triplets # Function to count the triplets def CountTriplets(arr, d): count = 0; n = len(arr); # Three nested for loops to count the # triplets that satisfy the given criteria for i in range(n - 1): for j in range(i + 1, n - 1): for k in range(j + 1, n): if ((arr[j] - arr[i] == d) and (arr[k] - arr[j] == d)): count += 1; return count; # Driver Code if __name__ == '__main__': A = [ 1, 6, 7, 7, 8, 10, 12, 13, 14 ]; D = 3; print(CountTriplets(A, D)); # This code is contributed by Rajput-Ji |
C#
// C# program to count the triplets using System; class GFG { // Function to count the triplets static int CountTriplets(int[] arr, int d) { int count = 0; int n = arr.Length; // Three nested for loops to count the // triplets that satisfy the given criteria for (int i = 0; i < n - 2; i++) { for (int j = i + 1; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if ((arr[j] - arr[i] == d) && (arr[k] - arr[j] == d)) { count++; } } } } return count; } // Driver Code public static void Main(String []args) { int []A = { 1, 6, 7, 7, 8, 10, 12, 13, 14 }; int D = 3; Console.WriteLine(CountTriplets(A, D)); } } // This code is contributed by PrinciRaj1992 |
2
Time Complexity: O(N^3).
Efficient Approach:
- An efficient approach for this problem is to use a map to store (key, values) pair where value will be count of key.
-
The idea is to traverse the array from 0 to N and do following:
- check that A[i] – D and A[i] – 2*D are present in the map or not.
- If it’s in the map then we will simply multiply their respective values and increase answer by that.
- Now, we just have to put A[i] into the map and update the map.
Below is the implementation of the above approach.
C++14
// C++14 program to count the number // of triplets from an array. #include<bits/stdc++.h> using namespace std; // Function to count the triplets int countTriplets (int arr[], int d, int n) { int count = -1; // Create a map to store (key, values) pair. map<int, int> set; // Traverse the array and check that we // have already put (a[i]-d and a[i]-2*d) // into map or not. If yes we have to get // the values of both the keys and // multiply them, else put a[i] into the map. for(int i = 0; i < n; i++) { if ((set.find(arr[i] - d) != set.end()) && (set.find(arr[i] - 2 * d) != set.end())) { count += (set[arr[i] - d] * set[arr[i] - 2 * d]); } // Update the map if (set.find(arr[i]) == set.end()) set[arr[i]] = 1; else set[arr[i]] += 1; } return count; } // Driver Code int main() { // Test Case 1: int a1[] = { 1, 6, 7, 7, 8, 10, 12, 13, 14 }; int d1 = 3; cout << countTriplets(a1, d1, 9) << endl; // Test Case 2: int a2[] = { 6, 3, 4, 5 }; int d2 = 1; cout << countTriplets(a2, d2, 4) << endl; // Test Case 3: int a3[] = { 1, 2, 4, 5, 7, 8, 10 }; int d3 = 3; cout << countTriplets(a3, d3, 7) << endl; return 0; } // This code is contributed by himanshu77 |
Java
// Java program to count the number // of triplets from an array. import java.util.*; class GFG { // Function to count the triplets static int countTriplets(int[] arr, int d) { int count = -1; // Create a map to store (key, values) pair. Map<Integer, Integer> set = new HashMap<>(); // Traverse the array and check that we // have already put (a[i]-d and a[i]-2*d) // into map or not. If yes we have to get // the values of both the keys and // multiply them, else put a[i] into the map. for (int i = 0; i < arr.length; i++) { if (set.get(arr[i] - d) != null && set.get(arr[i] - 2 * d) != null) { count += (set.get(arr[i] - d) * set.get(arr[i] - 2 * d)); } // Update the map. if (set.get(arr[i]) == null) { set.put(arr[i], 1); } else { set.put(arr[i], set.get(arr[i]) + 1); } } return count; } // Driver Code public static void main(String args[]) { // Test Case 1: int a1[] = { 1, 6, 7, 7, 8, 10, 12, 13, 14 }; int d1 = 3; System.out.println(countTriplets(a1, d1)); // Test Case 2: int a2[] = { 6, 3, 4, 5 }; int d2 = 1; System.out.println(countTriplets(a2, d2)); // Test Case 3: int a3[] = { 1, 2, 4, 5, 7, 8, 10 }; int d3 = 3; System.out.println(countTriplets(a3, d3)); } } |
C#
// C# program to count the number // of triplets from an array. using System; using System.Collections.Generic; class GFG { // Function to count the triplets static int countTriplets(int[] arr, int d) { int count = -1; // Create a map to store (key, values) pair. Dictionary<int, int> set = new Dictionary<int, int>(); // Traverse the array and check that we // have already put (a[i]-d and a[i]-2*d) // into map or not. If yes we have to get // the values of both the keys and // multiply them, else put a[i] into the map. for (int i = 0; i < arr.Length; i++) { if (set.ContainsKey(arr[i] - d) && set.ContainsKey(arr[i] - 2 * d)) { count += (set[arr[i] - d] * set[arr[i] - 2 * d]); } // Update the map. if (!set.ContainsKey(arr[i])) { set.Add(arr[i], 1); } else { set[arr[i]] = set[arr[i]] + 1; } } return count; } // Driver Code public static void Main(String []args) { // Test Case 1: int []a1 = { 1, 6, 7, 7, 8, 10, 12, 13, 14 }; int d1 = 3; Console.WriteLine(countTriplets(a1, d1)); // Test Case 2: int []a2 = { 6, 3, 4, 5 }; int d2 = 1; Console.WriteLine(countTriplets(a2, d2)); // Test Case 3: int []a3 = { 1, 2, 4, 5, 7, 8, 10 }; int d3 = 3; Console.WriteLine(countTriplets(a3, d3)); } } // This code is contributed by Princi Singh |
1 0 2
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Count of triplets in an Array such that A[i] * A[j] = A[k] and i < j < k
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Count of triplets from the given Array such that sum of any two elements is the third element
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k]
- Find number of triplets in array such that a[i]>a[j]>a[k] and i<j<k
- Count Triplets such that one of the numbers can be written as sum of the other two
- Count the triplets such that A[i] < B[j] < C[k]
- Count triplets such that sum of any two number is equal to third | Set 2
- Count of triplets of numbers 1 to N such that middle element is always largest
- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- Count of triplets in an array that satisfy the given conditions
- Count of triplets in a given Array having GCD K
- Maximum number of unique Triplets such that each element is selected only once
- Find triplets in an array whose AND is maximum
- Count triplets with sum smaller than a given value
- Count number of triplets with product equal to given number
- Count ways to form minimum product triplets
- Count number of triplets with product equal to given number | Set 2
- Count number of triplets with product equal to given number with duplicates allowed
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

