Find Indices With Index and Value Difference II - Problem

๐ŸŽฏ Find Indices With Index and Value Difference II

You're given a 0-indexed integer array nums of length n, along with two constraints: indexDifference and valueDifference.

Your mission: Find two indices i and j (both in range [0, n-1]) that satisfy both conditions:

  • ๐Ÿ“ Index constraint: abs(i - j) >= indexDifference
  • ๐Ÿ“Š Value constraint: abs(nums[i] - nums[j]) >= valueDifference

Return: An array [i, j] if such indices exist, otherwise [-1, -1]. If multiple valid pairs exist, return any one of them.

Note: Indices i and j can be equal if they satisfy both constraints.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [4,1,2,3], indexDifference = 1, valueDifference = 1
โ€บ Output: [0,2]
๐Ÿ’ก Note: Indices 0 and 2 satisfy both constraints: |0-2| = 2 โ‰ฅ 1 and |4-2| = 2 โ‰ฅ 1. Other valid pairs exist like [0,1] but we return the first found.
example_2.py โ€” No Solution
$ Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
โ€บ Output: [-1,-1]
๐Ÿ’ก Note: No pair of indices satisfies both constraints. The maximum index difference is 2 (indices 0,2) but |1-3| = 2 < 4, so the value difference constraint fails.
example_3.py โ€” Same Index Valid
$ Input: nums = [5], indexDifference = 0, valueDifference = 0
โ€บ Output: [0,0]
๐Ÿ’ก Note: Single element array where i=j=0 satisfies both constraints: |0-0| = 0 โ‰ฅ 0 and |5-5| = 0 โ‰ฅ 0.

Visualization

Tap to expand
๐ŸŽฏ Security Camera PlacementHallway with Rooms4123Room 0Room 1Room 2Room 3Min: 4Max: 4โœ“ Valid PairDistance Check:|0 - 2| = 2 โ‰ฅ 1 (indexDifference) โœ“Activity Check:|4 - 2| = 2 โ‰ฅ 1 (valueDifference) โœ“๐Ÿ” Algorithm InsightTrack min/max from valid positionsCompare current with extremesO(n) time, O(1) spaceOptimal Solution!
Understanding the Visualization
1
Setup Window
Start checking from positions that satisfy minimum distance
2
Track Extremes
Keep track of minimum and maximum activity levels from valid previous positions
3
Compare Current
Check if current position has sufficient activity difference with tracked extremes
4
Found Solution
Return the first valid pair that satisfies both distance and activity constraints
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking all pairs (O(nยฒ)), we use sliding window technique to track min/max values from valid previous positions, achieving optimal O(n) time complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, each operation is O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track min/max values and indices

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i] โ‰ค 5 ร— 104
  • 0 โ‰ค indexDifference โ‰ค nums.length
  • 0 โ‰ค valueDifference โ‰ค 5 ร— 104
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.4K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen