1. A cycle must start and end at the same index.
2. The cycle’s length should be greater than 1. For example, if the given array is {1,-2,4,1} then there is a cycle of length 1 from index 3 (1 based indexing).
3. All movements in a cycle must follow a single direction, they should be either in a clockwise or counterclockwise direction.
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
The first line of each test case contains a positive integer N, which represents the size of the circular array.
The next line contains 'N' single space-separated integers representing the elements of the circular array.
For each test case, print “True” if there is a cycle, else print “False”.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
-10^5 <= ARR[i] <= 10^5 (ARR[i] != 0)
Time Limit: 1 sec
In this approach, for each index, we will check if there can be a cycle passing through this index. We will follow the below steps for every index idx.
Algorithm
We will define a state array which will denote the current state of indices.
Initially, all indices are in state 0.
We will make the state equal to 2 for those indices which contain cycle of length 1, means state[idx] = 2 ,if |Arr[idx]| % N = 0.
Iterate over the indices and if it’s state is 0, then we will recursively check if there can be a cycle.
Recursive State: circularArrayLoopHelper(Arr, idx, dir), where Arr is the given array, idx is the current index and dir is the direction in which we are moving (dir = Arr[idx]/abs(Arr[idx]), where abs gives the absolute value).
The recursive function will return true if there is a cycle else false.