Input Format
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains an integer ‘N’ representing the size of the ‘vertices’ array.
The second line of each test case contains the ‘N’ space-separated integers representing the element of the ‘VERTICES’ array.
Output Format:
For each test case, print a single line containing a single integer denoting the size of the largest connected component in the graph.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 1000
1 <= VERTICES[i] <= 5000
All ‘VERTICES[i]’ are distinct.
Where ‘T’ is the number of test cases, ‘N’ is the size of the ‘VERTICES’ array, and ‘VERTICES[i]’ is an element in the ‘VERTICES’ array at index ‘i’.
Time limit: 1 sec.
2
6
8 10 13 15 6 28
9
4 25 7 14 27 23 17 34 3
5
5
Test Case 1 :

Connected Component 1: 8 10 15 6 28, Size = 5.
Connected Component 2: 13, Size = 1.
Therefore, the largest component size = max(5, 1) = 5.
Test Case 2 :

Connected Component 1: 4 7 14 17 34, Size = 5.
Connected Component 2: 25, Size = 1.
Connected Component 3: 27 3, Size = 2.
Connected Component 4: 23, Size = 1.
Therefore,the largest component size = max(5, 1, 2, 1) = 5.
2
4
847 644 54 965
7
1 2 3 4 5 6 6
3
4
For every number, find and link all of its factors to a unique parent number. This ensures that numbers that have a common factor greater than 1 are in the same group. Can you now find the largest connected component?
The idea is to use a disjoint set data structure. We will maintain a disjoint set where different numbers are linked together if and only if they have a common factor greater than 1. So, we have to link each element in ‘VERTICES’ with all its factors greater than 1. For this purpose, we will check all the integers ranging from 2 to sqrt(VERTICES[i]). Since if ‘X’ is a factor of ‘P’ then ‘P / X’ is also a factor of 'P’.
After linking, we only have to find which set has the maximum number of elements from the ‘VERTICES’ array. This can be done by maintaining the frequency of the representative element of every set. We will count the elements from ‘VERTICES’ that have the given representative element and output the maximum such frequency.
Algorithm:
Description of ‘DisjointSet’ class
private:
The private part of the ‘DisjointSet’ class will contain two data members:
public:
The private part of the ‘DisjointSet’ class will contain one constructor and two member functions:
Description of ‘DisjointSet’ constructor
The constructor will take one parameter:
DisjointSet(n):
Description of ‘find’ function
The function will take one parameter:
find(x):
Description of ‘Union’ function
The function will take two parameters:
Union(u, v):
O(N * sqrt(maxElement)), where ‘N’ is the size of ‘vertices’ array and ‘maxElement’ is the maximum element in the ‘vertices’ array.
The time complexity of linking factors of every element to a parent element is O(N * sqrt(maxElement)). Since the loop will make ‘N’ iterations and in every iteration, we are traversing till sqrt(num) which will be sqrt(maxElement) in the worst case.
The time complexity of finding the maximum frequency of representative element is O(N * log(maxElement)). Since the for loop will make ‘N’ iterations and in every iteration, we are finding the representative element of ‘num’ which is O(log(num)) operation that will become log(maxElement) in the worst case.
Hence, overall time complexity is O(N * sqrt(maxElement)) + O(N * log(maxElement)) = O(N * sqrt(maxElement)).
O(maxElement), where ‘maxElement’ is the maximum element in the ‘vertices’ array.
Since O(maxElement) space is required for the DisjointSet ‘ds’ object and also for ‘hashMap’ in the worst case.