Pair with largest sum which is less than K in the array | Function to find max sum less than k ; Sort the array ; While l is less than r ; Driver Code ; Function Call
function maxSum ( arr , k ) { arr . sort ( ( a , b ) => a - b ) ; var n = arr . length , l = 0 , r = n - 1 , ans = 0 ; while ( l < r ) { if ( arr [ l ] + arr [ r ] < k ) { ans = Math . max ( ans , arr [ l ] + arr [ r ] ) ; l ++ ; } else { r -- ; } } return ans ; } var A = [ 20 , 10 , 30 , 100 , 110 ] ; var k = 85 ; document . write ( maxSum ( A , k ) ) ;