package leetcode;

/**
 * https://leetcode.com/problems/circular-array-loop/
 */
public class Problem457 {
    public boolean circularArrayLoop(int[] nums) {
        boolean[] visited = new boolean[nums.length];
        for (int i = 0; i < nums.length; i++) {
            if (visited[i]) {
                continue;
            }
            visited[i] = true;
            int j = i + nums[i];
            j %= nums.length;
            if (j < 0) {
                j = nums.length + j;
            }
            boolean found = false;
            boolean skip = false;
            while (!visited[j]) {
                if (nums[i] < 0 != nums[j] < 0) {
                    skip = true;
                }
                visited[j] = true;
                j += nums[j];
                j %= nums.length;
                if (j < 0) {
                    j = nums.length + j;
                }
                if (i == j && !skip) {
                    found = true;
                }
            }
            if (found) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Problem457 prob = new Problem457();
        System.out.println(prob.circularArrayLoop(new int[]{2, -1, 1, 2, 2})); // true
        System.out.println(prob.circularArrayLoop(new int[]{2, -1, 1, 1, 2})); // false
        System.out.println(prob.circularArrayLoop(new int[]{-1, 2})); // false
        System.out.println(prob.circularArrayLoop(new int[]{-1, 1})); // false
        System.out.println(prob.circularArrayLoop(new int[]{-1, -2})); // false
        System.out.println(prob.circularArrayLoop(new int[]{-1, -1, -2})); // true
        System.out.println(prob.circularArrayLoop(new int[]{1, -1})); // false
        System.out.println(prob.circularArrayLoop(new int[]{3, 1, 2})); // true
        System.out.println(prob.circularArrayLoop(new int[]{2, 1, -1, -2, -2})); // false
    }
}

@@ -155,62 +155,8 @@
 ) %7B%0A
-        boolean%5B%5D visited = new boolean%5Bnums.length%5D;%0A
     
@@ -215,26 +215,28 @@
     if (
-visited%5Bi%5D
+nums%5Bi%5D == 0
 ) %7B%0A    
@@ -287,25 +287,51 @@
     
-visited
+int val1 = nums%5Bi%5D;%0A            nums
 %5Bi%5D = 
-true
+0
 ;%0A  
@@ -352,23 +352,20 @@
 j = i + 
-nums%5Bi%5D
+val1
 ;%0A      
@@ -456,32 +456,64 @@
 ;%0A            %7D%0A
+            int val2 = nums%5Bj%5D;%0A
             bool
@@ -588,19 +588,20 @@
 le (
-!visited%5Bj%5D
+nums%5Bj%5D != 0
 ) %7B%0A
@@ -624,23 +624,20 @@
 if (
-nums%5Bi%5D
+val1
  %3C 0 != 
 nums
@@ -632,23 +632,20 @@
  %3C 0 != 
-nums%5Bj%5D
+val2
  %3C 0) %7B%0A
@@ -715,25 +715,19 @@
     
-visited
+nums
 %5Bj%5D = 
-true
+0
 ;%0A  
@@ -745,23 +745,20 @@
    j += 
-nums%5Bj%5D
+val2
 ;%0A      
@@ -956,32 +956,64 @@
               %7D%0A
+                val2 = nums%5Bj%5D;%0A
             %7D%0A  
@@ -1119,848 +1119,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem457 prob = new Problem457();%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B2, -1, 1, 2, 2%7D)); // true%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B2, -1, 1, 1, 2%7D)); // false%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B-1, 2%7D)); // false%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B-1, 1%7D)); // false%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B-1, -2%7D)); // false%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B-1, -1, -2%7D)); // true%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B1, -1%7D)); // false%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B3, 1, 2%7D)); // true%0A        System.out.println(prob.circularArrayLoop(new int%5B%5D%7B2, 1, -1, -2, -2%7D)); // false%0A    %7D
 %0A%7D%0A

Solve problem 457