Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Circular Array Loop problem solution

YASH PAL, 31 July 2024

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;
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes