Count subsequence of length 4 having product of the first three elements equal to the fourth element | C ++ program for the above approach ; Function to find the total number of subsequences satisfying the given criteria ; Stores the count of quadruplets ; Stores the frequency of product of the triplet ; Traverse the given array arr [ ] ; Consider arr [ i ] as fourth element of subsequences ; Generate all possible pairs of the array [ 0 , i - 1 ] ; Increment the frequency of the triplet ; Return the total count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countQuadruples ( int A [ ] , int N ) { int ans = 0 ; unordered_map < int , int > 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 << countQuadruples ( arr , N ) ; return 0 ; }