Open In App

# Check if the end of the Array can be reached from a given position

Given an array arr[] of N positive integers and a number S, the task is to reach the end of the array from index S. We can only move from current index i to index (i + arr[i]) or (i – arr[i]). If there is a way to reach the end of the array then print “Yes” else print “No”.

Examples:

Input: arr[] = {4, 1, 3, 2, 5}, S = 1
Output: Yes
Explanation:
initial position: arr[S] = arr[1] = 1.
Jumps to reach the end: 1 -> 4 -> 5
Hence end has been reached.

Input: arr[] = {2, 1, 4, 5}, S = 2
Output: No
Explanation:
initial position: arr[S] = arr[2] = 2.
Possible Jumps to reach the end: 4 -> (index 7) or 4 -> (index -2)
Since both are out of bounds, Hence end can’t be reached.

Approach 1: This problem can be solved using Breadth First Search. Below are the steps:

• Consider start index S as the source node and insert it into the queue.
• While the queue is not empty do the following:
1. Pop the element from the top of the queue say temp.
2. If temp is already visited or it is array out of bound index then, go to step 1.
3. Else mark it as visited.
4. Now if temp is the last index of the array, then print “Yes”.
5. Else take two possible destinations from temp to (temp + arr[temp]), (temp – arr[temp]) and push it into the queue.
• If the end of the array is not reached after the above steps then print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to check if we can reach to``// the end of the arr[] with possible moves``void` `solve(``int` `arr[], ``int` `n, ``int` `start)``{` `    ``// Queue to perform BFS``    ``queue<``int``> q;` `    ``// Initially all nodes(index)``    ``// are not visited.``    ``bool` `visited[n] = { ``false` `};` `    ``// Initially the end of``    ``// the array is not reached``    ``bool` `reached = ``false``;` `    ``// Push start index in queue``    ``q.push(start);` `    ``// Until queue becomes empty``    ``while` `(!q.empty()) {` `        ``// Get the top element``        ``int` `temp = q.front();` `        ``// Delete popped element``        ``q.pop();` `        ``// If the index is already``        ``// visited. No need to``        ``// traverse it again.``        ``if` `(visited[temp] == ``true``)``            ``continue``;` `        ``// Mark temp as visited``        ``// if not``        ``visited[temp] = ``true``;``        ``if` `(temp == n - 1) {` `            ``// If reached at the end``            ``// of the array then break``            ``reached = ``true``;``            ``break``;``        ``}` `        ``// If temp + arr[temp] and``        ``// temp - arr[temp] are in``        ``// the index of array``        ``if` `(temp + arr[temp] < n) {``            ``q.push(temp + arr[temp]);``        ``}` `        ``if` `(temp - arr[temp] >= 0) {``            ``q.push(temp - arr[temp]);``        ``}``    ``}` `    ``// If reaches the end of the array,``    ``// Print "Yes"``    ``if` `(reached == ``true``) {``        ``cout << ``"Yes"``;``    ``}` `    ``// Else print "No"``    ``else` `{``        ``cout << ``"No"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given array arr[]``    ``int` `arr[] = { 4, 1, 3, 2, 5 };` `    ``// Starting index``    ``int` `S = 1;``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function Call``    ``solve(arr, N, S);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to check if we can reach to``// the end of the arr[] with possible moves``static` `void` `solve(``int` `arr[], ``int` `n, ``int` `start)``{` `    ``// Queue to perform BFS``    ``Queue q = ``new` `LinkedList<>();` `    ``// Initially all nodes(index)``    ``// are not visited.``    ``boolean` `[]visited = ``new` `boolean``[n];` `    ``// Initially the end of``    ``// the array is not reached``    ``boolean` `reached = ``false``;` `    ``// Push start index in queue``    ``q.add(start);` `    ``// Until queue becomes empty``    ``while` `(!q.isEmpty())``    ``{` `        ``// Get the top element``        ``int` `temp = q.peek();` `        ``// Delete popped element``        ``q.remove();` `        ``// If the index is already``        ``// visited. No need to``        ``// traverse it again.``        ``if` `(visited[temp] == ``true``)``            ``continue``;` `        ``// Mark temp as visited``        ``// if not``        ``visited[temp] = ``true``;``        ` `        ``if` `(temp == n - ``1``)``        ``{` `            ``// If reached at the end``            ``// of the array then break``            ``reached = ``true``;``            ``break``;``        ``}` `        ``// If temp + arr[temp] and``        ``// temp - arr[temp] are in``        ``// the index of array``        ``if` `(temp + arr[temp] < n)``        ``{``            ``q.add(temp + arr[temp]);``        ``}` `        ``if` `(temp - arr[temp] >= ``0``)``        ``{``            ``q.add(temp - arr[temp]);``        ``}``    ``}` `    ``// If reaches the end of the array,``    ``// Print "Yes"``    ``if` `(reached == ``true``)``    ``{``        ``System.out.print(``"Yes"``);``    ``}` `    ``// Else print "No"``    ``else``    ``{``        ``System.out.print(``"No"``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given array arr[]``    ``int` `arr[] = { ``4``, ``1``, ``3``, ``2``, ``5` `};` `    ``// Starting index``    ``int` `S = ``1``;``    ``int` `N = arr.length;` `    ``// Function call``    ``solve(arr, N, S);``}``}` `// This code is contributed by gauravrajput1`

## Python3

 `# Python3 program for the above approach``from` `queue ``import` `Queue`` ` `# Function to check if we can reach to``# the end of the arr[] with possible moves``def` `solve(arr, n, start):`` ` `    ``# Queue to perform BFS``    ``q ``=` `Queue()`` ` `    ``# Initially all nodes(index)``    ``# are not visited.``    ``visited ``=` `[``False``] ``*` `n``   ` `    ``# Initially the end of``    ``# the array is not reached``    ``reached ``=` `False``   ` `    ``# Push start index in queue``    ``q.put(start);`` ` `    ``# Until queue becomes empty``    ``while` `(``not` `q.empty()):`` ` `        ``# Get the top element``        ``temp ``=` `q.get()`` ` `        ``# If the index is already``        ``# visited. No need to``        ``# traverse it again.``        ``if` `(visited[temp] ``=``=` `True``):``            ``continue`` ` `        ``# Mark temp as visited, if not``        ``visited[temp] ``=` `True``        ``if` `(temp ``=``=` `n ``-` `1``):`` ` `            ``# If reached at the end``            ``# of the array then break``            ``reached ``=` `True``            ``break`` ` `        ``# If temp + arr[temp] and``        ``# temp - arr[temp] are in``        ``# the index of array``        ``if` `(temp ``+` `arr[temp] < n):``            ``q.put(temp ``+` `arr[temp])`` ` `        ``if` `(temp ``-` `arr[temp] >``=` `0``):``            ``q.put(temp ``-` `arr[temp])`` ` `    ``# If reaches the end of the array,``    ``# Print "Yes"``    ``if` `(reached ``=``=` `True``):``        ``print``(``"Yes"``)`` ` `    ``# Else print "No"``    ``else``:``        ``print``(``"No"``)`` ` `# Driver code``if` `__name__ ``=``=` `'__main__'``:`` ` `    ``# Given array arr[]``    ``arr ``=` `[ ``4``, ``1``, ``3``, ``2``, ``5` `]`` ` `    ``# starting index``    ``S ``=` `1``    ``N ``=` `len``(arr)`` ` `    ``# Function call``    ``solve(arr, N, S)`` ` `# This code is contributed by himanshu77`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to check if we can reach to``// the end of the []arr with possible moves``static` `void` `solve(``int` `[]arr, ``int` `n,``                  ``int` `start)``{` `    ``// Queue to perform BFS``    ``Queue<``int``> q = ``new` `Queue<``int``>();` `    ``// Initially all nodes(index)``    ``// are not visited.``    ``bool` `[]visited = ``new` `bool``[n];` `    ``// Initially the end of``    ``// the array is not reached``    ``bool` `reached = ``false``;` `    ``// Push start index in queue``    ``q.Enqueue(start);` `    ``// Until queue becomes empty``    ``while` `(q.Count != 0)``    ``{` `        ``// Get the top element``        ``int` `temp = q.Peek();` `        ``// Delete popped element``        ``q.Dequeue();` `        ``// If the index is already``        ``// visited. No need to``        ``// traverse it again.``        ``if` `(visited[temp] == ``true``)``            ``continue``;` `        ``// Mark temp as visited``        ``// if not``        ``visited[temp] = ``true``;``        ` `        ``if` `(temp == n - 1)``        ``{` `            ``// If reached at the end``            ``// of the array then break``            ``reached = ``true``;``            ``break``;``        ``}` `        ``// If temp + arr[temp] and``        ``// temp - arr[temp] are in``        ``// the index of array``        ``if` `(temp + arr[temp] < n)``        ``{``            ``q.Enqueue(temp + arr[temp]);``        ``}` `        ``if` `(temp - arr[temp] >= 0)``        ``{``            ``q.Enqueue(temp - arr[temp]);``        ``}``    ``}` `    ``// If reaches the end of the array,``    ``// Print "Yes"``    ``if` `(reached == ``true``)``    ``{``        ``Console.Write(``"Yes"``);``    ``}` `    ``// Else print "No"``    ``else``    ``{``        ``Console.Write(``"No"``);``    ``}``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array []arr``    ``int` `[]arr = { 4, 1, 3, 2, 5 };` `    ``// Starting index``    ``int` `S = 1;``    ``int` `N = arr.Length;` `    ``// Function call``    ``solve(arr, N, S);``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``

Output

```Yes

```

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

### Recursive approach:

Approach:

In this approach, we recursively check if we can reach the end of the array by making valid jumps from the current position.

Define a function can_reach_end that takes an array arr and a starting position pos.
Check the base case: If the current position is the last index of the array, then we can reach the end, so return True.
Find the maximum number of steps we can take from the current position without going out of bounds, i.e., max_jump.
Loop through all possible next positions, starting from pos + 1 up to max_jump.
For each next position, recursively call can_reach_end with the next position as the new starting position.
If any of the recursive calls return True, then we can reach the end, so return True.
If we can’t reach the end from any of the possible jumps, return False.

## Java

 `import` `java.io.*;``public` `class` `GFG``{``    ``public` `static` `boolean` `canReachEnd(``int``[] arr, ``int` `pos) {``        ``// Base case: If the current position is the last index of the array then we can reach the end.``        ``if` `(pos == arr.length - ``1``) {``            ``return` `true``;``        ``}``        ``// Recursive case: Try all possible jumps from the current position.``        ``int` `maxJump = Math.min(pos + arr[pos], arr.length - ``1``);``        ``for` `(``int` `nextPos = pos + ``1``; nextPos <= maxJump; nextPos++) {``            ``if` `(canReachEnd(arr, nextPos)) {``                ``return` `true``;``            ``}``        ``}``        ``// If we can't reach the end from any of the possible jumps return false.``        ``return` `false``;``    ``}``    ``public` `static` `void` `main(String[] args) {``        ``int``[] arr = {``4``, ``1``, ``3``, ``2``, ``5``};``        ``int` `startPos = ``1``;``        ``// Example usage``        ``if` `(canReachEnd(arr, startPos)) {``            ``System.out.println(``"Yes"``);``        ``} ``else` `{``            ``System.out.println(``"No"``);``        ``}``    ``}``}`

## Python3

 `def` `can_reach_end(arr, pos):``    ``# Base case: If the current position is the last index of the array, then we can reach the end.``    ``if` `pos ``=``=` `len``(arr) ``-` `1``:``        ``return` `True``    ` `    ``# Recursive case: Try all possible jumps from the current position.``    ``max_jump ``=` `min``(pos ``+` `arr[pos], ``len``(arr) ``-` `1``)``    ``for` `next_pos ``in` `range``(pos ``+` `1``, max_jump ``+` `1``):``        ``if` `can_reach_end(arr, next_pos):``            ``return` `True``    ` `    ``# If we can't reach the end from any of the possible jumps, return False.``    ``return` `False` `# Example usage``arr ``=` `[``4``, ``1``, ``3``, ``2``, ``5``]``start_pos ``=` `1``if` `can_reach_end(arr, start_pos):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)`

## C#

 `using` `System;` `public` `class` `GFG``{``    ``public` `static` `bool` `CanReachEnd(``int``[] arr, ``int` `pos)``    ``{``        ``// Base case: If the current position is the last``        ``// index of the array then we can reach the end.``        ``if` `(pos == arr.Length - 1)``        ``{``            ``return` `true``;``        ``}` `        ``// Recursive case: Try all possible jumps from the current position.``        ``int` `maxJump = Math.Min(pos + arr[pos], arr.Length - 1);``        ``for` `(``int` `nextPos = pos + 1; nextPos <= maxJump; nextPos++)``        ``{``            ``if` `(CanReachEnd(arr, nextPos))``            ``{``                ``return` `true``;``            ``}``        ``}` `        ``// If we can't reach the end from any of the possible jumps, return false.``        ``return` `false``;``    ``}` `    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 4, 1, 3, 2, 5 };``        ``int` `startPos = 1;` `        ``// Example usage``        ``if` `(CanReachEnd(arr, startPos))``        ``{``            ``Console.WriteLine(``"Yes"``);``        ``}``        ``else``        ``{``            ``Console.WriteLine(``"No"``);``        ``}``    ``}``}`

## Javascript

 `function` `canReachEnd(arr, pos) {``    ``// Base case: If the current position is the last index of the array then we can reach the end.``    ``if` `(pos == arr.length - 1) {``        ``return` `true``;``    ``}``    ``// Recursive case: Try all possible jumps from the current position.``    ``let maxJump = Math.min(pos + arr[pos], arr.length - 1);``    ``for` `(let nextPos = pos + 1; nextPos <= maxJump; nextPos++) {``        ``if` `(canReachEnd(arr, nextPos)) {``            ``return` `true``;``        ``}``    ``}``    ``// If we can't reach the end from any of the possible jumps return false.``    ``return` `false``;``}` `let arr = [4, 1, 3, 2, 5];``let startPos = 1;``// Example usage``if` `(canReachEnd(arr, startPos)) {``    ``console.log(``"Yes"``);``} ``else` `{``    ``console.log(``"No"``);``}`

Output

```Yes

```

Time Complexity: O(2^n) where n is the length of the array.
Space Complexity: O(n) where n is the length of the array.