#C14732. Maximum Single Transaction Profit
Maximum Single Transaction Profit
Maximum Single Transaction Profit
You are given a sequence of stock prices over consecutive days. Your task is to determine the maximum profit achievable from a single buy-sell transaction. In other words, you need to choose a day to buy and a later day to sell so that the profit (P = price_{sell} - price_{buy}) is maximized. If no positive profit can be made, output 0.
Note: You are only allowed to complete at most one transaction.
inputFormat
The input is read from standard input (stdin).
The first line contains an integer (n) representing the number of days. The second line contains (n) space-separated integers denoting the stock prices for each day.
outputFormat
Output a single integer to standard output (stdout) which is the maximum profit that can be achieved from a single transaction. If no profit is possible, output 0.## sample
6
7 1 5 3 6 4
5