This is the "Medium" version of the previous problem. In the Find Indices With Index and Value Difference II coding problem, the array length can be up to . You still need to find indices such that and . Because the constraints are large, an brute-force solution will time out. You need a linear approach.
Companies like Paytm use this to evaluate if a candidate can optimize a search by using Two Pointers or Precomputation patterns. It tests your ability to maintain a "window of potential candidates" and identify that you only need to compare the current element with the minimum or maximum elements seen so far that are far enough away.
This problem uses Tracking Min/Max with a Fixed Offset.
minIndex) and maximum (maxIndex) values encountered so far in the range [0, i - indexDifference].nums = [5, 1, 4, 8, 1], idxDiff = 2, valDiff = 4.
minIndex = 0 (val 5), maxIndex = 0 (val 5).
minIndex to 1 (val 1) and maxIndex to 0 (val 5).indexDifference away from the current index .When you need to satisfy a condition like , you usually only care about the most extreme candidates for (the smallest and largest). This "extremum tracking" is a powerful way to reduce problems to .
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Next Permutation | Medium | Solve | |
| Find Indices With Index and Value Difference I | Easy | Solve | |
| Maximum Product of First and Last Elements of a Subsequence | Medium | Solve | |
| Number of Subarrays with Bounded Maximum | Medium | Solve | |
| Product of Two Run-Length Encoded Arrays | Medium | Solve |