Maximum Number of Jumps to Reach the Last Index - Problem
You are given a 0-indexed array nums of n integers and an integer target.
You are initially positioned at index 0. In one step, you can jump from index i to any index j such that:
0 <= i < j < n-target <= nums[j] - nums[i] <= target
Return the maximum number of jumps you can make to reach index n - 1.
If there is no way to reach index n - 1, return -1.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [4,2,5,1], target = 3
›
Output:
2
💡 Note:
From index 0 (value 4), we can jump to indices 1, 2, or 3. However, from index 2 we cannot reach index 3 since |1-5|=4 > target=3. The optimal path is 0→1→3, giving us 2 jumps maximum.
Example 2 — Limited Jumps
$
Input:
nums = [3,5,7], target = 1
›
Output:
-1
💡 Note:
From index 0 (value 3), we cannot jump to index 1 since |5-3|=2 > target=1, and we cannot jump to index 2 since |7-3|=4 > target=1. Therefore, it's impossible to reach the end.
Example 3 — No Path
$
Input:
nums = [1,10], target = 2
›
Output:
-1
💡 Note:
From index 0 (value 1), we cannot reach index 1 (value 10) because |10-1| = 9 > target = 2.
Constraints
- 2 ≤ nums.length ≤ 1000
- -109 ≤ nums[i] ≤ 109
- 1 ≤ target ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code