Count number of triplets with product equal to given number with duplicates allowed | C ++ program for above implementation ; 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
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int target = 93 ; int arr [ ] = { 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 } ; int length = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int totalCount = 0 ; for ( int i = 0 ; i < length - 2 ; i ++ ) { if ( target % arr [ i ] == 0 ) { for ( int j = i + 1 ; j < length - 1 ; j ++ ) { if ( target % ( arr [ i ] * arr [ j ] ) == 0 ) { int toFind = target / ( arr [ i ] * arr [ j ] ) ; for ( int k = j + 1 ; k < length ; k ++ ) { if ( arr [ k ] == toFind ) { totalCount ++ ; } } } } } } cout << " Total ▁ number ▁ of ▁ triplets ▁ found ▁ : ▁ " << totalCount ; return 0 ; }