#C11060. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a sequence of daily stock prices. Your task is to determine the maximum profit possible by buying on one day and selling on a later day. Formally, if the stock prices are given by (P_1, P_2, \dots, P_n), you need to compute (\max_{1 \leq i < j \leq n} (P_j - P_i)). If no profit can be obtained, output 0.
Problem Details:
- Input consists of an integer \(n\) representing the number of days, followed by \(n\) integers indicating the stock prices for each day.
- You must determine the maximum difference \(P_j - P_i\), where buying occurs on day \(i\) and selling on day \(j\) with \(i < j\).
- If every price is in descending order or if there is only one day, the output should be 0.
inputFormat
The input is read from standard input (stdin). The first line contains an integer (n) (the number of days). The second line contains (n) space-separated integers representing the stock prices on each day.
outputFormat
Output a single integer to standard output (stdout) representing the maximum profit achievable. If no profit can be achieved, output 0.## sample
6
7 1 5 3 6 4
5