Simple C++ Solution w/ comments (time O(N) space O(1) )
\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& nums)  {\n        int maxlen = 0;\n\t\t\t\t\n\t\t// try each index\n        for (int i = 0; i < n
```\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& nums)  {\n        int maxlen = 0;\n\t\t\t\t\n\t\t// try each index\n        for (int i = 0; i < nums.size(); i++) {\n\t\t\t// Only explore if it is not INT_MAX (Hasn\'t been explored)\n            if (nums[i] != INT_MAX) {\n                int num=i;\n                int len=0;\n\t\t\t\t// start exploring... while "current number" is NOT INT_MAX\n                while(nums[num] != INT_MAX){\n\t\t\t\t\t// set next_index, set current number to INT_MAX (visited)\n                    int next_index = nums[num];\n                    nums[num] = INT_MAX;\n\t\t\t\t\t// add 1 to len\n                    len++;\n\t\t\t\t\t// update index to next_index\n                    num = next_index;                    \n                }\n\t\t\t\t// end exploration, update max length\n                maxlen = max(maxlen, len);\n            }\n   \n        }\n        return maxlen;\n    }\n};\n```
[]