Maximize profit that can be earned by selling an item among N buyers | Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Sort the array ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Update the maximum profit ; Return the maximum possible price ; Driver code
function maximumProfit ( arr , N ) { let ans = Number . MIN_VALUE ; let price = 0 ; arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < N ; i ++ ) { let count = ( N - i ) ; if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } let arr = [ 22 , 87 , 9 , 50 , 56 , 43 ] ; document . write ( maximumProfit ( arr , 6 ) ) ;