id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n queue<int>q;\n vector<int>vis(n,0);\n q.push(start);\n vis[start]=1;\n \n while(!q.empty()){\n \n int node =q.front();\n cout<<node<<endl;\n q.pop();\n if(arr[node]==0) return true;\n int x=node+arr[node];\n int y=node-arr[node];\n \n if(x>=0&&x<n&&vis[x]==0){\n q.push(x);\n vis[x]=1;\n }\n if(y>=0&&y<n&&vis[y]==0){\n\n q.push(y);\n\n vis[y]=1;\n\n }\n }\n\n \n return false; \n \n \n }\n};", "memory": "34890" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int N=arr.size();\n vector<int> v(N,0);\n queue<int> q;\n q.push(start);\n while(!q.empty())\n {\n int n=q.size();\n for(int i=0;i<n;i++)\n {\n int node=q.front();q.pop();\n v[node]=1;\n if(arr[node]==0) return true;\n if(node-arr[node]>=0 && !v[node-arr[node]]) \n q.push(node-arr[node]);\n if(node+arr[node]<N && !v[node+arr[node]]) \n q.push(node+arr[node]);\n }\n }\n return false;\n }\n};", "memory": "35150" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> visited(n, 0);\n queue<int> q;\n q.push(start);\n while (!q.empty()) {\n int ind = q.front();\n q.pop();\n if (ind < 0 || ind >= n) {\n continue;\n }\n if (arr[ind] == 0) {\n return true;\n }\n if (!visited[ind]) {\n q.push(ind + arr[ind]);\n q.push(ind - arr[ind]);\n }\n visited[ind] = 1;\n }\n return false;\n }\n};", "memory": "35150" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n queue<int> q;\n vector<int> vis(n, 0);\n q.push(start);\n\n while(!q.empty())\n {\n int pos = q.front();\n q.pop();\n\n if(arr[pos] == 0)\n return true;\n \n if(vis[pos])\n continue;\n \n vis[pos] = 1;\n\n if(pos + arr[pos] < n)\n q.push(pos + arr[pos]);\n \n if(pos - arr[pos] >= 0)\n q.push(pos - arr[pos]);\n }\n\n return false;\n }\n};", "memory": "35410" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n queue<int> q;\n vector<bool>seen(n);\n \n q.push(start);\n seen[start] = true;\n \n while(!q.empty()) {\n int currIndex = q.front();\n q.pop();\n \n if(arr[currIndex] == 0)\n return true;\n \n vector<int> neighbors({currIndex - arr[currIndex], currIndex + arr[currIndex]});\n for(int neighbor : neighbors) {\n if(valid(neighbor, n) && !seen[neighbor]) {\n seen[neighbor] = true;\n q.push(neighbor);\n }\n }\n \n }\n \n return false;\n \n \n }\n \n bool valid(int indx, int size) {\n return 0 <= indx && indx < size; \n }\n};", "memory": "36190" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool bfs(vector<int> arr, int start, vector<int> &mp){\n\n queue<int> q;\n q.push(start);\n mp[start] = 1;\n\n while(!q.empty()){\n int t = q.front();\n q.pop();\n if(arr[t] == 0){\n return true;\n }\n\n \n if(t + arr[t] >= 0 && t + arr[t] < arr.size()){\n if(mp[t + arr[t]] != 1){\n q.push( t + arr[t]);\n mp[t + arr[t]] = 1;\n }\n \n }\n if(t - arr[t] >= 0 && t - arr[t] < arr.size()){\n if(mp[t - arr[t]] != 1){\n q.push( t - arr[t]);\n mp[t - arr[t]] = 1;\n }\n }\n \n \n \n }\n return false;\n\n }\n \n\n bool canReach(vector<int>& arr, int start) {\n vector<int> mp(arr.size(), 0);\n return bfs(arr, start, mp); \n }\n};", "memory": "36190" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n /** recursive **/\n if(arr[start] == 0)\n return true;\n if(arr[start] == -1)\n return false;\n int n = arr.size();\n int cur = arr[start];\n arr[start] = -1;\n bool left = false, right = false;\n if(start - cur >= 0)\n left = canReach(arr, start - cur);\n if(start + cur < n)\n right = canReach(arr, start + cur);\n return left || right;\n //TC: O(n)\n //SC: O(n)\n }\n};", "memory": "36450" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int curr) {\n const int n = arr.size() - 1;\n if (curr < 0 || curr > n) return false;\n if (arr[curr] == -1) return false;\n if (arr[curr] == 0) return true;\n int val = arr[curr]; \n arr[curr] = -1; \n bool result = canReach(arr, curr - val) || canReach(arr, curr + val);\n arr[curr] = val; \n return result;\n }\n};\n", "memory": "36450" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n queue<int> q;\n vector<int> v1;\n for(int i=0;i<arr.size();i++)\n {\n v1.push_back(0);\n }\n q.push(start);\n while(!q.empty())\n {\n int temp = q.front();\n q.pop();\n if(arr[temp] == 0)\n {\n return true;\n }\n if(temp-arr[temp] >=0 && v1[temp-arr[temp]] == 0)\n {\n q.push(temp-arr[temp]);\n v1[temp-arr[temp]] = 1;\n }\n if(temp+arr[temp] <arr.size() && v1[temp+arr[temp]] == 0)\n {\n q.push(temp+arr[temp]);\n v1[temp+arr[temp]] = 1;\n }\n }\n return false;\n }\n};", "memory": "36710" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start)\n {\n if (arr[start] == 0) {\n return true;\n }\n std::list<int> list;\n int len = arr.size();\n std::vector<bool> visited(len, false);\n list.push_back(start);\n visited[start] = true;\n\n while (list.size()) {\n int u = list.front();\n list.pop_front();\n if (u + arr[u] < len && !visited[u + arr[u]]) {\n if (0 == arr[u + arr[u]]) {\n return true;\n }\n list.push_back(u + arr[u]);\n visited[u + arr[u]] = true;\n }\n if (u - arr[u] >= 0 && !visited[u - arr[u]]) {\n if (0 == arr[u - arr[u]]) {\n return true;\n }\n list.push_back(u - arr[u]);\n visited[u-arr[u]] = true;\n }\n }\n\n return false;\n }\n};", "memory": "36710" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n\n bool solve(vector<int>&arr, int i ){\n \n\n if(i>=arr.size() || i<0 || arr[i]<0) return false;\n if(arr[i] == 0) return true;\n arr[i]*=-1;\n return solve(arr, i+abs(arr[i])) || solve(arr, i-abs(arr[i]));\n \n \n }\n\n bool canReach(vector<int>& arr, int start) {\n bool flag=solve(arr,start);\n return flag;\n }\n};", "memory": "36970" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& a, int s) {\n queue<int>q;\n int n = a.size();\n vector<int>vis(n);\n vis[s] = 1;\n q.push(s);\n while(!q.empty()){\n vector<int>v;\n while(!q.empty()){\n v.push_back(q.front());\n \n q.pop();\n }\n\n \n\n for(int i = 0; i < (int)v.size(); i++){\n if(a[v[i]] == 0)return true;\n if(a[v[i]] + v[i] < n && !vis[a[v[i]] + v[i] ]){\n vis[a[v[i]] + v[i] ] = 1;\n q.push(a[v[i]] + v[i] );\n }\n\n cout << -a[v[i]] + v[i];\n\n if(-a[v[i]] + v[i] >= 0 && !vis[-a[v[i]] + v[i] ]){\n vis[-a[v[i]] + v[i] ] = 1;\n q.push(-a[v[i]] + v[i] );\n }\n }\n }\n\n return false;\n }\n};", "memory": "37230" }
1,428
<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>
1
{ "code": "class Solution {\n int visited[50001];\npublic:\n bool canReach(vector<int>& arr, int start) {\n if(start < 0 || start >= arr.size()){\n return false;\n }\n if(arr[start] == 0){\n return true;\n }\n if(visited[start] == 1){\n return false;\n }\n visited[start] = 1;\n if(canReach(arr, start + arr[start])){\n return true;\n }\n if(canReach(arr, start - arr[start])){\n return true;\n }\n return false;\n }\n};\n", "memory": "37230" }
1,428
<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>
1
{ "code": "class Solution {\npublic:\n\n bool canReach(vector<int>& arr, int start) \n {\n if(start < 0 || start >= arr.size())\n {\n return false;\n }\n if(arr[start] == 0)\n {\n return true;\n }\n if(arr[start] < 0)\n {\n return false;\n }\n\n int step = arr[start];\n arr[start] = -1;\n\n return canReach(arr, start + step) || canReach(arr, start - step);\n }\n};", "memory": "37490" }
1,428
<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>
1
{ "code": "class Solution {\n int dp[50005];\n int n;\npublic:\nint f( int i , vector<int>&arr){\n if(i>=n || i<0) return 0;\n if(arr[i]==0) return 1;\n \n\n if(dp[i]!=-1) return dp[i];\n dp[i] = 0;\n\n int move_left= f(i-arr[i] , arr);\n int move_right= f(i+ arr[i] , arr);\n\n if(move_left==1 || move_right==1) {\n dp[i]=1;\n }\n else{\n dp[i]=0;\n }\n\n return dp[i];\n \n\n}\n bool canReach(vector<int>& arr, int start) {\n n=arr.size(); \n memset(dp , -1 , sizeof(dp));\n if(f(start , arr)==1) return true;\n else return false;\n\n }\n};", "memory": "37490" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n set<int> found; found.insert(start);\n stack<int> s; s.push(start);\n set<int>::iterator it; int v; int w;\n while( s.empty() == false ){\n \n v = s.top(); //take element from top of stack\n s.pop();\n\n w = v - arr[v]; //add new elements to stack and check if they are a 0 element\n if( 0 <= w ){ \n if( arr[w] == 0 ){ return true; }\n it = found.find(w); \n if( it == found.end() ){ found.insert(w); s.push(w); }\n }\n w = v + arr[v];\n if( w < n ){ \n if( arr[w] == 0 ){ return true; }\n it = found.find(w); \n if( it == found.end() ){ found.insert(w); s.push(w); }\n }\n \n }\n return false;\n }\n};", "memory": "37750" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool func(vector<int>& arr, vector<int>& vis,int index){\n if(vis[index]!=0) return false;\n if(arr[index]==0) return true;\n int left = index-arr[index];\n int right = index+arr[index];\n vis[index]=1;\n bool flag= false;\n if (left>=0)\n flag|=func(arr, vis,left);\n if(right<arr.size())\n flag|=func(arr,vis,right);\n return flag;\n \n }\n bool canReach(vector<int>& arr, int start) {\n vector<int> vis(arr.size(),0);\n return func(arr,vis, start);\n \n }\n};", "memory": "37750" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool func(vector<int> const& arr, int start, vector<pair<bool, bool>> &dp) {\n if (arr[start] == 0)\n return true;\n\n if (dp[start].first && dp[start].second)\n return false;\n\n int left = start - arr[start];\n int right = start + arr[start];\n\n bool l_jump{false};\n bool r_jump{false};\n\n dp[start].first = true;\n if (left >= 0)\n l_jump = func(arr, left, dp);\n\n dp[start].second = true;\n if (right < arr.size())\n r_jump = func(arr, right, dp);\n\n return l_jump || r_jump;\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<pair<bool, bool>> dp(arr.size(), pair<bool, bool>(false, false));\n\n return func(arr, start, dp);\n }\n};", "memory": "38010" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n vector<int> a;\n int visited[50000];\n\n int dfs(int i) {\n if(a[i] == 0) return 1;\n\n if(visited[i]) return 0;\n\n int ans = 0;\n visited[i] = 1;\n if(i + a[i] < a.size()) {\n ans = dfs(i+a[i]);\n }\n if(i - a[i] >= 0) {\n ans = max(ans, dfs(i-a[i]));\n }\n\n return ans;\n }\n\n bool canReach(vector<int>& arr, int start) {\n a = arr;\n return dfs(start);\n }\n};", "memory": "38010" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& nums, int start) {\n unordered_map<int , bool> vm;\n stack<int>st;\n int ci=start;\n vm[start]=true;\n\n\n if(nums[ci]==0 && ci>=0 && ci<nums.size())\n return true;\n\n int fi= ci+nums[ci];\n int bi=ci-nums[ci];\n\n\n if(fi<nums.size())\n st.push(fi);\n\n if(bi>=0)\n st.push(bi);\n\n while(!st.empty())\n {\n ci=st.top();\n st.pop();\n vm[ci]=true;\n \n if(nums[ci]==0 && ci<nums.size() && ci>=0 )\n return true;\n\n fi=ci+nums[ci];\n bi=ci-nums[ci];\n\n if(fi<nums.size() && !vm[fi])\n st.push(fi);\n\n if(bi>=0 && !vm[bi])\n st.push(bi);\n \n }\n return false ;\n\n }\n};", "memory": "38270" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\nbool vis[50001];\nint dp[50001];\nbool f(vector<int>& arr, int cur){\n if(cur>=arr.size() || cur <0) return 0;\n if(arr[cur] == 0) return 1;\n if(vis[cur] == true) return 0;\n vis[cur]=true;\n if(dp[cur] !=-1) return dp[cur];\n int increment = f(arr, cur+arr[cur]);\n int decrement = f(arr, cur-arr[cur]);\n if((increment||decrement) == true) return true;\n return dp[cur] = (increment||decrement);\n}\n bool canReach(vector<int>& arr, int start) {\n\n memset(dp, -1, sizeof dp);\n return f(arr, start);\n }\n};", "memory": "38270" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool dfs(vector<int> &arr, vector<int> &vis, int i){\n if( arr[i] == 0) return true;\n vis[i] = 1;\n if((i+arr[i])<arr.size() && vis[i+arr[i]] == 0){\n if(dfs(arr, vis, i+arr[i])) return true;\n }\n if((i-arr[i])>=0 && vis[i-arr[i]] == 0){\n if(dfs(arr, vis, i-arr[i])) return true;\n }\n vis[i] = 0;\n return false;\n }\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> vis(n, 0);\n return dfs(arr, vis, start);\n }\n};", "memory": "38530" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int>visited(n,0);\n return f(arr,n,start,visited);\n }\n bool f(vector<int>&arr,int n,int i,vector<int>&visited)\n {\n if(i<0 || i>=n || visited[i]!=0)\n return false;\n if(arr[i] == 0)\n return true;\n visited[i] = 1;\n return f(arr,n,i+arr[i],visited)||f(arr,n,i-arr[i],visited);\n }\n};", "memory": "38530" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n bool flag = false;\n for(int i = 0; i < n; i++){\n if(arr[i] == 0){\n flag = true; break;\n }\n }\n if(flag == false) return false;\n\n if(arr[start] == 0) return true;\n\n vector<int> memo(n, -1);\n memo[start] = 0;\n return helper(arr, start, memo);\n }\n\n bool helper(vector<int>& arr, int now, vector<int>& memo){\n if(now < 0 || now > arr.size()) return false;\n if(arr[now] == 0){\n memo[now] = 1;\n return true;\n }\n bool f1 = false, f2 = false;\n int l = now - arr[now], r = now + arr[now];\n if(l >= 0 && memo[l] == -1){\n memo[l] = 0;\n f1 = helper(arr, l, memo);\n }\n if(f1 == false && r < arr.size() && memo[r] == -1){\n memo[r] = 0;\n f2 = helper(arr, r, memo);\n }\n memo[now] = f1 || f2;\n\n return memo[now];\n }\n};", "memory": "38790" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n queue<int> q;\n unordered_set<int> visit;\n\n q.push(start);\n visit.insert(start);\n\n while(!q.empty()){\n int index=q.front();\n q.pop();\n\n if(arr[index]==0){\n return true;\n }\n\n int leftindex=index-arr[index];\n int rightindex=index+arr[index];\n\n if(leftindex>=0 && visit.find(leftindex)==visit.end()){\n q.push(leftindex);\n visit.insert(leftindex);\n }\n\n if(rightindex<=n-1 && visit.find(rightindex)==visit.end()){\n q.push(rightindex);\n visit.insert(rightindex);\n }\n } \n return false;\n }\n};", "memory": "38790" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n unordered_set<int> Zeros;\n for (int i=0; i<arr.size(); i++){\n if (arr[i]== 0){\n Zeros.insert(i);\n }\n }\n queue<int> Q;\n Q.push(start);\n int Pivot;\n unordered_set<int> Checked;\n while(!Q.empty()){\n Pivot = Q.front();\n Q.pop();\n if (Zeros.find(Pivot) != Zeros.end()){\n return true;\n }\n if (Checked.find(Pivot) == Checked.end()){\n Checked.insert(Pivot);\n if (Pivot - arr[Pivot] >= 0 && Pivot - arr[Pivot] < arr.size()){\n Q.push(Pivot - arr[Pivot]);\n }\n if (Pivot + arr[Pivot] >= 0 && Pivot + arr[Pivot] < arr.size()){\n Q.push(Pivot + arr[Pivot]);\n }\n }\n }\n return false;\n }\n};", "memory": "39050" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool solve(int index,vector<int>&dp,vector<int>& arr,int n ){\n //base case\n if(index<0 || index>=n) return false;\n if(dp[index]!=-1) return dp[index];\n\n if(arr[index]==0) return true;\n \n dp[index]=0;\n //if forward\n bool forward=solve(index+arr[index],dp,arr,n);\n\n //if backward\n bool backward=solve(index-arr[index],dp,arr,n);\n\n return dp[index]=forward || backward;\n }\n\n\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<int>dp(n,-1);\n return solve(start,dp,arr,n);\n\n }\n};", "memory": "39050" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) \n {\n int n=arr.size();\n queue<int>q;\n q.push(start);//idx\n map<int,int>m;\n m[start]=1;\n while(!q.empty())\n {\n int curr_idx=q.front();q.pop();\n if(arr[curr_idx]==0)return 1;\n int prev=curr_idx-arr[curr_idx];\n int next=curr_idx+arr[curr_idx];\n if(prev>=0 and prev<n and !m[prev])\n {\n q.push(prev);\n m[prev]=1;\n }\n if(next>=0 and next<n and !m[next])\n {\n q.push(next);\n m[next]=1;\n }\n }\n return 0;\n }\n};", "memory": "39310" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n if (arr[start] == 0) {\n return true;\n }\n \n vector<int> visited(arr.size(), 0);\n return dfs(arr, visited, start);\n }\n\n bool dfs(vector<int>& arr, vector<int>& visited, int index) {\n if (index < 0 || index >= arr.size() || visited[index]) {\n return false;\n }\n\n if (arr[index] == 0) {\n return true;\n }\n\n visited[index] = 1;\n return dfs(arr, visited, index + arr[index]) || dfs(arr, visited, index - arr[index]);\n }\n};", "memory": "39310" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n int dp[500005];\n int solve(vector<int>&arr,int i){\n if(i>=arr.size() || i<0)return 0;\n if(arr[i]==0)return 1;\n if(dp[i]!=-1)return dp[i];\n dp[i]=0;\n int take = solve(arr,i+arr[i]);\n int backtake =solve(arr,i-arr[i]);\n return dp[i]=take |backtake;\n }\n bool canReach(vector<int>& arr, int start) {\n memset(dp,-1,sizeof(dp));\n int ans = solve(arr,start);\n if(ans ==1)return true;\n return false;\n }\n};", "memory": "39570" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n std::vector<int> neighbors(std::vector<int>& arr, int pos) {\n std::vector<int> nbrs;\n const int left = pos + arr[pos];\n const int right = pos - arr[pos];\n\n if (left >= 0 && left < arr.size()) nbrs.push_back(left);\n if (right >= 0 && right < arr.size()) nbrs.push_back(right);\n return nbrs;\n }\n\n bool canReach(vector<int>& arr, int start) {\n\n // init\n std::queue<int> q;\n std::vector<bool> visited(arr.size(), false);\n q.push(start);\n visited[start] = true;\n\n // bfs\n while (!q.empty()) {\n \n // see if we reached our goal of index 0\n int idx = q.front(); q.pop();\n if (arr[idx] == 0) return true;\n\n // try the neighbors\n for (int nbr : neighbors(arr, idx)) {\n if (!visited[nbr]) {\n visited[nbr] = true;\n q.push(nbr);\n }\n }\n }\n // no solution found\n return false;\n }\n};", "memory": "39570" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& arr, int start, vector<pair<bool, bool>>& visited, bool& flag) {\n if(flag || start >= arr.size() || start < 0 || visited[start].first) return false;\n visited[start].first = true;\n if(arr[start] == 0) {\n flag = true;\n visited[start].second = true;\n return true;\n }\n // move right\n bool t1 = helper(arr, start + arr[start], visited, flag);\n // move left\n bool t2 = helper(arr, start - arr[start], visited, flag);\n return t1 || t2;\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<pair<bool, bool>> visited(arr.size(), {false, false});\n bool flag = false;\n return helper(arr, start, visited, flag);\n }\n};\n", "memory": "39830" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& arr, int start, vector<pair<bool, bool>>& visited, bool& flag) {\n if(flag || start >= arr.size() || start < 0 || visited[start].first) return false;\n visited[start].first = true;\n if(arr[start] == 0) {\n flag = true;\n visited[start].second = true;\n return true;\n }\n // move right\n bool t1 = helper(arr, start + arr[start], visited, flag);\n // move left\n bool t2 = helper(arr, start - arr[start], visited, flag);\n return t1 || t2;\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<pair<bool, bool>> visited(arr.size(), {false, false});\n bool flag = false;\n return helper(arr, start, visited, flag);\n }\n};\n", "memory": "39830" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool jumpHelper(vector<int> &arr, int i,vector<int> &dp){\n if(i>=arr.size() ||i<0) return false;\n if(arr[i]==0) return true;\n if(dp[i]==1) return false;\n dp[i]=1;\n bool op1=jumpHelper(arr,i+arr[i],dp);\n bool op2=jumpHelper(arr,i-arr[i],dp);\n return op1||op2;\n\n }\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<int> dp(n,-1);\n return jumpHelper(arr,start,dp);\n }\n};", "memory": "40090" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool solve(vector<int>&arr,int index,vector<int>&visited){\n if(index>=arr.size() || index<0) return 0;\n if(arr[index]==0) return 1;\n if(visited[index]) return 0;\n visited[index]=true;\n return solve(arr,index+arr[index],visited) || solve(arr,index-arr[index],visited);\n }\n bool canReach(vector<int>& arr, int start) {\n vector<int>visited(arr.size()+1,0);\n return solve(arr,start,visited);\n }\n};", "memory": "40090" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n int solve(int idx, int n,vector<int>&arr,vector<int>&check,int t[50001]){\n if(idx>=n || idx<0)return 0;\n if(t[idx] != -1)return t[idx];\n if(arr[idx]==0)return 1;\n if(check[idx]==1)return false;\n check[idx]=1;\n int aage = solve(idx+arr[idx],n,arr,check,t);\n int peeche = solve(idx-arr[idx],n,arr,check,t);\n\n return t[idx]=max(aage,peeche);\n }\n\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int>check(n,-1);\n int t[50001];\n memset(t,-1,sizeof(t));\n return solve(start,n,arr,check,t);\n }\n};", "memory": "40350" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool r(vector<int>& arr, int start,vector<int>&visited,vector<int>&dp){\n if(start<0 || start>=arr.size() || visited[start]==1){\n return false;\n }\n if(arr[start]==0)return true;\n if(dp[start]!=-1){\n return dp[start];\n }\n visited[start]=1;\n dp[start]=r(arr,start-arr[start],visited,dp) || r(arr,start+arr[start],visited,dp);\n return dp[start];\n \n \n \n }\n bool canReach(vector<int>& arr, int start) {\n vector<int>visited(arr.size(),0);\n vector<int>dp(arr.size(),-1);\n return r(arr,start,visited,dp);\n }\n};", "memory": "41130" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool isReachable(vector<int> &arr, int cur, vector<int> &memo, vector<int> &visited){\n\n if( cur < 0 || cur >= arr.size() || visited[cur] ){\n return false;\n }\n\n if( arr[cur] == 0 ){\n return true;\n }\n\n visited[cur] = 1;\n return isReachable(arr, cur + arr[cur], memo, visited) || isReachable(arr, cur - arr[cur], memo, visited);\n\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<int> visited(arr.size(), 0);\n vector<int> memo(arr.size(), -1);\n return isReachable(arr, start, memo, visited);\n }\n};", "memory": "41130" }
1,428
<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>
2
{ "code": "class Solution {\nprivate:\n bool is_valid(int ind, int n) {\n return ind >= 0 && ind < n;\n }\n\n bool f(vector<int>& arr, int st, vector<int>& vis, vector<int>& dp) {\n if (dp[st] != -1) return dp[st];\n if (arr[st] == 0) return dp[st] = true;\n\n vis[st] = 1;\n\n int id1 = st + arr[st];\n int id2 = st - arr[st];\n\n if (is_valid(id1, arr.size()) && vis[id1] == 0) {\n if (f(arr, id1, vis, dp)) return dp[st] = true;\n }\n\n if (is_valid(id2, arr.size()) && vis[id2] == 0) {\n if (f(arr, id2, vis, dp)) return dp[st] = true;\n }\n\n return dp[st] = false;\n }\n\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> vis(n, 0);\n vector<int> dp(n, -1);\n\n return f(arr, start, vis, dp);\n }\n};", "memory": "41390" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool solve(int i, vector<int>& arr, vector<int>& vis, int n, vector<int>& dp) {\n if(i >= n || i < 0 || vis[i] == 1) return false;\n if(arr[i] == 0) return true;\n\n if(dp[i] != -1) return dp[i];\n\n vis[i] = 1;\n bool right = solve(i + arr[i], arr, vis, n, dp);\n bool left = solve(i - arr[i], arr, vis, n, dp);\n\n return dp[i] = right || left;\n }\n\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n\n vector<int> vis(n, 0);\n vector<int> dp(n, -1);\n return solve(start, arr, vis, n, dp);\n }\n};", "memory": "41390" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n\nbool f(int ind,vector<int> &arr,vector<int> &vis, vector<int> &dp){\n // base case\n if (arr[ind]==0){\n return true;\n }\n if (ind>=arr.size()||ind<0){\n return false;\n }\n \n if (vis[ind]){\n return false;\n }\n if (dp[ind]!=-1){\n return dp[ind];\n }\n \n vis[ind]=1;\n\n\n\n bool fl1=0;\n bool fl2=0;\n\n\n if (ind+arr[ind]<arr.size()){\n fl1=f(ind+arr[ind],arr,vis,dp);\n\n }\n if (ind-arr[ind]>=0){\n fl2=f(ind-arr[ind],arr,vis,dp);\n }\n \n return dp[ind]=fl1||fl2;\n}\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<int> vis(n,0);\n vector<int> dp(n,-1);\n return f(start,arr,vis,dp);\n \n }\n};", "memory": "41650" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n vector<int> dp;\n vector<int> vis;\n int n;\n int canr(vector<int>& arr, int start) {\n if(start >= n || start < 0 || vis[start]) return 0;\n if(dp[start]!=-1) return dp[start];\n if(arr[start]==0) return dp[start]=1;\n vis[start]=1;\n dp[start] = canr(arr, start+arr[start]) || canr(arr, start-arr[start]);\n vis[start]=0;\n return dp[start];\n }\n bool canReach(vector<int>& arr, int start) {\n n = arr.size();\n dp.resize(n, -1);\n vis.resize(n,0);\n bool ans = canr(arr, start)==1;\n return ans;\n }\n};", "memory": "41910" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n unordered_map<int,int>m;\n int n= arr.size();\n \n for(int i=0;i<arr.size();i++)\n {\n int x =arr[i];\n if(x == 0)\n m[i]++;\n }\n unordered_map<int,int> vis;\n queue<int> q;\n q.push(start);\n\n while(!q.empty()){\n\n int x = q.front();\n q.pop();\n\n if(vis[x] || x<0 || x>=n)\n continue;\n\n vis[x]=true;\n \n if(m[x])\n return true;\n\n int a = x+arr[x];\n \n\n q.push(a);\n a = x- arr[x];\n q.push(a);\n\n }\n\n return false;\n }\n};", "memory": "42170" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n if(arr[start] == 0) {\n return true;\n }\n\n if(arr.size() == 1) {\n return true;\n }\n // stores all the indices that can be reached by jumping\n unordered_set<int> reachableIndices = {start};\n\n unordered_set<int> toSearch = {start};\n unordered_set<int> temp;\n\n while (toSearch.size() > 0) {\n temp.clear();\n for (int i : toSearch) {\n if (i + arr[i] < arr.size() && reachableIndices.find(i + arr[i]) == reachableIndices.end()) {\n if (arr[i + arr[i]] == 0) {\n return true;\n }\n temp.insert(i + arr[i]);\n reachableIndices.insert(i + arr[i]);\n }\n if (i - arr[i] >= 0 && reachableIndices.find(i - arr[i]) == reachableIndices.end()) {\n if (arr[i - arr[i]] == 0) {\n return true;\n }\n temp.insert(i - arr[i]);\n reachableIndices.insert(i - arr[i]);\n }\n }\n toSearch.clear();\n toSearch.insert(temp.begin(), temp.end());\n }\n return false;\n }\n};", "memory": "42430" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n if(arr[start] == 0) {\n return true;\n }\n\n if(arr.size() == 1) {\n return true;\n }\n // stores all the indices that can be reached by jumping\n unordered_set<int> reachableIndices = {start};\n\n unordered_set<int> toSearch = {start};\n unordered_set<int> temp;\n\n while (toSearch.size() > 0) {\n temp.clear();\n for (int i : toSearch) {\n if (i + arr[i] < arr.size() && reachableIndices.find(i + arr[i]) == reachableIndices.end()) {\n if (arr[i + arr[i]] == 0) {\n return true;\n }\n temp.insert(i + arr[i]);\n reachableIndices.insert(i + arr[i]);\n }\n if (i - arr[i] >= 0 && reachableIndices.find(i - arr[i]) == reachableIndices.end()) {\n if (arr[i - arr[i]] == 0) {\n return true;\n }\n temp.insert(i - arr[i]);\n reachableIndices.insert(i - arr[i]);\n }\n }\n toSearch.clear();\n toSearch.insert(temp.begin(), temp.end());\n }\n return false;\n }\n};", "memory": "42430" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\nbool ispossible(int start,vector<int>&arr,vector<int>&visited,vector<int>&dp){\n if(start<0||start>=arr.size())return false;\n if(arr[start]==0)return true;\n if(visited[start]==1){\n return false;\n }\n if(dp[start]!=-1)return dp[start];\n visited[start]=1;\n bool option1=false,option2=false;\n if(start+arr[start]>=0 && start+arr[start]<arr.size()){\n option1=ispossible(start+arr[start],arr,visited,dp);\n if(option1==true)return true;\n }\n if(start-arr[start]>=0 && start-arr[start]<arr.size()){\n option2=ispossible(start-arr[start],arr,visited,dp);\n if(option2==true)return true;\n }\n visited[start]=0;\n return dp[start]=option1||option2;\n}\n bool canReach(vector<int>& arr, int start) {\n if(start<0||start>=arr.size())return false;\n vector<int>visited(arr.size(),0);\n vector<int>dp(arr.size(),-1);\n return ispossible(start,arr,visited,dp);\n }\n};", "memory": "42690" }
1,428
<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>
2
{ "code": "#pragma GCC optimize (\"O3,unroll-loops\")\n#pragma GCC target (\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nclass Solution {\npublic:\n vector<bool> explored;\n\n bool explore(vector<int> &nums, int i) {\n if (i < 0 || i >= nums.size() || explored[i]) return false;\n explored[i] = true;\n return nums[i] == 0 ||\n explore(nums, i+nums[i]) ||\n explore(nums, i-nums[i]);\n }\n\n bool canReach(vector<int>& arr, int start) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n explored.resize(arr.size());\n return explore(arr, start);\n }\n};", "memory": "42950" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n int n;\n bool solve(vector<int>& arr, int start,vector<bool>&vis){\n if(start<0 || start>=n || vis[start])\n return 0;\n if(arr[start]==0)\n return 1;\n vis[start]=1;\n return solve(arr,start+arr[start],vis)||solve(arr,start-arr[start],vis);\n }\n bool canReach(vector<int>& arr, int start) {\n n=arr.size();\n vector<bool>vis(n,0);\n return solve(arr,start,vis);\n }\n};", "memory": "43210" }
1,428
<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>
2
{ "code": "const static auto _ = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();\nclass Solution {\npublic:\n bool solve(vector<int>& arr,int index,vector<bool>& visited){\n if(index<0 || index>=arr.size() || visited[index])\n return false;\n if(!arr[index]){\n return true;\n }\n visited[index] = true;\n return solve(arr,index+arr[index],visited) || solve(arr,index-arr[index],visited);\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<bool> visited(arr.size(),0);\n return solve(arr,start,visited);\n }\n};", "memory": "43210" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> done(n);\n function<bool(int)> dfs = [&](int u) {\n if(u<0 || u>=n || done[u]) return false;\n if(arr[u]==0) return true;\n done[u] = 1;\n if(dfs(u - arr[u])) return true;\n if(dfs(u + arr[u])) return true;\n return false;\n };\n return dfs(start);\n }\n};", "memory": "43470" }
1,428
<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>
2
{ "code": "class Solution {\npublic:\n\n bool dfs(vector<int>& arr, int idx, vector<bool>& visited) {\n if (idx < 0 || idx >= arr.size() || visited[idx]) {\n return false;\n }\n if (arr[idx] == 0) {return true;}\n visited[idx] = true;\n return(dfs(arr, idx - arr[idx], visited) || dfs(arr, idx + arr[idx], visited));\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<bool> x(arr.size(), false);\n return dfs(arr, start, x);\n }\n};", "memory": "43730" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n\n bool dfs(vector<int>& arr, int idx, vector<bool>& visited) {\n if (idx < 0 || idx >= arr.size() || visited[idx]) {\n return false;\n }\n if (arr[idx] == 0) {return true;}\n visited[idx] = true;\n return(dfs(arr, idx - arr[idx], visited) || dfs(arr, idx + arr[idx], visited));\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<bool> x(arr.size(), false);\n return dfs(arr, start, x);\n }\n};", "memory": "43990" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n\n bool canReachHelper(vector<int>& arr, int start, vector<bool>& isVisited) {\n if(arr[start] == 0) return true;\n if(isVisited[start]) return false;\n isVisited[start] = true;\n\n if(start + arr[start] < arr.size()) {\n if(canReachHelper(arr, start + arr[start], isVisited)) return true;\n }\n if(start - arr[start] >= 0){\n if(canReachHelper(arr, start - arr[start], isVisited)) return true;\n }\n return false;\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<bool> isVisited(arr.size(), false);\n return canReachHelper(arr, start, isVisited);\n }\n};", "memory": "44250" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n queue<int> q;\n q.push(start);\n\n unordered_set<string> visited;\n\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n\n if(arr[cur] == 0) return true; \n\n if (cur + arr[cur] < n) {\n int dis = cur + arr[cur];\n string temp = to_string(cur) + \" r \" + to_string(dis);\n if (!visited.count(temp)) {\n visited.insert(temp);\n q.push(dis);\n }\n }\n\n if (cur - arr[cur] >= 0) {\n int dis = cur - arr[cur];\n string temp = to_string(cur) + \" r \" + to_string(dis);\n if (!visited.count(temp)) {\n visited.insert(temp);\n q.push(dis);\n }\n }\n }\n\n return false;\n }\n};", "memory": "44510" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n queue<int> q;\n q.push(start);\n\n unordered_set<string> visited;\n\n while (!q.empty()) {\n int cur = q.front();\n q.pop();\n\n if(arr[cur] == 0) return true; \n\n if (cur + arr[cur] < n) {\n int dis = cur + arr[cur];\n string temp = to_string(cur) + \" r \" + to_string(dis);\n if (!visited.count(temp)) {\n visited.insert(temp);\n q.push(dis);\n }\n }\n\n if (cur - arr[cur] >= 0) {\n int dis = cur - arr[cur];\n string temp = to_string(cur) + \" l \" + to_string(dis);\n if (!visited.count(temp)) {\n visited.insert(temp);\n q.push(dis);\n }\n }\n }\n\n return false;\n }\n};", "memory": "44510" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\nvector<int>dp;\n bool solve(vector<int>& nums, int i, vector<bool>& visited) {\n if (i < 0 || i >= nums.size() || visited[i]) return false;\n \n if (nums[i] == 0) return true;\n if(dp[i]!=-1)\n {\n return dp[i];\n }\n visited[i] = true;\n \n return dp[i]=solve(nums, i + nums[i], visited) || solve(nums, i - nums[i], visited);\n }\n \n bool canReach(vector<int>& arr, int start) {\n dp.resize(arr.size()+1,-1);\n vector<bool> visited(arr.size(), false);\n return solve(arr, start, visited);\n }\n};\n", "memory": "44770" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n int help(vector<int>& arr, int start,vector<bool>&dp,vector<int> &v){\n if(start<0 || start>= arr.size())return false;\n if(v[start]!=-1)return v[start];\n if(dp[start])return false;\n if(arr[start]==0)return true;\n dp[start]=true;\n bool x=help(arr, start+arr[start],dp,v);\n bool y=help(arr, start-arr[start],dp,v);\n return x||y;\n }\n \n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<int> v(n,-1);\n vector<bool> dp(n,false);\n return help(arr,start,dp,v);\n }\n};", "memory": "45030" }
1,428
<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>
3
{ "code": "class Solution{\nprivate:\n void f(vector<bool>& vis,int node,vector<int> &arr){\n vis[node]=true;\n int n=arr.size();\n if(node+arr[node]<n && vis[node+arr[node]]==false) f(vis,node+arr[node],arr);\n if(node-arr[node]>=0 && vis[node-arr[node]]==false) f(vis,node-arr[node],arr);\n }\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<bool> vis(n,false);\n f(vis,start,arr);\n bool ans=false;\n for(int i=0;i<n;i++){\n if(arr[i]==0 && vis[i]){\n return true;\n }\n }\n\n return false; \n }\n};", "memory": "45290" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& arr, vector<bool>& dp, int start, int n) {\n if(start>=n || start<0)\n return false;\n \n if(arr[start]==0 || dp[start]==true) {\n return dp[start] = true;\n }\n\n dp[start] = true;\n auto left = helper(arr, dp, start-arr[start], n);\n auto right = helper(arr, dp, start+arr[start], n);\n return left || right;\n }\n\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<bool> dp(n, false);\n\n helper(arr, dp, start, n);\n\n for(int i=0; i<n; i++) {\n if(arr[i]==0 && dp[i]==true)\n return true;\n }\n return false;\n }\n};", "memory": "45550" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n vector<bool> visited(arr.size(), false);\n dfs(start,arr, visited);\n for (auto num : visited) cout << \" \" << num << endl;\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i] == 0 and visited[i]) {\n return true;\n }\n }\n return false;\n }\n\n void dfs(int pos, vector<int>& arr, vector<bool>&visited) {\n visited[pos] = true;\n if (pos+arr[pos] < arr.size() and !visited[pos+arr[pos]]) {\n dfs(pos+arr[pos], arr, visited);\n }\n if (pos-arr[pos] >= 0 and !visited[pos-arr[pos]]) {\n dfs(pos-arr[pos], arr, visited);\n }\n }\n};", "memory": "45810" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<bool> dp(n+1 , false);\n return solve(start , dp , arr);\n }\n\n bool solve( int i , vector<bool> & dp , vector<int> & arr){ \n if(i>= arr.size() || i<0 || dp[i]) return false;\n if(arr[i] == 0) return true;\n if(dp[i] == false) dp[i] = true;\n return solve(i+ arr[i] , dp , arr) || solve(i-arr[i] , dp , arr);\n }\n};", "memory": "46070" }
1,428
<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>
3
{ "code": "// class Solution {\n// public:\n// int final_ans=0;\n// int jump(int ind,vector<int>&arr,int start,int n,vector<int>&dp){\n// //if(ind<0 || ind>=n) return false;\n// if(ind+arr[ind]>=n || ind-arr[ind]<0) return 0;\n// if(arr[ind]==0) {\n// final_ans=1;\n// return dp[ind]=1;\n// }\n// //if(visited[ind]) return dp[false;\n// // visited[ind]=true;\n// if(dp[ind]!=-1) return dp[ind];\n// if(final_ans) return dp[ind]=1;\n// int forward=jump(ind+arr[ind],arr,start,n,dp);\n// int backward=jump(ind-arr[ind],arr,start,n,dp);\n// return dp[ind]=(forward|backward);\n// }\n// bool canReach(vector<int>& arr, int start) {\n// //vector<bool>visited(arr.size(),false);\n// vector<int>dp(arr.size()+1,-1);\n// int ans= jump(start,arr,start,arr.size(),dp);\n// if(ans==0 || ans==-1) return false;\n// return false;\n// }\n// };\nclass Solution {\npublic:\nbool final_answer=false;\n bool jump(int ind,vector<int>&arr,int start,int n,vector<bool>&visited){\n //if(ind<0 || ind>=n) return false;\n if(arr[ind]==0) {\n final_answer=true;\n return true;\n }\n if(final_answer) return true;\n if(visited[ind]) return false;\n visited[ind]=true;\n bool forward=false;\n if(ind+arr[ind]<n) forward=jump(ind+arr[ind],arr,start,n,visited);\n bool backward=false;\n if(ind-arr[ind]>=0) backward=jump(ind-arr[ind],arr,start,n,visited);\n return forward|backward;\n }\n bool canReach(vector<int>& arr, int start) {\n vector<bool>visited(arr.size(),false);\n return jump(start,arr,start,arr.size(),visited);\n }\n};", "memory": "46330" }
1,428
<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>
3
{ "code": "// class Solution {\n// public:\n// int final_ans=0;\n// int jump(int ind,vector<int>&arr,int start,int n,vector<int>&dp){\n// //if(ind<0 || ind>=n) return false;\n// if(ind+arr[ind]>=n || ind-arr[ind]<0) return 0;\n// if(arr[ind]==0) {\n// final_ans=1;\n// return dp[ind]=1;\n// }\n// //if(visited[ind]) return dp[false;\n// // visited[ind]=true;\n// if(dp[ind]!=-1) return dp[ind];\n// if(final_ans) return dp[ind]=1;\n// int forward=jump(ind+arr[ind],arr,start,n,dp);\n// int backward=jump(ind-arr[ind],arr,start,n,dp);\n// return dp[ind]=(forward|backward);\n// }\n// bool canReach(vector<int>& arr, int start) {\n// //vector<bool>visited(arr.size(),false);\n// vector<int>dp(arr.size()+1,-1);\n// int ans= jump(start,arr,start,arr.size(),dp);\n// if(ans==0 || ans==-1) return false;\n// return false;\n// }\n// };\nclass Solution {\npublic:\n//bool final_answer=false;\n bool jump(int ind,vector<int>&arr,int start,int n,vector<bool>&visited){\n //if(ind<0 || ind>=n) return false;\n if(arr[ind]==0) {\n //final_answer=true;\n return true;\n }\n //if(final_answer) return true;\n if(visited[ind]) return false;\n visited[ind]=true;\n bool forward=false;\n if(ind+arr[ind]<n) forward=jump(ind+arr[ind],arr,start,n,visited);\n bool backward=false;\n if(ind-arr[ind]>=0) backward=jump(ind-arr[ind],arr,start,n,visited);\n return forward|backward;\n }\n bool canReach(vector<int>& arr, int start) {\n vector<bool>visited(arr.size(),false);\n return jump(start,arr,start,arr.size(),visited);\n }\n};", "memory": "46590" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool dfs(vector<int>& arr, vector<bool>& vis, int index) {\n int n = arr.size();\n if (arr[index] == 0)\n return true;\n \n if(vis[index]==true) return false;\n \n vis[index]=true;\n \n \n if((index+arr[index])<n and dfs(arr, vis, (index + arr[index]))){\n return true;\n }\n if((index-arr[index])>=0 and dfs(arr, vis, (index - arr[index]))){\n return true;\n }\n\n return false;\n \n }\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<bool> vis(n, false);\n return dfs(arr, vis, start);\n }\n};", "memory": "46850" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool dfs(vector<int>& arr, vector<bool>& vis, int index) {\n int n = arr.size();\n if (arr[index] == 0)\n return true;\n\n if (vis[index] == true)\n return false;\n\n vis[index] = true;\n\n if ((index + arr[index]) < n and dfs(arr, vis, (index + arr[index]))) {\n return true;\n }\n if ((index - arr[index]) >= 0 and dfs(arr, vis, (index - arr[index]))) {\n return true;\n }\n\n return false;\n }\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<bool> vis(n, false);\n return dfs(arr, vis, start);\n }\n};", "memory": "46850" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<int> adj[n+1];\n for(int i=0;i<n;i++){\n \n if(i-arr[i]>=0){\n int num=i-arr[i];\n adj[i].push_back(num);\n }\n if(i+arr[i]<n){\n int num=i+arr[i];\n adj[i].push_back(num);\n }\n }\n vector<int> vis(n+1,0);\n queue<int> q;\n vis[start]=1;\n q.push(start);\n while(!q.empty()){\n int node=q.front();\n vis[node]=1;\n q.pop();\n if(arr[node]==0){\n return true;\n }\n for(auto it:adj[node]){\n if(!vis[it]){\n q.push(it);\n }\n }\n }\n return false;\n }\n};", "memory": "48150" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> adj[n];\n for(int i = 0; i < n; i++){\n if(i + arr[i] < n) adj[i].push_back(i+arr[i]);\n if(i - arr[i] >= 0) adj[i].push_back(i-arr[i]);\n }\n set<int> st;\n for(int i = 0; i < n; i++){\n if(arr[i] == 0) st.insert(i);\n }\n\n queue<int> q;\n vector<int> vis(n, 0);\n q.push(start);\n vis[start] = 1;\n\n while(!q.empty()){\n auto node = q.front();\n q.pop();\n\n for(auto it : adj[node]){\n if(!vis[it]){\n vis[it] = 1;\n q.push(it);\n }\n }\n }\n for(int i = 0; i < n; i++){\n if(vis[i] == 1 and st.find(i) != st.end()) return true;\n }\n return false;\n }\n};", "memory": "48410" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> done(n);\n function<bool(int,int)> dfs = [&](int u, int p) {\n if(u<0 || u>=n || done[u]) return false;\n if(arr[u]==0) return true;\n done[u] = 1;\n if(u-arr[u]!=p && dfs(u - arr[u],u)) return true;\n if(u+arr[u]!=p && dfs(u + arr[u],u)) return true;\n done[u] = 2;\n return false;\n };\n return dfs(start,-1);\n }\n};", "memory": "48670" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vector<int> done(n);\n function<bool(int,int)> dfs = [&](int u, int p) {\n if(u<0 || u>=n || done[u]) return false;\n if(arr[u]==0) return true;\n done[u] = 1;\n if(u-arr[u]!=p && dfs(u - arr[u],u)) return true;\n if(u+arr[u]!=p && dfs(u + arr[u],u)) return true;\n done[u] = 2;\n return false;\n };\n return dfs(start,-1);\n }\n};", "memory": "48930" }
1,428
<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>
3
{ "code": "class Solution {\n vector<bool> visited;\n vector<int> dp;\n int n;\npublic:\n bool helper(vector<int> &nums,int i) {\n if(i>=n or i<0) return false;\n if(visited[i]) return false;\n if(dp[i]!=-1) return dp[i];\n if(nums[i]==0) return dp[i] = true;\n visited[i] = true;\n bool left = helper(nums,i-nums[i]);\n bool right = helper(nums,i+nums[i]);\n return dp[i] = left or right;\n }\n\n bool canReach(vector<int>& nums, int start) {\n n = nums.size();\n visited = vector<bool> (n,false);\n dp = vector<int> (n,-1);\n return helper(nums,start);\n }\n};", "memory": "49190" }
1,428
<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>
3
{ "code": "class Solution {\n vector<bool> visited;\n vector<int> dp;\n int n;\npublic:\n bool helper(vector<int> &nums,int i) {\n if(i>=n or i<0) return false;\n if(dp[i]!=-1) return dp[i];\n if(visited[i]) return false;\n if(nums[i]==0) return dp[i] = true;\n visited[i] = true;\n bool left = helper(nums,i-nums[i]);\n bool right = helper(nums,i+nums[i]);\n return dp[i] = left or right;\n }\n\n bool canReach(vector<int>& nums, int start) {\n n = nums.size();\n visited = vector<bool> (n,false);\n dp = vector<int> (n,-1);\n return helper(nums,start);\n }\n};", "memory": "49190" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n int rec(vector<int>& arr, vector<int> &dp,int start,vector<bool> &vis){\n int n=arr.size();\n if(start<0||start>=n)\n return 0;\n if(arr[start]==0) return 1;\n if(vis[start]){\n return dp[start]!=-1?dp[start]:0;\n }\n vis[start]=true;\n if(dp[start]!=-1)\n return dp[start];\n return dp[start]=rec(arr,dp,start+arr[start],vis)+rec(arr,dp,start-arr[start],vis)>0?1:0;\n }\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<int> dp(n,-1);\n vector<bool> vis(n);\n return rec(arr,dp,start,vis);\n }\n};", "memory": "49450" }
1,428
<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>
3
{ "code": "class Solution {\n\n void dfs(int i, bool &flag, vector<int>&arr, vector<bool>&vis)\n {\n \n vis[i] = true;\n if(arr[i] == 0)flag = true;\n\n if(i+arr[i] < arr.size() && !vis[i+arr[i]])\n dfs(i+arr[i], flag, arr, vis);\n\n if(i-arr[i] >= 0 && !vis[i-arr[i]])\n dfs(i-arr[i], flag, arr, vis);\n }\n\npublic:\n bool canReach(vector<int>& arr, int start) {\n \n vector<bool>vis(arr.size(), false);\n\n bool flag = false;\n\n dfs(start, flag, arr, vis);\n\n\n return flag;\n }\n};", "memory": "49710" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& arr,int s,unordered_map<int,int>& visited){\n \n if(s < 0 || s>= arr.size())\n return false;\n if(arr[s] == 0)\n return true;\n \n if(visited[s])\n return false;\n \n visited[s]=true;\n bool include=helper(arr , s+arr[s] , visited);\n bool exclude=helper(arr , s-arr[s] , visited);\n visited[s]=false;\n return (include || exclude);\n }\n bool canReach(vector<int>& arr, int start) {\n unordered_map<int,int> visited;\n return helper(arr , start , visited);\n }\n};", "memory": "49710" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& arr,int s,unordered_map<int,int>& visited){\n \n if(s < 0 || s>= arr.size())\n return false;\n if(arr[s] == 0)\n return true;\n \n if(visited[s])\n return false;\n \n visited[s]=true;\n bool include=helper(arr , s+arr[s] , visited);\n bool exclude=helper(arr , s-arr[s] , visited);\n visited[s]=false;\n return (include || exclude);\n }\n bool canReach(vector<int>& arr, int start) {\n unordered_map<int,int> visited;\n return helper(arr , start , visited);\n }\n};", "memory": "49970" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n map<int, bool> visited;\n bool canReach(vector<int>& arr, int start) {\n if (start < 0 || start >= (int)arr.size()) {\n return false;\n }\n\n if (arr[start] == 0) {\n return true; \n }\n\n if (visited.count(start) == 0) {\n visited[start] = false;\n visited[start] = canReach(arr, start + arr[start]) || canReach(arr, start - arr[start]);\n }\n\n return visited[start];\n }\n};", "memory": "49970" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& nums, int start)\n {\n return canJump(nums, start);\n }\n\nprivate:\n unordered_map<int, int> mp;\n bool canJump(vector<int>& nums, int index)\n {\n if (mp.find(index) != mp.end())\n return mp[index];\n\n if (index >= nums.size() || index < 0)\n return mp[index] = false;\n\n if (nums[index] == 0)\n return mp[index] = true;\n\n mp[index] = false;\n bool flag = false;\n flag = flag || canJump(nums, index + nums[index]) || canJump(nums, index - nums[index]);\n \n // cout << index << \": \"<< nums[index] << \" -> \" << flag << endl;\n return mp[index] = flag;\n }\n};", "memory": "50230" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n map<int,int> vis;\n bool canReach(vector<int>& arr, int start) {\n if(vis[start]==1){\n return false;\n }\n if(start<0||start>=arr.size()){\n return false;\n }\n if(arr[start]==0){\n return true;\n }\n vis[start]=1;\n bool ans=canReach(arr,start+arr[start])||canReach(arr,start-arr[start]);\n \n return ans;\n }\n};", "memory": "50230" }
1,428
<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>
3
{ "code": "class Solution {\nprivate:\n vector<bool> visited;\npublic:\n bool DFS(vector<int>& arr, int cur) {\n if (cur < 0 || cur >= arr.size()) {\n return false;\n }\n\n if (arr[cur] == 0) {\n return true;\n }\n\n if (visited[cur]) {\n return false;\n }\n\n visited[cur] = true;\n\n return canReach(arr, cur - arr[cur]) || canReach(arr, cur + arr[cur]);\n }\n\n bool canReach(vector<int>& arr, int start) {\n visited.resize(arr.size());\n\n return DFS(arr, start);\n }\n};", "memory": "50490" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n \n bool solve(vector<int>&arr, int idx,int n, vector<bool>&visited,vector<int>&dp){\n if(idx<0||idx>=n||visited[idx])return false;\n if(arr[idx]==0)return true;\n if(dp[idx]!=-1)return dp[idx];\n visited[idx]=true;\n bool ans=false;\n if(!ans)ans=solve(arr,idx-arr[idx],n,visited,dp);\n if(!ans)ans=solve(arr,idx+arr[idx],n,visited,dp);\n visited[idx]=false;\n return dp[idx]=ans;\n }\n bool canReach(vector<int>& arr, int start) {\n int n=arr.size();\n vector<bool>visited(n,false);\n vector<int>dp(n,-1);\n return solve(arr,start,n,visited,dp);\n }\n};", "memory": "50750" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool DFS(vector<int>& visited, vector<int>& arr, int i) {\n if (i < 0 || i >= arr.size() || visited[i]) {\n return 0;\n }\n if (!arr[i]) {\n return 1;\n }\n\n visited[i] = 1;\n\n return DFS(visited, arr, i + arr[i]) || DFS(visited, arr, i - arr[i]);\n }\n\n bool canReach(vector<int>& arr, int start) {\n vector<int> visited(50001);\n\n return DFS(visited, arr, start);\n }\n};", "memory": "51010" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n set<int> visit;\n return helper(arr, start, visit);\n }\n\n bool helper(vector<int>& arr, int start, set<int>& visit) {\n if(start < 0 || start >= arr.size() || visit.contains(start)) return false;\n if(arr[start] == 0) return true;\n visit.insert(start);\n return helper(arr, start + arr[start], visit) || \n helper(arr, start - arr[start], visit);\n }\n};", "memory": "51010" }
1,428
<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>
3
{ "code": "\n\n\n\nclass Solution {\npublic:\n bool rx(std::vector<int> &nums, std::vector<bool> &visited, int i) {\n if(i >= nums.size() or i < 0 or visited[i]) { return false; }\n if(visited[i] or (nums[i] == 0)) { return true; }\n if(not visited[i]) {\n visited[i] = true;\n return rx(nums, visited, i-nums[i]) or rx(nums, visited, i+nums[i]);\n }\n return false;\n }\n \n bool canReach(vector<int>& nums, int start) {\n int const n = nums.size();\n std::vector<bool> visited(n);\n return rx(nums, visited, start);\n }\n};", "memory": "51270" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n std::vector<bool> visited(arr.size());\n std::function<bool(int)> search = [&search, &arr, &visited](int i) -> bool {\n if(arr[i] == 0){\n return true;\n }\n auto ret = false;\n visited[i] = true;\n if(i + arr[i] < arr.size() && !visited[i+arr[i]]){\n ret = ret || search(i+arr[i]);\n }\n if(i - arr[i] >= 0 && !visited[i-arr[i]]){\n ret = ret || search(i-arr[i]);\n }\n return ret;\n };\n return search(start);\n }\n};", "memory": "51270" }
1,428
<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>
3
{ "code": "bool isValid(vector<int> &arr, int index, vector<bool> &visited)\n{\n return (index >= 0 && index < arr.size() && !visited[index]);\n}\n\nbool helper(vector<int> &arr, int start, vector<bool> &visited)\n{\n if(arr[start] == 0)\n {\n return true;\n }\n\n visited[start] = true;\n bool ans1 = false, ans2 = false;\n\n if(isValid(arr, start + arr[start], visited))\n {\n ans1 = helper(arr, start + arr[start], visited);\n }\n \n if(isValid(arr, start - arr[start], visited))\n {\n ans2 = helper(arr, start - arr[start], visited);\n }\n\n visited[start] = false;\n return (ans1 || ans2);\n}\n\nclass Solution {\npublic:\n bool canReach(vector<int>& arr, int start) \n {\n vector<bool> visited(arr.size(), false);\n return helper(arr, start, visited);\n }\n};", "memory": "51530" }
1,428
<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>
3
{ "code": "class Solution {\n bool dfs(vector<int>& v , vector<bool>& vis, int start, int n) {\n if (v[start] == 0)\n return true;\n int a = v[start] + start;\n int b = start - v[start];\n bool a1 = false;\n if (a < n && !vis[a]){\n vis[a] = true;\n a1 = dfs(v, vis, a, n);\n }\n bool a2 = false;\n if (b >= 0 && !vis[b]){\n vis[b] = true;\n a2 = dfs(v, vis, b, n);\n }\n\n return a1||a2;\n\n }\npublic:\n bool canReach(vector<int>& arr, int start) {\n\n vector<bool> vis(arr.size(), false);\n vis[start] = true;\n return dfs(arr, vis, start, arr.size());\n }\n};", "memory": "51530" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\nunordered_set<int>st;\n bool canReach(vector<int>& arr, int start) {\n if(st.count(start) || start<0 || start>=arr.size()) return false;\n if(arr[start]==0) return true;\n st.insert(start);\n return canReach(arr,start+arr[start]) || canReach(arr,start-arr[start]);\n \n }\n};", "memory": "51790" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n unordered_set<int> visited;\n bool rec(vector<int>& arr, int i) {\n if (visited.count(i) || i < 0 || i >= arr.size())\n return false;\n if (arr[i] == 0)\n return true;\n visited.insert(i);\n return rec(arr, i+arr[i]) || rec(arr, i-arr[i]);\n }\n\n bool canReach(vector<int>& arr, int start) {\n return rec(arr, start);\n }\n};", "memory": "51790" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool solve(vector<int>&arr,int start,int n,unordered_set<int>&s)\n {\n if(start>=n)\n {\n return false;\n }\n if(start<0)\n {\n return false;\n }\n if(s.find(start)!=s.end())\n {\n return false;\n }\n s.insert(start);\n if(arr[start]==0)\n {\n return true;\n }\n bool op1=solve(arr,start-arr[start],n,s);\n bool op2=solve(arr,start+arr[start],n,s);\n return op1||op2;\n }\n bool canReach(vector<int>& arr, int start) \n {\n int n=arr.size();\n if(arr[start]==0)\n return true;\n unordered_set<int>s;\n return solve(arr,start,n,s); \n }\n};", "memory": "52050" }
1,428
<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>
3
{ "code": "#include <vector>\n#include <unordered_set>\n#include <algorithm>\n\nclass Solution {\npublic:\n bool solve(const std::vector<int> &arr, const std::unordered_set<int> &target, int start, std::unordered_set<int> &visited) {\n // Check if the current index is out of bounds\n if (start < 0 || start >= arr.size()) {\n return false;\n }\n \n // Check if the index is already visited\n if (visited.find(start) != visited.end()) {\n return false;\n }\n \n // Mark the current index as visited\n visited.insert(start);\n \n // Check if the current index is a target\n if (target.find(start) != target.end()) {\n return true;\n }\n \n // Recursive calls for left and right shifts\n bool reAns = false;\n reAns = reAns || solve(arr, target, start - arr[start], visited);\n reAns = reAns || solve(arr, target, start + arr[start], visited);\n \n return reAns;\n }\n\n bool canReach(const std::vector<int>& arr, int start) {\n std::unordered_set<int> target;\n std::unordered_set<int> visited;\n \n // Collect all indices with value 0 in the target set\n for (int i = 0; i < arr.size(); ++i) {\n if (arr[i] == 0) {\n target.insert(i);\n }\n }\n \n // Start the recursive search\n return solve(arr, target, start, visited);\n }\n};\n", "memory": "52050" }
1,428
<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>
3
{ "code": "class Solution {\nprivate:\n unordered_set<int> memo;\n vector<int> arr;\n bool dfs(int idx){\n if(idx < 0 || idx >= this->arr.size() || this->memo.find(idx) != this->memo.end()) return false;\n if(this->arr[idx] == 0) return true;\n\n int val = this->arr[idx];\n this->memo.insert(idx);\n\n return dfs(idx + val) || dfs(idx - val);\n }\npublic:\n bool canReach(vector<int>& arr, int start) {\n this->arr = arr;\n return dfs(start);\n }\n};", "memory": "53090" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n if(arr[start]==0) return true;\n int n=arr.size();\n vector<vector<int>> adj(n);\n for(int i=0;i<n;i++){\n if(i+arr[i]<n&&i+arr[i]>=0) adj[i].push_back(i+arr[i]);\n if(i-arr[i]<n&&i-arr[i]>=0) adj[i].push_back(i-arr[i]);\n }\n queue<int> q;\n q.push(start);\n vector<bool> vis(n,false);\n vis[start]=true;\n while(!q.empty()){\n int k=q.size();\n for(int i=0;i<k;i++){\n int p=q.front();\n for(auto it:adj[p]){\n if(!vis[it]){\n vis[it]=true;\n if(arr[it]==0) return true;\n q.push(it);\n }\n }\n q.pop();\n }\n }\n return false;\n }\n};", "memory": "53090" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n vector<vector<int>> adj(arr.size());\n for (int idx = 0; idx < arr.size(); idx++) {\n int left = idx - arr[idx]; \n int right = idx + arr[idx]; \n \n if (left >= 0) {\n adj[idx].push_back(left); \n }\n if (right < arr.size()) {\n adj[idx].push_back(right); \n }\n }\n queue<int>q;\n vector<bool>visited(arr.size(),false);\n q.push(start);\n visited[start]=true;\n while(!q.empty()){\n int pick = q.front();\n q.pop();\n if(0==arr[pick]){\n return true;\n }\n for(auto it : adj[pick]){\n if(!visited[it]){\n visited[it]=true;\n q.push(it);\n }\n }\n }\n return false;\n }\n};", "memory": "53350" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n vector<int> vist;\n void dfs(vector<int>& a,int at,vector<int> adj[]){\n vist[at] = 1;\n for(auto it: adj[at]){\n if(!vist[it]){\n dfs(a,it,adj);\n }\n }\n }\n bool canReach(vector<int>& a, int start) {\n int n = a.size();\n vector<int> adj[n];\n for(int i=0;i<n;i++){\n int v1 = i-a[i];\n int v2 = i+a[i];\n if(v1>=0 && v1<n){\n adj[i].push_back(v1);\n // adj[v1].push_back(i);\n }\n if(v2>=0 && v2<n){\n adj[i].push_back(v2);\n // adj[v2].push_back(i);\n }\n }\n vist = vector<int> (n,0);\n dfs(a,start,adj);\n for(int i=0;i<n;i++){\n if(a[i]==0 && vist[i]) return true;\n }\n return false;\n }\n};", "memory": "53350" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n set<int> st;\n vector<int> vis;\n // vector<int> adj[n];\n bool dfs(int node , vector<int> adj[] , vector<int>& arr){\n vis[node] = 1;\n // if(st.find(node) != st.end()) return true;\n if(arr[node]==0) return true;\n for(auto child : adj[node]){\n if(vis[child]) continue;\n if(dfs(child,adj,arr)) return true;\n }\n return false;\n }\n bool canReach(vector<int>& arr, int start) {\n int n = arr.size();\n vis.resize(n+1 , 0);\n vector<int> adj[n];\n for(int i=0;i<n;i++){\n if(arr[i]==0) st.insert(i);\n int u = i;\n int fv = i + arr[i];\n int bv = i - arr[i];\n if(fv < n){\n adj[u].push_back(fv);\n }\n if(bv>=0){\n adj[u].push_back(bv);\n }\n }\n\n return dfs(start , adj,arr);\n }\n};", "memory": "53610" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool canReach(vector<int>& arr, int start) {\n unordered_set<int> visited;\n\n return dfs(visited,arr,start);\n // if (visited.size() == arr.size()) return true;\n // return false;\n \n }\n bool dfs(unordered_set<int> &visited, vector<int> &arr, int curr){\n if(arr[curr] == 0) return true;\n // bool can_visit = false;\n visited.insert(curr);\n int t = curr + arr[curr]; \n if(t<arr.size() and t>=0 and visited.find(t)==visited.end()){\n if (arr[t] == 0) return true;\n if (dfs(visited,arr,t)) return true;\n // if (visited.size() == arr.size())\n // return;\n }\n t = curr - arr[curr]; \n if(t<arr.size() and t>=0 and visited.find(t)==visited.end() ){\n if (arr[t] == 0) return true;\n return dfs(visited,arr,t);\n // if (visited.size() == arr.size())\n // return;\n }\n // visited.erase(curr);\n return false;\n\n\n }\n};", "memory": "53610" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool solverecur(vector<int>& arr, int index, int final,unordered_map<int,bool>& visited) {\n if (index == final) {\n return true;\n }\n if(index<0)\n return false;\n if(index>=arr.size())\n return false;\n \n visited[index]=true;\n if (index + arr[index] < arr.size()&&visited[index+arr[index]]!=true) { \n if( solverecur(arr, index + arr[index], final,visited))\n\n return true;\n }\n if (index - arr[index] >= 0&&visited[index-arr[index]]!=true) { \n if(solverecur(arr, index - arr[index], final,visited))\n return true;\n }\n visited[index]=false;\n return false;\n }\n\n bool canReach(vector<int>& arr, int start) {\n unordered_map<int,bool>visited;\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i] == 0) {\n if (solverecur(arr, start, i,visited)) {\n return true;\n }\n }\n }\n return false;\n }\n};\n", "memory": "53870" }
1,428
<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>
3
{ "code": "class Solution {\npublic:\n bool solverecur(vector<int>& arr, int index, int final,unordered_map<int,bool>& visited) {\n if (index == final) {\n return true;\n }\n if(index<0)\n return false;\n if(index>=arr.size())\n return false;\n \n visited[index]=true;\n if (index + arr[index] < arr.size()&&visited[index+arr[index]]!=true) { \n if( solverecur(arr, index + arr[index], final,visited))\n\n return true;\n }\n if (index - arr[index] >= 0&&visited[index-arr[index]]!=true) { \n if(solverecur(arr, index - arr[index], final,visited))\n return true;\n }\n visited[index]=false;\n return false;\n }\n\n bool canReach(vector<int>& arr, int start) {\n unordered_map<int,bool>visited;\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i] == 0) {\n if (solverecur(arr, start, i,visited)) {\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "53870" }
1,435
<p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p> <p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p> <p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] <strong>Output:</strong> [2,7,14,8] <strong>Explanation:</strong> The binary representation of the elements in the array are: 1 = 0001 3 = 0011 4 = 0100 8 = 1000 The XOR values for queries are: [0,1] = 1 xor 3 = 2 [1,2] = 3 xor 4 = 7 [0,3] = 1 xor 3 xor 4 xor 8 = 14 [3,3] = 8 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]] <strong>Output:</strong> [8,0,4,4] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length, queries.length &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= left<sub>i</sub> &lt;= right<sub>i</sub> &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();", "memory": "8400" }
1,435
<p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p> <p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p> <p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] <strong>Output:</strong> [2,7,14,8] <strong>Explanation:</strong> The binary representation of the elements in the array are: 1 = 0001 3 = 0011 4 = 0100 8 = 1000 The XOR values for queries are: [0,1] = 1 xor 3 = 2 [1,2] = 3 xor 4 = 7 [0,3] = 1 xor 3 xor 4 xor 8 = 14 [3,3] = 8 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]] <strong>Output:</strong> [8,0,4,4] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length, queries.length &lt;= 3 * 10<sup>4</sup></code></li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>0 &lt;= left<sub>i</sub> &lt;= right<sub>i</sub> &lt; arr.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();", "memory": "8500" }