1024. Minimum Steps to Reach End in Circular Array

EasyGraphsBFSGraph

Given a circular array 'arr' of non-negative integers of length n, you start at index 0. From index i, you may jump to (i + arr[i]) mod n or (i - arr[i]) mod n. Return the minimum number of jumps to reach the last index n-1, or -1 if it cannot be reached. The input is JSON {arr}.

Input: JSON {arr}.

Output: Integer — minimum jumps, or -1.

Examples

Example 1
Input: {"arr":[2,3,1,1,4]}
Output: 2
Explanation: 0->3->4 in two jumps.
Example 2
Input: {"arr":[1]}
Output: 0
Explanation: Already at the last index.
Example 3
Input: {"arr":[0,0,0]}
Output: -1
Explanation: Cannot move.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →