-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirculararray.py
45 lines (32 loc) · 1.19 KB
/
circulararray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def circular_array_loop_exists(arr):
for i in range(len(arr)):
is_forward = arr[i] >= 0 # if we are moving forward or not
slow, fast = i, i
# if slow or fast becomes '-1' this means we can't find cycle for this number
while True:
# move one step for slow pointer
slow = find_next_index(arr, is_forward, slow)
# move one step for fast pointer
fast = find_next_index(arr, is_forward, fast)
if (fast != -1):
# move another step for fast pointer
fast = find_next_index(arr, is_forward, fast)
if slow == -1 or fast == -1 or slow == fast:
break
if slow != -1 and slow == fast:
return True
return False
def find_next_index(arr, is_forward, current_index):
direction = arr[current_index] >= 0
if is_forward != direction:
return -1 # change in direction, return -1
next_index = (current_index + arr[current_index]) % len(arr)
# one element cycle, return -1
if next_index == current_index:
next_index = -1
return next_index
def main():
print(circular_array_loop_exists([1, 2, -1, 2, 2]))
print(circular_array_loop_exists([2, 2, -1, 2]))
print(circular_array_loop_exists([2, 1, -1, -2]))
main()