Leetcode Problem 2903. Find Indices With Index and Value Difference I

2903. Find Indices With Index and Value Difference I

Leetcode Solutions

One Pass, O() Space

  1. Initialize two variables mini and maxi to store the indices of the minimum and maximum values in the prefix of the array.
  2. Iterate through the array starting from index indexDifference.
  3. Update mini and maxi if the current value at i - indexDifference is less than or greater than the values at mini and maxi, respectively.
  4. Check if the current value has a valueDifference with the values at mini or maxi.
  5. If the condition is met, return the pair of indices [mini, i] or [maxi, i].
  6. If no such pair is found by the end of the iteration, return [-1, -1].
UML Thumbnail

Brute-force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...