website/content.en/ChapterFour/0400~0499/0457.Circular-Array-Loop.md
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:
Follow up:
Could you solve it in O(n) time complexity and O(1) extra space complexity?
Given a circular array nums containing positive and negative integers. If the number k at an index is positive, move forward k indices. Conversely, if it is negative (-k), move backward k indices. Because the array is circular, you may assume that the next element of the last element is the first element, and the previous element of the first element is the last element.
Determine whether there is a loop (or cycle) in nums. The loop must start and end at the same index and have a length > 1. Furthermore, all movements in a loop must proceed in the same direction. In other words, a loop cannot include both forward and backward movements.
Hints:
Follow-up:
+ means moving right (forward), and - means moving left (backward). Determine whether there is a loop in this circular array, and the loop cannot contain only one element; all directions within the loop must be the same.num[i] * num[j] > 0 to determine whether they are in the same direction (if they are in opposite directions, their product must be negative). If no cycle is found, set all num[] values along the path already traversed to 0, marking them as visited. The next time the loop encounters a visited element, num[i] * num[j] > 0 will be 0, and the cycle-finding process will exit early.
package leetcode
func circularArrayLoop(nums []int) bool {
if len(nums) == 0 {
return false
}
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
continue
}
// slow/fast pointer
slow, fast, val := i, getNextIndex(nums, i), 0
for nums[fast]*nums[i] > 0 && nums[getNextIndex(nums, fast)]*nums[i] > 0 {
if slow == fast {
// check for loop with only one element
if slow == getNextIndex(nums, slow) {
break
}
return true
}
slow = getNextIndex(nums, slow)
fast = getNextIndex(nums, getNextIndex(nums, fast))
}
// loop not found, set all element along the way to 0
slow, val = i, nums[i]
for nums[slow]*val > 0 {
next := getNextIndex(nums, slow)
nums[slow] = 0
slow = next
}
}
return false
}
func getNextIndex(nums []int, index int) int {
return ((nums[index]+index)%len(nums) + len(nums)) % len(nums)
}