`{"id":"bcbd746d-8dd8-4e6f-b171-ec35dc923882","name":"Buy And Sell Stocks - K 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 given a number k, representing the number of transactions allowed.\r\n3. You are required to print the maximum profit you can make if you are allowed k 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\r\nA number k","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\r\n0 &lt;= k &lt;= n / 2","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <climits>\n\nusing namespace std;\n\n void transactions(vector<int> arr,int k){\n //write your code here\n }\n \n int main(){\n \n int n;\n cin>>n;\n vector<int> arr(n,0);\n\n for (int i = 0; i < n; i++)\n {\n cin>>arr[i];\n }\n int k ;\n cin>>k;\n \n transactions(arr,k);\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 math\n\ndef kTransactions(arr,n,k):\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 k = int(input())\n kTransactions(array,n,k)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n9\r\n6\r\n7\r\n6\r\n3\r\n8\r\n1","sampleOutput":"5","questionVideo":"https://www.youtube.com/embed/3YILP-PdEJA?end=47","hints":[],"associated":[{"id":"28f6d011-3922-48f0-9c8e-9f3cbba6a59c","name":"What is the time complexity of Buy And Sell Stocks","slug":"what-is-the-time-complexity-of-buy-and-sell-stocks","type":4},{"id":"40a29e50-9b23-4e77-ba6a-12c4ba5cc72f","name":"What is the space complexity of Buy And Sell Stocks","slug":"what-is-the-space-complexity-of-buy-and-sell-stocks","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}],"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":"9f37d622-050a-4077-99d7-cd6ca71ebcc8","name":"Buy And Sell Stocks - K Transactions Allowed","slug":"buy-and-sell-stocks-k-transactions-allowed","type":1}],"next":{"id":"7a35b39b-f5ce-44e7-b3b6-f3345aa3b5d8","name":"Buy and Sell Stocks- Two Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-two-transactions-allowed"},"prev":{"id":"046350af-067c-43d3-87d8-4a54d35307b8","name":"Buy & Sell Stock with Cooldown - Infinite Transaction Allowed","type":3,"slug":"buy-sell-stock-with-cooldown-infinite-transaction-allowed"}}}`

# Buy And Sell Stocks - K 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 given a number k, representing the number of transactions allowed. 3. You are required to print the maximum profit you can make if you are allowed k 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":"bcbd746d-8dd8-4e6f-b171-ec35dc923882","name":"Buy And Sell Stocks - K 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 given a number k, representing the number of transactions allowed.\r\n3. You are required to print the maximum profit you can make if you are allowed k 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\r\nA number k","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\r\n0 &lt;= k &lt;= n / 2","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n#include <climits>\n\nusing namespace std;\n\n void transactions(vector<int> arr,int k){\n //write your code here\n }\n \n int main(){\n \n int n;\n cin>>n;\n vector<int> arr(n,0);\n\n for (int i = 0; i < n; i++)\n {\n cin>>arr[i];\n }\n int k ;\n cin>>k;\n \n transactions(arr,k);\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 math\n\ndef kTransactions(arr,n,k):\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 k = int(input())\n kTransactions(array,n,k)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n9\r\n6\r\n7\r\n6\r\n3\r\n8\r\n1","sampleOutput":"5","questionVideo":"https://www.youtube.com/embed/3YILP-PdEJA?end=47","hints":[],"associated":[{"id":"28f6d011-3922-48f0-9c8e-9f3cbba6a59c","name":"What is the time complexity of Buy And Sell Stocks","slug":"what-is-the-time-complexity-of-buy-and-sell-stocks","type":4},{"id":"40a29e50-9b23-4e77-ba6a-12c4ba5cc72f","name":"What is the space complexity of Buy And Sell Stocks","slug":"what-is-the-space-complexity-of-buy-and-sell-stocks","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}],"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":"9f37d622-050a-4077-99d7-cd6ca71ebcc8","name":"Buy And Sell Stocks - K Transactions Allowed","slug":"buy-and-sell-stocks-k-transactions-allowed","type":1}],"next":{"id":"7a35b39b-f5ce-44e7-b3b6-f3345aa3b5d8","name":"Buy and Sell Stocks- Two Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-two-transactions-allowed"},"prev":{"id":"046350af-067c-43d3-87d8-4a54d35307b8","name":"Buy & Sell Stock with Cooldown - Infinite Transaction Allowed","type":3,"slug":"buy-sell-stock-with-cooldown-infinite-transaction-allowed"}}}`

Editor

# Buy And Sell Stocks - K 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 given a number k, representing the number of transactions allowed. 3. You are required to print the maximum profit you can make if you are allowed k 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 0 <= k <= n / 2

## Format

### Input

A number n .. n more elements A number k

### Output

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

## 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;}6 9 6 7 6 3 8 1```

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

Question Video

Discussions

Show Discussion

Related Resources