package leetcode;

/**
 * https://leetcode.com/problems/array-nesting/
 */
public class Problem565 {
    public int arrayNesting(int[] nums) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem565 prob = new Problem565();
        System.out.println(prob.arrayNesting(new int[]{5, 4, 0, 3, 1, 6, 2})); // 4
    }
}

@@ -12,16 +12,41 @@
 tcode;%0A%0A
+import java.util.Stack;%0A%0A
 /**%0A * h
@@ -173,213 +173,593 @@
     
-// TODO%0A        return 0
+int max = 0;%0A        boolean%5B%5D processed = new boolean%5Bnums.length%5D
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem565 prob = new Problem565();%0A        System.out.println(prob.arrayNesting(new int%5B%5D%7B5, 4, 0, 3, 1, 6, 2%7D)); // 4
+for (int i = 0; i %3C nums.length; i++) %7B%0A            if (processed%5Bi%5D) %7B%0A                continue;%0A            %7D%0A            Stack%3CInteger%3E stack = new Stack%3C%3E();%0A            int idx = i;%0A            stack.push(nums%5Bidx%5D);%0A            int count = 0;%0A            while (!processed%5Bidx%5D) %7B%0A                count++;%0A                processed%5Bidx%5D = true;%0A                idx = stack.pop();%0A                stack.push(nums%5Bidx%5D);%0A            %7D%0A            max = Math.max(max, count);%0A        %7D%0A        return max;
 %0A   

Solve problem 565