Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-235

Jump Game III

Problem

Given arr and a start index, return True if any index holding the value 0 can be reached, where from index i you may jump to i + arr[i] or i - arr[i].

Use DFS or BFS with a visited set to avoid cycles. A start index whose value is 0 is an immediate success.

Hint

Only jump to indices inside the array, and mark visited indices before expanding them.

Test cases

case 1
in: [[4,2,3,0,3,1,2],5]
expected: true
case 2
in: [[4,2,3,0,3,1,2],0]
expected: true
case 3
in: [[3,0,2,1,2],2]
expected: false
case 4
in: [[0],0]
expected: true
case 5
in: [[2,0],0]
expected: false
Starter codePython
def can_reach_zero(arr, start):
    # Your code here
    pass

5 test cases · run your code against them in the editor