Clear | Simple | ✅
Code\n\nclass Solution {\n    Integer[]dp;\n    public int arrayNesting(int[] nums) {\n        dp=new Integer[nums.length];\n        int ans=0;\n        for(int
# Code\n```\nclass Solution {\n    Integer[]dp;\n    public int arrayNesting(int[] nums) {\n        dp=new Integer[nums.length];\n        int ans=0;\n        for(int i=0; i<nums.length; i++){\n            ans=Math.max(ans,dfs(nums,i,new HashSet<>()));\n        }\n        return ans;\n    }\n    public int dfs(int[]nums,int si,HashSet<Integer> set){  \n        if(set.contains(si)) return 0;    \n        if(nums[si]==si) return 1;\n        if(dp[si]!=null) return dp[si];        \n        set.add(si);\n        dp[si] = 1 + dfs(nums,nums[si],set);\n        return dp[si];\n    }\n}\n```\n______________________________________\n\n# **Up Vote if Helps**![image.png](https://assets.leetcode.com/users/images/23d8443e-ac59-49d5-99f0-9273a2147be2_1687635435.0337658.png)\n\n______________________________________
['Dynamic Programming', 'Depth-First Search', 'Graph', 'Recursion', 'Memoization', 'Java']