Dynamic programming - java
class Solution {\n   \n    public int arrayNesting(int[] nums) {\n         int[] dp = new int[nums.length];\n        int maxLen = Integer.MIN_VALUE;\n        fo
class Solution {\n   \n    public int arrayNesting(int[] nums) {\n         int[] dp = new int[nums.length];\n        int maxLen = Integer.MIN_VALUE;\n        for(int i=0;i<nums.length;i++){\n            HashSet<Integer> set = new HashSet<>();\n          maxLen = Math.max(maxLen,dp(nums,i,dp,0,set));\n        }\n        return maxLen;\n    }\n    public int dp(int[] nums,int index, int[] dp,int curLen, HashSet<Integer> set){\n        if(!set.contains(nums[index])){\n         \n            set.add(nums[index]);\n            \n        if(dp[index]!=0)\n            return dp[index];\n        \n        curLen = dp(nums,nums[index],dp,curLen+1,set);\n        dp[nums[index]]=curLen;\n        }\n        return curLen;\n    }\n}
[]