#K85012. Maximum Profit from House Prices
Maximum Profit from House Prices
Maximum Profit from House Prices
You are given an array of integers representing the price of a house on different days. Your task is to determine the maximum profit achievable by buying the house on one day and selling it on a later day. Formally, given an array P of length n, you need to compute:
$$\max_{0 \leq i < j < n}\left(P[j]-P[i]\right)$$
If no profit can be made, output 0.
Note: The selling day must come after the buying day.
inputFormat
The input is read from stdin
and consists of:
- An integer
n
on the first line representing the number of days. - A line with
n
space-separated integers where the i-th integer is the price of the house on the i-th day.
outputFormat
Output a single integer to stdout
representing the maximum profit that can be achieved by buying and selling the house.
6
7 1 5 3 6 4
5