Java Array Program · A44
Minimum Jumps to Reach the End of an Array in Java
Think in ranges: all positions reachable with k jumps form one block. The next block reaches as far as the best jump from anywhere in the current block.
Problem Statement
Each element of the array is the maximum number of positions you may jump forward from that index. Starting at index 0, return the fewest jumps needed to reach the last index.
If the last index cannot be reached, return -1. An array of length 1 needs 0 jumps.
Input/Output Examples
[3, 1, 1, 2, 4, 1]Output2Explanation: jump to index 3, then 2 more to the end.[1, 1, 1]Output2Explanation: 0 → 1 → 2.[0, 1]Output-1Explanation: you cannot leave index 0.Visual Explanation
- Range 0indexes0reach 0 + 3 = 3 jump 1
- Range 1indexes123best reach: 3 + 2 = 5 jump 2
- Range 2indexes45contains the last index
- Done2 jumps
[3, 1, 1, 2, 4, 1]: from range 1 (indexes 1 to 3), index 3 reaches farthest, so the second jump can land on index 5.This is breadth-first search without a queue: each range is one level. When the scan reaches the end of the current range, a jump is counted and the range is extended to the farthest reach found so far.
Approaches
The greedy range method is linear. The DP version relaxes every reachable position.
Approach 1: Greedy Ranges (Recommended)
Idea: scan the array, tracking the end of the current range and the farthest reach; jump when the scan reaches the range end.
- Update
farthest = max(farthest, i + arr[i]). - When
i == currentEnd, count a jump and setcurrentEnd = farthest. - If
farthestcannot move pasti, return-1.
Time: O(n) · Space: O(1).
Approach 2: DP Over Positions
Idea: dp[i] is the fewest jumps to reach i; from each reachable i, relax every position within its jump.
- Set
dp[0] = 0and the rest to infinity. - For each reachable
i, updatedp[i + step]forstepin1..arr[i]. - Return
dp[n - 1], or-1.
Time: O(n × maxJump) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Greedy Ranges (Recommended)
The loop stops before the last index, because standing on it requires no further jump.
import java.util.Arrays;
public class JumpsGreedy {
// Breadth-first by ranges: each jump covers the farthest reach of the previous range
static int minJumps(int[] arr) {
int n = arr.length;
if (n <= 1) {
return 0;
}
int jumps = 0;
int currentEnd = 0;
int farthest = 0;
for (int i = 0; i < n - 1; i++) {
farthest = Math.max(farthest, i + arr[i]);
if (i == currentEnd) { // must jump to go beyond this range
if (farthest <= i) {
return -1; // stuck
}
jumps++;
currentEnd = farthest;
if (currentEnd >= n - 1) {
break;
}
}
}
return jumps;
}
public static void main(String[] args) {
int[][] inputs = {{3, 1, 1, 2, 4, 1}, {1, 1, 1}, {0, 1}, {5}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + minJumps(arr));
}
}
}
[3, 1, 1, 2, 4, 1] -> 2 [1, 1, 1] -> 2 [0, 1] -> -1 [5] -> 0
Time complexity: O(n) · Space complexity: O(1).
Approach 2: DP Over Positions
Slower, but it states the recurrence directly.
import java.util.Arrays;
public class JumpsDp {
// dp[i] = fewest jumps to reach i; relax every position reachable from i: O(n^2)
static int minJumps(int[] arr) {
int n = arr.length;
int[] dp = new int[n];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 0; i < n; i++) {
if (dp[i] == Integer.MAX_VALUE) {
continue; // i itself is unreachable
}
for (int step = 1; step <= arr[i] && i + step < n; step++) {
dp[i + step] = Math.min(dp[i + step], dp[i] + 1);
}
}
return dp[n - 1] == Integer.MAX_VALUE ? -1 : dp[n - 1];
}
public static void main(String[] args) {
int[][] inputs = {{3, 1, 1, 2, 4, 1}, {1, 1, 1}, {0, 1}, {5}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + minJumps(arr));
}
}
}
[3, 1, 1, 2, 4, 1] -> 2 [1, 1, 1] -> 2 [0, 1] -> -1 [5] -> 0
Time complexity: O(n × maxJump) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Greedy Ranges | O(n) | O(1) |
| DP Over Positions | O(n × maxJump) | O(n) |
Here n is the array length. The greedy scan visits each index once; the DP may relax every position reachable from every index.
Interview Tips and Recommended Approach
- Recognise the BFS levels. It explains why counting jumps at range ends is optimal.
- Detect being stuck. If the farthest reach does not move past the current index, the end is unreachable.
- Do not iterate to the last index. Jumping from it would count one jump too many.
- Know the yes/no version. "Can you reach the end?" only needs the farthest reach.
Recommended Approach: Greedy Ranges
Why: it finds the minimum in a single linear pass with three variables, which is the expected answer for this Hard problem.