Pair with largest sum which is less than K in the array | Function to find pair with largest sum which is less than K in the array ; To store the break point ; Sort the given array ; Find the break point ; No need to look beyond i 'th index ; Find the required pair ; Print the required answer ; Driver code ; Function call
function Max_Sum ( arr , n , k ) { let p = n ; arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= k ) { p = i ; break ; } } let maxsum = 0 , a = 0 , b = 0 ; for ( let i = 0 ; i < p ; i ++ ) { for ( let j = i + 1 ; j < p ; j ++ ) { if ( arr [ i ] + arr [ j ] < k && arr [ i ] + arr [ j ] > maxsum ) { maxsum = arr [ i ] + arr [ j ] ; a = arr [ i ] ; b = arr [ j ] ; } } } document . write ( a + " " + b + " " ) ; } let arr = [ 5 , 20 , 110 , 100 , 10 ] ; let k = 85 ; let n = arr . length ; Max_Sum ( arr , n , k ) ;