<p>Given an array of non-negative integers <code>arr</code>, you are initially positioned at <code>start</code>&nbsp;index of the array. When you are at index <code>i</code>, you can jump&nbsp;to <code>i + arr[i]</code> or <code>i - arr[i]</code>, check if you can reach&nbsp;<strong>any</strong> index with value 0.</p>

<p>Notice that you can not jump outside of the array at any time.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 5
<strong>Output:</strong> true
<strong>Explanation:</strong> 
All possible ways to reach at index 3 with value 0 are: 
index 5 -&gt; index 4 -&gt; index 1 -&gt; index 3 
index 5 -&gt; index 6 -&gt; index 4 -&gt; index 1 -&gt; index 3 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,2,3,0,3,1,2], start = 0
<strong>Output:</strong> true 
<strong>Explanation: 
</strong>One possible way to reach at index 3 with value 0 is: 
index 0 -&gt; index 4 -&gt; index 1 -&gt; index 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,0,2,1,2], start = 2
<strong>Output:</strong> false
<strong>Explanation: </strong>There is no way to reach at index 1 with value 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= arr[i] &lt;&nbsp;arr.length</code></li>
	<li><code>0 &lt;= start &lt; arr.length</code></li>
</ul>

// class Solution {
// public:
//    int final_ans=0;
//    int jump(int ind,vector<int>&arr,int start,int n,vector<int>&dp){
//     //if(ind<0 || ind>=n) return false;
//     if(ind+arr[ind]>=n || ind-arr[ind]<0) return 0;
//   if(arr[ind]==0) {
//     final_ans=1;
//     return dp[ind]=1;
//   }
//   //if(visited[ind]) return dp[false;
//   // visited[ind]=true;
//     if(dp[ind]!=-1) return dp[ind];
//     if(final_ans) return dp[ind]=1;
//     int  forward=jump(ind+arr[ind],arr,start,n,dp);
//     int backward=jump(ind-arr[ind],arr,start,n,dp);
//     return dp[ind]=(forward|backward);
//    }
//     bool canReach(vector<int>& arr, int start) {
//         //vector<bool>visited(arr.size(),false);
//         vector<int>dp(arr.size()+1,-1);
//     int ans= jump(start,arr,start,arr.size(),dp);
//     if(ans==0 || ans==-1) return false;
//     return false;
//     }
// };
class Solution {
public:
//bool final_answer=false;
   bool jump(int ind,vector<int>&arr,int start,int n,vector<bool>&visited){
    //if(ind<0 || ind>=n) return false;
  if(arr[ind]==0) {
    //final_answer=true;
    return true;
  }
  //if(final_answer) return true;
  if(visited[ind]) return false;
  visited[ind]=true;
    bool forward=false;
    if(ind+arr[ind]<n) forward=jump(ind+arr[ind],arr,start,n,visited);
    bool backward=false;
    if(ind-arr[ind]>=0) backward=jump(ind-arr[ind],arr,start,n,visited);
    return forward|backward;
   }
    bool canReach(vector<int>& arr, int start) {
        vector<bool>visited(arr.size(),false);
        return jump(start,arr,start,arr.size(),visited);
    }
};
46590