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
def countQuadruples ( A , N ) : NEW_LINE INDENT ans = 0 NEW_LINE freq = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if A [ i ] in freq : NEW_LINE INDENT ans += freq [ A [ i ] ] NEW_LINE DEDENT else : NEW_LINE INDENT freq [ A [ i ] ] = 0 NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT for k in range ( j ) : NEW_LINE INDENT if A [ i ] * A [ j ] * A [ k ] in freq : NEW_LINE INDENT freq [ A [ i ] * A [ j ] * A [ k ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ A [ i ] * A [ j ] * A [ k ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 2 , 2 , 7 , 40 , 160 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countQuadruples ( arr , N ) ) NEW_LINE DEDENT