#K12001. Maximum Stock Profit
Maximum Stock Profit
Maximum Stock Profit
You are given a series of stock prices for N consecutive days. Your task is to compute the maximum profit that can be made by performing exactly one buy transaction and one sell transaction. You must buy before you sell. If no profit can be achieved, output 0.
The problem can be formulated as: Given an integer \( N \) (where \( 2 \leq N \leq 200 \)) and a list of \( N \) integers representing the stock prices over \( N \) days, find the maximum difference \( p_j - p_i \) such that \( i p_i \). If there is no such pair, return 0.
inputFormat
The input is given via standard input (stdin) and consists of two lines. The first line contains a single integer \( N \) (where \( 2 \leq N \leq 200 \)) denoting the number of days. The second line contains \( N \) space-separated integers representing the stock prices on each day.
outputFormat
Output a single integer representing the maximum profit possible from a single buy and sell transaction. If no profit is possible, output 0.
## sample6
7 1 5 3 6 4
5