#C8251. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
Given the daily prices of a stock, determine the maximum profit that can be achieved by making at most one transaction (i.e., buy once and sell once). If no profit can be gained, return 0. The profit is computed using the formula:
\(\text{profit} = p_j - p_i\) for some \(i < j\)
where \(p_i\) and \(p_j\) are the stock prices on day \(i\) and day \(j\) respectively.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains an integer \(n\) representing the number of days.
- The second line contains \(n\) space-separated integers where the \(i\)-th integer is the price of the stock on day \(i\).
outputFormat
The output is written to stdout
and should be a single integer representing the maximum profit that can be achieved. If no transaction is profitable, output 0.
6
7 1 5 3 6 4
5