Count subsequence of length 4 having product of the first three elements equal to the fourth element | 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
function countQuadruples ( A , N ) { let ans = 0 ; let freq = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { ans += freq . get ( A [ i ] ) ; } for ( let j = 0 ; j < i ; j ++ ) { for ( let k = 0 ; k < j ; k ++ ) { 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 ans ; } let arr = [ 10 , 2 , 2 , 7 , 40 , 160 ] ; let N = arr . length ; document . write ( countQuadruples ( arr , N ) ) ;