End of Array ReachableLoading saved progress…

End of Array Reachable

You're given an array of non-negative integers. You start standing on the first index. The number at each index is the maximum jump length you can take from there — from index i you may step forward anywhere from 1 to nums[i] indices (a 0 means you can't move at all). Picture a row of stepping stones across a river, where each stone is marked with how far you're allowed to leap from it. Return true if there's some sequence of jumps that lands you on (or past) the last index, and false if you'd always get stranded. This is the classic Jump Game problem.

Signature

// nums: non-negative integers; nums[i] is the MAX jump length from index i
// returns: true if the last index is reachable from index 0, else false
function arrayReachableEnd(nums: number[]): boolean;

Examples

arrayReachableEnd([2, 3, 1, 1, 4]);
// → true
// From index 0 jump 1 to index 1 (value 3), then jump 3 to index 4. Reached.
arrayReachableEnd([3, 2, 1, 0, 4]);
// → false
// Every route lands on index 3, whose value is 0. You're stuck before the end.

Notes

  • Start at index 0. You always begin on the first stone, even if its value is 0.
  • nums[i] is a maximum, not an exact distance. From a stone marked 3 you may jump 1, 2, or 3 forward — whatever helps.
  • Reach the LAST index. You don't need to land exactly on it from a single jump; you just need to be able to stand on it (overshooting the final index also counts as reaching it).
  • A single-element array is true. You're already standing on the last index.
  • Zeros can trap you. A 0 is only fatal if you have no way to jump over or past it from an earlier stone.
Loading editor…