forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestComponentSizebyCommonFactor.java
91 lines (65 loc) · 1.89 KB
/
LargestComponentSizebyCommonFactor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class LargestComponentSizebyCommonFactor {
public int largestComponentSize(int[] A) {
// Highest rank parent, size of its subset
Map<Integer,Integer> map = new HashMap<>();
int maxEl = A[0];
for(int i=1; i<A.length; i++){
maxEl = Math.max(A[i], maxEl);
}
UnionFind unionFind = new UnionFind(maxEl);
// 20 <=4
// 2, 4 (5, 10)
for(int el:A) {
for(int num=2;num<=Math.sqrt(el);num++) {
if(el%num==0) {
unionFind.union(el,num);
unionFind.union(el,el/num);
}
}
}
int ans = 0;
for(int el: A) {
// find the topmost parent
int topParent = unionFind.find(el);
map.put(topParent, map.getOrDefault(topParent, 0) + 1);
ans = Math.max( ans, map.get(topParent) );
}
return ans;
}
}
class UnionFind {
int parent[];
int rank[];
UnionFind(int max) {
// starting from 0 to Max initialize 2 arrays
this.parent = new int[max+1];
this.rank = new int[max+1];
// treat all the nodes as independent of each other
for(int i=0;i<=max;i++) {
parent[i] = i;
}
}
public void union(int a,int b) {
int pA = find(a);
int pB = find(b);
if(pA == pB) {
return;
}
if(rank[pA] < rank[pB]) {
parent[pA] = pB;
} else if(rank[pA] > rank[pB]) {
parent[pB] = pA;
} else {
// since rank of both the elements is equal (can make one A parent of B or vice versa)
parent[pA] = pB;
rank[pB]++;
}
}
public int find(int a) {
while(a != parent[a]) {
parent[a]= parent[parent[a]];
a = parent[a];
}
return a;
}
}