#K35352. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a series of stock prices that occur sequentially over time. Your task is to determine the maximum profit that can be achieved with exactly one buy and one sell operation, where the buy must occur before the sell.
Formally, given a sequence of prices \(p_1, p_2, \dots, p_n\), you are to find the maximum value of \(p_j - p_i\) under the condition that \(j > i\). If no profitable transaction exists, the answer should be 0.
Note: You can only perform one transaction. This means you must buy once and sell once.
inputFormat
The input is read from standard input (stdin). The first line contains an integer \(n\) representing the number of stock prices. The second line contains \(n\) space-separated integers which represent stock prices in chronological order.
outputFormat
Output a single integer to standard output (stdout), which is the maximum profit obtainable from a single buy-sell transaction. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5