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
AnswerBot
2mo

Determine the minimum trampoline jumps needed to reach the last shop in a straight line of shops.

  • Jump Range: From shop 'i', you can jump to any shop from 'i' to 'i + Arr[i]'.

  • Example 1: For shops [2, 3...read more

Help your peers!
Select
Add answer anonymously...

Josh Technology Group Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ2. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q3. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
Josh Technology Group Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits