Faster || Easy To Understand || C++ Code
Using Union Find\n\n Time Complexity : O(N * sqrt(N))\n\n Space Complexity : O(N)\n\n\nclass Solution {\npublic:\n    \n    vector<int> parent;\n    \n    vecto
* ***Using Union Find***\n\n* ***Time Complexity : O(N * sqrt(N))***\n\n* ***Space Complexity : O(N)***\n\n```\nclass Solution {\npublic:\n    \n    vector<int> parent;\n    \n    vector<int> rank;\n    \n    int find(int x)\n    {\n        if(parent[x] == x)\n        {\n            return x;\n        }\n        \n        return parent[x] = find(parent[x]);\n    }\n    \n    void _union(int x, int y)\n    {\n        int x_par = find(x);\n        \n        int y_par = find(y);\n        \n        if(x_par == y_par)\n        {\n            return;\n        }\n        if(rank[x_par] > rank[y_par])\n        {\n            parent[y_par] = x_par;\n        }\n        else if(rank[x_par] < rank[y_par])\n        {\n            parent[x_par] = y_par;\n        }\n        else\n        {\n            parent[y_par] = x_par;\n            \n            rank[x_par]++;\n        }\n    }\n    \n    int largestComponentSize(vector<int>& nums) {\n        \n        int n = nums.size();\n        \n        int size = *max_element(nums.begin(), nums.end());\n        \n        parent.resize(size + 1);\n        \n        rank.resize(size + 1);\n        \n        for(int i = 0; i <= size; i++)\n        {\n            parent[i] = i;\n            \n            rank[i] = 1;\n        }\n        \n        int maxi = 0;\n        \n        for(int i = 0; i < n; i++)\n        {\n            for(int j = 2; j <= sqrt(nums[i]); j++)\n            {\n                if(nums[i] % j == 0)\n                {\n                    _union(nums[i], j);\n                    \n                    _union(nums[i], nums[i] / j);\n                }\n            }\n        }\n        \n        unordered_map<int, int> mp;\n        \n        for(int i = 0; i < n; i++)\n        {\n            int x_par = find(nums[i]);\n            \n            mp[x_par]++;\n            \n            maxi = max(maxi, mp[x_par]);\n        }\n        \n        return maxi;\n    }\n};\n```
['Union Find', 'C']