All problems

Problem 623

Jump Game III

Medium · 20 pts · Array, Breadth-First Search

Problem

You start at index START of an array of non-negative integers. From index i you can jump to i + A[i] or to i - A[i], as long as you stay inside the array. Decide whether you can reach any index whose value is 0. Treat the indices as nodes of a graph with up to two edges each, and run a breadth-first or depth-first search from START, marking visited indices so that cycles do not loop forever.

Input

The first line contains N and START. The second line contains N integers.

Output

Print YES if an index with value 0 can be reached, otherwise NO.

Constraints

1 <= N <= 50000; 0 <= A[i] < N; 0 <= START < N

Example input

7 5
4 2 3 0 3 1 2

Example output

YES

Explanation

In 4 2 3 0 3 1 2 starting at index 5, jump 5 → 4 (5 - 1), then 4 → 1 (4 - 3), then 1 → 3 (1 + 2), and A[3] = 0. So the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Making a Large Island Next: Bus Routes