Given an array of non-negative integers arr, you are initially positioned at index start. When you are at index i, you can jump to i + arr[i] or i - arr[i]. Check if you can reach any index with value 0. Notice that you can not jump outside of the array at any time.
Given an array of non-negative integers arr, you are initially positioned at index start. When you are at index i, you can jump to i + arr[i] or i - arr[i]. Check if you can reach any index with value 0. Notice that you can not jump outside of the array at any time.
Example 1:
Input: {"arr":[4,2,3,0,3,1,2],"start":5}
Output: true
Constraints:
1≤arr.length≤5×104
0≤arr[i]<arr.length
0≤start<arr.length
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"arr":[4,2,3,0,3,1,2],"start":5}
Stand on index 5. Each index i is a node with two edges: to i + arr[i] and i - arr[i]. BFS from 5 and stop at the first index holding 0.