#K84492. Maximum Stock Transaction Profit

    ID: 36431 Type: Default 1000ms 256MiB

Maximum Stock Transaction Profit

Maximum Stock Transaction Profit

You are given a list of stock prices where the i-th element represents the stock price on day \(i\). Your task is to determine the maximum profit that can be achieved from one single buy and sell transaction.

If no profit can be made, return 0.

The profit is calculated as \(\text{price}_{sell} - \text{price}_{buy}\) with the constraint that the selling day comes after the buying day. In other words, if you buy on day \(i\) and sell on day \(j\) where \(j > i\), the profit is \(\text{prices}[j] - \text{prices}[i]\), and your goal is to maximize this value.

inputFormat

The input is read from standard input (stdin). The first line contains an integer (N) representing the number of days. The second line contains (N) space-separated integers where each integer represents the stock price for that day. (N) can be zero, which indicates an empty list of prices.

outputFormat

Output the maximum profit that can be achieved from a single buy and sell transaction. If no profit is possible, output 0. The answer is printed to standard output (stdout).## sample

6
7 1 5 3 6 4
5