{"id":"b2e03ba1-3617-47ce-9a69-42875e62efee","name":"Buy And Sell Stocks - Two 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 required to print the maximum profit you can make if you are allowed two transactions at-most.\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","outputFormat":"A number representing the maximum profit you can make if you are allowed a single transaction.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <climits>\n\nusing namespace std;\n\n\n void transactions(vector<int> arr){\n // write your code here\n }\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\n transactions(arr);\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":"import sys\nimport math\n\n\ndef twoTransactions(arr,n):\n #write your code here\n \n\ndef main():\n n = int(input())\n array = []\n for i in range(n):\n array.append(int(input()))\n\n twoTransactions(array,n)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"easy","sampleInput":"9\r\n11\r\n6\r\n7\r\n19\r\n4\r\n1\r\n6\r\n18\r\n4","sampleOutput":"30","questionVideo":"https://www.youtube.com/embed/wuzTpONbd-0?end=103","hints":[],"associated":[{"id":"0cf78cd6-4546-4ea9-aa97-d331f10f479b","name":"what is the time complexity of the DP solution?","slug":"what-is-the-time-complexity-of-the-dp-solution","type":4},{"id":"5c7c1dc8-1b86-4cc9-a171-fc2d6c78c0df","name":"How are we ensuring non overlapping transactions","slug":"how-are-we-ensuring-non-overlapping-transactions","type":4},{"id":"d332f18d-48e1-4215-a454-a6fcda869bb1","name":"what can be the max profit on first day","slug":"what-can-be-the-max-profit-on-first-day","type":4},{"id":"f07bff36-a303-4fbb-ad07-5b07b673ec5d","name":"\"BSBSBBSS\" is it a valid transaction?","slug":"bsbsbbss-is-it-a-valid-transaction","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":"3ecb8122-205d-496a-8e20-d42b3c0d10bf","name":"Buy And Sell Stocks - Two Transactions Allowed","slug":"buy-and-sell-stocks-two-transactions-allowed","type":1}],"next":{"id":"e8427129-93d8-47e4-89f0-bc072a1b419a","name":"Buy and Sell Stocks- K Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-k-transactions-allowed"},"prev":{"id":"7a35b39b-f5ce-44e7-b3b6-f3345aa3b5d8","name":"Buy and Sell Stocks- Two Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-two-transactions-allowed"}}}

Buy And Sell Stocks - Two 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 required to print the maximum profit you can make if you are allowed two transactions at-most. 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":"b2e03ba1-3617-47ce-9a69-42875e62efee","name":"Buy And Sell Stocks - Two 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 required to print the maximum profit you can make if you are allowed two transactions at-most.\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","outputFormat":"A number representing the maximum profit you can make if you are allowed a single transaction.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <climits>\n\nusing namespace std;\n\n\n void transactions(vector<int> arr){\n // write your code here\n }\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\n transactions(arr);\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":"import sys\nimport math\n\n\ndef twoTransactions(arr,n):\n #write your code here\n \n\ndef main():\n n = int(input())\n array = []\n for i in range(n):\n array.append(int(input()))\n\n twoTransactions(array,n)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"easy","sampleInput":"9\r\n11\r\n6\r\n7\r\n19\r\n4\r\n1\r\n6\r\n18\r\n4","sampleOutput":"30","questionVideo":"https://www.youtube.com/embed/wuzTpONbd-0?end=103","hints":[],"associated":[{"id":"0cf78cd6-4546-4ea9-aa97-d331f10f479b","name":"what is the time complexity of the DP solution?","slug":"what-is-the-time-complexity-of-the-dp-solution","type":4},{"id":"5c7c1dc8-1b86-4cc9-a171-fc2d6c78c0df","name":"How are we ensuring non overlapping transactions","slug":"how-are-we-ensuring-non-overlapping-transactions","type":4},{"id":"d332f18d-48e1-4215-a454-a6fcda869bb1","name":"what can be the max profit on first day","slug":"what-can-be-the-max-profit-on-first-day","type":4},{"id":"f07bff36-a303-4fbb-ad07-5b07b673ec5d","name":"\"BSBSBBSS\" is it a valid transaction?","slug":"bsbsbbss-is-it-a-valid-transaction","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":"3ecb8122-205d-496a-8e20-d42b3c0d10bf","name":"Buy And Sell Stocks - Two Transactions Allowed","slug":"buy-and-sell-stocks-two-transactions-allowed","type":1}],"next":{"id":"e8427129-93d8-47e4-89f0-bc072a1b419a","name":"Buy and Sell Stocks- K Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-k-transactions-allowed"},"prev":{"id":"7a35b39b-f5ce-44e7-b3b6-f3345aa3b5d8","name":"Buy and Sell Stocks- Two Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-two-transactions-allowed"}}}
plane

Editor


Loading...

Buy And Sell Stocks - Two Transactions Allowed

easy

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 required to print the maximum profit you can make if you are allowed two transactions at-most. 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

Format

Input

A number n .. n more elements

Output

A number representing the maximum profit you can make if you are allowed a single transaction.

Example

Sample Input

9 11 6 7 19 4 1 6 18 4

Sample Output

30

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode