Print triplets with sum less than k | A Simple JavaScript program to count triplets with sum smaller than a given value ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver program
function printTriplets ( arr , n , sum ) { for ( let i = 0 ; i < n - 2 ; i ++ ) { for ( let j = i + 1 ; j < n - 1 ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) document . write ( arr [ i ] + " " + arr [ j ] + " " + arr [ k ] + " " ) ; } } } let arr = [ 5 , 1 , 3 , 4 , 7 ] ; let n = arr . length ; let sum = 12 ; printTriplets ( arr , n , sum ) ;