
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximize the profit by selling at-most M products in C++
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
Approach used in the below program as follows
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;
Example
#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; }
Output
If we run the above code we will get the following output −
Maximum Profit: 28
- Related Articles
- 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
- Program to find maximum profit we can make by holding and selling profit in Python
- Maximize profit when divisibility by two numbers have associated profits in C++
- Maximize the maximum subarray sum after removing at most one element in C++
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Maximum profit after buying and selling the stocks in C++
- Maximize the total profit of all the persons X in Java
- By selling 100 oranges, a vendor gains a profit equal to the selling price of 20 oranges. Find his gain percent
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Most Profit Assigning Work in C++
- Find Selling Price from given Profit Percentage and Cost in C++
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- If cost price = Rs 400 and selling price = Rs 500, profit% =
- By selling a bedsheet for Rs $640$ a shopkeeper earns a profit of 28%. How much did the bedsheet cost the shopkeeper?
