Problem of the day
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.
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.
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.
You do not need to print anything, it has already been taken care of. Just implement the given function.
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
1
7
3 3 5 0 3 1 4
6
The maximum profit can be earned by:
Transaction 1: Buying the stock on day 4 ( price 0 ) and then selling it on day 5 ( price 3 ).
Transaction 2: Buying the stock on day 6 ( price 1 ) and then selling it on day 6 ( price 4 ).
Total profit earned will be (3 - 0) + ( 4 - 1) = 6.
1
5
5 4 3 2 1
0
It's better to not do any transaction as the prices of the stock are decreasing.