## DEV Community is a community of 615,123 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# 💥Minimum jumps to reach the end of the array💥

Question :

Given an array of integers where each element represents the max number of steps that can be made forward from that element. Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.

Example 1:

``````Input:N=11
arr=1 3 5 8 9 2 6 7 6 8 9
Output: 3
Explanation:
First jump from 1st element to 2nd
element with value 3. Now, from here
``````

Logic:

1. Dynamic Programming:
• Follow the pseudocode alongside the representation you will understand it else watch Tushar Roy's video regarding this.
``````import java.util.*;

public class Solution{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}

//This is a Dynamic Programming solution
int jump[] = new int[n];
for(int i = 1; i < jump.length; i++){
jump[i] = 1000000;
}
int path[] = new int[n];
jump[0] = 0;

for(int i = 0; i<arr.length-1; i++){
for(int j = i+1; j < arr.length; j++){
if(arr[i] >= j - i){
if(jump[i]+1 <= jump[j]){
jump[j] = jump[i]+1;
path[j] = i;
}
}else{
break;
}
}
}

System.out.println();
System.out.println("The jump array is: ");
for(int i = 0; i < jump.length; i++){
System.out.print(jump[i]+" ");
}

System.out.println();
System.out.println("The path array is: ");

for(int i = 0; i < path.length; i++){
System.out.print(path[i]+" ");
}

boolean unreached = false;
for(int i = 0; i < jump.length; i++){
if(jump[i] == 1000000){
unreached = true;
}
}

if(unreached == false){
System.out.println();
System.out.println("The jump route is: ");
int jumpCount = 0;
int k = path.length-1;
while(k != 0){
System.out.print(k+" ");
k = path[k];
jumpCount++;
}
System.out.println();