#C2284. Maximum Stock Trading Profit
Maximum Stock Trading Profit
Maximum Stock Trading Profit
You are given the daily stock prices of a company over a period of days. Your task is to determine the maximum possible profit that can be obtained by buying on one day and selling on a later day. More formally, you are given an array of prices, and you need to find indices i and j (with 1 ≤ i < j ≤ n) such that the profit P defined by
\( P = \text{price}_{j} - \text{price}_{i} \)
is maximized. If no profit can be made (i.e. the prices are non-increasing), output a profit of 0 and set both the buy and sell days to -1. Days are considered as 1-indexed. In the case where there are fewer than two days (i.e. the price list is empty or has one element), the answer is also 0, -1, -1.
inputFormat
The input is given from standard input (stdin) in the following format:
- The first line contains an integer n — the number of days.
- If n > 0, the second line contains n space-separated integers representing the stock prices.
If n = 0, there will be no second line, and the list of prices should be considered empty.
outputFormat
Print to standard output (stdout) three integers separated by spaces: the maximum profit, the 1-indexed day to buy, and the 1-indexed day to sell. If no profit can be made, print 0 -1 -1
.
6
7 1 5 3 6 4
5 2 5