Imagine you're playing a video game where you can jump forward on platforms, but each platform limits how far you can jump next. Can you reach the last platform?
Given an array of non-negative integers nums, where each element represents your maximum jump length at that position, determine if you can reach the last index starting from the first index. Return true if you can reach the last index, otherwise false.
1 ≤ nums.length ≤ 10^50 ≤ nums[i] ≤ 10^5
Edge cases: Single element array [0] → true (already at last index)Array with zeros blocking path [1,0,1] → falseArray with large jumps at start [100,0,0,0] → true
def canJump(nums: list[int]) -> bool:public boolean canJump(int[] nums)bool canJump(vector<int> &nums)function canJump(nums)
def canJump(nums):
# Write your solution here
pass
class Solution {
public boolean canJump(int[] nums) {
// Write your solution here
return false;
}
}
#include <vector>
using namespace std;
bool canJump(vector<int> &nums) {
// Write your solution here
return false;
}
function canJump(nums) {
// Write your solution here
}
Common Bugs to Avoid
Wrong: falseFailing to update or track the maximum reachable index correctly.✅ Update maxReach = max(maxReach, i + nums[i]) and check if i <= maxReach during iteration.
Wrong: trueNot detecting when the path is blocked by zeros and maxReach stops short.✅ Return false if current index exceeds maxReach before reaching last index.
Wrong: trueConfusing 0/1 jump constraint, returning true when start position cannot move.✅ Return false if nums[0] == 0 and length > 1.
Wrong: falseOff-by-one error in jump range calculation causing premature failure.✅ Use inclusive range min(i + nums[i], last_index) when calculating furthest jump.
Wrong: TLEUsing exponential or quadratic backtracking instead of O(n) greedy approach.✅ Implement single pass greedy maxReach tracking to achieve O(n) time complexity.