[C++] [Java] Clean Code - O(N)
The idea is to, start from every number, find circles in those index-pointer-chains, every time you find a set (a circle) mark every number as visited (-1) so t
The idea is to, start from every number, find ``circle``s in those ``index-pointer-chains``, every time you find a set (a circle) ``mark every number as visited (-1)`` so that next time you won't step on it again.\n**C++**\n```\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& a) {\n        size_t maxsize = 0;\n        for (int i = 0; i < a.size(); i++) {\n            size_t size = 0;\n            for (int k = i; a[k] >= 0; size++) {\n                int ak = a[k];\n                a[k] = -1; // mark a[k] as visited;\n                k = ak;\n            }\n            maxsize = max(maxsize, size);\n        }\n\n        return maxsize;\n    }\n};\n```\n**Java**\n```\npublic class Solution {\n    public int arrayNesting(int[] a) {\n        int maxsize = 0;\n        for (int i = 0; i < a.length; i++) {\n            int size = 0;\n            for (int k = i; a[k] >= 0; size++) {\n                int ak = a[k];\n                a[k] = -1; // mark a[k] as visited;\n                k = ak;\n            }\n            maxsize = Integer.max(maxsize, size);\n        }\n\n        return maxsize;\n    }\n}\n```
[]