Given an array **prices[]** which denotes the prices of the stocks on different days, the task is to find the maximum profit possible for buying and selling the stocks on different days using transaction where at most two transactions are allowed.**Note:** You cannot engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).**Examples:**

Input:prices[] = {3, 3, 5, 0, 0, 3, 1, 4}Output:6Explanation:

Buy on Day 4 and Sell at Day 6 => Profit = 3 0 = 3

Buy on Day 7 and Sell at Day 8 => Profit = 4 1 = 3

Therefore, Total Profit = 3 + 3 = 6Input:prices[] = {1, 2, 3, 4, 5}Output:4Explanation:

Buy on Day 1 and sell at Day 6 => Profit = 5 1 = 4

Therefore, Total Profit = 4

We have already discussed this problem using Dynamic Programming in O(N) space in this article.**Efficient Approach:** The idea used here is to keep track of the two transactions at the same time. The two stocks profit is computed as follows

- Maximum Profit for the First Transaction
- Find the minimum price we have to pay from our packet (buy1) to buy 1st stock which is the price of the stock on that day or on any previous day.
- Find the maximum profit by selling the 1st stock which on the current day.

- Maximum profit for second Transaction
- The minimum price we have to pay from our pocket to buy 2 stocks will be

buy2 = price[i] - profit1, // Profit 1 is the profit from // selling the first stock

- Find the maximum profit (profit2) by selling the 2nd stock.

- The minimum price we have to pay from our pocket to buy 2 stocks will be

**Explanation with Example:**

Let us take price[] = { 3, 5, 4, 5}

We buy 1st stock on day 1

buy1 = 3.

We sell 1st stock on day 2.

profit1 = 5 – 3 = 2.

We will make a profit of 2 after buying the stock on 1st day and selling on 2nd day.

So, profit1 = 2

Now we will buy the 2nd stock on 3rd day

Stock price on 3rd day is 4

Since we already made a profit of 2, We will spend only 2 extra from our packet to buy the stock

i.e., (4 – profit1) = 2

buy2 = 2

Now, we sell 2nd stock on day 4.

Stock price on 4th day is 5.

profit2 = 5 – buy2 = 5 – 2 = 3.

Now, we can see that the final profit includes the profit of buying and selling two stocks

Below is the implementation of the above approach:

## C++

`// C++ implmenetation to find ` `// maximum possible profit ` `// with at most two transactions ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// profit with two transactions ` `// on a given list of stock prices ` `int` `maxProfit(` `int` `price[], ` `int` `n) ` `{ ` ` ` ` ` `// buy1 - Money lent to buy 1 stock ` ` ` `// profit1 - Profit after selling ` ` ` `// the 1st stock buyed. ` ` ` `// buy2 - Money lent to buy 2 stocks ` ` ` `// including profit of selling 1st stock ` ` ` `// profit2 - Profit after selling 2 stocks ` ` ` `int` `buy1, profit1, buy2, profit2; ` ` ` ` ` `// Set initial buying values to ` ` ` `// INT_MAX as we want to minimize it ` ` ` `buy1 = buy2 = INT_MAX; ` ` ` ` ` `// Set initial selling values to ` ` ` `// zero as we want to maximize it ` ` ` `profit1 = profit2 = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Money lent to buy the stock ` ` ` `// should be minimum as possible. ` ` ` `// buy1 tracks the minimum possible ` ` ` `// stock to buy from 0 to i-1. ` ` ` `buy1 = min(buy1, price[i]); ` ` ` ` ` `// Profit after selling a stock ` ` ` `// should be maximum as possible. ` ` ` `// profit1 tracks maximum possible ` ` ` `// profit we can make from 0 to i-1. ` ` ` `profit1 = max(profit1, price[i] - buy1); ` ` ` ` ` `// Now for buying the 2nd stock, ` ` ` `// we will integrate profit made ` ` ` `// from selling the 1st stock ` ` ` `buy2 = min(buy2, price[i] - profit1); ` ` ` ` ` `// Profit after selling a 2 stocks ` ` ` `// should be maximum as possible. ` ` ` `// profit2 tracks maximum possible ` ` ` `// profit we can make from 0 to i-1. ` ` ` `profit2 = max(profit2, price[i] - buy2); ` ` ` `} ` ` ` ` ` `return` `profit2; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `price[] = { 2, 30, 15, 10, 8, 25, 80 }; ` ` ` `int` `n = ` `sizeof` `(price) / ` `sizeof` `(price[0]); ` ` ` `cout << ` `"Maximum Profit = "` ` ` `<< maxProfit(price, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implmenetation to find ` `// maximum possible profit ` `// with at most two transactions ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// profit with two transactions ` `// on a given list of stock prices ` `static` `int` `maxProfit(` `int` `price[], ` `int` `n) ` `{ ` ` ` ` ` `// buy1 - Money lent to buy 1 stock ` ` ` `// profit1 - Profit after selling ` ` ` `// the 1st stock buyed. ` ` ` `// buy2 - Money lent to buy 2 stocks ` ` ` `// including profit of selling 1st stock ` ` ` `// profit2 - Profit after selling 2 stocks ` ` ` `int` `buy1, profit1, buy2, profit2; ` ` ` ` ` `// Set initial buying values to ` ` ` `// Integer.MAX_VALUE as we want to ` ` ` `// minimize it ` ` ` `buy1 = buy2 = Integer.MAX_VALUE; ` ` ` ` ` `// Set initial selling values to ` ` ` `// zero as we want to maximize it ` ` ` `profit1 = profit2 = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Money lent to buy the stock ` ` ` `// should be minimum as possible. ` ` ` `// buy1 tracks the minimum possible ` ` ` `// stock to buy from 0 to i-1. ` ` ` `buy1 = Math.min(buy1, price[i]); ` ` ` ` ` `// Profit after selling a stock ` ` ` `// should be maximum as possible. ` ` ` `// profit1 tracks maximum possible ` ` ` `// profit we can make from 0 to i-1. ` ` ` `profit1 = Math.max(profit1, price[i] - buy1); ` ` ` ` ` `// Now for buying the 2nd stock, ` ` ` `// we will integrate profit made ` ` ` `// from selling the 1st stock ` ` ` `buy2 = Math.min(buy2, price[i] - profit1); ` ` ` ` ` `// Profit after selling a 2 stocks ` ` ` `// should be maximum as possible. ` ` ` `// profit2 tracks maximum possible ` ` ` `// profit we can make from 0 to i-1. ` ` ` `profit2 = Math.max(profit2, price[i] - buy2); ` ` ` `} ` ` ` `return` `profit2; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `price[] = { ` `2` `, ` `30` `, ` `15` `, ` `10` `, ` `8` `, ` `25` `, ` `80` `}; ` ` ` `int` `n = price.length; ` ` ` ` ` `System.out.print(` `"Maximum Profit = "` `+ ` ` ` `maxProfit(price, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# implmenetation to find ` `// maximum possible profit ` `// with at most two transactions ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// profit with two transactions ` `// on a given list of stock prices ` `static` `int` `maxProfit(` `int` `[]price, ` `int` `n) ` `{ ` ` ` ` ` `// buy1 - Money lent to buy 1 stock ` ` ` `// profit1 - Profit after selling ` ` ` `// the 1st stock buyed. ` ` ` `// buy2 - Money lent to buy 2 stocks ` ` ` `// including profit of selling 1st stock ` ` ` `// profit2 - Profit after selling 2 stocks ` ` ` `int` `buy1, profit1, buy2, profit2; ` ` ` ` ` `// Set initial buying values to ` ` ` `// int.MaxValue as we want to ` ` ` `// minimize it ` ` ` `buy1 = buy2 = ` `int` `.MaxValue; ` ` ` ` ` `// Set initial selling values to ` ` ` `// zero as we want to maximize it ` ` ` `profit1 = profit2 = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Money lent to buy the stock ` ` ` `// should be minimum as possible. ` ` ` `// buy1 tracks the minimum possible ` ` ` `// stock to buy from 0 to i-1. ` ` ` `buy1 = Math.Min(buy1, price[i]); ` ` ` ` ` `// Profit after selling a stock ` ` ` `// should be maximum as possible. ` ` ` `// profit1 tracks maximum possible ` ` ` `// profit we can make from 0 to i-1. ` ` ` `profit1 = Math.Max(profit1, price[i] - buy1); ` ` ` ` ` `// Now for buying the 2nd stock, ` ` ` `// we will integrate profit made ` ` ` `// from selling the 1st stock ` ` ` `buy2 = Math.Min(buy2, price[i] - profit1); ` ` ` ` ` `// Profit after selling a 2 stocks ` ` ` `// should be maximum as possible. ` ` ` `// profit2 tracks maximum possible ` ` ` `// profit we can make from 0 to i-1. ` ` ` `profit2 = Math.Max(profit2, price[i] - buy2); ` ` ` `} ` ` ` `return` `profit2; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]price = { 2, 30, 15, 10, 8, 25, 80 }; ` ` ` `int` `n = price.Length; ` ` ` ` ` `Console.Write(` `"Maximum Profit = "` `+ ` ` ` `maxProfit(price, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

Maximum Profit = 100

**Time complexity:** O(N) **Auxiliary Space:** O(1)

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.

## Recommended Posts:

- Maximum profit by buying and selling a share at most twice
- Maximum profit by buying and selling a share at most k times
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Maximum profit from sale of wines
- Sequences of given length where every element is more than or equal to twice of previous
- Maximum sum subarray removing at most one element
- Maximum path sum in the given arrays with at most K jumps
- Maximum subarray sum by flipping signs of at most K array elements
- Maximum Subarray Sum after inverting at most two elements
- Maximum possible GCD after replacing at most one element in the given array
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Maximum subsequence sum of at most K-distant adjacent elements
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count number of paths with at-most k turns
- Counting pairs when a person can form pair with at most one
- Longest Common Subsequence with at most k changes allowed
- Number of ways to reach Nth floor by taking at-most K leaps
- Number of ways of scoring R runs in B balls with at most W wickets
- Number of ways to cut a stick of length N into in even length at most K units long pieces

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.