C++ DFS & DP
\n class Solution {\npublic:\n    int dp[100001];\n    int solve(int k,vector<bool>& vis, vector<int>& nums)\n    {\n        if(vis[k])\n            return 0;\n
```\n class Solution {\npublic:\n    int dp[100001];\n    int solve(int k,vector<bool>& vis, vector<int>& nums)\n    {\n        if(vis[k])\n            return 0;\n        vis[k]=true;\n        if(dp[k]!=-1) return dp[k];\n        return dp[k] = 1 + solve(nums[k],vis,nums);\n    }\n    int arrayNesting(vector<int>& nums) {\n        int n=nums.size();\n        int ans=0;\n        memset(dp,-1,sizeof(dp));\n        for(int i=0;i<n;++i)\n        {\n            if(dp[i]==-1){\n            vector<bool>vis(n,false);\n            ans=max(ans,solve(i,vis,nums));   \n            }\n        }\n        return ans;\n    }\n};\n```
['Depth-First Search', 'C', 'C++']