Java Solution || Iterative & Recursive || O(n) time || O(1) space || DFS
Iterative Solution -> 4ms runtime faster than 99.8%\n\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int max=0;\n        for(int i=0;i<nu
## **Iterative Solution -> 4ms runtime faster than 99.8%**\n```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int max=0;\n        for(int i=0;i<nums.length;i++)\n        {\n            int c=0;\n            while(nums[i]!=-1)\n            {\n                int temp = nums[i];\n                nums[i]=-1;\n                i = temp;\n                c++;\n            }\n            max = Math.max(c,max);\n        }\n        return max;\n    }\n}\n```\n\n## **Recursive Solution**\n```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int max=0;\n        for(int i=0;i<nums.length;i++)\n            max = Math.max(dfs(nums,i,0),max);\n        return max;\n    }\n    \n    private int dfs(int[] nums,int i,int c)\n    {\n        if(nums[i]==-1)\n            return c;\n        int temp = nums[i];\n        nums[i]=-1;\n        return dfs(nums,temp,c+1);\n    }\n}\n```
['Depth-First Search']