Maximize profit that can be earned by selling an item among N buyers | Java program for 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 ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver Code
import java . util . * ; class GFG { public static int maximumProfit ( int arr [ ] ) { int ans = Integer . MIN_VALUE ; int price = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int 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 ; } public static void main ( String [ ] args ) { int arr [ ] = { 22 , 87 , 9 , 50 , 56 , 43 } ; System . out . print ( maximumProfit ( arr ) ) ; } }