#K94317. Maximum Profit Calculation

    ID: 38615 Type: Default 1000ms 256MiB

Maximum Profit Calculation

Maximum Profit Calculation

Given a series of test cases, your task is to determine the maximum profit obtainable by carrying out a single buy-sell transaction for each test case. For each test case, you are provided an array of integers. The first integer in each test case denotes the number of subsequent profit values, which represent the stock value on consecutive days. You are to compute the maximum profit, which is defined as \(\max_{i < j} (P[j]-P[i])\). If no profit is possible, output 0.

Note: The first number in each test case is used to indicate the number of days (or the length of the profit array) that follow, and should not be used in the profit computation.

inputFormat

The input is read from standard input (stdin). The first line contains an integer \(T\), the number of test cases. For each test case, there is a single line that begins with an integer \(N\) which indicates the number of values that follow. The following \(N\) integers represent the profit values (or stock prices) for consecutive days. Note that the first number (\(N\)) in each test case is used solely to indicate the number of subsequent values.

Example:

2
6 7 1 5 3 6 4
7 7 6 4 3 1 2 5

outputFormat

For each test case, output a single line containing one integer — the maximum profit obtainable from a single buy-sell transaction. If it is not possible to obtain any profit, output 0.

Example:

5
4
## sample
2
6 7 1 5 3 6 4
7 7 6 4 3 1 2 5
5

4

</p>