C++, time O(n), space O(1)
\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& nums) {\n        int ans = 0;\n        for(int i=0;i<nums.size();i++){\n            if(nums[i]!=-
```\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& nums) {\n        int ans = 0;\n        for(int i=0;i<nums.size();i++){\n            if(nums[i]!=-1){\n                int start = nums[i];\n                int cnt = 0;\n                while(nums[start]!=-1){\n                    int temp=start;\n                    start=nums[start];\n                    cnt++;\n                    nums[temp]=-1;\n                }\n                ans=max(ans,cnt);\n            }\n        }\n        return ans;\n    }\n};\n```
[]