Count number of triplets with product equal to given number with duplicates allowed | The target value for which we have to find the solution ; This variable contains the total count of triplets found ; Loop from the first to the third last integer in the list ; Check if arr [ i ] is a factor of target or not . If not , skip to the next element ; Check if the pair ( arr [ i ] , arr [ j ] ) can be a part of triplet whose product is equal to the target ; Find the remaining element of the triplet ; If element is found . increment the total count of the triplets
< ? php $ target = 93 ; $ arr = array ( 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 ) ; $ length = sizeof ( $ arr ) ; $ totalCount = 0 ; for ( $ i = 0 ; $ i < $ length - 2 ; $ i ++ ) { if ( $ target % $ arr [ $ i ] == 0 ) { for ( $ j = $ i + 1 ; $ j < $ length - 1 ; $ j ++ ) { if ( $ target % ( $ arr [ $ i ] * $ arr [ $ j ] ) == 0 ) { $ toFind = $ target / ( $ arr [ $ i ] * $ arr [ $ j ] ) ; for ( $ k = $ j + 1 ; $ k < $ length ; $ k ++ ) { if ( $ arr [ $ k ] == $ toFind ) { $ totalCount ++ ; } } } } } } echo ( " Total ▁ number ▁ of ▁ triplets ▁ found ▁ : ▁ " ) ; echo ( $ totalCount ) ; ? >