#C13105. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a sequence of daily stock prices. Your task is to determine the best day to buy and the best day to sell the stock so as to maximize your profit. If no profit can be made, output -1 -1
.
Formally, given prices on days 0 to n-1, you need to choose two indices i and j (with i < j) such that the profit, defined by the equation:
[ \text{profit} = \text{prices}[j] - \text{prices}[i] ]
is maximized. If the maximum profit is greater than 0, output the pair i and j (buy day and sell day respectively). Otherwise, if no profitable transaction exists or the list contains fewer than two prices, output -1 -1
.
Note: Days are zero-indexed.
inputFormat
The input is read from standard input (stdin) and has the following format:
n a0 a1 ... an-1
Here, n
is an integer representing the number of days, and ai
represents the stock price on day i.
outputFormat
The output should be written to standard output (stdout). Output two space-separated integers:
buy_day sell_day
If no profitable transaction exists, output -1 -1
.
6
7 1 5 3 6 4
1 4