1
// Solving the problem using Disjoint Set Union Find approach5
public int find(int x) {7
if (parent[x] == x) return x;9
// Optimising by placing the same parent for all the elements to reduce reduntant calls10
return parent[x] = find(parent[x]);13
public void union(int a, int b) {18
// Using Rank optimisation19
if (rank[a] > rank[b]) {33
public List<Boolean> areConnected(int n, int threshold, int[][] queries) {35
List<Boolean> ans = new ArrayList<Boolean>();36
parent = new int[n + 1];37
rank = new int[n + 1];39
for (int i = 1; i <= n; i++) {40
// Each element is its own parent42
// At beginning each element has rank 146
// Finding the possible divisors with pairs above given threshold47
for (int th = threshold + 1; th <= n; th++) {50
while (mul * th <= n) {51
// If possible pair then making a union of those paired element57
// Generating ans for all possible queries58
for (int[] query : queries) {59
ans.add((find(query[0]) == find(query[1])));