Learner Level
Implement the canReachLastIndex method that checks whether the last index can be reached from the first index.
The input contains an integer array nums and its length size. Each value represents the maximum jump length allowed from that index.
Your task is to check whether it is possible to reach the last index starting from index 0. Return true if it can be reached, otherwise return false. For example, [2,3,1,1,4] returns true, but [3,2,1,0,4] returns false.
Track the farthest index that can be reached while scanning the array.
If the current index is greater than the farthest reachable index, then there is no way to stand on that position, so the last index cannot be reached.
Otherwise, update the farthest reachable index using i + nums[i]. If the farthest value reaches or crosses the last index, return true.
Pseudocode:
function canReachLastIndex(nums, size):
farthest = 0
for i from 0 to size - 1:
if i > farthest:
return false
farthest = max(farthest, i + nums[i])
if farthest >= size - 1:
return true
return true