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 ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver Code
function maximumProfit ( arr , n ) { var ans = - 100000 ; ; var price = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } arr = [ 22 , 87 , 9 , 50 , 56 , 43 ] ; document . write ( maximumProfit ( arr , 6 ) ) ;