Maximize profit that can be earned by selling an item among N buyers | C # Program to implement the above approach ; 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
using System ; class GFG { public static int maximumProfit ( int [ ] arr ) { int ans = Int32 . MinValue ; int price = 0 ; Array . Sort ( arr ) ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int count = ( N - i ) ; if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void Main ( String [ ] args ) { int [ ] arr = { 22 , 87 , 9 , 50 , 56 , 43 } ; Console . WriteLine ( maximumProfit ( arr ) ) ; } }