`{"id":"e5a37d8e-19e9-4862-ada1-208962092e73","name":"Buy And Sell Stocks - One Transaction 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 a single transaction.","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\nusing namespace std;\n\n void Transaction(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 Transaction(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 oneTransaction(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 oneTransaction(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":"17","questionVideo":"https://www.youtube.com/embed/4YjEHmw1MX0?end=31","hints":[],"associated":[{"id":"222548b9-e979-4ca4-8be1-213879bcbdcf","name":"If the prices on 8 days are = { 11, 6, 7, 19, 4, 1, 6, 18, 4 } then what will be the maximum profit on 5th day ?","slug":"if-the-prices-on-8-days-are-11-6-7-19-4-1-6-18-4-then-what-will-be-the-maximum-profit-on-5th-day","type":4},{"id":"394bea61-c3f1-4c32-a3d8-6328c400d207","name":"Consider the array of days as [7,1,5,3,6,4]. The maximum profit can be achieved by:","slug":"consider-the-array-of-days-as-7-1-5-3-6-4-the-maximum-profit-can-be-achieved-by","type":4},{"id":"8d2c0825-d20e-441e-ac6a-fa4a1d8e0b71","name":"What's the time and space complexity for the given solution ?","slug":"what-s-the-time-and-space-complexity-for-the-given-solution","type":4},{"id":"a772eda3-2aba-4079-9f5e-5ee1fa243869","name":"The profit we can make with the necessary condition that today is the selling day can be calculated as:","slug":"the-profit-we-can-make-with-the-necessary-condition-that-today-is-the-selling-day-can-be-calculated-as","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":"7a10deee-d78f-41d3-a668-0fb708516b08","name":"Buy And Sell Stocks - One Transaction Allowed","slug":"buy-and-sell-stocks-one-transaction-allowed","type":1}],"next":{"id":"2ca3707b-8b2f-4076-bef0-ff516e0d8b09","name":"Buy and sell Stocks- 1 transaction allowed","type":3,"slug":"buy-and-sell-stocks-1-transaction-allowed"},"prev":{"id":"a6e59479-3069-4a4b-a8a5-bb5fc310a2a9","name":"Partition Into Subsets","type":3,"slug":"partition-into-subsets"}}}`

# Buy And Sell Stocks - One Transaction 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 a single transaction.

`{"id":"e5a37d8e-19e9-4862-ada1-208962092e73","name":"Buy And Sell Stocks - One Transaction 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 a single transaction.","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\nusing namespace std;\n\n void Transaction(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 Transaction(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 oneTransaction(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 oneTransaction(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":"17","questionVideo":"https://www.youtube.com/embed/4YjEHmw1MX0?end=31","hints":[],"associated":[{"id":"222548b9-e979-4ca4-8be1-213879bcbdcf","name":"If the prices on 8 days are = { 11, 6, 7, 19, 4, 1, 6, 18, 4 } then what will be the maximum profit on 5th day ?","slug":"if-the-prices-on-8-days-are-11-6-7-19-4-1-6-18-4-then-what-will-be-the-maximum-profit-on-5th-day","type":4},{"id":"394bea61-c3f1-4c32-a3d8-6328c400d207","name":"Consider the array of days as [7,1,5,3,6,4]. The maximum profit can be achieved by:","slug":"consider-the-array-of-days-as-7-1-5-3-6-4-the-maximum-profit-can-be-achieved-by","type":4},{"id":"8d2c0825-d20e-441e-ac6a-fa4a1d8e0b71","name":"What's the time and space complexity for the given solution ?","slug":"what-s-the-time-and-space-complexity-for-the-given-solution","type":4},{"id":"a772eda3-2aba-4079-9f5e-5ee1fa243869","name":"The profit we can make with the necessary condition that today is the selling day can be calculated as:","slug":"the-profit-we-can-make-with-the-necessary-condition-that-today-is-the-selling-day-can-be-calculated-as","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":"7a10deee-d78f-41d3-a668-0fb708516b08","name":"Buy And Sell Stocks - One Transaction Allowed","slug":"buy-and-sell-stocks-one-transaction-allowed","type":1}],"next":{"id":"2ca3707b-8b2f-4076-bef0-ff516e0d8b09","name":"Buy and sell Stocks- 1 transaction allowed","type":3,"slug":"buy-and-sell-stocks-1-transaction-allowed"},"prev":{"id":"a6e59479-3069-4a4b-a8a5-bb5fc310a2a9","name":"Partition Into Subsets","type":3,"slug":"partition-into-subsets"}}}`

Editor

Loading...

# Buy And Sell Stocks - One Transaction 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 a single transaction.

## 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

```.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;}17`

Question Video

Discussions

Show Discussion

Related Resources