You are given an integer array nums. You start at index 0, and each element represents your maximum jump length from that position. Return true if you can reach the last index, or false otherwise.
nums = [2,3,1,1,4]truenums = [3,2,1,0,4]falsemaxReach = 0 (furthest index reachable so far)i > maxReach: index unreachable → return falsemaxReach = max(maxReach, i + nums[i])trueWe never need to track exact jump paths. All we care about is the farthest index reachable from any position visited so far. If index i is within that range, we can reach it — and from it, potentially push the frontier further. The moment i exceeds our frontier, no combination of earlier jumps can save us.
Trying every possible jump from every index explodes, and even dynamic programming is O(n²). We only need to know the farthest index we can reach.
Keep a variable, far, for the farthest reachable index so far. At each index, if i is beyond far, we are stuck, so return false. Otherwise update far with i plus the jump length, and return true once far reaches the end.
Track only the farthest reachable index. If you ever stand beyond it, you are stuck.
Run the animation above on your own input, or watch the 1:18 video explanation of Jump Game with a dry run and Java code.