forked from 0xsec-debug/Assignment-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpgame.java
More file actions
25 lines (23 loc) · 763 Bytes
/
jumpgame.java
File metadata and controls
25 lines (23 loc) · 763 Bytes
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
import java.util.*;
public class jumpgame {
public static boolean canJump(int[] nums) {
int reachable = 0;
for (int i = 0; i < nums.length; i++) {
if (i > reachable) return false;
reachable = Math.max(reachable, i + nums[i]);
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
boolean result = canJump(nums);
System.out.println("Can reach last index? " + result);
}
}