Minimum number of jumps to reach end in linear time

Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.
Examples:

Input : arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}
Output :3
Approach.....
Here we are using the fact that each element of array represents a ladder of say a[i] stairs.we are considering two 
ladder current (it is the ladder which is currently being used for moving forward ) and backup ladder(it will be used when my current ladder will be empty ).
if my current ladder will be zero then we update my current ladder=backup ladder .
if we find satution where my current and backup both ladder are empty then we can not move to end and in this case answer will be -1. 
since we are moving forward then on each step my current and backup ladder's stairs will be decreased by 1 because i have moved forward one step.
if my we get an array element which is greater than my backup ladder it means we get a ladder of greater length so i will update my backup ladder. if we are in condition such that index where we are say (i+current)>(n-1) so we can move directly from index i to (n-1).

[sourcecode language=CPP]
#include
using namespace std;
int main()
{
int t; // test cases
cin >> t;
while (t–) {
int n; // number of elements in array
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int backup = a[0]; // it can be treated as a ladder which initially contains a[0] stairs backup ladder is used
for
future use if my current ladder will be zero(0 stairs) int current = a[0]; // it is my current ladder of stairs a[0]
int ans = 1;
int flg = 0;
if (a[0] == 0) // if first element of array is 0 we cannot move
cout << "-1" << endl; else if (n == 1) // if array contains only one element then answer will be 0 cout << "0" << endl; else { for (int j = 1; j < n; j++) // itterate from 1 to n { backup--; // decrement current and backup ladder because we are considering that on moving forward traverse both ladder's stairs current--; if (a[j] > backup) // if element of array is greater than my backup ladder then it means that we can get more number of stairs by choosing this element as my backup ladder so backup is updated
backup = a[j];
if (current == 0) // it means that i have no current stair to move so we should choose backup ladder
{
if ((current + j) >= (n – 1)) // if I can reach from index i to end of array then no need to do further computation
break;
ans++; // it means that we are jumping from current ladder to backup ladder
current = backup;
backup = 0;
if ((current + j) >= (n – 1)) // it indicates that i have current ladder by which we can go to end so no need to do further computation
break;
}
if ((backup == 0) && (current == 0)) // if current and backup both ladder is empty it means that we have no stair to move so we can not reach at end
{
cout << "-1" << endl; break; flg++; } } if (flg == 0) // if for loop run successfully then print minimum number of jumps to reach end cout << ans << endl; } } } [/sourcecode] Output:


This article is contributed by Anand Kumar Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.