#C1500. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a list of daily stock prices. Your task is to compute the maximum profit that can be achieved by buying on one day and selling on a later day. If no profit can be made, the answer should be 0.
Formally, given an array of prices, you need to find $$\max_{0 \le i < j < n}(prices[j] - prices[i])$$ If the result is negative, output 0.
Your solution should read input from stdin
and write the output to stdout
.
inputFormat
The input consists of two lines:
- The first line contains an integer
n
representing the number of days. - The second line contains
n
space-separated integers representing the stock prices on each day.
If n
is zero, the second line may be omitted or empty.
outputFormat
Print a single integer representing the maximum profit possible. If no profit can be achieved, print 0.
## sample6
7 1 5 3 6 4
5
</p>