Java Optimized Solution || Disjoint Set Union
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    int count =0;\n    public int largestComponentSize(int[] nums) {\n\n        int n=nums.length;\n        DisjointSet ds=new DisjointSet(n);\n        Map<Integer, Integer> map = new HashMap<>();\n\n        for (int i = 0; i < n; i++) {\n            int num = nums[i];\n            for (int factor = 2; factor * factor <= num; factor++) {\n                if (num % factor == 0) {\n                    if (!map.containsKey(factor))\n                        map.put(factor, i);\n                    else\n                        ds.unionBySize(i, map.get(factor));\n\n                    if (!map.containsKey(num / factor))\n                        map.put(num / factor, i);\n                    else \n                        ds.unionBySize(i, map.get(num / factor));\n                }\n            }\n\n            if (!map.containsKey(num))\n                map.put(num, i);\n            else\n                ds.unionBySize(i, map.get(num));\n        }\n\n        int max = 1;\n        int component[] = new int[n];\n        for (int i = 0; i < n; i++) {\n            int root = ds.findUParent(i);\n            component[root]++;\n            max = Math.max(max, component[root]);\n        }\n\n        return max;\n    }\n}\nclass DisjointSet{\n    List<Integer> rank=new ArrayList<>();\n    List<Integer> parent=new ArrayList<>();\n    List<Integer> size=new ArrayList<>();\n    \n    public DisjointSet(int n){\n        for(int i=0;i<=n;i++){\n            rank.add(0);\n            parent.add(i);\n            size.add(1);\n        }\n    }\n    \n    public int findUParent(int node){\n        if(node == parent.get(node))\n            return node;\n        int ultPar=findUParent(parent.get(node));\n        //Path Compression\n        parent.set(node, ultPar);\n        return parent.get(node);\n    }\n\n    public void unionBySize(int u, int v){\n        int ultPar_u=findUParent(u);\n        int ultPar_v=findUParent(v);\n        if(ultPar_u == ultPar_v)\n            return;\n        if(size.get(ultPar_u) < size.get(ultPar_v)){\n            parent.set(ultPar_u, ultPar_v);\n            size.set(ultPar_v,size.get(ultPar_v) + size.get(ultPar_u));\n        }\n        else{\n            parent.set(ultPar_v , ultPar_u);\n            size.set(ultPar_u,size.get(ultPar_v) + size.get(ultPar_u));\n        }\n    }\n}\n\n```
['Union Find', 'Graph', 'Java']