#K14506. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given the stock prices for the next n days. Your task is to compute the maximum profit that can be achieved by buying on one day and selling on another later day. If no profit is possible, output 0.
Note: You are allowed to complete at most one transaction (i.e., buy one and sell one share of the stock).
Mathematically, given a series of prices \(p_1, p_2, \ldots, p_n\), find the maximum \(p_j - p_i\) such that \(1 \leq i < j \leq n\). If \(p_j \leq p_i\) for every pair, the answer is 0.
inputFormat
The first line of input contains an integer n
indicating the number of days.
The second line contains n
space-separated integers representing the stock prices on these days.
If n = 0
, the list is considered empty.
outputFormat
Output a single integer indicating the maximum profit that can be achieved.
If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5
</p>