#C13784. Maximum Profit from a Single Transaction

    ID: 43360 Type: Default 1000ms 256MiB

Maximum Profit from a Single Transaction

Maximum Profit from a Single Transaction

You are given the stock prices of a company over a series of days. Your task is to compute the maximum profit obtainable by performing exactly one buy-sell transaction. In other words, if you buy the stock at day (i) (with price (P_i)) and sell it at a later day (j) (with price (P_j), where (j > i)), the profit is given by (P_j - P_i). If no profit is possible (i.e. the price never increases), the answer should be (0).

Input:

  • The first line contains an integer \(n\), the number of days.
  • The second line contains \(n\) space-separated integers representing the stock prices.

Output:

  • A single integer representing the maximum profit that can be gained from a single transaction.

For example, given the input:

6
7 1 5 3 6 4

The maximum profit is (5) (buy at price 1 and sell at price 6).

inputFormat

The first line of input contains an integer n (\(0 \le n \le 10^5\)) 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 which is the maximum profit that can be obtained by buying and selling the stock exactly once. If no profit is possible, output 0.

## sample
0
0