Circular Array Loop

Given a circular array of non-zero integers, determine whether the array contains a loop that moves in a single direction and has length greater than 1. This is a cycle-detection problem on an implicit directed graph.

Input Format

nums = circular array

Output Format

true if a valid cycle exists, otherwise false

Constraints

  • 1 <= nums.length <= 5000; -1000 <= nums[i] <= 1000; nums[i] != 0

Examples

Example 1:

Input:

nums = [2,-1,1,2,2]

Output:

true

Explanation:

A cycle exists with consistent direction.

Example 2:

Input:

nums = [-1,2]

Output:

false

Explanation:

Any loop would be invalid or of length 1.

Loading...
Circular Array Loop - Math DSA Problem