Find a triplet in an array whose sum is closest to a given number | Function to return the sum of a triplet which is closest to x ; To store the closest sum ; Run three nested loops each loop for each element of triplet ; Update the closestSum ; Return the closest sum found ; Driver code
function solution ( arr , x ) { let closestSum = Number . MAX_VALUE ; for ( let i = 0 ; i < arr . length ; i ++ ) { for ( let j = i + 1 ; j < arr . length ; j ++ ) { for ( let k = j + 1 ; k < arr . length ; k ++ ) { if ( Math . abs ( x - closestSum ) > Math . abs ( x - ( arr [ i ] + arr [ j ] + arr [ k ] ) ) ) closestSum = ( arr [ i ] + arr [ j ] + arr [ k ] ) ; } } } return closestSum ; } let arr = [ - 1 , 2 , 1 , - 4 ] ; let x = 1 ; document . write ( solution ( arr , x ) ) ;