# Maximum profit after buying and selling the stocks with transaction fees | Set 2

Given an array **arr[]** of positive integers representing **prices** of stocks and an integer **transactionFee**, the task is to find the **maximum profit** possible after buying and selling stocks any number of times and giving the transaction fee for each transaction.

**Examples:**

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

A maximum profit of 8 can be obtained by two transactions.

Transaction 1: Buy at price 1 and sell at price 7. Profit = 7 – 1 – 2 = 4.

Transaction 2: Buy at price 2 and sell at price 8. Profit = 8 – 2 – 2 = 4.

Therefore, total profit = 4 + 4 = 8, which is the maximum possible.

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

**Naive Approach:** Refer to the previous post for the simplest approach to solve the problem.

**Time Complexity: **O(N^{2})**Auxiliary Space:** O(1)

**Efficient Approach: **To optimize the above approach, the idea is to use Dynamic Programming. For each day, maintain the **maximum profit, **if stocks are bought on that day (**buy**) and the maximum profit if all stocks are sold on that day (**sell**). For each day, update **buy** and **sell** using the following relations:

buy = max(sell – arr[i], buy)sell = max(buy +arr[i] – transactionFee, sell)

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum` `// profit with transaction fee` `int` `MaxProfit(` `int` `arr[], ` `int` `n,` ` ` `int` `transactionFee)` `{` ` ` `int` `buy = -arr[0];` ` ` `int` `sell = 0;` ` ` `// Traversing the stocks for` ` ` `// each day` ` ` `for` `(` `int` `i = 1; i < n; i++) {` ` ` `int` `temp = buy;` ` ` `// Update buy and sell` ` ` `buy = max(buy, sell - arr[i]);` ` ` `sell = max(sell,` ` ` `temp + arr[i] - transactionFee);` ` ` `}` ` ` `// Return the maximum profit` ` ` `return` `max(sell, buy);` `}` `// Driver code` `int` `main()` `{` ` ` `// Given Input` ` ` `int` `arr[] = { 6, 1, 7, 2, 8, 4 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `transactionFee = 2;` ` ` `// Function Call` ` ` `cout << MaxProfit(arr, n, transactionFee);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `class` `GFG` `{` ` ` ` ` `// Function to find the maximum` ` ` `// profit with transaction fee` ` ` `static` `int` `MaxProfit(` `int` `arr[], ` `int` `n,` ` ` `int` `transactionFee)` ` ` `{` ` ` `int` `buy = -arr[` `0` `];` ` ` `int` `sell = ` `0` `;` ` ` `// Traversing the stocks for` ` ` `// each day` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) {` ` ` `int` `temp = buy;` ` ` `// Update buy and sell` ` ` `buy = Math.max(buy, sell - arr[i]);` ` ` `sell = Math.max(sell,` ` ` `temp + arr[i] - transactionFee);` ` ` `}` ` ` `// Return the maximum profit` ` ` `return` `Math.max(sell, buy);` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` ` ` `// Given Input` ` ` `int` `arr[] = { ` `6` `, ` `1` `, ` `7` `, ` `2` `, ` `8` `, ` `4` `};` ` ` `int` `n = arr.length;` ` ` `int` `transactionFee = ` `2` `;` ` ` `// Function Call` ` ` `System.out.println(` ` ` `MaxProfit(arr, n, transactionFee));` ` ` `}` `}` `// This code is contributed by Potta Lokesh` |

## Python3

`# Python3 program for the above approach` `# Function to find the maximum` `# profit with transaction fee` `def` `MaxProfit(arr, n, transactionFee):` ` ` ` ` `buy ` `=` `-` `arr[` `0` `]` ` ` `sell ` `=` `0` ` ` `# Traversing the stocks for` ` ` `# each day` ` ` `for` `i ` `in` `range` `(` `1` `, n, ` `1` `):` ` ` `temp ` `=` `buy` ` ` `# Update buy and sell` ` ` `buy ` `=` `max` `(buy, sell ` `-` `arr[i])` ` ` `sell ` `=` `max` `(sell, temp ` `+` `arr[i] ` `-` ` ` `transactionFee)` ` ` `# Return the maximum profit` ` ` `return` `max` `(sell, buy)` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Input` ` ` `arr ` `=` `[ ` `6` `, ` `1` `, ` `7` `, ` `2` `, ` `8` `, ` `4` `]` ` ` `n ` `=` `len` `(arr)` ` ` `transactionFee ` `=` `2` ` ` `# Function Call` ` ` `print` `(MaxProfit(arr, n, transactionFee))` ` ` `# This code is contributed by SURENDRA_GANGWAR` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` ` ` `// Function to find the maximum` ` ` `// profit with transaction fee` ` ` `static` `int` `MaxProfit(` `int` `[] arr, ` `int` `n,` ` ` `int` `transactionFee)` ` ` `{` ` ` `int` `buy = -arr[0];` ` ` `int` `sell = 0;` ` ` `// Traversing the stocks for` ` ` `// each day` ` ` `for` `(` `int` `i = 1; i < n; i++) {` ` ` `int` `temp = buy;` ` ` `// Update buy and sell` ` ` `buy = Math.Max(buy, sell - arr[i]);` ` ` `sell = Math.Max(sell,` ` ` `temp + arr[i] - transactionFee);` ` ` `}` ` ` `// Return the maximum profit` ` ` `return` `Math.Max(sell, buy);` ` ` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` ` ` `// Given Input` ` ` `int` `[] arr = { 6, 1, 7, 2, 8, 4 };` ` ` `int` `n = arr.Length;` ` ` `int` `transactionFee = 2;` ` ` `// Function Call` ` ` `Console.WriteLine(` ` ` `MaxProfit(arr, n, transactionFee));` ` ` `}` `}` `// This code is contributed by sanjoy_62.` |

## Javascript

`<script>` `// JavaScript program for the above approach` `// Function to find the maximum` `// profit with transaction fee` `function` `MaxProfit(arr, n, transactionFee)` `{` ` ` `let buy = -arr[0];` ` ` `let sell = 0;` ` ` `// Traversing the stocks for` ` ` `// each day` ` ` `for` `(let i = 1; i < n; i++) ` ` ` `{` ` ` `let temp = buy;` ` ` ` ` `// Update buy and sell` ` ` `buy = Math.max(buy, sell - arr[i]);` ` ` `sell = Math.max(sell,` ` ` `temp + arr[i] - transactionFee);` ` ` `}` ` ` ` ` `// Return the maximum profit` ` ` `return` `Math.max(sell, buy);` `}` `// Driver Code` `// Given Input` `let arr = [ 6, 1, 7, 2, 8, 4 ];` `let n = arr.length;` `let transactionFee = 2;` `// Function Call` `document.write(` ` ` `MaxProfit(arr, n, transactionFee));` ` ` `// This code is contributed by sanjoy_62` `</script>` |

**Output:**

8

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