C++ Solution DFS + Memo
The Idea is that, an index can only lead to one path, so we can use an array to store the value.\n\nint arrayNesting(vector<int>& nums) {\n       int n = nums.s
The Idea is that, an index can only lead to one path, so we can use an array to store the value.\n```\nint arrayNesting(vector<int>& nums) {\n       int n = nums.size(),ans = 0;\n        vector<bool>visited(n,false);\n        vector<int>dp(n,-1);\n        for(int i=0;i<n;i++)\n            ans = max(ans,dfs(nums,i,visited,dp));\n        return ans;\n    }\n    int dfs(vector<int>&nums,int n, vector<bool>& visited, vector<int>&dp)\n    {\n        if(visited[n]) return 0;\n        if(dp[n] != -1) return dp[n];\n        visited[n] = true;\n        dp[n] = 1 + dfs(nums,nums[n],visited,dp);\n        visited[n] = false;\n        return dp[n];\n    }\n\n```
['Depth-First Search']