Java Easy DFS
\nclass Solution\n{\n    public int arrayNesting(int[] arr)\n    {\n        this.dp = new int[arr.length];\n        this.arr = arr;\n        int ans = 0;\n     
```\nclass Solution\n{\n    public int arrayNesting(int[] arr)\n    {\n        this.dp = new int[arr.length];\n        this.arr = arr;\n        int ans = 0;\n        for(int i = 0; i < arr.length; i++)\n            if(dp[i] == 0)\n            {\n                this.vis = new HashSet<>();\n                ans = Math.max(ans, dfs(i));\n            }\n        return ans;\n    }\n    private int dfs(int i)\n    {\n        if(vis.contains(i))\n            return 0;\n        vis.add(i);\n        if(dp[i] != 0)\n            return dp[i];\n        dp[i] = 1 + dfs(arr[i]);\n        return dp[i];\n    }\n    private int[] arr, dp;\n    private Set<Integer> vis;\n}\n```
[]