Maximize Profit by trading stocks based on given rate per day
Last Updated :
13 Dec, 2022
Given an array arr[] of N positive integers which denotes the cost of selling and buying a stock on each of the N days. The task is to find the maximum profit that can be earned by buying a stock on or selling all previously bought stocks on a particular day.
Examples:
Input: arr[] = {2, 3, 5}
Output: 5
Explanation:
Price on Day 1 = 2. Therefore buy the stocks on Day 1 at this cost. Total_Spent = 2
Price on Day 2 = 3. Therefore buy the stocks on Day 2 at this cost. Total_Spent = 2 + 3 = 5
Price on Day 3 = 5. If the stocks are sold at this price, the profit will be maximum. Therefore sell the bought stocks on Day 3 at this cost. Total_gained = 5*2 = 10
Profit = 10 – 5 = 5
Input: arr[] = {8, 5, 1}
Output: 0
Explanation:
After buying any stock we can’t sell on any other day because it will lead to loss. Therefore Profit is 0.
Approach: The idea is to break the given prices of stock into different subarrays such that each subarray has the maximum value at the end of the array. Then, find the profit by subtracting each stock value from the last element in each subarray. Below are the steps:
- Traverse the array arr[] from the end and consider arr[N – 1] as the current highest price of a stock, say maxM.
- As long as the price is highest, all the stocks purchased previously will gain profit. Therefore, move towards the left in arr[] and keep adding maxM – arr[i] to profit for all indices until any index occurs with higher cost than maxM.
- If a higher price than maxM is encountered, then buying it causes a loss.So set the cost of stock for that particular day, i.e. arr[i], as the current value of maxM and repeat Step 2.
- After the array is traversed, print the sum of difference obtained in step 2 for each possible price in the arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int * prices, int n)
{
int profit = 0, currentDay = n - 1;
while (currentDay > 0) {
int day = currentDay - 1;
while (day >= 0
&& (prices[currentDay]
> prices[day])) {
profit += (prices[currentDay]
- prices[day]);
day--;
}
currentDay = day;
}
return profit;
}
int main()
{
int prices[] = { 2, 3, 5 };
int N = sizeof (prices) / sizeof (prices[0]);
cout << maxProfit(prices, N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int maxProfit( int [] prices, int n)
{
int profit = 0 , currentDay = n - 1 ;
while (currentDay > 0 )
{
int day = currentDay - 1 ;
while (day >= 0 &&
(prices[currentDay] > prices[day]))
{
profit += (prices[currentDay] -
prices[day]);
day--;
}
currentDay = day;
}
return profit;
}
public static void main(String[] args)
{
int prices[] = { 2 , 3 , 5 };
int N = prices.length;
System.out.print(maxProfit(prices, N));
}
}
|
Python3
def maxProfit(prices, n):
profit = 0
currentDay = n - 1
while (currentDay > 0 ):
day = currentDay - 1
while (day > = 0 and
(prices[currentDay] >
prices[day])):
profit + = (prices[currentDay] -
prices[day])
day - = 1
currentDay = day;
return profit;
prices = [ 2 , 3 , 5 ]
N = len (prices)
print (maxProfit(prices, N))
|
C#
using System;
class GFG{
static int maxProfit( int [] prices, int n)
{
int profit = 0, currentDay = n - 1;
while (currentDay > 0)
{
int day = currentDay - 1;
while (day >= 0 &&
(prices[currentDay] > prices[day]))
{
profit += (prices[currentDay] -
prices[day]);
day--;
}
currentDay = day;
}
return profit;
}
public static void Main(String[] args)
{
int []prices = { 2, 3, 5 };
int N = prices.Length;
Console.Write(maxProfit(prices, N));
}
}
|
Javascript
<script>
function maxProfit(prices, n)
{
let profit = 0, currentDay = n - 1;
while (currentDay > 0)
{
let day = currentDay - 1;
while (day >= 0 &&
(prices[currentDay] > prices[day]))
{
profit += (prices[currentDay] -
prices[day]);
day--;
}
currentDay = day;
}
return profit;
}
let prices = [2, 3, 5];
let N = prices.length;
document.write(maxProfit(prices, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...