#K38707. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given an array of stock prices, where the i-th element represents the stock price on day i. Your task is to determine the maximum profit you can achieve by executing a single buy-sell operation. Specifically, you must choose a day to buy and a day to sell such that the sell day comes after the buy day and the profit is maximized. If no profit can be achieved, return 0.
You are given an integer n and a sequence of prices \(p_1, p_2, \ldots, p_n\). The objective is to compute the maximum difference \(p_j - p_i\) where \(1 \leq i < j \leq n\). If for all valid \(i\) and \(j\) the difference is negative or zero, output 0.
Example:
Input: 6 7 1 5 3 6 4</p>Output: 5
inputFormat
The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer n indicating the number of stock prices. The second line contains n space-separated integers representing the stock prices.
For example:
6 7 1 5 3 6 4
outputFormat
Output a single integer representing the maximum profit obtainable with one buy-sell operation. If no profit is possible, output 0.
For example:
5## sample
6
7 1 5 3 6 4
5