#C12679. Maximum Profit in a Single Stock Trade

    ID: 42132 Type: Default 1000ms 256MiB

Maximum Profit in a Single Stock Trade

Maximum Profit in a Single Stock Trade

You are given an array of integers representing the stock prices of a company over time. Your task is to determine the maximum profit that can be made by executing a single buy-and-sell transaction. If no profit is possible, output 0.

The profit is defined as:

\( \text{profit} = \text{selling price} - \text{buying price} \)

You must choose a day to buy and a later day to sell the stock so that the profit is maximized. This is a classic problem often summarized as "Best Time to Buy and Sell Stock".

inputFormat

The input is read from stdin and has the following format:

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

outputFormat

Output a single integer to stdout representing the maximum profit achievable from exactly one buy-sell transaction. If no profit can be achieved, output 0.

## sample
6
7 1 5 3 6 4
5