Java using union find O(N * sqrt(M))
\nclass Solution {\n    public int largestComponentSize(int[] A) {\n        int max = 0;\n        for(int a : A) {\n            max = Math.max(max, a);\n       
```\nclass Solution {\n    public int largestComponentSize(int[] A) {\n        int max = 0;\n        for(int a : A) {\n            max = Math.max(max, a);\n        }\n        \n        int[] roots = new int[max + 1],sizes = new int[max + 1];\n        for(int idx = 1; idx <= max; idx++){\n            roots[idx] = idx;\n        }\n        \n        for(int a : A){\n            if(a == 1){\n                sizes[a] = 1;\n                continue;\n            }\n            \n            int sqrt = (int) Math.sqrt(a);\n            int thisRoot = getRoot(roots, a);\n            sizes[thisRoot]++;\n            \n            for(int factor = 1; factor <= sqrt; factor++){\n                if(a % factor == 0){\n                    int otherFactor = a / factor;\n                    int factorRoot = getRoot(roots, factor);\n                    int otherFactorRoot = getRoot(roots, otherFactor);\n                    \n                    if(factor != 1){\n                        union(roots, thisRoot, factor, sizes);\n                    }\n                    union(roots, thisRoot, otherFactorRoot, sizes);\n                }\n            }\n        }\n        \n        int maxConnection = 0;\n        for(int size : sizes){\n            maxConnection = Math.max(maxConnection, size);\n        }\n        \n        return maxConnection;\n    }\n    \n    public void union(int[] roots, int a, int b, int[] sizes){\n        int rootA = getRoot(roots, a);\n        int rootB = getRoot(roots, b);\n        if(rootA != rootB){    \n            sizes[rootA] += sizes[rootB];\n            roots[rootB] = rootA;\n        }\n    }\n    \n    public int getRoot(int[] roots, int a){\n        if(roots[a] == a){\n            return a;\n        }\n        return roots[a] = getRoot(roots, roots[a]);\n    }\n}\n```
['Union Find', 'Java']