Maximum Number of Jumps to Reach the Last Index - Problem

Imagine you're on a magical stepping stone path across a river! ๐ŸŒŠ You start at the first stone (index 0) and need to reach the last stone (index n-1) to complete your journey.

Here's the twist: each stone has a magical number on it, and you can only jump from stone i to stone j if:

  • You're jumping forward: i < j
  • The magical numbers are similar enough: |nums[j] - nums[i]| <= target

Your goal is to find the maximum number of jumps you can make to reach the final stone. If it's impossible to reach the end, return -1.

Example: With stones [1, 3, 6, 4, 1, 2] and target = 2, you could jump: 0โ†’1โ†’3โ†’5 (3 jumps) or 0โ†’2โ†’4โ†’5 (3 jumps).

Input & Output

example_1.py โ€” Basic Path Finding
$ Input: nums = [1, 3, 6, 4, 1, 2], target = 2
โ€บ Output: 3
๐Ÿ’ก Note: One optimal path is 0โ†’1โ†’3โ†’5: jump from index 0 to 1 (|3-1|=2โ‰ค2), then 1 to 3 (|4-3|=1โ‰ค2), then 3 to 5 (|2-4|=2โ‰ค2). This gives us 3 jumps total.
example_2.py โ€” No Valid Path
$ Input: nums = [1, 0, 2], target = 0
โ€บ Output: -1
๐Ÿ’ก Note: From index 0 (value=1), we cannot jump to index 1 (value=0) because |0-1|=1 > 0. We also cannot jump to index 2 (value=2) because |2-1|=1 > 0. No path exists.
example_3.py โ€” Single Jump
$ Input: nums = [1, 3], target = 5
โ€บ Output: 1
๐Ÿ’ก Note: We can jump directly from index 0 to index 1 because |3-1|=2โ‰ค5. This requires exactly 1 jump, which is optimal since we need to reach the last index.

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 0 โ‰ค nums[i] โ‰ค 105
  • 0 โ‰ค target โ‰ค 2 ร— 105
  • Must jump forward only: i < j for valid jumps
  • Jump constraint: |nums[j] - nums[i]| โ‰ค target

Visualization

Tap to expand
๐Ÿชจ Magical Stepping Stones Journey1i=03i=16i=24i=31i=42i=5|3-1|=2โ‰ค2 โœ“|6-3|=3>2 โœ—|4-3|=1โ‰ค2 โœ“|1-4|=3>2 โœ—|2-4|=2โ‰ค2 โœ“Dynamic Programming Solutiondp[0]=0 dp[1]=1 dp[2]=-1 dp[3]=2 dp[4]=-1 dp[5]=3โ€ข Green arrows: Valid jumps (difference โ‰ค target)โ€ข Optimal path: 0โ†’1โ†’3โ†’5 with 3 jumps totalโ€ข dp[i] = maximum jumps to reach stone i๐Ÿง™Magical traveler taking the scenic route!
Understanding the Visualization
1
Start at first stone
Begin your journey at stone 0 with nums[0] = 1
2
Find valid jumps
Look for stones you can jump to: |destination_value - current_value| โ‰ค target
3
Choose optimal path
Instead of the shortest path, choose the path with maximum jumps
4
Reach the end
Successfully arrive at the final stone with maximum scenic stops
Key Takeaway
๐ŸŽฏ Key Insight: This problem asks for the *maximum* number of jumps (longest path), not the minimum. We use DP to track the maximum jumps to reach each position, building up the solution iteratively.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
42.8K Views
Medium Frequency
~18 min Avg. Time
1.5K 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