forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleCycleCheck.java
More file actions
46 lines (37 loc) · 1.26 KB
/
SingleCycleCheck.java
File metadata and controls
46 lines (37 loc) · 1.26 KB
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
46
package algoexpert.medium;
/*
PROBLEM:
Find if single cycle exists in given array
-> starting at any index and following the jumps, every element is visited exactly once before landing back on the starting index
EXAMPLE:
[2,3,1,-4,-4,2] -> true
Solution:
1. HashMap -> time : O(n) | space : O(n)
2. Count visited -> time : O(n) | space : O(1) [we don't really need hashmap, just need to check if it is revisiting 0 only at end]
*/
public class SingleCycleCheck
{
public static void test()
{
System.out.println(hasSingleCycle(new int [] {2,3,1,-4,-4,2}));
System.out.println(hasSingleCycle(new int [] {1,1,1,1,2}));
}
public static int nextIndex(int current, int [] array)
{
int next = (current + array[current]) % array.length;
if (next < 0) { next = array.length + next; }
return next;
}
public static boolean hasSingleCycle(int[] array) {
if (array.length < 1) { return true; }
int visitedCount = 0;
int current = 0;
while (visitedCount < array.length)
{
if (visitedCount > 0 && current == 0) { return false; }
visitedCount += 1;
current = nextIndex(current, array);
}
return (current == 0) ? true : false;
}
}