#K13096. Maximize Stock Profit
Maximize Stock Profit
Maximize Stock Profit
You are given a sequence of stock prices over consecutive days. Your task is to determine the maximum profit that can be made by performing a single buy and a single sell operation. In other words, you must choose two days i and j (with i < j) such that the difference between the price on day j and day i is maximized.
If no positive profit can be obtained, return 0.
You can mathematically represent the problem as finding
where price[k]
represents the stock price on the kth day.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a single integer N which represents the number of days (and hence the number of stock prices).
- The second line contains N space-separated integers, each representing the stock price on a day.
outputFormat
Output a single integer to stdout representing the maximum profit achievable from one transaction. If no profitable transaction is possible, output 0.
## sample6
7 1 5 3 6 4
5
</p>