#C4716. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a list of stock prices in chronological order. Your task is to compute the maximum profit you can obtain from one single buy and one sell operation. Note that you must buy before you sell. If no profit is possible, the answer should be 0.
The profit is calculated by:
$$\max_{0 \le i < j < n} (prices[j] - prices[i])$$
For example, given the prices [7, 1, 5, 3, 6, 4], the maximum profit is 5 (buy at 1 and sell at 6).
inputFormat
The input consists of two lines:
- The first line contains a single integer n, the number of price values.
- The second line contains n space-separated integers representing the stock prices.
outputFormat
Output a single integer representing the maximum profit obtainable from one buy and one sell operation. If no profit can be made, output 0.
## sample6
7 1 5 3 6 4
5