Given an array, can I find in On the longest range, whose endpoints are the greatest values in the range?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This problem asks for the longest contiguous range whose two endpoints are also the maximum values inside that range. That means the endpoints must be equal, and nothing between them can be larger. A brute-force search is easy to write, but it is too slow for large arrays. The interesting part is that you can get to near-linear behavior by combining a monotonic stack with per-value grouping.
Rewrite the Condition in a More Useful Form
For a range from index i to j to be valid:
- '
a[i] == a[j]' - every value in
a[i..j]is less than or equal toa[i]
That second statement means there is no element greater than the endpoint value anywhere inside the interval. So if the endpoint value is v, the valid range must live entirely inside a maximal segment bounded by elements greater than v.
That observation is the key to the faster solution.
A Quadratic Baseline Helps Clarify Correctness
Before optimizing, it is useful to keep a simple baseline implementation for testing.
This is easy to trust, but it is O(n^2) and too slow when n grows.
Find the Maximal Segment for Each Position
Now switch to the faster view. For each index, compute the nearest position to the left with a strictly greater value, and the nearest position to the right with a strictly greater value. Inside those boundaries, the current value is never exceeded.
You can compute both arrays with monotonic decreasing stacks in linear time.
The exact stack condition matters because we want boundaries formed by strictly greater values, not by equal values.
Group Equal Values by Their Valid Segment
If two equal endpoints share the same greater-value boundaries, then the whole stretch between them is safe: nothing inside is larger than that value. So the problem reduces to finding, for each pair of boundaries and each value, the first and last occurrence inside that segment.
This works in linear time after the boundary computation because each index is processed a constant number of times.
Why the Grouping Logic Works
Suppose an index with value v has nearest greater boundaries L and R. Then every position between L + 1 and R - 1 has value less than or equal to v. If two occurrences of v lie in that same open interval, the range between them automatically satisfies the maximum-endpoint rule.
So the longest valid range for that value inside that segment is simply from the first occurrence to the last occurrence. That is why the hash key uses:
- the value itself
- the left greater boundary
- the right greater boundary
Without the boundaries, equal values from different segments would be merged incorrectly.
Keep the Quadratic Version for Tests
When you optimize array logic, a slow correctness oracle is incredibly useful. Randomized testing makes it easy to compare the fast implementation with the simple one.
That kind of test catches the subtle boundary mistakes that are common with monotonic-stack algorithms.
Common Pitfalls
The most common mistake is forgetting that the endpoints must be equal. Another is using greater-or-equal boundaries instead of strictly greater boundaries and accidentally splitting valid segments. Teams also often optimize without keeping a brute-force oracle, which makes stack-condition bugs much harder to detect.
Summary
- A valid range needs equal endpoints and no larger value inside.
- The nearest strictly greater elements define the maximal safe segment for each value.
- A monotonic stack can compute those boundaries in linear time.
- Grouping equal values by shared boundaries yields a near-linear full solution.
- Keep a quadratic version for testing, because boundary logic is easy to get subtly wrong.

