Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Jump Game problem solution

YASH PAL, 31 July 2024

In this Leetcode Jump Game problem solution, we have given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.

leetcode jump game problem solution

Problem solution in Python.

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        l = 0
        r = nums[l]
        x = len(nums)-1
        while(r<x):
            if l == r: return False
            l, r = r, max(i+nums[i] for i in range(l, r+1))
            
        return True

Problem solution in Java.

public class Solution {
    public boolean canJump(int[] nums) {
        int i = 0;
        while(i<nums.length)
        {
            if(nums[i] == 0&&i!=nums.length-1)
            {
                return false;
            }
            int max = Integer.MIN_VALUE;
            int next = i+1;
            for(int j = 1;j<nums[i]+1;j++)
            {
                if(i+j>nums.length-1)
                {
                    return true;
                }
                if(nums[i+j]+j-1>max)
                {
                    max = nums[i+j]+j-1;
                    next = i+j;
                }
            }
            i = next;
        }
        return true;
    }
}

Problem solution in C++.

class Solution {
public:
    bool canJump(vector<int>& nums) {
        if(nums.size()==1) return true  ; 
        if(!nums.size()) return false   ;
        int maxx = 0    ; 
        bool ans = true ;
        for(int i = 0 ; i < nums.size() ; i ++ )
        {
            if( nums[i] ) maxx = max( maxx , i + nums[i] ) ;
            else 
            {
                if( maxx <= i && i != nums.size() - 1 ) {ans = false ; break ; }
            }
        }
        return ans ;
    }
};

Problem solution in C.

bool canJump(int* nums, int numsSize) {

    int step=nums[0],i=0;
    
    while(step>0 && i<numsSize)
    {
        i++;
        step--;
        step = step>nums[i]? step:nums[i];
    }
    
    if(i<numsSize-1)
        return false;
    return true;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes