# Check if it is possible to reach to the index with value K when start index is given

Given an array arr[] of N positive integers and two positive integers S and K, the task is to reach the position of the array whose value is K 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 position of the array whose value is K then print “Yes” else print “No”.

Examples:

Input: arr[] = {4, 2, 3, 0, 3, 1, 2}, S = 5, K = 0.
Output: Yes
Explanation:
Initially we are standing at index 5 that is element 1 hence we can move one step forward or backward. Therefore, all possible ways to reach at index 3 with value 0 are:
index 5 -> index 4 -> index 1 -> index 3
index 5 -> index 6 -> index 4 -> index 1 -> index 3. Since it is possible to reach index 3 our output is yes.

Input: arr[] = {0, 3, 2, 1, 2}, S = 2, K = 3
Output: No
Explanation:
There is no way to reach index 1 with value 3.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 – Using BFS The Breadth-first search(BFS) approach is discussed below:

• 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 index of the array whose value is K, 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 index with value K is not reached after the above steps then print “No”.

Below is the implementation of the above approach:

 `// C++ program for the above approach  ` `#include   ` `using` `namespace` `std;  ` ` `  `// BFS approach to check if traversal  ` `// is possible or not  ` `bool` `check(``int` `arr[], ``int``& s_start,  ` `        ``int` `start, ``bool` `visited[],  ` `        ``int` `size, ``int` `K)  ` `{  ` `    ``queue<``int``> q;  ` ` `  `    ``// Push start index into queue  ` `    ``q.push(start);  ` ` `  `    ``// Until queue is not empty  ` `    ``while` `(!q.empty()) {  ` ` `  `        ``// Top element of queue  ` `        ``int` `front = q.front();  ` ` `  `        ``// Pop the topmost element  ` `        ``q.pop();  ` ` `  `        ``// mark as visited  ` `        ``visited[front] = ``true``;  ` ` `  `        ``if` `(arr[front] == K  ` `            ``&& front != s_start) {  ` `            ``return` `true``;  ` `        ``}  ` ` `  `        ``// Check for i + arr[i]  ` `        ``if` `(front + arr[front] >= 0  ` `            ``&& front + arr[front] < size  ` `            ``&& visited[front + arr[front]]  ` `                ``== ``false``) {  ` ` `  `            ``q.push(front + arr[front]);  ` `        ``}  ` ` `  `        ``// Check for i - arr[i]  ` `        ``if` `(front - arr[front] >= 0  ` `            ``&& front - arr[front] < size  ` `            ``&& visited[front - arr[front]]  ` `                ``== ``false``) {  ` ` `  `            ``q.push(front - arr[front]);  ` `        ``}  ` `    ``}  ` ` `  `    ``return` `false``;  ` `}  ` ` `  `// Function to check if index with value  ` `// K can be reached or not  ` `void` `solve(``int` `arr[], ``int` `n, ``int` `start,  ` `        ``int` `K)  ` `{  ` ` `  `    ``// Initialize visited array  ` `    ``bool` `visited[n] = { ``false` `};  ` ` `  `    ``// BFS Traversal  ` `    ``bool` `ans = check(arr, start, start,  ` `                    ``visited, n, K);  ` ` `  `    ``// Print the result  ` `    ``if` `(ans)  ` `        ``cout << ``"Yes"``;  ` `    ``else` `        ``cout << ``"No"``;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``// Given array arr[]  ` `    ``int` `arr[] = { 3, 0, 2, 1, 2 };  ` ` `  `    ``// Given start and end  ` `    ``int` `start = 2;  ` `    ``int` `K = 0;  ` ` `  `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);  ` ` `  `    ``// Function Call  ` `    ``solve(arr, N, start, K);  ` `    ``return` `0;  ` `}  `

Output:

```No
```

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

Method 2 – Using DFS: The Depth-first search(DFS) approach is discussed below:

1. Initialize an array visited[] to mark visited vertex as true.
2. Start with the start index S and explore other index depth-wise using recursion.
3. In each recursive call, we check if that index is valid or previously not visited. If not so, we return false.
4. Else if that index value is K, we return true.
5. Else mark that index as visited and process recursively for i + arr[i] and i – arr[i] from current index i.
6. If any of the recursive calls return true, this means that we reach to index with value K is possible and we print “Yes” Else print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach  ` `#include   ` `using` `namespace` `std;  ` ` `  `// DFS approach to check if traversal  ` `// is possible or not  ` `bool` `check(``int` `arr[], ``int``& s_start,  ` `        ``int` `start, ``bool` `visited[],  ` `        ``int` `size, ``int` `K)  ` `{  ` `    ``// Base cases  ` `    ``if` `(start < 0 || start >= size  ` `        ``|| visited[start]) {  ` `        ``return` `false``;  ` `    ``}  ` ` `  `    ``// Check if start index value K  ` `    ``if` `(arr[start] == K  ` `        ``&& start != s_start) {  ` `        ``return` `true``;  ` `    ``}  ` ` `  `    ``// Mark as visited  ` `    ``visited[start] = ``true``;  ` ` `  `    ``// Check for both i + arr[i]  ` `    ``// and i - arr[i] recursively  ` `    ``return` `(check(arr, s_start,  ` `                ``start + arr[start],  ` `                ``visited, size, K)  ` ` `  `            ``|| check(arr, s_start,  ` `                    ``start - arr[start],  ` `                    ``visited, size, K));  ` `}  ` ` `  `// Function to check if index with value  ` `// K can be reached or not  ` `void` `solve(``int` `arr[], ``int` `n, ``int` `start,  ` `        ``int` `K)  ` `{  ` ` `  `    ``// Initialize visited array  ` `    ``bool` `visited[n] = { ``false` `};  ` ` `  `    ``// DFS Traversal  ` `    ``bool` `ans = check(arr, start, start,  ` `                    ``visited, n, K);  ` ` `  `    ``// Print the result  ` `    ``if` `(ans)  ` `        ``cout << ``"Yes"``;  ` `    ``else` `        ``cout << ``"No"``;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``// Given array arr[]  ` `    ``int` `arr[] = { 3, 0, 2, 1, 2 };  ` ` `  `    ``// Given start and end  ` `    ``int` `start = 2;  ` `    ``int` `K = 0;  ` ` `  `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);  ` ` `  `    ``// Function Call  ` `    ``solve(arr, N, start, K);  ` `    ``return` `0;  ` `}  `

## Java

 `// Java program for the above approach  ` `import` `java.util.Arrays;  ` ` `  `class` `GFG{  ` `     `  `// DFS approach to check if traversal  ` `// is possible or not  ` `public` `static` `boolean` `check(``int``[] arr, ``int` `s_start,  ` `                            ``int` `start, ``boolean``[] visited,  ` `                            ``int` `size, ``int` `K)  ` `{  ` `     `  `    ``// Base cases  ` `    ``if` `(start < ``0` `|| start >= size ||  ` `        ``visited[start])  ` `    ``{  ` `        ``return` `false``;  ` `    ``}  ` `     `  `    ``// Check if start index value K  ` `    ``if` `(arr[start] == K &&  ` `            ``start != s_start)  ` `    ``{  ` `        ``return` `true``;  ` `    ``}  ` `     `  `    ``// Mark as visited  ` `    ``visited[start] = ``true``;  ` `     `  `    ``// Check for both i + arr[i]  ` `    ``// and i - arr[i] recursively  ` `    ``return` `(check(arr, s_start,  ` `                ``start + arr[start],  ` `                ``visited, size, K) ||  ` `            ``check(arr, s_start,  ` `                ``start - arr[start],  ` `                ``visited, size, K));  ` `}  ` `     `  `// Function to check if index with value  ` `// K can be reached or not  ` `public` `static` `void` `solve(``int``[] arr, ``int` `n,  ` `                        ``int` `start, ``int` `K)  ` `{  ` `     `  `    ``// Initialize visited array  ` `    ``boolean``[] visited = ``new` `boolean``[n];  ` `    ``Arrays.fill(visited, ``false``);  ` `     `  `    ``// DFS Traversal  ` `    ``boolean` `ans = check(arr, start, start,  ` `                        ``visited, n, K);  ` `     `  `    ``// Print the result  ` `    ``if` `(ans)  ` `        ``System.out.print(``"Yes"``);  ` `    ``else` `        ``System.out.print(``"No"``);  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `     `  `    ``// Given array arr[]  ` `    ``int` `arr[] = { ``3``, ``0``, ``2``, ``1``, ``2` `};  ` `     `  `    ``// Given start and end  ` `    ``int` `start = ``2``;  ` `    ``int` `K = ``0``;  ` `     `  `    ``int` `N = arr.length;  ` `     `  `    ``// Function call  ` `    ``solve(arr, N, start, K);  ` `}  ` `}  ` ` `  `// This code is contributed by divyeshrabadiya07  `

## C#

 `// C# program for the above approach  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// DFS approach to check if traversal  ` `// is possible or not  ` `public` `static` `bool` `check(``int``[] arr, ``int` `s_start,  ` `                         ``int` `start, ``bool``[] visited,  ` `                         ``int` `size, ``int` `K)  ` `{  ` `     `  `    ``// Base cases  ` `    ``if` `(start < 0 || start >= size||  ` `        ``visited[start]) ` `    ``{  ` `        ``return` `false``;  ` `    ``}  ` `     `  `    ``// Check if start index value K  ` `    ``if` `(arr[start] == K &&  ` `            ``start != s_start)  ` `    ``{  ` `        ``return` `true``;  ` `    ``}  ` `     `  `    ``// Mark as visited  ` `    ``visited[start] = ``true``;  ` `     `  `    ``// Check for both i + arr[i]  ` `    ``// and i - arr[i] recursively  ` `    ``return` `(check(arr, s_start,  ` `                  ``start + arr[start],  ` `                  ``visited, size, K) ||  ` `            ``check(arr, s_start,  ` `                  ``start - arr[start],  ` `                  ``visited, size, K));  ` `}  ` `     `  `// Function to check if index with value  ` `// K can be reached or not  ` `public` `static` `void` `solve(``int``[] arr, ``int` `n,  ` `                         ``int` `start, ``int` `K)  ` `{  ` `     `  `    ``// Initialize visited array  ` `    ``bool``[] visited = ``new` `bool``[n];  ` `     `  `    ``// DFS Traversal  ` `    ``bool` `ans = check(arr, start, start,  ` `                     ``visited, n, K);  ` `     `  `    ``// Print the result  ` `    ``if` `(ans)  ` `        ``Console.Write(``"Yes"``);  ` `    ``else` `        ``Console.Write(``"No"``);  ` `}  ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `     `  `    ``// Given array []arr  ` `    ``int` `[]arr = { 3, 0, 2, 1, 2 };  ` `     `  `    ``// Given start and end  ` `    ``int` `start = 2;  ` `    ``int` `K = 0;  ` `     `  `    ``int` `N = arr.Length;  ` `     `  `    ``// Function call  ` `    ``solve(arr, N, start, K);  ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```No
```

Time Complexity: O(N)
Auxiliary Space: O(N) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.