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
using System ; using System . Collections . Generic ; class GFG { static int countQuadruples ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( A [ i ] ) ) ans += freq [ A [ i ] ] ; else freq . Add ( A [ i ] , 0 ) ; for ( int j = 0 ; j < i ; j ++ ) { for ( int k = 0 ; k < j ; k ++ ) { 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 ans ; } public static void Main ( ) { int [ ] arr = { 10 , 2 , 2 , 7 , 40 , 160 } ; int N = arr . Length ; Console . Write ( countQuadruples ( arr , N ) ) ; } }