`{"id":"eb152155-c832-49c6-9eef-8465d976325b","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","description":"1. You are given a number n, representing the number of days.\r\n2. You are given n numbers, where ith number represents price of stock on ith day.\r\n3. You are give a number fee, representing the transaction fee for every transaction.\r\n4. You are required to print the maximum profit you can make if you are allowed infinite transactions, but has to pay \"fee\" for every closed transaction.\r\nNote - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).","inputFormat":"A number n\r\n.. n more elements\r\nA number fee","outputFormat":"A number representing the maximum profit you can make if you are allowed infinite transactions with transaction fee.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10\r\n0 &lt;= fee &lt;= 5","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid transactions(vector<int> arr,int fee){\n // write your code here\n}\n int main() {\n int n;\n cin>> n;\n vector<int> arr(n,0);\n for (int i = 0; i < arr.size(); i++) {\n cin>> arr[i];\n }\n int fee;\n cin>> fee;\n\n transactions(arr,fee);\n \n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def withFreeInfiniteTransaction(arr,n,fee):\n #write your code here\n\ndef main():\n n = int(input())\n array = []\n for i in range(n):\n array.append(int(input()))\n \n fee = int(input())\n withFreeInfiniteTransaction(array,n,fee)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"medium","sampleInput":"12\r\n10\r\n15\r\n17\r\n20\r\n16\r\n18\r\n22\r\n20\r\n22\r\n20\r\n23\r\n25\r\n3","sampleOutput":"13","questionVideo":"https://www.youtube.com/embed/pTQB9wbIpfU?end=85","hints":[],"associated":[{"id":"09dd0247-c971-4e76-8019-4c71741f6472","name":"Which of the following transactions are allowed?","slug":"which-of-the-following-transactions-are-allowed","type":4},{"id":"1cf099d2-f453-4795-bf0c-db19d0c17d3e","name":"What will be the output for given input?","slug":"what-will-be-the-output-for-given-input","type":4},{"id":"65da8908-6144-4ee5-9a7d-d89bd41474ef","name":"What will be the time complexity of a naive solution and efficient solution by dynamic programming for this question?","slug":"what-will-be-the-time-complexity-of-a-naive-solution-and-efficient-solution-by-dynamic-programming-for-this-question","type":4},{"id":"fe998939-255c-40d0-8c05-148ae132f442","name":"Why we return the value of ssp state of the last day as our result?","slug":"why-we-return-the-value-of-ssp-state-of-the-last-day-as-our-result","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"a4939658-c535-4a4c-ad38-e576da4e7874","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","slug":"buy-and-sell-stocks-with-transaction-fee-infinite-transactions-allowed","type":1}],"next":{"id":"26a59090-383b-4de5-a84e-bc6872b22a61","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-with-transaction-fee-infinite-transactions-allowed"},"prev":{"id":"022dcd2a-99b6-46d7-bd44-5bc4100f401c","name":"Buy & Sell Stock - Infinite Transactions Allowed","type":3,"slug":"buy-sell-stock-infinite-transactions-allowed"}}}`

# Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed

1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are give a number fee, representing the transaction fee for every transaction. 4. You are required to print the maximum profit you can make if you are allowed infinite transactions, but has to pay "fee" for every closed transaction. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).

`{"id":"eb152155-c832-49c6-9eef-8465d976325b","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","description":"1. You are given a number n, representing the number of days.\r\n2. You are given n numbers, where ith number represents price of stock on ith day.\r\n3. You are give a number fee, representing the transaction fee for every transaction.\r\n4. You are required to print the maximum profit you can make if you are allowed infinite transactions, but has to pay \"fee\" for every closed transaction.\r\nNote - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).","inputFormat":"A number n\r\n.. n more elements\r\nA number fee","outputFormat":"A number representing the maximum profit you can make if you are allowed infinite transactions with transaction fee.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10\r\n0 &lt;= fee &lt;= 5","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid transactions(vector<int> arr,int fee){\n // write your code here\n}\n int main() {\n int n;\n cin>> n;\n vector<int> arr(n,0);\n for (int i = 0; i < arr.size(); i++) {\n cin>> arr[i];\n }\n int fee;\n cin>> fee;\n\n transactions(arr,fee);\n \n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def withFreeInfiniteTransaction(arr,n,fee):\n #write your code here\n\ndef main():\n n = int(input())\n array = []\n for i in range(n):\n array.append(int(input()))\n \n fee = int(input())\n withFreeInfiniteTransaction(array,n,fee)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"medium","sampleInput":"12\r\n10\r\n15\r\n17\r\n20\r\n16\r\n18\r\n22\r\n20\r\n22\r\n20\r\n23\r\n25\r\n3","sampleOutput":"13","questionVideo":"https://www.youtube.com/embed/pTQB9wbIpfU?end=85","hints":[],"associated":[{"id":"09dd0247-c971-4e76-8019-4c71741f6472","name":"Which of the following transactions are allowed?","slug":"which-of-the-following-transactions-are-allowed","type":4},{"id":"1cf099d2-f453-4795-bf0c-db19d0c17d3e","name":"What will be the output for given input?","slug":"what-will-be-the-output-for-given-input","type":4},{"id":"65da8908-6144-4ee5-9a7d-d89bd41474ef","name":"What will be the time complexity of a naive solution and efficient solution by dynamic programming for this question?","slug":"what-will-be-the-time-complexity-of-a-naive-solution-and-efficient-solution-by-dynamic-programming-for-this-question","type":4},{"id":"fe998939-255c-40d0-8c05-148ae132f442","name":"Why we return the value of ssp state of the last day as our result?","slug":"why-we-return-the-value-of-ssp-state-of-the-last-day-as-our-result","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"a4939658-c535-4a4c-ad38-e576da4e7874","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","slug":"buy-and-sell-stocks-with-transaction-fee-infinite-transactions-allowed","type":1}],"next":{"id":"26a59090-383b-4de5-a84e-bc6872b22a61","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-with-transaction-fee-infinite-transactions-allowed"},"prev":{"id":"022dcd2a-99b6-46d7-bd44-5bc4100f401c","name":"Buy & Sell Stock - Infinite Transactions Allowed","type":3,"slug":"buy-sell-stock-infinite-transactions-allowed"}}}`

Editor

# Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed

medium

1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are give a number fee, representing the transaction fee for every transaction. 4. You are required to print the maximum profit you can make if you are allowed infinite transactions, but has to pay "fee" for every closed transaction. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).

## Constraints

0 <= n <= 20 0 <= n1, n2, .. <= 10 0 <= fee <= 5

## Format

### Input

A number n .. n more elements A number fee

### Output

A number representing the maximum profit you can make if you are allowed infinite transactions with transaction fee.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}12 10 15 17 20 16 18 22 20 22 20 23 25 3```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}13`

Question Video

Discussions

Show Discussion

Related Resources