#K37092. Maximum Price Difference
Maximum Price Difference
Maximum Price Difference
You are given a list of prices that represent the price of an item at different times. Your task is to determine the maximum positive difference between two prices such that the lower price comes before the higher price in the list. In other words, for a given array \(prices\) of length \(n\), find the maximum value of \(prices[j] - prices[i]\) with \(0 \leq i < j < n\). If no such pair exists that yields a positive difference, output \(-1\).
Example:
Input: [7, 1, 5, 3, 6, 4] Output: 5 (Explanation: 6 - 1 = 5 is the maximum positive difference.)
This problem will be presented with multiple test cases. For each test case, the first number indicates the number of prices, followed by the list of prices.
inputFormat
The first line contains an integer \(T\), the number of test cases. The description of each test case is as follows:
- The first integer of the test case is \(n\), representing the number of price entries.
- Then \(n\) space-separated integers follow, representing the prices.
All input is given via standard input (stdin).
outputFormat
For each test case, output a single integer representing the maximum difference as defined. Each result should be printed on a new line to standard output (stdout).
## sample3
6 7 1 5 3 6 4
5 9 8 7 6 5
6 2 4 1 6 2 8
5
-1
7
</p>