# Maximum profit after buying and selling stocks with transaction fees

Given an array of positive integers containing the price of stocks and transaction fee, the task is to find the maximum profit and the difference of days on which you are getting the maximum profit. **Examples:**

Input: arr[] = {6, 1, 7, 2, 8, 4} transactionFee = 2 Output: 8 1 Input: arr[] = {7, 1, 5, 3, 6, 4} transactionFee = 1 Output: 5 1

**Explanation:** Considering the first example: arr[] = {6, 1, 7, 2, 8, 4}, transactionFee = 2

- If we buy and sell on the
**same day**, we will**not get any profit**that’s why the difference between the buying and selling must be at least 1. - With the difference of
**1 day**, if we buy a stock of rupees 1 and sell it rupees 7 with the difference of day 1**which mean purchase on day 2 and sell it next day**,then after paying the transaction fee of rupees 2 i.e. 7-1-2=4, we will get profit of 4 rupees, same as if we purchase on day 4 and sell it on day 5 with the difference of day 1 then we get profit of 4 rupees. So the total profit is**8 rupees**. - With the difference of
**2 days**, we will**not get any profit**. - With the difference of
**3 days**, if we buy stock of rupees 1 and sell it rupees 8 with the difference of 3 days**which mean purchase on day 2 and sell it after 3 days**then maximum profit after paying the transaction fee of rupees 2 i.e.8-1-2=5 we will get the profit of**5 rupees.** - With the difference of
**4 days**, if we buy stocks of rupees 1 and sell it rupees 4 with the difference of 4 days**which mean purchase on day 2 and sell it after 4 days**then after paying the transaction fee of rupees 2 i.e. 4-1-2=1, we will get profit of**1 rupees**. - With the difference of
**5 days**, we will**not get any profit.**

**Approach:**

- Traverse the whole array with the difference of each day.
- Check the profit by subtracting the price of each day including transaction fee.
- Trace the maximum profit and store the diff_days on which we are getting the maximum profit.
- Repeat the above steps until the loop terminates.

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `max_profit(` `int` `a[],` `int` `b[],` `int` `n,` `int` `fee)` `{` `int` `i, j, profit;` `int` `l, r, diff_day = 1, sum = 0;` `//b[0] will contain the maximum profit` ` ` `b[0]=0; ` `//b[1] will contain the day` `//on which we are getting the maximum profit` ` ` `b[1]=diff_day;` `for` `(i=1;i<n;i++)` `{` ` ` `l=0;` ` ` `r=diff_day;` ` ` `sum=0; ` ` ` `for` `(j=n-1;j>=i;j--)` ` ` `{` ` ` `//here finding the max profit` ` ` `profit=(a[r]-a[l])-fee;` ` ` ` ` `//if we get less then or equal to zero` ` ` `// it means we are not getting the profit` ` ` `if` `(profit>0) ` ` ` `{` ` ` `sum=sum+profit;` ` ` `}` ` ` `l++;` ` ` ` ` `r++;` ` ` `}` `//check if sum is greater then maximum then store the new maximum` ` ` `if` `(b[0] < sum)` `{` ` ` `b[0] = sum;` ` ` ` ` `b[1] = diff_day;` ` ` `}` `diff_day++;` `}` `return` `0;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = { 6, 1, 7, 2, 8, 4 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `b[2];` ` ` `int` `tranFee = 2;` ` ` `max_profit(arr, b, n, tranFee);` ` ` `cout << b[0] << ` `", "` `<< b[1] << endl;` ` ` `return` `0;` `}` |

## Java

`// Java implementation of above approach` `import` `java.util.*;` `class` `solution` `{` `static` `int` `max_profit(` `int` `a[],` `int` `b[],` `int` `n,` `int` `fee)` `{` `int` `i, j, profit;` `int` `l, r, diff_day = ` `1` `, sum = ` `0` `;` `//b[0] will contain the maximum profit` ` ` `b[` `0` `]=` `0` `; ` `//b[1] will contain the day` `//on which we are getting the maximum profit` ` ` `b[` `1` `]=diff_day;` `for` `(i=` `1` `;i<n;i++)` `{` ` ` `l=` `0` `;` ` ` `r=diff_day;` ` ` `sum=` `0` `; ` ` ` `for` `(j=n-` `1` `;j>=i;j--)` ` ` `{` ` ` `//here finding the max profit` ` ` `profit=(a[r]-a[l])-fee;` ` ` ` ` `//if we get less then or equal to zero` ` ` `// it means we are not getting the profit` ` ` `if` `(profit>` `0` `) ` ` ` `{` ` ` `sum=sum+profit;` ` ` `}` ` ` `l++;` ` ` ` ` `r++;` ` ` `}` `//check if sum is greater then maximum then store the new maximum` ` ` `if` `(b[` `0` `] < sum)` `{` ` ` `b[` `0` `] = sum;` ` ` ` ` `b[` `1` `] = diff_day;` ` ` `}` `diff_day++;` `}` `return` `0` `;` `}` `// Driver code` `public` `static` `void` `main(String args[])` `{` ` ` `int` `arr[] = { ` `6` `, ` `1` `, ` `7` `, ` `2` `, ` `8` `, ` `4` `};` ` ` `int` `n = arr.length;` ` ` `int` `[] b = ` `new` `int` `[` `2` `];` ` ` `int` `tranFee = ` `2` `;` ` ` `max_profit(arr, b, n, tranFee);` ` ` `System.out.println(b[` `0` `]+` `", "` `+b[` `1` `]);` `}` `}` `//This code is contributed by Surendra_Gangwar` |

## Python3

`# Python3 implementation of above approach` `def` `max_profit(a, b, n, fee):` ` ` `i, j, profit ` `=` `1` `, n ` `-` `1` `, ` `0` ` ` ` ` `l, r, diff_day ` `=` `0` `, ` `0` `, ` `1` ` ` ` ` `# b[0] will contain the maximum profit` ` ` `b[` `0` `] ` `=` `0` ` ` ` ` `# b[1] will contain the day on which` ` ` `# we are getting the maximum profit` ` ` `b[` `1` `] ` `=` `diff_day` ` ` `for` `i ` `in` `range` `(` `1` `, n):` ` ` `l ` `=` `0` ` ` `r ` `=` `diff_day` ` ` `Sum` `=` `0` ` ` ` ` `for` `j ` `in` `range` `(n ` `-` `1` `, i ` `-` `1` `, ` `-` `1` `):` ` ` ` ` `# here finding the max profit` ` ` `profit ` `=` `(a[r] ` `-` `a[l]) ` `-` `fee` ` ` ` ` `# if we get less then or equal to zero` ` ` `# it means we are not getting the profit` ` ` `if` `(profit > ` `0` `):` ` ` `Sum` `=` `Sum` `+` `profit` ` ` ` ` `l ` `+` `=` `1` ` ` `r ` `+` `=` `1` ` ` ` ` `# check if Sum is greater then maximum` ` ` `# then store the new maximum` ` ` `if` `(b[` `0` `] < ` `Sum` `):` ` ` `b[` `0` `] ` `=` `Sum` ` ` `b[` `1` `] ` `=` `diff_day` ` ` ` ` `diff_day ` `+` `=` `1` ` ` ` ` `return` `0` ` ` `# Driver code` `arr ` `=` `[` `6` `, ` `1` `, ` `7` `, ` `2` `, ` `8` `, ` `4` `]` `n ` `=` `len` `(arr)` `b ` `=` `[` `0` `for` `i ` `in` `range` `(` `2` `)]` `tranFee ` `=` `2` `max_profit(arr, b, n, tranFee)` `print` `(b[` `0` `], ` `","` `, b[` `1` `])` `# This code is contributed by` `# Mohit kumar 29` |

## C#

`// C# implementation of above approach` `using` `System;` `class` `GFG` `{` ` ` `static` `int` `max_profit(` `int` `[]a, ` `int` `[]b,` ` ` `int` `n, ` `int` `fee)` `{` `int` `i, j, profit;` `int` `l, r, diff_day = 1, sum = 0;` `// b[0] will contain the` `// maximum profit` `b[0] = 0;` `// b[1] will contain the day on which` `// we are getting the maximum profit` `b[1] = diff_day;` `for` `(i = 1; i < n; i++)` `{` ` ` `l = 0; r = diff_day; sum = 0;` ` ` `for` `(j = n - 1; j >= i; j--)` ` ` `{` ` ` `// here finding the max profit` ` ` `profit = (a[r] - a[l]) - fee;` ` ` ` ` `// if we get less then or equal` ` ` `// to zero it means we are not` ` ` `// getting the profit` ` ` `if` `(profit > 0)` ` ` `{` ` ` `sum = sum + profit;` ` ` `}` ` ` `l++;` ` ` ` ` `r++;` ` ` `}` ` ` ` ` `// check if sum is greater then maximum` ` ` `// then store the new maximum` ` ` `if` `(b[0] < sum)` ` ` `{` ` ` `b[0] = sum;` ` ` ` ` `b[1] = diff_day;` ` ` ` ` `}` ` ` `diff_day++;` `}` `return` `0;` `}` `// Driver code` `static` `public` `void` `Main ()` `{` ` ` `int` `[]arr = { 6, 1, 7, 2, 8, 4 };` ` ` `int` `n = arr.Length;` ` ` `int` `[] b = ` `new` `int` `[2];` ` ` `int` `tranFee = 2;` ` ` ` ` `max_profit(arr, b, n, tranFee);` ` ` ` ` `Console.WriteLine(b[0] + ` `", "` `+ b[1]);` `}` `}` `// This code is contributed by Sachin` |

## PHP

`<?php` `// PHP implementation of above approach` `function` `max_profit(&` `$a` `, &` `$b` `, ` `$n` `, ` `$fee` `)` `{` ` ` `$diff_day` `= 1;` ` ` `$sum` `= 0;` ` ` ` ` `// b[0] will contain the maximum profit` ` ` `$b` `[0] = 0;` ` ` ` ` `// b[1] will contain the day on which we` ` ` `// are getting the maximum profit` ` ` `$b` `[1] = ` `$diff_day` `;` ` ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `{` ` ` `$l` `= 0;` ` ` `$r` `= ` `$diff_day` `;` ` ` `$sum` `= 0;` ` ` ` ` `for` `(` `$j` `= ` `$n` `- 1; ` `$j` `>= ` `$i` `; ` `$j` `--)` ` ` `{` ` ` `// here finding the max profit` ` ` `$profit` `= (` `$a` `[` `$r` `] - ` `$a` `[` `$l` `]) - ` `$fee` `;` ` ` ` ` `// if we get less then or equal to zero` ` ` `// it means we are not getting the profit` ` ` `if` `(` `$profit` `> 0) ` ` ` `{` ` ` `$sum` `= ` `$sum` `+ ` `$profit` `;` ` ` `}` ` ` `$l` `++;` ` ` ` ` `$r` `++;` ` ` `}` ` ` ` ` `// check if sum is greater then maximum` ` ` `// then store the new maximum` ` ` `if` `(` `$b` `[0] < ` `$sum` `)` ` ` `{` ` ` `$b` `[0] = ` `$sum` `;` ` ` ` ` `$b` `[1] = ` `$diff_day` `;` ` ` `}` ` ` `$diff_day` `++;` ` ` `}` `}` `// Driver code` `$arr` `= ` `array` `(6, 1, 7, 2, 8, 4 );` `$n` `= sizeof(` `$arr` `);` `$b` `= ` `array` `();` `$tranFee` `= 2;` `max_profit(` `$arr` `, ` `$b` `, ` `$n` `, ` `$tranFee` `);` `echo` `(` `$b` `[0]);` `echo` `(` `", "` `);` `echo` `(` `$b` `[1]);` `// This code is contributed` `// by Shivi_Aggarwal` `?>` |

## Javascript

`<script>` `// Javascript implementation of above approach` ` ` `function` `max_profit(a , b , n , fee) {` ` ` `var` `i, j, profit;` ` ` `var` `l, r, diff_day = 1, sum = 0;` ` ` `// b[0] will contain the maximum profit` ` ` `b[0] = 0;` ` ` `// b[1] will contain the day` ` ` `// on which we are getting the maximum profit` ` ` `b[1] = diff_day;` ` ` `for` `(i = 1; i < n; i++) {` ` ` `l = 0;` ` ` `r = diff_day;` ` ` `sum = 0;` ` ` `for` `(j = n - 1; j >= i; j--) {` ` ` `// here finding the max profit` ` ` `profit = (a[r] - a[l]) - fee;` ` ` `// if we get less then or equal to zero` ` ` `// it means we are not getting the profit` ` ` `if` `(profit > 0) {` ` ` `sum = sum + profit;` ` ` `}` ` ` `l++;` ` ` `r++;` ` ` `}` ` ` `// check if sum is greater then maximum` ` ` `// then store the new maximum` ` ` `if` `(b[0] < sum) {` ` ` `b[0] = sum;` ` ` `b[1] = diff_day;` ` ` `}` ` ` `diff_day++;` ` ` `}` ` ` `return` `0;` ` ` `}` ` ` `// Driver code` ` ` ` ` `var` `arr = [ 6, 1, 7, 2, 8, 4 ];` ` ` `var` `n = arr.length;` ` ` `var` `b = Array(2).fill(0);` ` ` `var` `tranFee = 2;` ` ` `max_profit(arr, b, n, tranFee);` ` ` `document.write(b[0] + ` `", "` `+ b[1]);` `// This code contributed by Rajput-Ji` `</script>` |

**Output:**

8, 1

**Time complexity:** O(N^{2})

**Better approach:**

Same as https://www.geeksforgeeks.org/stock-buy-sell/ but adds diff days as well

## Python3

`from` `typing ` `import` `List` `, ` `Tuple` `def` `max_profit(prices: ` `List` `[` `int` `], transaction_fee:` `int` `=` `0` `) ` `-` `> ` `Tuple` `[` `int` `, ` `int` `]:` ` ` `n ` `=` `len` `(prices)` ` ` `start ` `=` `0` ` ` `end ` `=` `1` ` ` `profit ` `=` `0` ` ` `max_profit_till_now ` `=` `float` `(` `'-inf'` `)` ` ` `diff ` `=` `0` ` ` `while` `start < n ` `-` `1` `and` `end < n:` ` ` `while` `start < n ` `-` `1` `and` `prices[start] > prices[start ` `+` `1` `]:` ` ` `start ` `+` `=` `1` ` ` `end ` `=` `start ` `+` `1` ` ` `while` `end < n ` `-` `1` `and` `prices[end] < prices[end ` `+` `1` `]:` ` ` `end ` `+` `=` `1` ` ` `if` `end ` `=` `=` `n:` ` ` `continue` ` ` `cur_profit ` `=` `prices[end] ` `-` `prices[start] ` `-` `transaction_fee` ` ` `if` `cur_profit > ` `0` `:` ` ` `profit ` `+` `=` `cur_profit` ` ` `if` `max_profit_till_now < cur_profit:` ` ` `max_profit_till_now ` `=` `cur_profit` ` ` `diff ` `=` `end ` `-` `start` ` ` `start ` `=` `end ` `+` `1` ` ` `return` `profit, diff` `print` `(max_profit([` `6` `, ` `1` `, ` `7` `, ` `2` `, ` `8` `, ` `4` `], ` `2` `))` |

**Output:**

(8, 1)

**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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.