Problem of the day
You can’t sell without buying first.
For the given array [ 2, 100, 150, 120],
The maximum profit can be achieved by buying the stock at minute 0 when its price is Rs. 2 and selling it at minute 2 when its price is Rs. 150.
So, the output will be 148.
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case contains a single integer ‘N’, representing the size of the array.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the given array.
For each test case, print a single integer representing the maximum profit you can achieve. If the maximum profit is negative, print 0.
Output for each test case will be printed in a separate line.
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 10
2 <= N <= 10^4
1 <= ARR[i] <= 10^9
Time Limit: 1 sec.
2
4
1 2 3 4
4
2 2 2 2
3
0
For the first test case, it’s optimal to buy the stock at minute 0 and sell it at minute 3 to get a maximum profit of 3.
For the second test case, the maximum profit will be 0 for all possible ways of buying and selling stock.
2
6
17 20 11 9 12 6
4
98 101 66 72
3
6