Circular Array Loop - Array - Medium - LeetCode
💻 coding

Circular Array Loop - Array - Medium - LeetCode

2 min read 362 words
2 min read
ShareWhatsAppPost on X
  • 1The problem involves detecting a cycle in a circular array based on positive and negative integer movements.
  • 2A valid cycle must start and end at the same index with a length greater than 1, moving in a single direction.
  • 3The provided solution uses depth-first search (DFS) with O(n) time and space complexity to determine the presence of a cycle.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The problem involves detecting a cycle in a circular array based on positive and negative integer movements."

Circular Array Loop - Array - Medium - LeetCode

You are given a circular array nums of positive and negative integers. If a number k at an index is positive, then move forward k steps. Conversely, if it's negative (-k), move backward k steps. Since the array is circular, you may assume that the last element's next element is the first element, and the first element's previous element is the last element.

Determine if there is a loop (or a cycle) in nums. A cycle must start and end at the same index and the cycle's length > 1. Furthermore, movements in a cycle must all follow a single direction. In other words, a cycle must not consist of both forward and backward movements.

Example 1:

Input: [2,-1,1,2,2] Output: true Explanation: There is a cycle, from index 0 -> 2 -> 3 -> 0. The cycle's length is 3. Example 2:

Input: [-1,2] Output: false Explanation: The movement from index 1 -> 1 -> 1 ... is not a cycle, because the cycle's length is 1. By definition the cycle's length must be greater than 1. Example 3:

Input: [-2,1,-1,-2,-2] Output: false Explanation: The movement from index 1 -> 2 -> 1 -> ... is not a cycle, because movement from index 1 -> 2 is a forward movement, but movement from index 2 -> 1 is a backward movement. All movements in a cycle must follow a single direction.

Note:

-1000 ≤ nums[i] ≤ 1000 nums[i] ≠ 0 1 ≤ nums.length ≤ 5000

public class Solution {
 public bool CircularArrayLoop(int[] nums) {
 int[] color = new int[nums.Length];
 for(int i = 0 ; i < nums.Length ; i++) {
 if(color[i] == 0 && DFS(nums, color, i)) return true;
 }
 return false;
 }
 
 private bool DFS(int[] nums, int[] color, int start) {
 
 if(color[start] == 2){ 
 return false;
 }
 
 color[start] = 1;
 int next = start + nums[start];
 next = next % nums.Length + nums.Length;
 next %= nums.Length;
 
 if(next == start || nums[next] * nums[start] < 0) {
 color[start] = 2;
 return false;
 }
 
 if(color[next] == 1) {
 color[start] = 2;
 return true;
 }
 
 if(DFS(nums, color, next)){ 
 return true;
 }
 color[start] = 2;
 return false;
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 19 November 2020 · 2 min read · 362 words

Part of AskGif Blog · coding

You might also like