You are playing a game involving a circular array of non-zero integers nums. Each nums[i] denotes the number of indices forward/backward you must move if you are located at index i:


	If nums[i] is positive, move nums[i] steps forward, and
	If nums[i] is negative, move nums[i] steps backward.


Since the array is circular, you may assume that moving forward from the last element puts you on the first element, and moving backwards from the first element puts you on the last element.

A cycle in the array consists of a sequence of indices seq of length k where:


	Following the movement rules above results in the repeating index sequence seq[0] -&gt; seq[1] -&gt; ... -&gt; seq[k - 1] -&gt; seq[0] -&gt; ...
	Every nums[seq[j]] is either all positive or all negative.
	k &gt; 1


Return true if there is a cycle in nums, or false otherwise.

&nbsp;
Example 1:

Input: nums = [2,-1,1,2,2]
Output: true
Explanation:
There is a cycle from index 0 -&gt; 2 -&gt; 3 -&gt; 0 -&gt; ...
The cycle's length is 3.


Example 2:

Input: nums = [-1,2]
Output: false
Explanation:
The sequence from index 1 -&gt; 1 -&gt; 1 -&gt; ... is not a cycle because the sequence's length is 1.
By definition the sequence's length must be strictly greater than 1 to be a cycle.


Example 3:

Input: nums = [-2,1,-1,-2,-2]
Output: false
Explanation:
The sequence from index 1 -&gt; 2 -&gt; 1 -&gt; ... is not a cycle because nums[1] is positive, but nums[2] is negative.
Every nums[seq[j]] must be either all positive or all negative.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 5000
	-1000 &lt;= nums[i] &lt;= 1000
	nums[i] != 0


&nbsp;
Follow up: Could you solve it in O(n) time complexity and O(1) extra space complexity?

class Solution:
    def circularArrayLoop(self, nums: List[int]) -> bool:
        n = len(nums)
        for i in range(n):
            seen = set()
            minval = float('inf')
            maxval = float('-inf')
            j = i
            while j not in seen:
                seen.add(j)
                minval = min(minval, nums[j])
                maxval = max(maxval, nums[j])
                k = 1 + abs(nums[j]) // n
                j = (k * n + j + nums[j]) % n
            if j == i and len(seen) > 1 and (minval > 0 or maxval < 0):
                return True
        return False
class Solution {
    public boolean circularArrayLoop(int[] nums) {
        for (int i=0; i<nums.length; i++) {
            boolean isForward = nums[i] > 0;
            int slow = i;
            int fast = i; 
            do {
                slow = findNextIndex(nums, isForward, slow);
                fast = findNextIndex(nums, isForward, fast);
                if (fast != -1) {
                    fast = findNextIndex(nums, isForward, fast);
                }
            } while (slow != -1 && fast != -1 && slow != fast);
            if (slow != -1 && slow == fast) {
                return true;
            }
        }
        return false;
    }
    private int findNextIndex(int[] arr, boolean isForward, int currentIndex) {
        boolean direction = arr[currentIndex] >= 0;
        if (isForward != direction) {
            return -1;
        }
        int nextIndex = (currentIndex + arr[currentIndex]) % arr.length;
        if (nextIndex < 0) {
            nextIndex += arr.length;
        }
        if (nextIndex == currentIndex) {
            nextIndex = -1;
        }
        return nextIndex;
    }
}
class Solution {
public:
    bool circularArrayLoop(vector<int>& nums) {
        unordered_set<int>us;
        for(int i{0};i<nums.size() && us.find(i)==us.end();i++){
            unordered_map<int,int>um;
            int index=0;
            int j=i;
            um[i];
            bool flag1=0,flag2=0;
            while(true){
                if(nums.at(i)<0){
                    index=(nums.size()+nums.at(j)+j)%nums.size();
                    flag1=1;
                }else{
                    index=(nums.at(j)+j)%nums.size();
                    flag2=1;
                }
                if(nums.at(index)>0 && flag1==1){
                    break;
                }else if(nums.at(index)<0 && flag2==1){
                    break;
                }
                if(um.find(index)==um.end()){
                    um[index];
                    us.insert(index);
                }else{
                    if(j==index){
                        break;
                    }
                    if(um.size()>1){
                        return true;
                    }else{
                        break;
                    }
                }
                j=index;
            }            
        }
        return false;
    }
};
var circularArrayLoop = function(nums) {
    // cannot be a cycle if there are less than 2 elements
    const numsLen = nums.length;
    if (numsLen < 2) return false;

    // init visited array
    const visited = Array(numsLen).fill(false);

    // check each index to see if a cycle can be produced
    for (let i = 0; i < numsLen; i++) {
        if (visited[i]) continue;
        visited[i] = true;
        // determine initial direction
        const isPositive = nums[i] > 0;
        
        // reset which indices were visited after each iteration
        const visitedPerIdx = Array(numsLen).fill(false);
        
        // reset cycle length and current index after each iteration
        let cycleLen = 0,
            currIdx = i;
        
        // loop while cycle is valid
        while (true) {
            // break if current index moves cycle in opposite direction
            if (isPositive !== nums[currIdx] > 0) break;
			
            // calc next valid index
            let nextIdx = (currIdx + nums[currIdx]) % numsLen;
            // map negative index to a positive index
            if (nextIdx < 0) nextIdx += numsLen; 
          
            // break if cycle points to same index
            if (currIdx === nextIdx) break;
            
            cycleLen++; 
			// a cycle is found when the index has already been visited in the current outer iteration, and
			// the cycle length is greater than 1.
            if (visitedPerIdx[nextIdx] && cycleLen > 1) return true;
			
            visitedPerIdx[nextIdx] = true;
            visited[nextIdx] = true;
            // set curr index to new index
            currIdx = nextIdx;
        }
    }

    return false;
};
Circular Array Loop