Jump Game

Decide whether the last index is reachable by tracking the farthest index any reachable position can jump to.

Time O(n)Space O(1)Code C#
Watch it animatedStep through it, with the running line highlighted. Free with an account.

The idea

Every index up to farthest is reachable: you can always jump a shorter distance than the maximum. Walk left to right; from each reachable index i you could land anywhere up to i + nums[i], so stretch farthest to that. If the walk ever reaches an index beyond farthest, no earlier index can get there and you are stuck. If farthest covers the last index, you are done.

Code

public static bool CanJump(int[] nums) {
    int farthest = 0;
    for (int i = 0; i < nums.Length; i++) {
        if (i > farthest) return false;
        farthest = Math.Max(farthest, i + nums[i]);
        if (farthest >= nums.Length - 1) return true;
    }
    return false;
}

Tips

In the .NET library

Math.Max keeps the loop branch-free; the early return true matters on long inputs where the first few elements already reach the end.