Circular Array Loop - Problem

You are playing a game involving a circular array of non-zero integers nums. Imagine this as a circular board where you move according to the values at each position.

Movement Rules:

  • If nums[i] is positive, move nums[i] steps forward
  • If nums[i] is negative, move abs(nums[i]) steps backward
  • Since the array is circular, moving forward from the last element takes you to the first, and moving backward from the first takes you to the last

A valid cycle exists if you can find a sequence of indices where:

  1. Following the movement rules creates a repeating sequence of indices
  2. All values in the cycle have the same direction (all positive or all negative)
  3. The cycle length is greater than 1

Return true if there is a valid cycle in nums, otherwise return false.

Example: [2, -1, 1, 2, 2] → At index 0: move 2 steps → index 2 → move 1 step → index 3 → move 2 steps → index 0 (cycle found!)

Input & Output

example_1.py — Valid Cycle Found
$ Input: [2, -1, 1, 2, 2]
Output: true
💡 Note: Starting at index 0: move 2 steps forward to index 2, then 1 step forward to index 3, then 2 steps forward to index 0. This creates a cycle 0→2→3→0 with all positive movements.
example_2.py — Direction Changes
$ Input: [-1, 2]
Output: false
💡 Note: Starting at index 0: move 1 step backward to index 1, then move 2 steps forward to index 1. No valid cycle because directions are inconsistent (backward then forward).
example_3.py — Self Loop
$ Input: [1, -1, 5, 1, 4]
Output: false
💡 Note: No valid cycles exist. Index 2 has value 5 which would create a self-loop (5 % 5 = 0), but cycles must have length > 1.

Visualization

Tap to expand
Circular Array Loop Detection+2+1+2+2Step 1Step 2Step 3Step 4Slow PointerFast Pointer✓ Valid Cycle Found: All movements are forward (+)
Understanding the Visualization
1
Choose starting position
Pick any position on the circular board
2
Follow movement rules
Move forward/backward based on the number at your current position
3
Track your path
Use two pointers moving at different speeds to detect when you loop back
4
Validate the cycle
Ensure all moves in the loop go the same direction and cycle length > 1
Key Takeaway
🎯 Key Insight: Use Floyd's cycle detection with direction validation to efficiently find valid cycles in O(n) time and O(1) space.

Time & Space Complexity

Time Complexity
⏱️
O(n²)

In worst case, we start from each of n positions and may traverse up to n positions for each start

n
2n
Quadratic Growth
Space Complexity
O(n)

Need to store visited indices for each path exploration

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 5000
  • -1000 ≤ nums[i] ≤ 1000
  • nums[i] ≠ 0
  • The array represents a circular structure
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.0K Views
Medium Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen