Jump Game | Practice | GeeksforGeeks

Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.

Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0} 
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach 
4th index, and now jump 2 steps to reach
the end.

Example 2:
Input:
N = 3
A[] =  {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.


Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 105
0 <= A[i] <= 105
0
shrustis1763 weeks ago
C++ solution:
Approach: Traverse the array from back, its like moving from Dest to Source
 
int canReach(int A[], int N) 
{
int lastInd=N-1, i;      for(i=N-1; i>=0; i--)      {          if(i+A[i] >= lastInd)          {              lastInd=i;          }                }     if(lastInd ==0)           return 1;       else           return 0;   }
0
imranwahid1 month ago
Easy C++ solution
0
hrithikraina20011 month ago
int maxreach= A[0];
        if(A[0]==0)
        {
            return 0;
        }
        if(N==1)
        {
            return 1;
        }
        
        int steps= A[0];
        int i=1;
        while(i<N)
        {
            if(i==N-1)
            {
                return 1;
            }
            steps--;
            
            maxreach= max(maxreach, i+ A[i]);
            if(steps==0)
            {
                if(i>= maxreach)
                {
                    return 0;
                }
                steps= maxreach-i;
            
               
            }
            i++;
            
        }
        return 0;
0
pratiksehjpal20001 month ago
class Solution {   static int canReach(int[] A, int N) {       int rec=0;       for(int i=0;i<N;i++)       {           if(rec<i)           {               return 0;           }           rec=Math.max(rec,i+A[i]);       }       return 1;   }};
0
swapniltayal4221 month ago
  public:   int canReach(int A[], int N) {       // code here       int sum = 0;       int index = 0;              for (int i=0; i<N; i++){           if (i > index){               return 0;           }           sum = A[i] + i;           if (index < sum){               index = sum;           }       }return 1;   }
0
vishalgupta799971 month ago
using DFS approach TC:-O(N)
bool dfs(int A[],int N,int idx){   if(idx>=N-1)   {       return true;   }   if(A[idx]==0)   {       return false;   }   int ele=A[idx];      for(int i=1;i<=ele;i++)   {       if(dfs(A,N,idx+i))return true;   }   return false;}class Solution { public:   int canReach(int A[], int N) {       // code here       return dfs(A,N,0);      // return 0;   }
+1
subhamraidec1 month ago
jay shree ram
 
class Solution { public:   int canReach(int A[], int N) {       // code here   int step=0,i=0;   //step+=A[0];   while(i<N){       if(step!=0 && step>=A[i]){                      step--;       }       else if(step!=0 && step<A[i]){           step=A[i];                             step--;       }       else if(step==0 && A[i]==0 && i!=N-1){           return 0;       }       else if(step==0 && A[i]!=0){           step=A[i];                  step--;              }       i++;           }   return 1;                 }};
+1
observer2 months ago
int canReach(int A[], int N) {
        // code here
        int jump = 0,index = 0;
        for(int i = 0; i < N; i++){
            if(i > index)
                return 0;
            jump = A[i] + i;
            if(index < jump)
                index = jump;
        }
        if(index >= N-1)
            return 1;
    }
0
mohnlal2 months ago
C++ Soln (0.2/1.3) 
TC → O(n)
SC → O(1)
 
int v = A[0]-1;for(int i=1;i<N-1;i++) {   v = max(A[i], v);   if(v == 0)       return 0;   v--;}return 1;
0
abhinavsinghbiz2 months ago
C++ Code; O(N) && O(1); Run Time=0.2!    
int canReach(int arr[], int n) {       int maxreach=0;       int reach=0;       for(int i=0;i<n;i++){           if(i>maxreach){               return 0;           }           reach=arr[i]+i;           if(reach>maxreach){               maxreach=reach;           }       }       if(maxreach>=n-1){           return 1;       }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.