[C++ & Java] DSU || Simple and easy to understand solution
Intuition
find if the nodes are connected or not. we just need to check each item of each property to be checked in the other item property, if its present or n
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- find if the nodes are connected or not. we just need to check each item of each property to be checked in the other item property, if its present or not. To do this further we can create a map of set and push each item to it. the other way around is sort each property and then iteratively match each item form both items property.
- Once we can evaluate connectedness, then simply initialise the total connected count to total items ( assuming all the items are disconnected at the begining). Then keep on reducing the count as we merge two distict group together.

# Approach
- Insert all item's property to the map of set data structure
- Iterate over all possible combinations of nodes
- check if they are connected or not ( standard DSU - find, add code)
- if connected then, reduce the count if the two nodes are distinct groups before combining to single group.

# Complexity
- Time complexity: O(n.n.p)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n.p)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
Here n is total node count and p is the length of property

# Code
```cpp []
bool isConnected(unordered_map<int, unordered_set<int>>& mp, int k, int i, int j){
    for(auto t: mp[i]) k -= (mp[j].find(t) != mp[j].end())?1:0;
    return (k <= 0);
}

int find(int node, vector<int>& par){
    return par[node] = ((par[node] == node)?node: find(par[node], par));
}

int add(int a, int b, vector<int>& par){
    int pa = find(a, par), pb = find(b, par);
    par[pb] = pa;
    return (pa != pb)?1:0;    // return 1 if bothe are in different group before ese return 0
}

int numberOfComponents(vector<vector<int>>& p, int k) {
    int count = p.size();
    vector<int> par(p.size(), -1);
    unordered_map<int, unordered_set<int>> mp;
    for(int i = 0; i < par.size(); ++i) par[i] = i;
    for(int i = 0; i < p.size(); ++i){           // create the lookup map 
        for(int j = 0; j < p[i].size(); ++j) mp[i].insert(p[i][j]);
    }
    for(int i = 0; i < p.size(); ++i){           
        for(int j = i+1; j < p.size(); ++j){
            if(isConnected(mp, k, i, j)) count -= add(i, j, par);  // check connected or not, if connected then add them and if we are merging two disconnected group then reduce the total count by 1
        }
    }
    return count;
}
```
```Java []
boolean isConnected(Map<Integer, Set<Integer>> map, int k, int i, int j){
    for(Integer t: map.get(i)) k -= (map.get(j).contains(t))?1:0;
    return (k <= 0);
}

int find(int node, int[] par){ System.out.println(node + " " + par[node]);
    return par[node] = ((par[node] == node)?node: find(par[node], par));
}

int add(int a, int b, int[] par){
    int pa = find(a, par), pb = find(b, par);
    par[pb] = pa;
    return (pa != pb)?1:0;    // return 1 if bothe are in different group before ese return 0
}

int numberOfComponents(int[][] p, int k) {
    int count = p.length;
    int[] par = new int[p.length];
    for(int i = 0; i < par.length; ++i) par[i] = i;
    Map<Integer, Set<Integer>> map = new HashMap<>();
    for(int i = 0; i < p.length; ++i){           // create the lookup map 
        for(int j = 0; j < p[i].length; ++j) map.computeIfAbsent(i, key -> new HashSet<>()).add(p[i][j]);
    }
    for(int i = 0; i < p.length; ++i){           
        for(int j = i+1; j < p.length; ++j){System.out.println(i + " " + j);
            if(isConnected(map, k, i, j)) count -= add(i, j, par);  // check connected or not, if connected then add them and if we are merging two disconnected group then reduce the total count by 1
        }
    }
    return count;
}
````



---

<span style="color:green">
<b>Here is an article of my last interview experience - A Journey to FAANG Company, I recomand you to go through this to know which all resources I have used & how I cracked interview at Amazon: </b> </span> 

https://leetcode.com/discuss/interview-experience/3171859/Journey-to-a-FAANG-Company-Amazon-or-SDE2-(L5)-or-Bangalore-or-Oct-2022-Accepted

---
['C++', 'Java']