#K7016. Maximum Book Trading Profit
Maximum Book Trading Profit
Maximum Book Trading Profit
Given a list of prices of a book for consecutive days, your task is to compute the maximum profit that can be achieved by buying on one day and selling on a later day. The profit is defined as \(\text{price}[j] - \text{price}[i]\) for \(i < j\). If no profit can be made, return 0.
For example, if the prices are [7, 1, 5, 3, 6, 4], the maximum profit is 5 (by buying at 1 and selling at 6).
inputFormat
The first line contains an integer \(n\) representing the number of days. The second line contains \(n\) space-separated integers, where each integer represents the price of the book on that day.
outputFormat
Output a single integer representing the maximum profit that can be achieved. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5