Print all triplets with given sum | Prints all triplets in arr [ ] with given sum ; Driver code
function findTriplets ( 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 = [ 0 , - 1 , 2 , - 3 , 1 ] ; let n = arr . length ; findTriplets ( arr , n , - 2 ) ;