Tuesday, 26 Oct 2021

# Maximize profit that can be earned by selling an item among N buyers

Maximize profit that can be earned by selling an item among N buyers
Given an array arr[] of size N, the task is to find the price of the item such that the profit earned by selling the item among N buyers is maximum possible consisting of budgets of N buyers. An item can be sold to any buyer if the budget of the buyer is greater than or equal to the price of the item.
Examples:

Input: arr[] = {34, 78, 90, 15, 67}Output: 67Explanation: For the item with price 67, the number of buyers who can buy the item is 3. Therefore, the profit earned is 67 * 3 = 201, which is maximum.
Input: arr[] = {300, 50, 32, 43, 42}Output: 300

Naive Approach: Follow the steps below to solve the problem:Initialize two variables, say price and profit as 0, to store the profit by selling an item and the possible price of the item respectively.
Traverse the given array arr[] and perform the following steps:
Set the price of the item as arr[i].
Find the number of buyers whose budget is at least arr[i] by traversing the given array. Let the value be count.
If the value of count*arr[i] is greater than the profit then update the profit as count*arr[i] and the price as arr[i].

After completing the above steps, print the value of the price as the resultant price.
Below is the implementation of the above approach:

Java

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]