Java Solution, Union Find
This is a typical Union Find problem.\n\n\npublic class Solution {\n    class UnionFind {\n        private int count = 0;\n        private int[] parent, rank;\n
This is a typical Union Find problem.\n\n```\npublic class Solution {\n    class UnionFind {\n        private int count = 0;\n        private int[] parent, rank;\n        \n        public UnionFind(int n) {\n            count = n;\n            parent = new int[n];\n            rank = new int[n];\n            for (int i = 0; i < n; i++) {\n                parent[i] = i;\n            }\n        }\n        \n        public int find(int p) {\n            int q = parent[p];\n            while (q != parent[q]) {\n                q = parent[q];\n            }\n            parent[p] = q;\n            return q;\n        }\n        \n        public void union(int p, int q) {\n            int rootP = find(p);\n            int rootQ = find(q);\n            if (rootP == rootQ) return;\n            if (rank[rootQ] > rank[rootP]) {\n                parent[rootP] = rootQ;\n            }\n            else {\n                parent[rootQ] = rootP;\n                if (rank[rootP] == rank[rootQ]) {\n                    rank[rootP]++;\n                }\n            }\n            count--;\n        }\n        \n        public int count() {\n            return count;\n        }\n        \n        public int getMaxUnion() {\n            Map<Integer, Integer> map = new HashMap<>();\n            int max = 1;\n            for (int i = 0; i < parent.length; i++) {\n                int p = find(i);\n                map.put(p, map.getOrDefault(p, 0) + 1);\n                max = Math.max(max, map.get(p));\n            }\n            return max;\n        }\n    }\n    \n    public int arrayNesting(int[] nums) {\n        int n = nums.length;\n        UnionFind uf = new UnionFind(n);\n        for (int i = 0; i < n; i++) {\n            uf.union(i, nums[i]);\n        }\n        return uf.getMaxUnion();\n    }\n}\n```
[]