Last Updated: 30 Sep, 2025

Circular Jump Game

Hard

Problem statement

A group of N people are seated in chairs numbered 1 to N arranged in a circle. You are given an array A, where A[i-1] represents the jump distance for the person on chair i.


From any chair i, a person can jump A[i-1] chairs in either a clockwise (right) or counter-clockwise (left) direction.


Bob starts at chair X and needs to reach chair Y. Your task is to find the minimum number of jumps required to get from chair X to chair Y. If it's impossible to reach chair Y, return -1.


Input Format:
The first line contains an integer, N, the number of chairs.

The second line contains an integer, X, Bob's starting chair.

The third line contains an integer, Y, the destination chair.

The next line contains N space-separated integers, representing the jump distances A[0] to A[N-1].


Output Format:
Print a single integer representing the minimum number of jumps.

If the destination is unreachable, print -1.


Note:
This problem can be modeled as finding the shortest path in an unweighted graph. The chairs are the vertices (nodes) of the graph, and the possible jumps are the edges.

Since all jumps have a "cost" of 1, a Breadth-First Search (BFS) is the ideal algorithm to find the shortest path.

The problem uses 1-based indexing for chairs X and Y. It's often easier to convert these to 0-based indices for calculations (e.g., start_node = X - 1).