2020-09-01 00:04:11 +05:30
//Jump Game:
/*Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position .
Determine if you are able to reach the last index . */
2020-09-01 00:08:54 +05:30
# include <vector>
# include<iostream>
2020-09-01 00:15:34 +05:30
2020-09-01 00:12:43 +05:30
bool canJump ( vector < int > nums ) {
2020-09-01 00:15:34 +05:30
auto lastPos = nums . size ( ) - 1 ;
2020-09-01 00:12:43 +05:30
for ( auto i = nums . size ( ) - 1 ; i > = 0 ; i - - ) {
2020-09-01 00:04:11 +05:30
if ( i + nums [ i ] > = lastPos ) {
lastPos = i ;
}
}
return lastPos = = 0 ;
}
void main ( ) {
//Sample test case
vector < int > num = { 4 , 3 , 1 , 0 , 5 } ;
cout < < canJump ( num ) ; //Should display true, as when you take one step from position 0, you reach position 1, from which 3 steps lead you to the destination
}