Given an array arr[] consisting of N positive integers, the task is to find the number of subsequences of length 4 having product of the first three elements equal to the fourth element.
Examples:
Input: arr[] = {10, 2, 2, 7, 40, 160}
Output: 2
Explanation:
Following are the subsequences of length 4 satisfying the given criteria:
- {10, 2, 2, 40}, the product of the first three elements is 10*2*2 = 40(= fourth element).
- {2, 2, 40, 160}, the product of the first three elements is 2*2*40 = 160(= fourth element).
Therefore, the total count of subsequence is 2.
Input: arr[] = {1, 1, 1, 1}
Output: 1
Naive Approach: The simplest approach to solve the given problem is to generate all possible subsequences and count those subsequences satisfying the given criteria. After checking for all subsequences, print the total count obtained.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by finding all subsequences of length 3 and store the product of the three in the HashMap and then count the subsequence by fixing each element as the fourth element and increment the frequency of the product of triplets. Follow the steps below to solve the given problem:
- Initialize a variable, say ans as 0, that stores the count of resultant subsequences.
- Initialize an unordered map say M that stores the frequencies of the product of triplets.
- Traverse the given array using the variable i and perform the following steps:
- Increment the value of ans by arr[i] by considering it as the fourth element of the subsequence.
- Generate all possible pairs of the array over the range [0, i – 1] and store the frequency of the product of the two with arr[i] in the map M.
- After completing the above steps, print the value of ans as the resultant count of subsequences.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the total number of // subsequences satisfying the given // criteria int countQuadruples( int A[], int N) { // Stores the count of quadruplets int ans = 0; // Stores the frequency of product // of the triplet unordered_map< int , int > freq; // Traverse the given array arr[] for ( int i = 0; i < N; i++) { // Consider arr[i] as fourth // element of subsequences ans += freq[A[i]]; // Generate all possible pairs // of the array [0, i - 1] for ( int j = 0; j < i; j++) { for ( int k = 0; k < j; k++) { // Increment the frequency // of the triplet freq[A[i] * A[j] * A[k]]++; } } } // Return the total count obtained return ans; } // Driver Code int main() { int arr[] = { 10, 2, 2, 7, 40, 160 }; int N = sizeof (arr) / sizeof (arr[0]); cout << countQuadruples(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find the total number of // subsequences satisfying the given // criteria static int countQuadruples( int A[], int N) { // Stores the count of quadruplets int ans = 0 ; // Stores the frequency of product // of the triplet HashMap<Integer, Integer> freq = new HashMap<Integer, Integer>(); // Traverse the given array arr[] for ( int i = 0 ; i < N; i++) { // Consider arr[i] as fourth // element of subsequences if (freq.containsKey(A[i])) ans += freq.get(A[i]); // Generate all possible pairs // of the array [0, i - 1] for ( int j = 0 ; j < i; j++) { for ( int k = 0 ; k < j; k++) { // Increment the frequency // of the triplet if (freq.containsKey(A[i] * A[j] * A[k])) { freq.put(A[i] * A[j] * A[k], freq.get(A[i] * A[j] * A[k]) + 1 ); } else { freq.put(A[i] * A[j] * A[k], 1 ); } } } } // Return the total count obtained return ans; } // Driver Code public static void main(String[] args) { int arr[] = { 10 , 2 , 2 , 7 , 40 , 160 }; int N = arr.length; System.out.print(countQuadruples(arr, N)); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 program for the above approach # Function to find the total number of # subsequences satisfying the given # criteria def countQuadruples(A, N): # Stores the count of quadruplets ans = 0 # Stores the frequency of product # of the triplet freq = {} # Traverse the given array arr[] for i in range (N): # Consider arr[i] as fourth # element of subsequences if A[i] in freq: ans + = freq[A[i]] else : freq[A[i]] = 0 # Generate all possible pairs # of the array [0, i - 1] for j in range (i): for k in range (j): # Increment the frequency # of the triplet if A[i] * A[j] * A[k] in freq: freq[A[i] * A[j] * A[k]] + = 1 else : freq[A[i] * A[j] * A[k]] = 1 # Return the total count obtained return ans # Driver Code if __name__ = = '__main__' : arr = [ 10 , 2 , 2 , 7 , 40 , 160 ] N = len (arr) print (countQuadruples(arr, N)) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the total number of // subsequences satisfying the given // criteria static int countQuadruples( int []A, int N) { // Stores the count of quadruplets int ans = 0; // Stores the frequency of product // of the triplet Dictionary< int , int > freq = new Dictionary< int , int >(); // Traverse the given array arr[] for ( int i = 0; i < N; i++) { // Consider arr[i] as fourth // element of subsequences if (freq.ContainsKey(A[i])) ans += freq[A[i]]; else freq.Add(A[i],0); // Generate all possible pairs // of the array [0, i - 1] for ( int j = 0; j < i; j++) { for ( int k = 0; k < j; k++) { // Increment the frequency // of the triplet if (freq.ContainsKey(A[i] * A[j] * A[k])) freq[A[i] * A[j] * A[k]]++; else freq.Add(A[i] * A[j] * A[k],1); } } } // Return the total count obtained return ans; } // Driver Code public static void Main() { int []arr = { 10, 2, 2, 7, 40, 160 }; int N = arr.Length; Console.Write(countQuadruples(arr, N)); } } // This code is contributed by ipg2016107. |
Javascript
<script> // JavaScript program for the above approach; // Function to find the total number of // subsequences satisfying the given // criteria function countQuadruples(A, N) { // Stores the count of quadruplets let ans = 0; // Stores the frequency of product // of the triplet let freq = new Map(); // Traverse the given array arr[] for (let i = 0; i < N; i++) { // Consider arr[i] as fourth // element of subsequences if (freq.has(arr[i])) { ans += freq.get(A[i]); } // Generate all possible pairs // of the array [0, i - 1] for (let j = 0; j < i; j++) { for (let k = 0; k < j; k++) { // Increment the frequency // of the triplet if (freq.has(A[i] * A[j] * A[k])) { freq.set(freq.get(A[i] * A[j] * A[k]), freq.get([A[i] * A[j] * A[k]]) + 1); } else { freq.set(A[i] * A[j] * A[k], 1); } } } } // Return the total count obtained return ans; } // Driver Code let arr = [10, 2, 2, 7, 40, 160]; let N = arr.length; document.write(countQuadruples(arr, N)); // This code is contributed by Potta Lokesh </script> |
2
Time Complexity: O(N3)
Auxiliary Space: O(N3)