Interview Questions/Coding/Maximum Future Rise

Maximum Future Rise

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Maximum Future Rise

A river station records water levels over time. You want to find the largest rise where a lower reading happened earlier and a higher reading happened later. Return the maximum possible difference between the later higher reading and the earlier lower reading. If the readings never rise after a previous lower value, return -1.

Examples

Example 1
Input:
2 3 10 6 4 8 1
Output:
8

Explanation: The best rise is from 2 to 10, giving 10 - 2 = 8.

Approach hint

While scanning left to right, keep the smallest reading seen so far.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp