#K41927. Maximum Stock Trading Profit
Maximum Stock Trading Profit
Maximum Stock Trading Profit
You are given the stock prices of a company for n consecutive days. Your task is to determine the maximum profit achievable by performing exactly one purchase and one sale of the stock. Remember that you cannot sell the stock before you buy it.
The profit is defined as:
$$\text{Profit} = \max_{0 \leq i < j < n} \{prices[j] - prices[i]\}$$
If no profit is possible, output 0.
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 stock price on that day.
outputFormat
Output a single integer, the maximum profit you can achieve with one transaction. If no profit can be achieved, output 0
.
6
7 1 5 3 6 4
5