- Related Questions & Answers
- Maximum profit by buying and selling a share at most twice
- Program to find maximum profit after buying and selling stocks at most two times in python
- Maximum profit after buying and selling the stocks in C++
- Maximize the maximum subarray sum after removing at most one element in C++
- Maximize profit when divisibility by two numbers have associated profits in C++
- Most Profit Assigning Work in C++
- Find Selling Price from given Profit Percentage and Cost in C++
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Maximize the number by rearranging bits in C++
- Find cost price from given selling price and profit or loss percentage in C++
- Maximize the sum of products of the degrees between any two vertices of the tree in C++
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Find Maximum number possible by doing at-most K swaps in C++
- Maximize a given unsigned number by swapping bits at its extreme positions in C++
- Smallest number formed by shuffling one digit at most in JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given the task is to calculate the maximum profit that can be made by selling at-most ‘M’ products.

The total number of products are ‘N’ and the cost price and the selling price of each product is given in the lists CP[] and SP[] respectively.

**Input**

N=6, M=4 CP[]={1,9,5,8,2,11} SP[]={1,15,10,16,5,20}

**Output**

28

**Explanation** − The profit obtained from selling all the products are 0,6,5,8,3,9 respectively.

So, in order to make maximum profit by selling only 4 products, the products with the highest profit need to be chosen, that is, product number 2,3,4 and 6.

Maximum profit= 6+5+8+9= 28

**Input**

N=3, M=2 CP[]={10,20,30} SP[]={19,22,38}

**Output**

17

Create an array Profit[] of type int and size ‘N’ to store the profit obtained from each product.

Create a variable Total of type int to store the final maximum profit.

Loop from i=0 till i<N

While in loop, set Profit[i] = Sp[i] – Cp[i]

Call function sort(Profit, Profit + N, greater<int>() ); to arrange the Profit[] array in descending array.

Again loop from i=0 till i<M

While in loop set an if condition, if(Profit[i]>0) to check if the value positive or not and if so then set total+=Profit[i];

return total;

#include <bits/stdc++.h> using namespace std; //Function to find profit int MaxProfit(int N, int M, int Cp[], int Sp[]){ int Profit[N]; int total = 0; //Calculating profit from each product for (int i = 0; i < N; i++) Profit[i] = Sp[i] - Cp[i]; //Arranging profit array in descending order sort(Profit, Profit + N, greater<int>()); //Adding the best M number of profits for (int i = 0; i < M; i++){ if (Profit[i] > 0) total += Profit[i]; else break; } return total; } //Main function int main(){ int MP; int N=6,M=4; int CP[] = { 1, 9, 5, 8, 2, 11 }; int SP[] = { 1, 15, 10, 16, 5, 20 }; MP = MaxProfit(N, M, CP, SP); cout<<”Maximum Profit:”<<MP; return 0; }

If we run the above code we will get the following output −

Maximum Profit: 28

Advertisements