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
Be the first one to answer
Add answer anonymously...
MakeMyTrip Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter