Related Articles

# Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier

• Difficulty Level : Medium
• Last Updated : 17 Jun, 2021

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++14

 `// C++ program for the above approach``#include``using` `namespace` `std;` `// Function to find the maximum profit``// by selling M number of products``void` `findMaximumProfit(``int` `arr[], ``int` `M, ``int` `N)``{``    ` `    ``// Initialize a Max-Heap to keep``    ``// track of the maximum value``    ``priority_queue<``int``> max_heap;` `    ``// Stores the maximum profit``    ``int` `maxProfit = 0;` `    ``// Traverse the array and push``    ``// all the elements in max_heap``    ``for``(``int` `i = 0; i < N; i++)``        ``max_heap.push(arr[i]);` `    ``// Iterate a loop until M > 0``    ``while` `(M > 0)``    ``{``        ` `        ``// Decrement the value``        ``// of M by 1``        ``M--;` `        ``// Pop the maximum element``        ``// from the heap``        ``int` `X = max_heap.top();``        ``max_heap.pop();``          ` `        ``// Update the maxProfit``        ``maxProfit += X;` `        ``// Push (X - 1) to max heap``        ``max_heap.push(X - 1);``    ``}` `    ``// Print the result``    ``cout<

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `    ``// Function to find the maximum profit``    ``// by selling M number of products``    ``static` `void` `findMaximumProfit(``        ``int``[] arr, ``int` `M, ``int` `N)``    ``{` `        ``// Initialize a Max-Heap to keep``        ``// track of the maximum value``        ``PriorityQueue max_heap``            ``= ``new` `PriorityQueue<>((a, b) -> b - a);` `        ``// Stores the maximum profit``        ``int` `maxProfit = ``0``;` `        ``// Traverse the array and push``        ``// all the elements in max_heap``        ``for` `(``int` `i = ``0``; i < N; i++)``            ``max_heap.add(arr[i]);` `        ``// Iterate a loop until M > 0``        ``while` `(M > ``0``) {` `            ``// Decrement the value``            ``// of M by 1``            ``M--;` `            ``// Pop the maximum element``            ``// from the heap``            ``int` `X = max_heap.poll();` `            ``// Update the maxProfit``            ``maxProfit += X;` `            ``// Push (X - 1) to max heap``            ``max_heap.add(X - ``1``);``        ``}` `        ``// Print the result``        ``System.out.println(maxProfit);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] arr = { ``4``, ``6` `};``        ``int` `M = ``4``;``        ``int` `N = arr.length;``        ``findMaximumProfit(arr, M, N);``    ``}``}`
Output:
`19`

Time Complexity: O(M * log(N))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up