#C11770. Stock Profit Maximization
Stock Profit Maximization
Stock Profit Maximization
You are given a series of stock prices in chronological order. Your task is to compute the maximum profit achievable by buying one share of the stock on one day and selling it on a later day. In other words, you need to find two indices \(i\) and \(j\), where \(i < j\), such that the difference \(prices[j] - prices[i]\) is maximized. If no profit is possible, return 0.
Note: The profit is computed as \(\text{maxProfit} = \max_{0 \leq i < j < n} (prices[j]-prices[i])\). If the prices are constantly decreasing or if there is only one or no price, then the maximum profit is 0.
inputFormat
The input is given from stdin and has the following format:
- The first line contains a single integer \(n\) which represents the number of stock prices.
- The second line contains \(n\) space-separated integers, where each integer represents the stock price on that day.
outputFormat
Output a single integer to stdout representing the maximum profit achievable. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5