Java O(n) Time O(1) Space
\npublic int arrayNesting(int[] nums) {\n    int res = 0;\n    for (int i=0;i<nums.length;i++) {\n        if (nums[i] < 0) continue;\n        int length = 1, va
```\npublic int arrayNesting(int[] nums) {\n    int res = 0;\n    for (int i=0;i<nums.length;i++) {\n        if (nums[i] < 0) continue;\n        int length = 1, val = nums[i];\n        while (Math.abs(val) != i) {\n            length++;\n            val = nums[Math.abs(val)];\n            nums[Math.abs(val)] *= -1;\n        }\n        res = Math.max(res, length);\n    }\n    return res;\n}\n```
[]