Simple java Solution using DFS
\n\n# Code\n\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int n=nums.length;\n        boolean visited[]=new boolean[n];\n        int an
\n\n# Code\n```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int n=nums.length;\n        boolean visited[]=new boolean[n];\n        int ans=0;\n        for(int i=0;i<nums.length;i++){\n            if(!visited[i]){\n         ans=Math.max(ans,helper(nums,i,visited));\n        }\n        }\n        return  ans+1;\n    }\n    public int helper(int nums[],int idx, boolean visited[]){\n        if(visited[idx]==true || idx>=nums.length){\n            return -1;\n        }\n        visited[idx]=true;\n    idx=nums[idx];\n    int ans=0;\n    if(!visited[idx]){\n     ans=ans+1+helper(nums,idx,visited);\n    }\n       return ans;\n    }\n}\n```
['Array', 'Depth-First Search', 'Java']