# Calculate Maximum profit by buying and selling a share at most twice in Python

In this article, we will understand how to calculate the maximum profit earned by trading a share at most twice in Python with some examples.

However, before going to the main topic let’s understand how the trading process works.

## Maximum profit by buying and selling a share in Python

In shares trading, a buyer buys shares and sells them to another buyer on the same day. A buyer can’t buy another share until the first transaction is complete.

Here, we have to find maximum profit earned by trading at most twice, and also the second transaction can only start after the completion of the first transaction.

Given a list of stock prices, find the maximum profit that a share trader can earn.

Input: We will input the list of stock prices.

### Some Examples:

1. Input: Price[] ={2,32,73,42,8, 5,50,87} Output: 153

Explanation:

First Transaction – Trader buys shares at price 2 and sells at price 73 because, at this price, the maximum profit is 71.

Second Transaction – Similarly, Trader buys again at price 5 (which is the least price next after selling at price 73 in the first transaction) and sell at price 87 because, at this price, the max profit is 82.

Hence, the total profit that can be earned is the sum of profit earned in first as well as second transaction i.e. (71 + 82) = 153.

2. Input: Price[] = {97,56,49,25,3,36,120} Output: 117

Explanation:

First Transaction – Trader buys shares at price 3 (the lowest price while traversing from left to right starting from array [0]) and sells at price 120 because, at this price, the maximum profit is 71.

Here, no second transaction possible.

Hence, the total profit that can be earned is the sum of profit earned in the first transaction i.e. 117.

```
3. Input: Price[] = {120,100,80,60,40,20}
Output: 0
```

Explanation:

A trader can’t make even the first transaction. Therefore no profit can be earned here.

Hence, the total profit is 0.

### Steps to solve this problem :

- Create an array of Profit[0…N-1]. Then, initialize all values to 0.
- Update Profit[i] such that it stores maximum profit earned by traversing the array of Price[ ] from right to left to get the maximum price during the first transaction.
- Update Profit[i] by taking the maximum of Profit[i-1] which represents the maximum profit earned by the first transaction and the profit earned after completion of the first transaction which can be calculated by traversing the array of price[ ] from left to right to get the minimum price.
- Return the result of Profit[N-1].

### Below is the implementation :

#Function to find maximum profit by buying and selling a share #at most twice on given stock prices[0..N-1]. def find_max_profit(Price, N): #Create an array of profit of size N. Profit = [0]*N #Initialize the last element of the array to 0. Profit[N-1] = 0 #To get the maximum profit for the first transaction only. #After this loop, Profit[i] updates the maximum profit #from the list of stock prices [i..N-1] for the #first transaction. maximum_Price = Price[N-1] for i in range(N-2,0,-1): if Price[i] > maximum_Price: maximum_Price = Price[i] Profit[i] = max(Profit[i+1], maximum_Price - Price[i]) #To get the maximum profit for the second transaction. #After this loop, the last element of profit[] stores #the result. minimum_Price = Price[0] for i in range (1,N): if Price[i] < minimum_Price: minimum_Price = Price[i] Profit[i] = max(Profit[i-1],(Price[i] - minimum_Price) + Profit[i]) result = Profit[N-1] return result input = map(int, input.split()) input = list(input) Price = input max_profit = find_max_profit(Price, len(Price)) print ("Maximum Profit is ", max_profit)

Output:

```
Input: 10 5 23 19 8 56 92
Output: Maximum Profit is 102
```

Explanation:

maximum_Price = Price[N-1] for i in range(N-2,0,-1): if Price[i] > maximum_Price: maximum_Price = Price[i] Profit[i] = max(Profit[i+1], maximum_Price - Price[i])

During the first transaction, Profit[i] can be calculated by taking the maximum of the following:

- The value of Profit[i+1] i.e. the previous maximum.
- Profit earned by buying at Price[i] i.e. the least price and selling at the maximum price to earn maximum profit.

minimum_Price = Price[0] for i in range (1,N): if Price[i] < minimum_Price: minimum_Price = Price[i] Profit[i] = max(Profit[i-1],(Price[i] - minimum_Price) + Profit[i]) result = Profit[N-1

Similarly, Profit[i] can be calculated by taking the maximum of the following :

- The value of Profit[i-1] i.e. the previous maximum.
- Profit earned by buying at the minimum price and selling at the maximum price to earn maximum profit.

Also read: How to generate a sine wave sound in Python

## Leave a Reply