#C6561. Maximum Profit
Maximum Profit
Maximum Profit
You are given an array representing the prices of a stock on different days. You are allowed to perform at most one transaction (i.e., buy one share and then sell one share).
Your task is to calculate the maximum profit that can be achieved. If no profit is possible, return 0.
The profit obtained by buying on day \(i\) and selling on day \(j\) (with \(i < j\)) is given by:
\(\text{Profit} = \text{Price}[j] - \text{Price}[i]\)
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains an integer \(n\), which is the number of days.
- The second line contains \(n\) space-separated integers representing the stock prices.
outputFormat
Output a single integer to standard output (stdout), which is the maximum profit that can be achieved. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5
</p>