Given an array V, we need to find two indices i,j such that Vj Vi and j - i is maximum
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The problem is to find indices i and j such that V[j] is greater than V[i] and the distance j - i is as large as possible. A quadratic scan works, but there is a standard linear-time solution that is much better for large arrays.
Why the Naive Solution Is Too Slow
The direct approach checks every pair:
- choose
i - try every
jto the right - keep the best valid distance
That costs O(n^2), which is fine for tiny inputs but quickly becomes expensive.
We want something closer to one pass through the data.
The Linear-Time Idea
The efficient solution separates the comparison into two helper arrays:
- '
left_min[i]is the smallest value seen from the left up to indexi' - '
right_max[j]is the largest value seen from indexjto the end'
Once those arrays exist, use two pointers:
- start
iat the left - start
jat the left - if
left_min[i]is less thanright_max[j], the condition can be satisfied, so update the answer and movej - otherwise move
i
This works because left_min summarizes the best candidate on the left and right_max summarizes the best candidate on the right.
Building the Helper Arrays
These arrays cost O(n) time and O(n) extra memory. After that, the final scan is also linear.
Full Working Solution
The version below returns both the maximum distance and one valid pair of indices.
For this input, the best answer uses the smallest useful value on the left and the farthest larger value on the right.
Why It Works
The two-pointer scan is efficient because left_min_index[i] never hides a better left candidate. If the smallest value seen so far on the left cannot form a valid pair with the best possible right-side value at j, then no later left index summarized by that prefix will help, so moving i is safe.
Likewise, when the condition succeeds, moving j is safe because a farther right position might increase the gap.
That monotonic movement is what keeps the scan linear.
Alternative View: Prefix Minima and Suffix Maxima
Many explanations write the solution using value arrays only, then reconstruct indices later. That is fine if you only need the maximum gap. If you also need the actual indices, storing the best prefix-minimum index and suffix-maximum index directly is cleaner.
If duplicates matter, be explicit about the comparison. The title says V[j] must be greater than V[i], not greater than or equal to it, so the test should remain strict.
Common Pitfalls
Using >= instead of > changes the problem and can return a different pair than the one requested.
Building left_min and right_max with values but forgetting how to recover the original indices leads to an incomplete answer.
Trying to move both pointers on every iteration usually breaks the invariant and misses valid long-distance pairs.
Falling back to sorting destroys the original index positions unless you do extra bookkeeping, and it is unnecessary here anyway.
Summary
- The brute-force solution is
O(n^2), but the standard optimized solution isO(n). - Build prefix-minimum information from the left and suffix-maximum information from the right.
- Use two pointers to scan for the farthest valid pair.
- Keep the comparison strict because the condition is "greater than," not "greater than or equal to."
- Store indices, not just values, if you need the actual pair as output.

