- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum profit by buying and selling a share at most twice
In a trading, one buyer buys and sells the shares, at morning and the evening respectively. If at most two transactions are allowed in a day. The second transaction can only start after the first one is completed. If stock prices are given, then find the maximum profit that the buyer can make.
Input and Output
Input: A list of stock prices. {2, 30, 15, 10, 8, 25, 80} Output: Here the total profit is 100. As buying at price 2 and selling at price 30. so profit 28. Then buy at price 8 and sell it again at price 80. So profit 72. So the total profit 28 + 72 = 100
Algorithm
findMaxProfit(pricelist, n)
Input − List of all prices, number of items in the list.
Output − Maximum Profit.
Begin define profit array of size n and fill with 0 maxPrice := pricelist[n-1] //last item is chosen for i := n-2 down to 0, do if pricelist[i] > maxPrice, then maxPrice := pricelist[i] profit[i] := maximum of profit[i+1] and maxProfit – pricelist[i] done minProce := pricelist[0] //first item is chosen for i := 1 to n-1, do if pricelist[i] < minPrice, then minPrice := pricelist[i] profit[i] := maximum of profit[i-1] and (profit[i]+(pricelist[i] - minPrice)) done return profit[n-1] End
Example
#include<iostream> using namespace std; int max(int a, int b) { return (a>b)?a:b; } int findMaxProfit(int priceList[], int n) { int *profit = new int[n]; for (int i=0; i<n; i++) //initialize profit list with 0 profit[i] = 0; int maxPrice = priceList[n-1]; //initialize with last element of price list for (int i=n-2;i>=0;i--) { if (priceList[i] > maxPrice) maxPrice = priceList[i]; profit[i] = max(profit[i+1], maxPrice - priceList[i]); //find the profit for selling in maxPrice } int minPrice = priceList[0]; //first item of priceList as minimum for (int i=1; i<n; i++) { if (priceList[i] < minPrice) minPrice = priceList[i]; profit[i] = max(profit[i-1], profit[i] + (priceList[i]- minPrice) ); } int result = profit[n-1]; return result; } int main() { int priceList[] = {2, 30, 15, 10, 8, 25, 80}; int n = 7; cout << "Maximum Profit = " << findMaxProfit(priceList, n); }
Output
Maximum Profit = 100
- Related Articles
- 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++
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Maximize the profit by selling at-most M products in C++
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Program to find the maximum profit we can get by buying on stock market once in Python
- Program to find the maximum profit we can get by buying on stock market multiple times in Python
- C++ code to find money after buying and selling shares
- Program to find maximum profit after cutting rods and selling same length rods in Python
- By selling 100 oranges, a vendor gains a profit equal to the selling price of 20 oranges. Find his gain percent
- Find Maximum number possible by doing at-most K swaps in C++
- Find Selling Price from given Profit Percentage and Cost in C++
- If cost price = Rs 400 and selling price = Rs 500, profit% =

Advertisements