Count subsequence of length 4 having product of the first three elements equal to the fourth element | Java 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
import java . util . * ; class GFG { static int countQuadruples ( int A [ ] , int N ) { int ans = 0 ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . containsKey ( A [ i ] ) ) ans += freq . get ( A [ i ] ) ; for ( int j = 0 ; j < i ; j ++ ) { for ( int k = 0 ; k < j ; k ++ ) { 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 ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 2 , 2 , 7 , 40 , 160 } ; int N = arr . length ; System . out . print ( countQuadruples ( arr , N ) ) ; } }