Count of triplets in an Array such that A [ i ] * A [ j ] = A [ k ] and i < j < k | C # program to implement the above approach ; Returns total number of valid triplets possible ; Stores the count ; Map to store frequency of array elements ; Increment the frequency of A [ j + 1 ] as it can be a valid A [ k ] ; If target exists in the map ; Return the readonly count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int [ ] A , int N ) { int ans = 0 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int j = N - 2 ; j >= 1 ; j -- ) { if ( map . ContainsKey ( A [ j + 1 ] ) ) map [ A [ j + 1 ] ] = map [ A [ j + 1 ] ] + 1 ; else map . Add ( A [ j + 1 ] , 1 ) ; for ( int i = 0 ; i < j ; i ++ ) { int target = A [ i ] * A [ j ] ; if ( map . ContainsKey ( target ) ) ans += map [ target ] ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 2 , 3 , 4 , 6 , 12 } ; Console . Write ( countTriplets ( A , N ) ) ; } }