o(n^2 * m ) - DSU but only union under specific conditions
IntuitionDSU is great for solving connected component questions but we need to modify this one. we only want to union 2 nodes if and only if there are k similar
# Intuition
DSU is great for solving connected component questions but we need to modify this one. we only want to union 2 nodes if and only if there are k similar numbers between them. so we have to make sure we have that union logic in there otherwise we will union every node. 
# Approach
DSU but modified to union only if there are k similarites between 2 unioned nodes.
# Complexity
- Time complexity:
o(n^2 * m)
- Space complexity:
o(n + m) - > o(n)
# Code
```java []
class DSU {
HashMap<Integer,Integer> rank;
HashMap<Integer,Integer>  parent;

    public DSU (int n ){
        rank = new HashMap<>();
        parent = new HashMap<>();
        for(int i = 0; i < n; i++){
            rank.put(i, 1);
            parent.put(i, i);
        }
    }

    public int find(int n) {
    if (parent.get(n) != n) {
        parent.put(n, find(parent.get(n))); // Path compression
    }
    return parent.get(n);
}

    public void union(int a, int b){
        int node1 = find(a);
        int node2 = find(b);

        
        if(rank.get(node1) > rank.get(node2)){
            parent.put(node2, node1);
            rank.put(node1, rank.get(node1)+1);
        }
        else if(rank.get(node1) < rank.get(node2)){
            parent.put(node1, node2);
            rank.put(node2, rank.get(node2)+1);
        }
        else{
            parent.put(node2, node1);
            rank.put(node1, rank.get(node1)+1);
        }
    }
}


class Solution {
    public int numberOfComponents(int[][] properties, int k) {
       
        DSU dsu = new DSU(properties.length);
        for(int i = 0; i < properties.length; i++){
            Set<Integer> set1 = new HashSet<>();
            for(int x = 0 ; x < properties[i].length; x++){
                set1.add(properties[i][x]);
            }

            for(int j = i + 1; j < properties.length; j++){
                  Set<Integer> set2 = new HashSet<>();
        
                for(int x = 0 ; x < properties[i].length; x++){
                    set2.add(properties[j][x]);
                }
                set2.retainAll(set1);
                if(set2.size() >=k)
                dsu.union(i, j);
            }
          
            
            
        }

        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < properties.length; i++) {
    set.add(dsu.find(i));
}


        return set.size();
    }
}
```
['Java']