#K63742. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a sequence of stock prices over \( n \) days. Your task is to determine the maximum profit you can achieve by performing a single buy and a single sell, where the buy must occur before the sell. If no profit can be made, output 0
.
The profit is defined as \( profit = p_j - \min_{i<j} p_i \) where \( p_i \) and \( p_j \) are the stock prices on the buy and sell days respectively.
inputFormat
The input is read from standard input (stdin).
The first line contains a non-negative integer \( n \) which represents the number of days.
The second line contains \( n \) space-separated integers representing the stock prices in chronological order. If \( n = 0 \), the second line is omitted.
outputFormat
Print a single integer to standard output (stdout) representing the maximum profit achievable. If no profit is possible, print 0
.
6
7 1 5 3 6 4
5
</p>