Jump Game

Medium
77.

Jump Game

Jump Game is a medium coding problem on arrays, dynamic programming and greedy, asked in Morgan Stanley interviews.

You are given an integer array nums. You start at the first index; each element is your maximum jump length from that position.

Return true if you can reach the last index.

Example 1

Input
nums = [2,3,1,1,4]
Output
true
Explanation
Jump 1 step to index 1, then 3 steps to the end.

Example 2

Input
nums = [3,2,1,0,4]
Output
false
Explanation
You always land on index 3, whose jump length is 0.

Constraints

  • 1 <= nums.length <= 30
  • 0 <= nums[i] <= 5
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Jump Game — Solution in Python, Java & C++

Jump Game solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem, asked at Morgan Stanley.

Jump Game is a medium coding problem on arrays, dynamic programming and greedy, asked in Morgan Stanley interviews.

Problem statement

You are given an integer array nums. You start at the first index; each element is your maximum jump length from that position.

Return true if you can reach the last index.

Example 1

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step to index 1, then 3 steps to the end.

Example 2

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You always land on index 3, whose jump length is 0.

Constraints

  • 1 <= nums.length <= 30
  • 0 <= nums[i] <= 5

Hints for Jump Game

  1. Hint 1

    Track the furthest index reachable so far.

  2. Hint 2

    If the current index is beyond that reach, you're stuck.

How to solve Jump Game

You never need to know which jumps to take — only how far it is possible to get. Sweep left to right carrying a single number, reach, the furthest index any sequence of jumps could land on so far. If the sweep ever reaches an index beyond reach, that index is unreachable and the answer is no.

Approach

  1. Start with reach = 0.
  2. Walk i from 0 to the last index.
  3. If i > reach, no jump sequence can get here — return false.
  4. Otherwise update reach = max(reach, i + nums[i]).
  5. Surviving the whole walk means the last index was reachable — return true.

Why it works

The set of reachable indices is a prefix: if you can reach index i, you can reach every index below it, because a jump of length nums[i] allows any shorter hop too. So the reachable set is fully described by its maximum, reach, and a single number is enough state. Since i <= reach means i is genuinely reachable, extending with i + nums[i] is legitimate, and the first i exceeding reach is a real wall — everything beyond it is cut off as well.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • A 0 in the array is only fatal if it sits exactly at the current reach — zeros you can jump over are harmless.
  • Check i > reach before using nums[i], or you extend the reach from a position you never actually got to.
  • A single-element array is trivially true: you are already at the last index.

Reference solution

Python

from typing import List

def canJump(nums: List[int]) -> bool:
    reach = 0
    for i, x in enumerate(nums):
        if i > reach:
            return False
        reach = max(reach, i + x)
    return True

Java

public static boolean canJump(int[] nums) {
    int reach = 0;
    for (int i = 0; i < nums.length; i++) {
        if (i > reach) return false;
        if (i + nums[i] > reach) reach = i + nums[i];
    }
    return true;
}

C++

bool canJump(vector<int>& nums) {
    int reach = 0;
    for (int i = 0; i < (int) nums.size(); i++) {
        if (i > reach) return false;
        if (i + nums[i] > reach) reach = i + nums[i];
    }
    return true;
}

JavaScript

var canJump = function(nums) {
    let reach = 0;
    for (let i = 0; i < nums.length; i++) {
        if (i > reach) return false;
        reach = Math.max(reach, i + nums[i]);
    }
    return true;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Dynamic Programming