Union-find using path compression with commented code
\nclass Solution {\n    static class UF {\n        int[] size;\n        int[] parent;\n        int max;\n        \n        public UF(int n) {\n            size 
```\nclass Solution {\n    static class UF {\n        int[] size;\n        int[] parent;\n        int max;\n        \n        public UF(int n) {\n            size = new int[n];\n            parent= new int[n];\n            for(int i=0; i<n; i++) {\n                parent[i] = i;\n                size[i] = 1;\n            }\n        }\n        \n        int find(int x) {\n            int temp = x;\n            while(parent[x] != x) {\n                x = parent[x];\n            }\n            \n            while(temp != x) {\n                int ptemp = parent[temp];\n                parent[temp] = x;\n                temp = ptemp;\n            }\n            return x;\n        }\n        \n        public void union(int x, int y) {\n            int px = find(x);\n            int py = find(y);\n            if(px == py) return;\n            \n            if(size[px] > size[py]) {\n                size[px] += size[py];\n                parent[py] = px;\n                max= Math.max(max, size[px]);\n            } else {\n                size[py] += size[px];\n                parent[px] = py;\n                max= Math.max(max, size[py]);\n            }\n        }\n    }\n    public int largestComponentSize(int[] A) {\n        int n = A.length;\n        UF uf = new UF(n);\n        Map<Integer, Integer> map = new HashMap<>();\n        \n        for(int i=0; i<n; i++) {\n            int num = A[i];\n            // to compute the factorization we need to go upto sqrt(num) times\n            for(int j=2; j*j <=num; ++j) {\n                // if j is one of the factor of num\n                if(num%j == 0) {\n                    if(map.containsKey(j)){\n                        uf.union(i, map.get(j));\n                    } else {\n                        map.put(j, i);\n                    }\n                \n                    if(map.containsKey(num/j)){\n                        uf.union(i, map.get(num/j));\n                    } else {\n                        map.put(num/j, i);\n                    }\n                }\n                \n            }\n            \n            // if factor is not possible\n            // then number itself is an factor\n            if(map.containsKey(num)) {\n                uf.union(i, map.get(num));\n            } else {\n                map.put(num, i);\n            }\n        }\n        \n        return uf.max;\n    }\n}\n```
[]