#C3811. Maximum Profit from Stock Prices
Maximum Profit from Stock Prices
Maximum Profit from Stock Prices
You are given a sequence of daily stock prices. Your task is to determine the maximum profit that can be achieved by buying on one day and selling on a later day.
Given an array of integers P
representing the stock prices on successive days, you need to find two indices i and j such that i < j and the difference $P[j] - P[i]$ is maximized. If no profit can be made, return 0
.
Note: You must buy before you sell.
inputFormat
The input is given in two lines:
- The first line contains a single integer
n
— the number of days. - The second line contains
n
space-separated integers representing the stock prices.
If n
is 0, the second line may be empty.
outputFormat
Output a single integer — the maximum profit that can be achieved. If no profit is possible, output 0
.
6
7 1 5 3 6 4
5
</p>