#K84577. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a list of stock prices for consecutive days. You need to determine the maximum profit you can achieve by buying on one day and selling on a later day. If no profit is possible, output 0.
More formally, given an array of prices \(P = [P_1, P_2, \dots, P_n]\), you need to compute:
\(\max_{1 \leq i < j \leq n}(P_j - P_i)\)
If this value is negative, then you should return 0.
inputFormat
The input is read from stdin
and consists of two parts:
- An integer
n
on the first line representing the number of days (prices). - A line with
n
space-separated integers, where each integer represents the stock price on that day.
If n
is 0 or no prices are provided, the output should be 0.
outputFormat
The output is written to stdout
and should be a single integer representing the maximum profit achievable. If no profit is possible, output 0.
6
7 1 5 3 6 4
5