Given an array A[ ] consisting of N positive integers, the task is to find the number of triplets A[i], A[j] & A[k] in the array such that i < j < k and A[i] * A[j] = A[k].
Examples:
Input: N = 5, A[ ] = {2, 3, 4, 6, 12}
Output: 3
Explanation:
The valid triplets from the given array are:
(A[0], A[1], A[3]) = (2, 3, 6) where (2*3 = 6)
(A[0], A[3], A[4]) = (2, 6, 12) where (2*6 = 12)
(A[1], A[2], A[4]) = (3, 4, 12) where (3*4 = 12)
Hence, a total of 3 triplets exists which satisfies the given condition.
Input: N = 3, A[ ] = {1, 1, 1}
Output: 1
Explanation:
The only valid triplet is (A[0], A[1], A[2]) = (1, 1, 1)
Naive Approach:
The simplest approach to solve the problem is to generate all possible triplets and for each triplet, check if it satisfies the required condition. If found to be true, increase the count of triplets. After complete traversal of the array and generating all possible triplets, print the final count.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be optimized using Two Pointers and HashMap.
Follow the steps below to solve the problem:
- Initialize a Map to store frequencies of array elements.
- Iterate over the array in reverse, i.e. loop with a variable j in the range [N – 2, 1].
- For every j, increase the count of A[j + 1] in the map. Iterate over the range [0, j – 1] using variable i and check if A[i] * A[j] is present in the map or not.
- If A[i] * A[j] is found in the map, increase the count of triplets by the frequency of A[i] * A[j] stored in the map.
- After complete traversal of the array, print the final count.
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Returns total number of // valid triplets possible int countTriplets( int A[], int N) { // Stores the count int ans = 0; // Map to store frequency // of array elements map< int , int > map; for ( int j = N - 2; j >= 1; j--) { // Increment the frequency // of A[j+1] as it can be // a valid A[k] map[A[j + 1]]++; for ( int i = 0; i < j; i++) { int target = A[i] * A[j]; // If target exists in the map if (map.find(target) != map.end()) ans += map[target]; } } // Return the final count return ans; } // Driver Code int main() { int N = 5; int A[] = { 2, 3, 4, 6, 12 }; cout << countTriplets(A, N); return 0; } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Returns total number of // valid triplets possible static int countTriplets( int A[], int N) { // Stores the count int ans = 0 ; // Map to store frequency // of array elements HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for ( int j = N - 2 ; j >= 1 ; j--) { // Increment the frequency // of A[j+1] as it can be // a valid A[k] if (map.containsKey(A[j + 1 ])) map.put(A[j + 1 ], map.get(A[j + 1 ]) + 1 ); else map.put(A[j + 1 ], 1 ); for ( int i = 0 ; i < j; i++) { int target = A[i] * A[j]; // If target exists in the map if (map.containsKey(target)) ans += map.get(target); } } // Return the final count return ans; } // Driver Code public static void main(String[] args) { int N = 5 ; int A[] = { 2 , 3 , 4 , 6 , 12 }; System.out.print(countTriplets(A, N)); } } // This code is contributed by sapnasingh4991 |
Python3
# Python3 program for the above approach from collections import defaultdict # Returns total number of # valid triplets possible def countTriplets(A, N): # Stores the count ans = 0 # Map to store frequency # of array elements map = defaultdict( lambda : 0 ) for j in range (N - 2 , 0 , - 1 ): # Increment the frequency # of A[j+1] as it can be # a valid A[k] map [A[j + 1 ]] + = 1 for i in range (j): target = A[i] * A[j] # If target exists in the map if (target in map .keys()): ans + = map [target] # Return the final count return ans # Driver code if __name__ = = '__main__' : N = 5 A = [ 2 , 3 , 4 , 6 , 12 ] print (countTriplets(A, N)) # This code is contributed by Shivam Singh |
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG{ // Returns total number of // valid triplets possible static int countTriplets( int []A, int N) { // Stores the count int ans = 0; // Map to store frequency // of array elements Dictionary< int , int > map = new Dictionary< int , int >(); for ( int j = N - 2; j >= 1; j--) { // Increment the frequency // of A[j+1] as it can be // a valid A[k] if (map.ContainsKey(A[j + 1])) map[A[j + 1]] = map[A[j + 1]] + 1; else map.Add(A[j + 1], 1); for ( int i = 0; i < j; i++) { int target = A[i] * A[j]; // If target exists in the map if (map.ContainsKey(target)) ans += map[target]; } } // Return the readonly count return ans; } // Driver Code public static void Main(String[] args) { int N = 5; int []A = { 2, 3, 4, 6, 12 }; Console.Write(countTriplets(A, N)); } } // This code is contributed by sapnasingh4991 |
3
Time Complexity: O(N2)
Auxiliary Space: 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.