Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Jump Game | Leetcode 55 | Greedy Solution
    Data Structures & Algorithms

    Jump Game | Leetcode 55 | Greedy Solution

    Anirudh KhuranaBy Anirudh Khurana21 August 2025Updated:16 November 2026No Comments6 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured image of a question to solve jump game
    Share
    Facebook Twitter LinkedIn Pinterest Email

    The Jump Game is a popular interview problem and a classic example of applying a Greedy Algorithm. It appears frequently in coding tests and competitive programming because it tests both logical reasoning and the ability to optimize efficiently.

    In this blog, we will carefully understand the problem, build the intuition for solving it, and then walk through the optimal Python implementation with explanation and complexity analysis.

    Here’s the [Problem Link] to begin with.


    Problem Statement – What Does the Problem Say?

    You are given an array nums where each element nums[i] represents the maximum jump length you can move forward from index i.

    Starting at index 0, your task is to determine whether you can reach the last index.

    Example 1:

    Input: nums = [2,3,1,1,4]
    Output: True
    Explanation:
    - Start at index 0 → you can jump at most 2 steps.
    - Jump to index 1 → nums[1] = 3 allows reaching further indices.
    - From index 1, you can reach the last index (4).
    So, the answer is True.

    Example 2:

    Input: nums = [3,2,1,0,4]
    Output: False
    Explanation:
    - Start at index 0 → you can jump at most 3 steps.
    - You can reach up to index 3.
    - But at index 3, nums[3] = 0, so you are stuck and cannot reach the last index.
    So, the answer is False.

    The challenge is to decide whether the last index can be reached while following the given jumping rules.

    Interactive Visualizer: Greedy Farthest Reach

    Choose an example or enter your own jump lengths, then press Next. Green cells are the indices we can already reach (up to maxIndex). If the loop ever lands on a cell outside that range, the answer is False.



    Intuition and Approach (Optimal – Greedy)

    The greedy approach is the most efficient way to solve this problem. The key idea is to keep track of the farthest index you can reach at every step.

    Why this works:

    • If at any index i you are beyond the farthest index you can reach, it means you are stuck → return False.
    • Otherwise, update the farthest reachable index as max(max_index, i + nums[i]).
    • If you make it through the entire array, then the last index is reachable.

    Steps to Solve:

    1. Initialize max_index = 0, which keeps track of the farthest position you can reach.
    2. Iterate through the array:
      • If i > max_index, it means you cannot even reach index i. Return False.
      • Otherwise, update max_index = max(max_index, i + nums[i]).
    3. If the loop finishes successfully, return True.

    This greedy choice ensures that you always know the maximum range you can reach at every point.


    Code Implementation

    Here’s the Python code for the Jump Game problem using the greedy method (with added comments for clarity):

    class Solution:
        def canJump(self, nums: List[int]) -> bool:
            max_index = 0  # farthest index we can reach
    
            for i in range(len(nums)):
                if i > max_index:
                    # if we are at an index we cannot reach
                    return False
                # update the farthest index reachable
                max_index = max(max_index, i + nums[i])
    
            return True
    class Solution {
        public boolean canJump(int[] nums) {
            int maxIndex = 0; // farthest index we can reach
    
            for (int i = 0; i < nums.length; i++) {
                if (i > maxIndex) {
                    // if we are at an index we cannot reach
                    return false;
                }
                // update the farthest index reachable
                maxIndex = Math.max(maxIndex, i + nums[i]);
            }
            return true;
        }
    }
    class Solution {
    public:
        bool canJump(vector<int>& nums) {
            int maxIndex = 0; // farthest index we can reach
    
            for (int i = 0; i < (int)nums.size(); i++) {
                if (i > maxIndex) {
                    // if we are at an index we cannot reach
                    return false;
                }
                // update the farthest index reachable
                maxIndex = max(maxIndex, i + nums[i]);
            }
            return true;
        }
    };

    Code Explanation

    • Start with max_index = 0, meaning at the beginning you can only stay at index 0.
    • For each index i, check:
      • If i is greater than max_index, it means you are stuck because you cannot even reach i.
      • Otherwise, extend the farthest reachable index using i + nums[i].
    • By the time the loop ends, if you have not returned False, it means you can reach the last index.

    For example:

    • nums = [2,3,1,1,4]
      • Start with max_index = 0.
      • At index 0, update max_index = max(0, 0+2) = 2.
      • At index 1, update max_index = max(2, 1+3) = 4.
      • At index 2, max_index remains 4.
      • At index 3, max_index remains 4.
      • At index 4, loop ends → reachable. Return True.

    Time and Space Complexity

    • Time Complexity:
      • We go through the array once.
      • O(n) where n = length of the array.
    • Space Complexity:
      • We only use one variable max_index.
      • O(1) constant extra space.

    This makes the solution optimal and efficient.


    Conclusion

    The Jump Game problem is a great demonstration of how Greedy Algorithms simplify complex problems. By keeping track of the farthest reachable index, we can solve the problem in O(n) time and O(1) space.

    This approach is both clean and efficient, making it the ideal solution for interviews and competitive programming.

    Frequently Asked Questions

    What is the Jump Game problem?

    You get an array nums where nums[i] is the maximum jump length from index i. Starting at index 0, you must decide whether you can reach the last index. For [2,3,1,1,4] the answer is True, and for [3,2,1,0,4] it is False.

    Why does the greedy approach work for Jump Game?

    If you can reach some index, you can also reach every index before it. So you only need to track the farthest reachable index, maxIndex. If the loop ever reaches an index greater than maxIndex, that index is unreachable and so is the last index.

    What is the time and space complexity of the greedy solution?

    O(n) time, because we scan the array once, and O(1) extra space, because we only store maxIndex.

    Can Jump Game be solved with dynamic programming?

    Yes. A DP solution marks each index as reachable or not by checking earlier indices, which takes O(n²) time. The greedy solution gives the same answer in O(n), so it is the one interviewers expect.

    What is the difference between Jump Game and Jump Game II?

    Jump Game (LeetCode 55) only asks whether the last index is reachable. Jump Game II (LeetCode 45) guarantees it is reachable and asks for the minimum number of jumps, which is also solved greedily.


    Join our Advance DSA COURSE

    For any changes to the article, kindly email at co**@**********ug.in or contact us at +91-9712928220.

    Greedy Algorithm Medium
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleN meetings in one room | Greedy Solution
    Next Article Jump Game II | Leetcode 45 | Recursive and Greedy Solution
    Anirudh Khurana
    • Website

    Anirudh Khurana is the founder of Code and Debug, an Indian EdTech platform that helps students and working professionals crack coding interviews and move into data and AI roles. He teaches DSA, Python, SQL, data engineering and machine learning through live bootcamps and his YouTube channel, Code and Debug, which has over 50,000 subscribers. Every guide on this blog is written or reviewed by him and tested with real code.

    Related Posts

    Data Structures & Algorithms

    DSA Interview Cheat Sheet: All Patterns You Need to Know

    4 April 2026
    Data Structures & Algorithms

    Symmetric Tree | Leetcode 101 | Recursive DFS Approach

    2 September 2025
    Data Structures & Algorithms

    Binary Tree Right Side View | Leetcode 199 | BFS and DFS Approaches

    2 September 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • AI & Machine Learning (8)
    • Data Structures & Algorithms (241)
      • Beginner (82)
      • Expert (52)
      • Intermediate (106)
    • Interview Questions (6)
    • Pandas (1)
    • Python (1)
    • Resume (1)
    • SQL (2)
    Recent Posts

    Normalization in DBMS: 1NF, 2NF and 3NF With SQL Examples

    25 November 2026

    100 Machine Learning Interview Questions and Answers (Freshers to Experienced)

    25 November 2026

    K-Means Clustering in Machine Learning: An Interactive Guide (with Python)

    20 November 2026

    Decision Tree in Machine Learning: An Interactive Guide (with Python)

    18 November 2026

    Support Vector Machine (SVM) in Machine Learning: An Interactive Guide

    16 November 2026
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.