Buy and Sell Stock - III
Given an array "prices". In "prices" the ith element is the price of the stock on the ith day. Your task is to find maximum profit at the end of the ith day. You may complete at max 2 transactions.
You can perform a transition with these conditions -
1. Not allowed to engage in more than 1 transaction at a time, which means if you have bought stock then you can buy another stock before selling the first stock.
2. If you bought a stock at ‘X’ price and sold it at ‘Y’ price then the profits ‘Y - X’.
Note:
It is not compulsory to perform an exact '2' transaction.
Input format:
The first line of input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains a single integer 'N', 'N' denotes the length of array ‘prices’.
The second line of each test case contains an 'N' space-separated integers, in which every integer denotes an element of the array "prices".
Output Format
For each test case, you need to print the maximum profit made by selling the stocks.
Note :
You do not need to print anything; it has already been taken care of.
Constraints:
1 <= T <= 50
1 <= N <= 10^4
0 <= A[i] <= 10^9
Time limit: 1 second
Anonymous
2y
very good question
thanks
Help your peers!
Add answer anonymously...
Top Cognizant Big Data Analyst interview questions & answers
Popular interview questions of Big Data Analyst
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