Largest Component Size by Common Factor in C++

Suppose we have an array A of unique positive integers, now consider the following graph −
There are length of A number of nodes, these are labelled A[0] to A[size of A - 1]; There is an edge between A[i] and A[j] when A[i] and A[j] share a common factor greater than 1. We have to find the size of the largest connected component in the graph.
So, if the input is like [4,6,15,35], then the output will be 4
To solve this, we will follow these steps −
Define an array parent
Define an array parent
Define an array rank
Define an array rank
Define an array rank
Define an array rank
if parent[x] is same as -1, then −return x
if parent[x] is same as -1, then −
return x
return x
return parent[x] = getParent(parent[x])
return parent[x] = getParent(parent[x])
Define a function unionn(), this will take x, y,
Define a function unionn(), this will take x, y,
parX := getParent(x)
parX := getParent(x)
parY := getParent(y)
parY := getParent(y)
if parX is same as parY, then −return
if parX is same as parY, then −
return
return
if rank[parX] >= rank[parY], then −rank[parX] := rank[parX] + rank[parY]parent[parY] := parX
if rank[parX] >= rank[parY], then −
rank[parX] := rank[parX] + rank[parY]
rank[parX] := rank[parX] + rank[parY]
parent[parY] := parX
parent[parY] := parX
Otherwiserank[parY] := rank[parY] + rank[parX]parent[parX] := parY
Otherwise
rank[parY] := rank[parY] + rank[parX]
rank[parY] := rank[parY] + rank[parX]
parent[parX] := parY
parent[parX] := parY
From the main method do the following −
From the main method do the following −
ret := 0, n := size of A
ret := 0, n := size of A
parent := Define an array of size n fill this with -1
parent := Define an array of size n fill this with -1
rank := Define an array of size n fill this with 1
rank := Define an array of size n fill this with 1
Define one map m
Define one map m
for initialize i := 0, when i < n, update (increase i by 1), do −x := A[i]for initialize j := 2, when j * j <= x, update (increase j by 1), do −if x mod j is same as 0, then &minsu;if j is in m, then −unionn(m[j], i)Otherwisem[j] := iif (x / j) is in m, then −unionn(m[x / j], i)Otherwisem[x / j] = iif x is in m, then −unionn(m[x], i)Otherwisem[x] := iret := maximum of ret and rank[getParent(i)]
for initialize i := 0, when i < n, update (increase i by 1), do −
x := A[i]
x := A[i]
for initialize j := 2, when j * j <= x, update (increase j by 1), do −if x mod j is same as 0, then &minsu;if j is in m, then −unionn(m[j], i)Otherwisem[j] := iif (x / j) is in m, then −unionn(m[x / j], i)Otherwisem[x / j] = i
for initialize j := 2, when j * j <= x, update (increase j by 1), do −
if x mod j is same as 0, then &minsu;if j is in m, then −unionn(m[j], i)Otherwisem[j] := iif (x / j) is in m, then −unionn(m[x / j], i)Otherwisem[x / j] = i
if x mod j is same as 0, then &minsu;
if j is in m, then −unionn(m[j], i)
if j is in m, then −
unionn(m[j], i)
unionn(m[j], i)
Otherwisem[j] := i
Otherwise
m[j] := i
m[j] := i
if (x / j) is in m, then −unionn(m[x / j], i)
if (x / j) is in m, then −
unionn(m[x / j], i)
unionn(m[x / j], i)
Otherwisem[x / j] = i
Otherwise
m[x / j] = i
m[x / j] = i
if x is in m, then −unionn(m[x], i)
if x is in m, then −
unionn(m[x], i)
unionn(m[x], i)
Otherwisem[x] := i
Otherwise
m[x] := i
m[x] := i
ret := maximum of ret and rank[getParent(i)]
ret := maximum of ret and rank[getParent(i)]
return ret
return ret
Let us see the following implementation to get better understanding −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   vector<int> parent;
   vector<int> rank;
   int getParent(int x){
      if (parent[x] == -1)
      return x;
      return parent[x] = getParent(parent[x]);
   }
   void unionn(int x, int y){
      int parX = getParent(x);
      int parY = getParent(y);
      if (parX == parY)
      return;
      if (rank[parX] >= rank[parY]) {
         rank[parX] += rank[parY];
         parent[parY] = parX;
      } else {
         rank[parY] += rank[parX];
         parent[parX] = parY;
      }
   }
   int largestComponentSize(vector<int>& A) {
      int ret = 0;
      int n = A.size();
      parent = vector<int>(n, -1);
      rank = vector<int>(n, 1);
      unordered_map<int, int> m;
      for (int i = 0; i < n; i++) {
         int x = A[i];
         for (int j = 2; j * j <= x; j++) {
            if (x % j == 0) {
               if (m.count(j)) {
                  unionn(m[j], i);
               } else {
                  m[j] = i;
               }
               if (m.count(x / j)) {
                  unionn(m[x / j], i);
               } else {
                  m[x / j] = i;
               }
            }
         }
         if (m.count(x)) {
            unionn(m[x], i);
         } else {
            m[x] = i;
         }
         ret = max(ret, rank[getParent(i)]);
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<int> v = {4,6,15,35};
   cout << (ob.largestComponentSize(v));
}
{4,6,15,35}
4