Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

JUMP GAME

Moderate
0/80
Average time to solve is 40m
1 upvote
Asked in companies
Tata Consultancy Services (TCS)GrowwWalmart

Problem statement

Given an array of non-negative integers ‘ARR’ of length ‘N’, you are initially positioned at the array's first index.

Each element in the array represents your maximum jump length at that position.

Return the minimum number of jumps required to reach the last index.

If it is not possible to reach the last index, return -1.

Example:
Input:

‘N’ = 3
‘ARR’ = [ 2, 1, 1 ]

The shortest way to reach index 2 is
Index 0 => Index 2
that requires only 1 jump.
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 10    
1 <= N <= 10000
0 <= ARR [ i ] <= N
Time Limit: 1 sec
Sample Input 1:
2
5
2 3 1 1 4
4
1 1 1 1

Sample Output 1:

2
3
Explanation Of Sample Input 1:
For the first test case:-
The optimal path is:
0 => 1 => 4

For the second test case:-
0 => 1 => 2 => 3
Sample Input 2:
2
5
2 3 0 1 4
3
1 0 2    
Sample Output 2:
2
-1
Full screen
Console