An elevation profile is given as n measurements in order. A ramp is a pair of positions (i, j) with i <= j and value[i] <= value[j]; its width is j - i. Find the maximum width over all ramps.
Since taking i = j is always a valid ramp of width 0, the answer is never negative; it is a positive number exactly when some earlier position has a value less than or equal to some later position's value.
Line 1: an integer n.
Line 2: n space-separated integers, the measurements in order.
A single integer: the maximum ramp width.
Example 1
Input
6 6 0 8 2 1 5
Expected
4
Explanation
Position 1 holds value 0; the later position 5 holds value 5, and 0 <= 5, giving a ramp of width 5 - 1 = 4, which is the widest possible here.
Example 2
Input
4 9 8 1 0
Expected
0
Explanation
The profile strictly decreases, so no earlier value is <= any later value; the only valid ramps have i = j, giving maximum width 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →