Buy and Sell Stock
You are Harshad Mehta’s friend. He told you the price of a particular stock for the next ‘N’ days. You can either buy or sell a stock. Also, you can only complete at most 2-transactions. Find the maximum profit that you can earn from these transactions. You can take help from Mr. Mehta as well.
Note
1. Buying a stock and then selling it is called one transaction.
2. You are not allowed to do multiple transactions at the same time. This means you have to sell the stock before buying it again.
Input Format:
The first line contains a single integer 'T' representing the number of test cases.
Then 'T' test cases follow:
The first line of each test case contains an integer 'N' denoting the number of days.
The second line of each test case contains 'N' space-separated integers, where the 'i-th' element is the price of the particular stock on the 'i-th' day.
Output Format :
For each test case, the first and only line of output contains an integer denoting the maximum profit.
The output of every test case is printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10^4
0 <= price <= 10^3
Where ‘price’ is the price of the stock on each day.
Time Limit: 1 sec
Be the first one to answer
Add answer anonymously...
Top American Express Full Stack Developer interview questions & answers
Popular interview questions of Full Stack Developer
>
American Express Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app