Maximize profit that can be earned by selling an item among N buyers
Last Updated :
05 Apr, 2022
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: 67
Explanation: 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:
C++
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int maximumProfit( int arr[], int n)
{
int ans = INT_MIN;
int price = 0;
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;
}
int main()
{
int arr[] = { 22, 87, 9, 50, 56, 43 };
cout<<maximumProfit(arr,6);
return 0;
}
|
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] <= 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));
}
}
|
Python3
import sys
def maximumProfit(arr, n):
ans = - sys.maxsize - 1
price = 0
for i in range (n):
count = 0
for j in range (n):
if (arr[i] < = arr[j]):
count + = 1
if (ans < count * arr[i]):
price = arr[i]
ans = count * arr[i]
return price;
if __name__ = = '__main__' :
arr = [ 22 , 87 , 9 , 50 , 56 , 43 ]
print (maximumProfit(arr, 6 ))
|
C#
using System;
class GFG{
public static int maximumProfit( int [] arr)
{
int ans = Int32.MinValue;
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 };
Console.Write(
maximumProfit(arr));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by sorting the array such that the count of elements greater than the current element can be calculated in O(1) time. 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.
- Sort the array in ascending order.
- Traverse the given array arr[i] and perform the following steps:
- Set the price of the item as arr[i].
- Now, the number of buyers whose budget is at least arr[i] is given by (N – i). 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:
C++
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int maximumProfit( int arr[], int N)
{
int ans = INT_MIN;
int price = 0;
sort(arr, arr + N);
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;
}
int main()
{
int arr[] = { 22, 87, 9, 50, 56, 43 };
cout << maximumProfit(arr,6);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maximumProfit( int arr[])
{
int ans = Integer.MIN_VALUE;
int price = 0 ;
Arrays.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 };
System.out.print(
maximumProfit(arr));
}
}
|
C#
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));
}
}
|
Python3
import sys
def maximumProfit(arr, N):
ans = - sys.maxsize - 1
price = 0
arr.sort()
for i in range (N):
count = (N - i)
if (ans < count * arr[i]):
price = arr[i]
ans = count * arr[i]
return price
if __name__ = = "__main__" :
arr = [ 22 , 87 , 9 , 50 , 56 , 43 ]
print (maximumProfit(arr, 6 ))
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...