|
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +def circular_array_loop(nums: List[int]) -> bool: |
| 5 | + """ |
| 6 | + Checks if there is a cycle in the provided list of non-zero integers |
| 7 | +
|
| 8 | + Problem Explanation: |
| 9 | + A cycle exists if we can start at an index and follow the direction of the array (either forward or backward) and |
| 10 | + return to the starting index without getting stuck in a loop that doesn't include all elements. |
| 11 | +
|
| 12 | + The array is circular, meaning that if you reach the end of the array, you wrap around to the beginning, and vice |
| 13 | + versa. |
| 14 | +
|
| 15 | + Algorithm: |
| 16 | + - Direction Check: Determine the direction of movement (forward or backward) based on the sign of the current |
| 17 | + element. |
| 18 | + - Cycle Detection: Use a slow and fast pointer approach (similar to Floyd's Tortoise and Hare algorithm) to detect |
| 19 | + a cycle. |
| 20 | + - Cycle Validation: Ensure that the cycle is valid (i.e., it covers all elements and is not a subset of the array). |
| 21 | +
|
| 22 | + Time Complexity: |
| 23 | + The time complexity is O(n), where n is the length of the array. This is because each element is processed at |
| 24 | + most once. |
| 25 | +
|
| 26 | + Space Complexity: |
| 27 | + The space complexity is O(1) since we are using a constant amount of extra space. |
| 28 | +
|
| 29 | + Args: |
| 30 | + nums (list): list of non-zero integers |
| 31 | + Returns: |
| 32 | + bool: True if there is a circular array loop, False otherwise |
| 33 | + """ |
| 34 | + length = len(nums) |
| 35 | + |
| 36 | + for i in range(length): |
| 37 | + # Skip if we've already visited this index |
| 38 | + if nums[i] == 0: |
| 39 | + continue |
| 40 | + |
| 41 | + # Determine the direction of the cycle based on the sign of the current element |
| 42 | + direction = 1 if nums[i] > 0 else -1 |
| 43 | + |
| 44 | + # set the slow and the fast pointers to the current index |
| 45 | + slow, fast = i, i |
| 46 | + |
| 47 | + while True: |
| 48 | + # Move slow pointer |
| 49 | + slow = (slow + nums[slow]) % length |
| 50 | + |
| 51 | + if nums[slow] * direction <= 0: |
| 52 | + break # Invalid direction |
| 53 | + |
| 54 | + # Move fast pointer twice |
| 55 | + |
| 56 | + # first time |
| 57 | + fast = (fast + nums[fast]) % length |
| 58 | + if nums[fast] * direction <= 0: |
| 59 | + break # Invalid direction |
| 60 | + |
| 61 | + # second time |
| 62 | + fast = (fast + nums[fast]) % length |
| 63 | + if nums[fast] * direction <= 0: |
| 64 | + break # Invalid direction |
| 65 | + |
| 66 | + # If slow and fast meet, a cycle is detected |
| 67 | + if slow == fast: |
| 68 | + # Check if the cycle length is greater than 1 |
| 69 | + if slow == (slow + nums[slow]) % length: |
| 70 | + break # Cycle length is 1, invalid |
| 71 | + return True |
| 72 | + |
| 73 | + # Mark all visited indices in this path to avoid reprocessing |
| 74 | + # Note that this modifies the input list of elements and may not be desirable. This however, has a space |
| 75 | + # complexity of O(1) as no extra space is allocated. An alternative solution is to use a list to keep track of |
| 76 | + # visited indices and avoid reprocessing, this list would be initialized the the length of the input list such |
| 77 | + # as visited = [0 for _ in range(length)] or visited = [False for _ in range(length)] or |
| 78 | + # visited = [False] * length |
| 79 | + # and then that is used to avoid reprocessing like below: |
| 80 | + # while nums[slow] * direction > 0 and not visited[slow]: |
| 81 | + # visited[slow] = True |
| 82 | + # slow = (slow + nums[slow]) % length |
| 83 | + slow = i |
| 84 | + while nums[slow] * direction > 0: |
| 85 | + next_slow = (slow + nums[slow]) % length |
| 86 | + nums[slow] = 0 |
| 87 | + slow = next_slow |
| 88 | + |
| 89 | + return False |
0 commit comments