# Count subsequence of length 4 having product of the first three elements equal to the fourth element

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: 2Explanation: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: 1Naive 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:Below is the implementation of the above approach:C++ #include using namespace std; int countQuadruples(int A[], int N){ int ans = 0; unordered_map freq; for (int i = 0; i < N; i++) { ans += freq[A[i]]; for (int j = 0; j < i; j++) { for (int k = 0; k < j; k++) { freq[A[i] * A[j] * A[k]]++; } } } return ans;} int main(){ int arr[] = { 10, 2, 2, 7, 40, 160 }; int N = sizeof(arr) / sizeof(arr[0]); cout