Easy java solution naive to optimized o(n) soln
```\n\n//naive approach\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int max = 0;\n        \n        for(int i : nums) {\n            m
```\n\n//naive approach\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int max = 0;\n        \n        for(int i : nums) {\n            max = Math.max(max, find(nums,i));\n        }\n        \n        return max;\n    }\n    \n    \n    public int find(int[] nums, int i) {\n        int count = 0;\n        boolean[] flag = new boolean[nums.length];\n        \n        while (!flag[i]) {\n            count++;\n            flag[i] = true;\n            i = nums[i];\n        }\n        \n        return count;\n    }\n}\n\n//optimized approach finding cycle length as elements are unique so only one entry and exit\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int count = 0;\n        int max = 0;\n        \n        boolean[] flag = new boolean[nums.length];\n        \n        for (int i : nums) {\n            count = 0;\n            \n            while (!flag[i]) {\n                flag[i] =true;\n                i = nums[i];\n                count++;\n            }\n            \n            max = Math.max(max, count);\n            \n            if (max > nums.length/2) return max;\n            \n        }\n        \n        return max;\n    }\n\n}
['Array', 'Java']