Open In App

Check if last index can be reached by jumping atmost jump[i] in each position

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of jumps[] of length N, where each element denotes the maximum length of a jump forward from each index. The task is to find if it is possible to reach the last index.

Examples:

Input: arr[] = {2, 3, 1, 1, 4}
Output: True
Explanation: Possible ways to reach last index are:
(0->2->3->4), (0->2->3->1->1->4), (0->2->3->1->4) and (0->2->1->1->4).

Input:  arr[] = {3, 2, 1, 0, 4}
Output: False
Explanation: There is no way to reach last index.

 

Approach: The idea to solve the problem is as mentioned below:

We know that the last index is always reachable from itself. Assume that destination is jumping towards the first index. So the basic task is to check if the last possible index is reachable from the current index and update the last possible index accordingly.

Follow the below steps to solve the problems:

  • Maintain a variable LastAccuratePos (initialized to N-1) from which we can reach the last position.
  • Now start iterating the input array jumps[] from the right (second last position) to left.
    • In each iteration, calculate FurthestJump which is the summation of the index and the value at that index (i.e jumps[i]+i).
    • Check if FurthestJump is greater than or equal to LastAccuratePos. If yes, then we will update the value of LastAccuratePos with the current index.
  • After the iteration, check if LastAccuratePos is 0 then return True, else return False.

Below is the implementation of the above approach.

C++14




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if
// last index can be reached
bool isPossibleLastIndex(int* jumps, int& n)
{
    // Variables to destination index and
    // the furthest jump possible
    int LastAccuratePos = n - 1, FurthestJump = 0;
 
    for (int i = n - 2; i >= 0; i--) {
 
        // Furthest jump possible from index i
        FurthestJump = jumps[i] + i;
 
        // If this furthest jump is able to reach
        // current destination then update destination
        // to the current index
        if (FurthestJump >= LastAccuratePos)
            LastAccuratePos = i;
    }
 
    // Check if destination successfully reached 0
    return LastAccuratePos == 0;
}
 
// Driver code
int main()
{
    int jumps[] = { 2, 3, 1, 1, 4 };
    int N = sizeof(jumps) / sizeof(jumps[0]);
 
    // Function call
    if (isPossibleLastIndex(jumps, N))
        cout << "True";
    else
        cout << "False";
 
    return 0;
}


Java




// Java code to implement the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if last index can be reached
    public static boolean isPossibleLastIndex(int[] jumps,
                                              int n)
    {
 
        // Variables to destination index and the furthest
        // jump possible
        int LastAccuratePos = n - 1;
        int FurthestJump = 0;
 
        for (int i = n - 2; i >= 0; i--) {
            // Furthest jump possible from index i
            FurthestJump = jumps[i] + i;
 
            // If this furthest jump is able to reach
            // current destination then update destination
            // to the current index
            if (FurthestJump >= LastAccuratePos)
                LastAccuratePos = i;
        }
        // Check if destination successfully reached 0
        return LastAccuratePos == 0;
    }
 
    public static void main(String[] args)
    {
        int[] jumps = { 2, 3, 1, 1, 4 };
        int N = jumps.length;
 
        // Function call
        if (isPossibleLastIndex(jumps, N))
            System.out.println("True");
        else
            System.out.println("False");
    }
}
 
// This code is contributed by lokesh (lokeshmvs21).


Python3




# Python3 code to implement the approach
 
# Function to check if
# last index can be reached
def isPossibleLastIndex(jumps, n) :
 
    # Variables to destination index and
    # the furthest jump possible
    LastAccuratePos = n - 1; FurthestJump = 0;
 
    for i in range(n-2,-1,-1) :
 
        # Furthest jump possible from index i
        FurthestJump = jumps[i] + i;
 
        # If this furthest jump is able to reach
        # current destination then update destination
        # to the current index
        if (FurthestJump >= LastAccuratePos) :
            LastAccuratePos = i;
 
    # Check if destination successfully reached 0
    if (LastAccuratePos == 0) :
        return True;
 
# Driver code
if __name__ == "__main__" :
 
    jumps = [ 2, 3, 1, 1, 4 ];
    N = len(jumps);
 
    # Function call
    if (isPossibleLastIndex(jumps, N)) :
        print("True");
    else :
        print("False");
     
    # This code is contributed by AnkThon


C#




using System;
 
public class GFG{
 
  // Function to check if
  // last index can be reached
  public static bool isPossibleLastIndex(int[] jumps, int n)
  {
     
    // Variables to destination index and
    // the furthest jump possible
    int LastAccuratePos = n - 1;
    int FurthestJump = 0;
 
    for (int i = n - 2; i >= 0; i--) {
 
      // Furthest jump possible from index i
      FurthestJump = jumps[i] + i;
 
      // If this furthest jump is able to reach
      // current destination then update destination
      // to the current index
      if (FurthestJump >= LastAccuratePos)
        LastAccuratePos = i;
    }
 
    // Check if destination successfully reached 0
    return LastAccuratePos == 0;
  }
 
 
  static public void Main (){
 
    int[] jumps = { 2, 3, 1, 1, 4 };
    int N = jumps.Length;
 
    // Function call
    if (isPossibleLastIndex(jumps, N))
      Console.WriteLine("True");
    else
      Console.WriteLine("False");
 
  }
}
 
// This code is contributed by akashish__


Javascript




<script>
 
// Javascript code to implement the approach
 
// Function to check if
// last index can be reached
function isPossibleLastIndex(jumps,n)
{
    // Variables to destination index and
    // the furthest jump possible
    let LastAccuratePos = n - 1;
    let FurthestJump = 0;
 
    for (let i = n - 2; i >= 0; i--) {
 
        // Furthest jump possible from index i
        FurthestJump = jumps[i] + i;
 
        // If this furthest jump is able to reach
        // current destination then update destination
        // to the current index
        if (FurthestJump >= LastAccuratePos)
            LastAccuratePos = i;
    }
 
    // Check if destination successfully reached 0
    return LastAccuratePos == 0;
}
 
// Driver code
let jumps = [2, 3, 1, 1, 4];
let N = jumps.length;
 
// Function call
if (isPossibleLastIndex(jumps, N))
    console.log("True");
else
    console.log("False");
 
// This code is contributed by akashish__
 
</script>


Output

True

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



Last Updated : 21 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads