Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Minimum Jumps
MEDIUM
25 mins
121 upvotes
Arrays
Dynamic Programming
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Dynamic Programming
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Minimum Jumps

Contributed by
Shivam
Medium
yellow-spark
0/80
Avg time to solve 25 mins
Success Rate 75 %
Share
121 upvotes

Problem Statement

Bob lives with his wife in a city named Berland. Bob is a good husband, so he goes out with his wife every Friday to ‘Arcade’ mall.

‘Arcade’ is a very famous mall in Berland. It has a very unique transportation method between shops. Since the shops in the mall are laying in a straight line, you can jump on a very advanced trampoline from the shop i, and land in any shop between (i) to (i + Arr[i]), where Arr[i] is a constant given for each shop.

There are N shops in the mall, numbered from 0 to N-1. Bob's wife starts her shopping journey from shop 0 and ends it in shop N-1. As the mall is very crowded on Fridays, unfortunately, Bob gets lost from his wife. So he wants to know, what is the minimum number of trampoline jumps from shop 0 he has to make in order to reach shop N-1 and see his wife again. If it is impossible to reach the last shop, return -1.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
5
2 1 3 2 4
3
3 2 1
Sample Output 1:
2
1
Explanation For Sample Input 1:
In the 1st test case, Bobs jumps from shop 0 to shop 2 and then jumps from shop 2 to shop 4, so he needs two jumps to reach the last shop.

In the 2nd test case, Bobs jumps from shop 0 to shop 2, so he needs only one jump to reach the last shop.
Sample Input 2:
2
5
1 0 3 2 1
4
1 1 1 1
Sample Output 2:
-1
3
Reset Code
Full screen
Auto
copy-code
Console