Using Cycle Detection approach
```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        \n        int max = 0;\n        boolean[] visited = new boolean[nums.length];\n       
```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        \n        int max = 0;\n        boolean[] visited = new boolean[nums.length];\n        for(int i=0 ; i < nums.length ; i++)\n        {\n            if(visited[i] == false) \n                max = Math.max(max, dfs(i, nums, visited,0));\n        }     \n        return max;\n    }\n    \n    \n    public int dfs(int index, int[] nums, boolean[] visited, int count)\n    {\n        if(visited[index] == true)\n            return count;     \n        visited[index] = true;\n        return dfs(nums[index] , nums, visited, count+1);\n    }\n}\n
['Depth-First Search', 'Recursion']