-
Notifications
You must be signed in to change notification settings - Fork 819
/
LargestComponentSizebyCommonFactor.java
160 lines (152 loc) · 3.91 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package math;
import java.util.*;
import java.util.stream.Collectors;
/**
* Created by gouthamvidyapradhan on 20/08/2019 Given a non-empty array of unique positive integers
* A, consider the following graph:
*
* <p>There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and
* A[j] if and only if A[i] and A[j] share a common factor greater than 1. Return the size of the
* largest connected component in the graph.
*
* <p>Example 1:
*
* <p>Input: [4,6,15,35] Output: 4
*
* <p>Example 2:
*
* <p>Input: [20,50,9,63] Output: 2
*
* <p>Example 3:
*
* <p>Input: [2,3,6,7,4,12,21,39] Output: 8
*
* <p>Note:
*
* <p>1 <= A.length <= 20000 1 <= A[i] <= 100000
*
* <p>Solution: O(primes upto max[A[i]] x N) Find all the primes upto maximum of A[i] and build
* components (using union-find) by finding all the numbers in A[] which are divisible by each prime
* number - keep track of size of each component and return the size of the largest component.
*/
public class LargestComponentSizebyCommonFactor {
private static class UnionFind {
private int[] p;
private int[] rank;
private int[] size;
UnionFind(int s) {
this.p = new int[s];
this.rank = new int[s];
this.size = new int[s];
init();
}
/** Initialize with its same index as its parent */
private void init() {
for (int i = 0; i < p.length; i++) {
p[i] = i;
size[i] = 1;
}
}
/**
* Find the representative vertex
*
* @param i
* @return
*/
private int findSet(int i) {
if (p[i] != i) {
p[i] = findSet(p[i]);
}
return p[i];
}
/**
* Perform union of two vertex
*
* @param i
* @param j
* @return true if union is performed successfully, false otherwise
*/
public boolean union(int i, int j) {
int x = findSet(i);
int y = findSet(j);
if (x != y) {
if (rank[x] > rank[y]) {
p[y] = p[x];
size[x] = size[x] + size[y];
} else {
p[x] = p[y];
size[y] = size[x] + size[y];
if (rank[x] == rank[y]) {
rank[y]++; // increment the rank
}
}
return true;
}
return false;
}
/**
* is attached to roof
*
* @param i
* @return
*/
public int size(int i) {
return size[findSet(i)];
}
}
public static void main(String[] args) {
int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(new LargestComponentSizebyCommonFactor().largestComponentSize(A));
}
public int largestComponentSize(int[] A) {
int max = 0;
for (int a : A) {
max = Math.max(max, a);
}
UnionFind unionFind = new UnionFind(max + 1);
List<Integer> primeNums = primes(max, A);
int answer = 1;
for (int p : primeNums) {
int curr = -1;
for (int a : A) {
if ((a % p) == 0) {
if (curr != -1) {
unionFind.union(curr, a);
} else curr = a;
}
}
answer = Math.max(answer, unionFind.size(curr));
}
return answer;
}
private List<Integer> primes(int N, int[] A) {
boolean[] P = new boolean[N + 1];
int[] pF = new int[N + 1];
int sqRt = (int) Math.sqrt(N);
for (int i = 2; i <= sqRt; i++) {
if (!P[i]) {
for (int j = 2; ; j++) {
if (i * j > N) break;
P[i * j] = true;
if (pF[i * j] == 0) {
pF[i * j] = i;
}
}
}
}
Map<Integer, Integer> result = new HashMap<>();
for (int a : A) {
if (a == 1) continue;
int n = pF[a];
while (n != 0) {
result.putIfAbsent(n, 0);
result.put(n, result.get(n) + 1);
a /= n;
n = pF[a];
}
result.putIfAbsent(a, 0);
result.put(a, result.get(a) + 1);
}
return result.keySet().stream().filter(x -> result.get(x) > 1).collect(Collectors.toList());
}
}