Given an array arr[] consisting of N positive integers, such that arr[i] represents the number of products the ith supplier has and a positive integer, M, the task is to find the maximum profit by selling M products if the profit of a particular product is the same as the number of products left of that supplier.
Examples:
Input: arr[] = {4, 6}, M = 4
Output: 19
Explanation:
Below are the order of the product sell to gain the maximum profit:
Product 1: Sell a product from the second supplier, then the array modifies to {4, 5} and the profit is 6.
Product 2: Sell a product from the second supplier, then the array modifies to{4, 4} and the profit is 6 + 5 = 11.
Product 3: Sell a product from the second supplier, then the array modifies to {4, 3} and the profit is 6 + 5 + 4 = 15.
Product 4: Sell a product from the first supplier, then the array modifies to {3, 3} and the profit is 6 + 5 + 4 + 4 = 19.
Therefore, the maximum profit that can be obtained by selling 4 products is 19.
Input: arr[] = {1, 2, 3}, M = 2
Output: 5
Naive Approach: The given problem can be solved by selling the product from suppliers having the current maximum number of products left. So, the idea is to iterate a loop M times, and in each iteration find the value of the largest element in the array, and add its value to the profit and then decrementing its value in the array by 1. After the loop, print the value of the profit.
Time Complexity: O(M * N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the Max-Heap to keep track of the maximum element in the array in O(log N) time. Follow the below steps to solve the problem:
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void findMaximumProfit( int arr[], int M, int N)
{
priority_queue< int > max_heap;
int maxProfit = 0;
for ( int i = 0; i < N; i++)
max_heap.push(arr[i]);
while (M > 0)
{
M--;
int X = max_heap.top();
max_heap.pop();
maxProfit += X;
max_heap.push(X - 1);
}
cout<<maxProfit;
}
int main()
{
int arr[] = { 4, 6 };
int M = 4;
int N = sizeof (arr) / sizeof (arr[0]);
findMaximumProfit(arr, M, N);
}
|
Java
import java.util.*;
class GFG {
static void findMaximumProfit(
int [] arr, int M, int N)
{
PriorityQueue<Integer> max_heap
= new PriorityQueue<>((a, b) -> b - a);
int maxProfit = 0 ;
for ( int i = 0 ; i < N; i++)
max_heap.add(arr[i]);
while (M > 0 ) {
M--;
int X = max_heap.poll();
maxProfit += X;
max_heap.add(X - 1 );
}
System.out.println(maxProfit);
}
public static void main(String[] args)
{
int [] arr = { 4 , 6 };
int M = 4 ;
int N = arr.length;
findMaximumProfit(arr, M, N);
}
}
|
Python3
def findMaximumProfit(arr, M, N):
max_heap = []
maxProfit = 0
for i in range ( 0 , N):
max_heap.append(arr[i])
max_heap.sort()
max_heap.reverse()
while (M > 0 ):
M - = 1
X = max_heap[ 0 ]
max_heap.pop( 0 )
maxProfit + = X
max_heap.append(X - 1 )
max_heap.sort()
max_heap.reverse()
print (maxProfit)
arr = [ 4 , 6 ]
M = 4
N = len (arr)
findMaximumProfit(arr, M, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findMaximumProfit( int [] arr, int M, int N)
{
List< int > max_heap = new List< int >();
int maxProfit = 0;
for ( int i = 0; i < N; i++)
max_heap.Add(arr[i]);
max_heap.Sort();
max_heap.Reverse();
while (M > 0)
{
M--;
int X = max_heap[0];
max_heap.RemoveAt(0);
maxProfit += X;
max_heap.Add(X - 1);
max_heap.Sort();
max_heap.Reverse();
}
Console.Write(maxProfit);
}
static void Main() {
int [] arr = { 4, 6 };
int M = 4;
int N = arr.Length;
findMaximumProfit(arr, M, N);
}
}
|
Javascript
<script>
function findMaximumProfit(arr, M, N)
{
let max_heap = [];
let maxProfit = 0;
for (let i = 0; i < N; i++)
max_heap.push(arr[i]);
max_heap.sort( function (a, b){ return a - b});
max_heap.reverse();
while (M > 0)
{
M--;
let X = max_heap[0];
max_heap.shift();
maxProfit += X;
max_heap.push(X - 1);
max_heap.sort( function (a, b){ return a - b});
max_heap.reverse();
}
document.write(maxProfit);
}
let arr = [ 4, 6 ];
let M = 4;
let N = arr.length;
findMaximumProfit(arr, M, N);
</script>
|
Time Complexity: O(M * log(N))
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Aug, 2021
Like Article
Save Article