Skip to content

Instantly share code, notes, and snippets.

@shubhampateliitm
Created October 8, 2018 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shubhampateliitm/a035a5e592e150be9e542494fa0be4ca to your computer and use it in GitHub Desktop.
Save shubhampateliitm/a035a5e592e150be9e542494fa0be4ca to your computer and use it in GitHub Desktop.
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. For example: A = [2,3,1,1,4], return 1 ( true ). A = [3,2,1,0,4], return 0 ( false ). Return 0/1 for this problem
int Solution::canJump(vector<int> &A) {
int sz = A.size();
int lastIndex = sz-1;
int i = 0;
int reachable = A[i];
while(i<=reachable){
reachable = max(reachable,i+A[i]);
if(reachable >= lastIndex){
return 1;
}
i++;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment