Minimum Jumps Problem Statement
Bob and his wife are in the famous 'Arcade' mall in the city of Berland. This mall has a unique way of moving between shops using trampolines. Each shop is laid out in a straight line, and from any shop 'i', you can jump to any shop ranging from 'i' to 'i + Arr[i]', where 'Arr[i]' is a given constant for each shop.
The task is to determine the minimum number of trampoline jumps Bob has to make from shop 0 to reach the final shop (N-1) to reunite with his wife. If Bob cannot reach the last shop, return -1.
Input:
The input begins with a single integer T, representing the number of test cases.
For each test case, the first line contains a positive integer N, indicating the number of shops.
The second line includes N space-separated integers, representing the array 'Arr' for the shops.
Output:
For each test case, output the minimum number of jumps required to reach the last shop, or -1 if it's impossible.
Example:
Input:
2
5
2 3 1 1 4
5
3 2 1 0 4
Output:
2
-1
Explanation:
For the first test case, starting at shop 0, a jump of 1 goes to shop 1, followed by another jump directly to shop 4, a total of 2 jumps. For the second test case, once reaching shop 3, no more jumps can be made (Arr[3] = 0), so reaching the last shop is impossible.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10^4
0 <= Arr[i] <= N
Top MakeMyTrip Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month