`{"id":"94d3e10f-281e-42c5-adbe-0c67d4aeaaea","name":"Buy And Sell Stocks - 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 required to print the maximum profit you can make if you are allowed infinite transactions.\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 infinite transactions.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n void transactions(vector<int> arr){\n\n // write your code here\n\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 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 InfiniteTransaction(arr,n):\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 InfiniteTransaction(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/HWJ9kIPpzXs?end=82","hints":[],"associated":[{"id":"2ab2c1b9-a941-445a-b762-69ec19728851","name":"Time complexity of Buy and sell Stocks-Infinite tran allowed?","slug":"time-complexity-of-buy-and-sell-stocks-infinite-tran-allowed","type":4},{"id":"3f7d1871-4089-4805-ac94-d7c64f992800","name":"What will be the answer for the input of: arr=[11, 6, 7, 19, 4, 1, 6, 18, 4]","slug":"what-will-be-the-answer-for-the-input-of-arr-11-6-7-19-4-1-6-18-4","type":4},{"id":"9fc3f205-d3cb-4906-bd08-f7243e841ca6","name":"Which of the following transactions are not allowed?","slug":"which-of-the-following-transactions-are-not-allowed","type":4},{"id":"df73ec83-0036-42d1-ade2-e735b0777dd4","name":"What will be the answer for the input of: arr=[7,1,5,3,6,4]","slug":"what-will-be-the-answer-for-the-input-of-arr-7-1-5-3-6-4","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":"0c23eea3-c9be-4ff8-8714-6384e82737f8","name":"Buy And Sell Stocks - Infinite Transactions Allowed","slug":"buy-and-sell-stocks-infinite-transactions-allowed","type":1}],"next":{"id":"022dcd2a-99b6-46d7-bd44-5bc4100f401c","name":"Buy & Sell Stock - Infinite Transactions Allowed","type":3,"slug":"buy-sell-stock-infinite-transactions-allowed"},"prev":{"id":"2ca3707b-8b2f-4076-bef0-ff516e0d8b09","name":"Buy and sell Stocks- 1 transaction allowed","type":3,"slug":"buy-and-sell-stocks-1-transaction-allowed"}}}`

# Buy And Sell Stocks - 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 required to print the maximum profit you can make if you are allowed infinite transactions. 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":"94d3e10f-281e-42c5-adbe-0c67d4aeaaea","name":"Buy And Sell Stocks - 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 required to print the maximum profit you can make if you are allowed infinite transactions.\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 infinite transactions.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n void transactions(vector<int> arr){\n\n // write your code here\n\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 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 InfiniteTransaction(arr,n):\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 InfiniteTransaction(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/HWJ9kIPpzXs?end=82","hints":[],"associated":[{"id":"2ab2c1b9-a941-445a-b762-69ec19728851","name":"Time complexity of Buy and sell Stocks-Infinite tran allowed?","slug":"time-complexity-of-buy-and-sell-stocks-infinite-tran-allowed","type":4},{"id":"3f7d1871-4089-4805-ac94-d7c64f992800","name":"What will be the answer for the input of: arr=[11, 6, 7, 19, 4, 1, 6, 18, 4]","slug":"what-will-be-the-answer-for-the-input-of-arr-11-6-7-19-4-1-6-18-4","type":4},{"id":"9fc3f205-d3cb-4906-bd08-f7243e841ca6","name":"Which of the following transactions are not allowed?","slug":"which-of-the-following-transactions-are-not-allowed","type":4},{"id":"df73ec83-0036-42d1-ade2-e735b0777dd4","name":"What will be the answer for the input of: arr=[7,1,5,3,6,4]","slug":"what-will-be-the-answer-for-the-input-of-arr-7-1-5-3-6-4","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":"0c23eea3-c9be-4ff8-8714-6384e82737f8","name":"Buy And Sell Stocks - Infinite Transactions Allowed","slug":"buy-and-sell-stocks-infinite-transactions-allowed","type":1}],"next":{"id":"022dcd2a-99b6-46d7-bd44-5bc4100f401c","name":"Buy & Sell Stock - Infinite Transactions Allowed","type":3,"slug":"buy-sell-stock-infinite-transactions-allowed"},"prev":{"id":"2ca3707b-8b2f-4076-bef0-ff516e0d8b09","name":"Buy and sell Stocks- 1 transaction allowed","type":3,"slug":"buy-and-sell-stocks-1-transaction-allowed"}}}`

Editor

# Buy And Sell Stocks - Infinite 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 infinite transactions. 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 infinite transactions.

## 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;}9 11 6 7 19 4 1 6 18 4```

### 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;}30`

Question Video

Discussions

Show Discussion

Related Resources