C++ Simple and Easy Explained Solution, O(n) TC, O(1) SC
Idea:\nWe try to start from each index and build the set as in the problem statement.\nTo check for duplicates, we will change each visited number to -1.\nTime 
**Idea:**\nWe try to start from each index and build the set as in the problem statement.\nTo check for duplicates, we will change each visited number to -1.\n**Time Complexity:** O(n) - we visit each element once.\n**Space Complexity:** O(1) - we use only constant extra space.\n```\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& nums) {\n        int res = 0, curr, count, prev;\n        \n        for (int i = 0; i < nums.size(); i++) {\n            if (nums[i] == -1) continue;\n            \n            curr = nums[i], count = 1, nums[i] = -1;\n            \n            while (nums[curr] != -1) {\n                prev = curr;\n                curr = nums[curr];\n                nums[prev] = -1;\n                count++;\n            }\n            \n            res = max(res, count);\n        }\n        \n        return res;\n    }\n};\n```\n**Like it? please upvote!**
['C']