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 rank

  • Define an array rank

  • if parent[x] is same as -1, then −

    • return x

  • return parent[x] = getParent(parent[x])

  • Define a function unionn(), this will take x, y,

  • parX := getParent(x)

  • parY := getParent(y)

  • if parX is same as parY, then −

    • return

  • if rank[parX] >= rank[parY], then −

    • rank[parX] := rank[parX] + rank[parY]

    • parent[parY] := parX

  • Otherwise

    • rank[parY] := rank[parY] + rank[parX]

    • parent[parX] := parY

  • From the main method do the following −

  • ret := 0, n := size of A

  • parent := 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

  • 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)

        • Otherwise

          • m[j] := i

        • if (x / j) is in m, then −

          • unionn(m[x / j], i)

        • Otherwise

          • m[x / j] = i

    • if x is in m, then −

      • unionn(m[x], i)

    • Otherwise

      • m[x] := i

    • ret := maximum of ret and rank[getParent(i)]

  • return ret

Let us see the following implementation to get better understanding −

Example

 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));
}

Input

{4,6,15,35}

Output

4

Updated on: 04-Jun-2020

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements