In this Leetcode Circular Array Loop problem solution, 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:

  1. If nums[i] is positive, move nums[i] steps forward, and
  2. 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 backward 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:

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

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

Leetcode Circular Array Loop problem solution


Problem solution in Python.

class Solution:
    def circularArrayLoop(self, nums: List[int]) -> bool:
        
        total_nums = len(nums)
        
        for start in range(total_nums):
            if isinstance(nums[start], str):
                continue

            j = start
            direction = 1 if nums[j] > 0 else -1

            while isinstance(nums[j], int) and nums[j] * direction > 0 and nums[j] % total_nums:
                j_next = (j + nums[j]) % total_nums
                nums[j] = str(start)
                j = j_next
                if nums[j] == str(start):
                    return True

        return False



Problem solution in Java.

public class Solution {
    public boolean circularArrayLoop(int[] nums) {
        if(nums == null || nums.length == 0) return false;
        int n = nums.length;
        for(int i=0; i<n; i++){
            if(nums[i] == 0) continue;
            int slow = i, fast = i, count = 0;
            boolean forward = nums[slow] > 0;
            do{
                int tempSlow = slow;
                slow = (slow + nums[slow] + n) % n;
        
                if(forward && nums[fast] < 0 || !forward && nums[fast] > 0) return false;
                fast = (fast + nums[fast] + n) % n;
        
                if(forward && nums[fast] < 0 || !forward && nums[fast] > 0) return false;
                fast = (fast + nums[fast] + n) % n;
        
                nums[tempSlow] = 0;
                count++;
                
            } while(slow != fast);
            if(count > 1) return true;
        }
        return false;
    }
}


Problem solution in C++.

class Solution {
public:
    bool circularArrayLoop(vector<int>& nums) 
    {
        int n = nums.size();
        for(int i=0;i<nums.size();i++)
        {
            int start = i,end = i;
            if(nums[i]>0)
            {
                int k = 1;
                do
                {
                    int prev_end = end;
                    end = (end+nums[end])%n;
                    if(end==prev_end) break;
                }
                while(nums[end]>0 && end!=start && k++ && k<=n);
                if(end==start && k>1) return true;
            }
            else
            {
                int k = 1;
                do
                {
                    int prev_end = end;
                    end = ((end+nums[end])%n +n)%n;
                    if(end==prev_end) break;
                }
                while(nums[end]<0 && end!=start && k++ && k<=n);
                if(end==start && k>1) return true;
            }
        }
        return false;
    }
};