Minimum Number of Jumps to Reach End Using Java



In this article, we will learn how to solve the "Minimum Number of Jumps to Reach the End" problem using Java. Let's break it down step-by-step. The idea is to find the smallest number of jumps needed to get from the start to the end of an array. Each element in the array represents the maximum number of steps you can take from that position.

Problem Statement

Given an array arr[], where each element represents the maximum number of steps you can move forward from that position, the goal is to start from the beginning of the array and reach the end with the fewest possible jumps. If reaching the end is not possible, return -1.

Input

For an array arr = [2, 3, 1, 1, 2, 4, 2, 0, 1, 1]

Output

Minimum jumps required: 4
At index 0, arr[0] = 2, meaning you can jump to index 1 or 2.
At index 1, arr[1] = 3, so from here, you can jump to index 2, 3, or 4.
The task is to figure out the minimum jumps needed to reach the last index.

Steps to find the minimum number of jumps to reach the end

Following are the steps to find the minimum number of jumps to reach the end using Java ?

  • Initialize jumps to count the jumps made, maxReach to track the farthest index we can reach, and steps to count the remaining steps within the current jump range.
  • Traverse the array: for each element, update maxReach with the farthest index reachable and decrement steps as we progress.
  • If steps reach zero, increase jumps (indicating a jump is needed to move further) and reset steps based on the new maximum reachable index.
  • For the final condition, if the current position plus maxReach is enough to reach the end, return the jump count. If we exhaust steps or can't move further, return -1.

Java program to find the minimum number of jumps

Below is the demonstration of doing a minimum number of jumps to reach the end using Java ?

public class MinJumpsToEnd {
	public static int minJumps(int[] arr) {
		int n = arr.length;
		if (n <= 1) return 0;// Already at the end
		if (arr[0] == 0) return -1;// Cannot move anywhere
		int maxReach = arr[0]; // Farthest we can reach with initial jump
		int steps = arr[0]; // Steps we can still take in the current range
		int jumps = 1;// Start with the first jump
		for (int i = 1; i < n; i++) {
			if (i == n - 1) return jumps; // Reached end
			maxReach = Math.max(maxReach, i + arr[i]);
			steps--;
			if (steps == 0) {
				jumps++; // Need to jump again to keep moving
				if (i >= maxReach) return -1;// Cannot reach further
				steps = maxReach - i;// New steps to move further
			}
		}
		return -1;
	}
	public static void main(String[] args) {
		int[] arr = {2, 3, 1, 1, 2, 4, 2, 0, 1, 1};
		System.out.println("Minimum jumps required: " + minJumps(arr));
	}
}

Output

Minimum jumps required: 4

Time Complexity: O(n)
Space Complexity: O(1)
This approach ensures that we make the minimum number of jumps to reach the end.

Code explanation

This Java program calculates the minimum jumps needed to reach the array's end by updating maxReach, steps, and jumps as we go. Starting at index 0 with a maxReach set by arr[0], we move forward, reducing steps for each element. If steps hit zero, we increment jumps and reset steps to maxReach - i to start the next jump.
For example, starting at index 0 with 2 steps, we jump to index 1 and update maxReach to 4. At index 1, we have 3 steps, allowing us to reach index 4. This process continues until we reach the end with minimal jumps.

Pushpa kumari
Pushpa kumari

A computer Science Student

Updated on: 2024-11-01T13:13:39+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements