Using Grpah
\nclass Solution {\n  public:\n\n\tint doDfs(vector < vector<int>> &edge, vector<bool>&seen, int src) {\n\t\tseen[src] = true;\n\t\tint res = 0;\n\t\tfor (int d
```\nclass Solution {\n  public:\n\n\tint doDfs(vector < vector<int>> &edge, vector<bool>&seen, int src) {\n\t\tseen[src] = true;\n\t\tint res = 0;\n\t\tfor (int dst : edge[src]) {\n\t\t\tif (seen[dst]) continue;\n\t\t\tres = max(res, doDfs(edge, seen, dst));\n\t\t}\n\t\treturn res + 1;\n\t}\n\n\tint arrayNesting(vector<int>& nums) {\n\t\tint n = nums.size();\n\t\tvector < vector<int>> edge(n, vector<int>());\n\n\t\tfor (int i : nums) {\n\t\t\tedge[nums[i]].push_back(nums[nums[i]]);\n\t\t}\n\t\tvector<bool> seen(n, false);\n\n\t\tint res = 0;\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (seen[nums[i]])continue;\n\t\t\tres = max(res, doDfs(edge, seen, nums[i]));\n\t\t}\n\t\treturn res;\n\t}\n};\n```
[]