-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Jump.java
More file actions
54 lines (34 loc) · 1.14 KB
/
Copy pathMinimum_Jump.java
File metadata and controls
54 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class Minimum_Jump {
int minJumps(int[] arr) {
int n = arr.length;
if (n == 1) return 0;
if (arr[0] == 0) return -1;
int maxReach = arr[0];
int steps = arr[0];
int jumps = 1;
for (int i = 1; i < n; i++) {
if (i == n - 1) return jumps;
maxReach = Math.max(maxReach, i + arr[i]);
steps--;
if (steps == 0) {
jumps++;
if (i >= maxReach) return -1;
steps = maxReach - i;
}
}
return -1;
}
public static void main(String []arg){
Minimum_Jump m = new Minimum_Jump();
Scanner d = new Scanner(System.in);
System.out.println("Enter the Array Size:");
int n = d.nextInt();
int arr[] = new int[n];
System.out.println("Enter Array Elements:");
for(int i=0;i<n;i++)
arr[i]=d.nextInt();
int jumps = m.minJumps(arr);
System.out.println("Minimum Jumps:"+jumps);
}
}