Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X | Function to count the number of triplets ; Iterate through all the triplets ; Rearrange the numbers in ascending order ; Check if the necessary conditions satisfy ; Increment count ; Return the answer ; Driver code
function countTriplets ( a , n , x ) { var answer = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { for ( var k = j + 1 ; k < n ; k ++ ) { var temp = [ ] ; temp . push ( a [ i ] ) ; temp . push ( a [ j ] ) ; temp . push ( a [ k ] ) ; temp . sort ( ( a , b ) => a - b ) if ( temp [ 0 ] < temp [ 1 ] && temp [ 1 ] < temp [ 2 ] && temp [ 0 ] * temp [ 1 ] * temp [ 2 ] <= x ) answer ++ ; } } } return answer ; } var A = [ 3 , 2 , 5 , 7 ] ; var N = A . length ; var X = 42 ; document . write ( countTriplets ( A , N , X ) ) ;