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:** 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:
- Pop the element from the top of the queue say
**temp**. - If
**temp**is already visited or it is array out of bound index then, go to step 1. - Else mark it as visited.
- Now if
**temp**is the last index of the array, then print**“Yes”**. - Else take two possible destinations from temp to
**(temp + arr[temp])**,**(temp – arr[temp])**and push it into the queue.

- Pop the element from the top of the queue say
- 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 <bits/stdc++.h> ` `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); ` ` ` ` ` `// Untill 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; ` `} ` |

*chevron_right*

*filter_none*

## 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); ` ` ` ` ` `# Untill 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 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count all possible position that can be reached by Modified Knight
- Maximum number of nodes which can be reached from each node in a graph.
- Check if frequency of each character is equal to its position in English Alphabet
- Find the position of the given row in a 2-D array
- Reverse an array upto a given position
- Find the position of the last removed element from the array
- Sort an array without changing position of negative numbers
- Count of right shifts for each array element to be in its sorted position
- Reverse a subarray of the given array to minimize the sum of elements at even position
- Shuffle the position of each Array element by swapping adjacent elements
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Check whether an array can be fit into another array rearranging the elements in the array
- Check if it is possible to make array increasing or decreasing by rotating the array
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Check if an array can be Arranged in Left or Right Positioned Array
- Check if each element of an Array is the Sum of any two elements of another Array
- Check whether a given array is a k sorted array or not
- Check if an array is Wave Array
- Check whether an Array is Subarray of another Array
- Check if Array elements can be maximized upto M by adding all elements from another array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.