Skip to content

Practice Circular Array Loop

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#457
LeetCode Problem

Circular Array Loop

Problem Statement

The nonzero integers in nums describe moves around a circular array. At index i, a positive nums[i] moves forward by that many positions, while a negative value moves backward by its absolute value; wrap around at either end. Return true if following these moves can repeat a cycle containing more than one index whose values are all positive or all negative. A one-index loop or a loop that changes direction does not qualify. Otherwise return false. Follow up: Can you achieve O(n) time and O(1) extra space?

Source: Circular Array Loop on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,-1,1,2,2]
Output
true

# Example 2

Input
nums = [-1,-2,-3,-4,-5,6]
Output
false

# Example 3

Input
nums = [1,-1,5,1,4]
Output
true

Constraints

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

Related LeetCode Problems

Practice more problems similar to Circular Array Loop.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.