[Java] O(n) solution O(1) space.
Overall idea here is that we need to form sets of elements from the given array and return the length of largest set. All the elements in a particular set will 
Overall idea here is that we need to form sets of elements from the given array and return the length of largest set. All the elements in a particular set will be reachable to each other in the original array.\nTaking an example to understand this : \n`index `   0 1 2 3 4 \n `nums`    2 3 4 1 0   \n\nWe start traversal from index = 0, include element 2 (nums[0]) in the set. Then we go to index 2 and include element 4 (nums[2]) in the set. Then we move to index 4 and include the element 0 (nums[4]) in the set. Now when we go to index 0, we can see that the element there is already been seen previously and hence the set formed is (2,4,0) with size 3. Likewise there will be another set formed when we start the traversal from 1 which will be (1,3) with size 2. Largest set size here would be 3 which would be our answer.  \n\nFew points to be highlighted here: \n1. We donot actually need to form sets, so we can keep a counter and increment it till we see an element which is seen previously. Maintain a maxCounter that would hold the length of largest set seen till now.\n2. We can make use of an explicit visited array to mark an element being visited. visited[i] = 1 when nums[i] is being processed. However inorder to save space, we can make use of the original array to denote an element being processed. Since the array elements will never be negative (as all elements are index values which cannot go negative), have used -1 to denote that an element has being processed. We can also use nums.length + 1 for this as this value will never be in the array.\n3. For all the elements that belong to same set, we can start from either of the elements in that set and all the elements in that set would be reachable. Taking few examples would clarify this. Putting some for reference : eg1:  nums[] = [5, 2, 3, 1, 0, 4], sets here would be (5, 4, 0) and (2, 3, 1), here we start traversal from 0 or 4 or 5 we can visit all elements since they are linked.       eg2:  nums[] =  [6, 0, 4, 2, 3, 1, 5] sets here would be (6, 5, 1, 0) and (4, 2, 3). Also we can have  values such that all belong to one set, nums[] = [6, 0, 1, 2, 3, 4, 5] set (6, 5, 4, 3, 2, 1, 0).\n\n```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int index = -1;\n        int value = 0;\n        int maxLen = 0;\n        int len = 0;\n        for (int i = 0; i < nums.length; i++) {\n            if (nums[i] != -1) {\n                index = i;\n                len = 0;\n                while (nums[index] != -1) {\n                    value = nums[index]; //get the value in the set \n                    nums[index] = -1; // mark the value as visited \n                    index = value; //next index to jump\n                    len++;\n                }\n                maxLen = Math.max(len, maxLen);\n            }\n        }\n        return maxLen;\n    }\n}\n```
['Array', 'Depth-First Search', 'Java']