#K36297. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given the stock prices for N consecutive days. Your task is to compute the maximum profit that can be achieved by buying the stock on one day and selling it on a later day. If it is impossible to make a profit, output 0.
The profit is computed as \( \text{profit} = \max(\text{price}_j - \text{price}_i, \; 0) \) where \( 1 \leq i < j \leq N \).
This problem tests your ability to iterate through an array and maintain the smallest element observed so far along with the maximum profit possible as you iterate.
inputFormat
The input begins with a single integer \( T \) indicating the number of test cases. For each test case, the first line contains an integer \( N \) representing the number of days, followed by a line containing \( N \) space-separated integers, each denoting the stock price on that day.
outputFormat
For each test case, output a single integer on a new line representing the maximum profit obtainable by performing one buy and one sell transaction.
## sample2
6
7 1 5 3 6 4
5
7 6 4 3 1
5
0
</p>