#C13772. Maximum Profit from Stock Prices
Maximum Profit from Stock Prices
Maximum Profit from Stock Prices
You are given a list of stock prices where the i-th element represents the price of a given stock on day \(i\). Your task is to determine the maximum profit you can achieve from a single buy-sell transaction. In other words, find two indices \(i\) and \(j\) with \(i < j\) such that the difference \(prices[j] - prices[i]\) is maximized. If no profit is possible, output 0.
The maximum profit can be expressed mathematically as:
[ \max_{0 \leq i < j < n} (prices[j] - prices[i]) ]
If there is no opportunity to make a profit, return 0.
inputFormat
The input consists of two lines:
- The first line contains an integer \(n\) which represents the number of days (or the length of the prices list).
- The second line contains \(n\) space-separated integers representing the stock prices for each day. If \(n\) is 0, the second line may be empty.
outputFormat
Output a single integer which is the maximum profit achievable from one buy and one sell transaction. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5