Easy to Understand - Queue + BFS - Python/Java/C++
IntuitionApproachComplexity
Time complexity:

Space complexity:
Note:While the Python and Java code execute successfully, the C++ code results in a TLE. Yet, wh
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
## Note:
While the Python and Java code execute successfully, the C++ code results in a TLE. Yet, when I run the C++ code specifically against the 667th test case, it succeeds?
# Code
```java []
class Solution {
    public int numberOfComponents(int[][] properties, int k) {
        int n = properties.length;
        List<List<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < n; i += 1) {
            adj.add(new ArrayList<>());
        }
        
        for (int i = 0; i < n; i += 1) {
            for (int j = i + 1; j < n; j += 1) {
                if (intersect(properties[i], properties[j]) >= k) {
                    adj.get(i).add(j);
                    adj.get(j).add(i);
                }
            }
        }
        
        boolean[] visited = new boolean[n];
        int count = 0;
        
        for (int i = 0; i < n; i += 1) {
            if (!visited[i]) {
                count += 1;
                Queue<Integer> queue = new LinkedList<>();
                queue.offer(i);
                visited[i] = true;
                
                while (!queue.isEmpty()) {
                    int current = queue.poll();
                    for (int neighbor : adj.get(current)) {
                        if (!visited[neighbor]) {
                            visited[neighbor] = true;
                            queue.offer(neighbor);
                        }
                    }
                }
            }
        }
        
        return count;
    }
    
    private int intersect(int[] a, int[] b) {
        Set<Integer> setA = new HashSet<>();
        for (int num : a) {
            setA.add(num);
        }
        Set<Integer> setB = new HashSet<>();
        for (int num : b) {
            setB.add(num);
        }
        int count = 0;
        for (int num : setB) {
            if (setA.contains(num)) {
                count += 1;
            }
        }
        return count;
    }
}

```
```cpp []
class Solution {
public:
    int intersect(const vector<int>& a, const vector<int>& b) {
        unordered_set<int> setA(a.begin(), a.end());
        unordered_set<int> setB(b.begin(), b.end());
        int count = 0;
        for (int num : setB) {
            if (setA.find(num) != setA.end()) {
                count += 1;
            }
        }
        return count;
    }
    
    int numberOfComponents(vector<vector<int>>& properties, int k) {
        int n = properties.size();
        vector<vector<int>> adj(n);
        
        for (int u = 0; u < n; u += 1) {
            for (int v = u + 1; v < n; v += 1) {
                if (intersect(properties[u], properties[v]) >= k) {
                    adj[u].push_back(v);
                    adj[v].push_back(u);
                }
            }
        }
        
        vector<bool> visited(n, false);
        int count = 0;
        
        for (int i = 0; i < n; i += 1) {
            if (!visited[i]) {
                count += 1;
                queue<int> q;
                q.push(i);
                visited[i] = true;
                
                while (!q.empty()) {
                    int current = q.front();
                    q.pop();
                    for (int neighbor : adj[current]) {
                        if (!visited[neighbor]) {
                            visited[neighbor] = true;
                            q.push(neighbor);
                        }
                    }
                }
            }
        }
        
        return count;
    }
};
```
```python []
class Solution:
    def intersect(self, a: List[int], b: List[int]) -> int:
        return len(set(a) & set(b))
    
    def numberOfComponents(self, properties: List[List[int]], k: int) -> int:
        n = len(properties)
        adj = [[] for _ in range(n)]
        for u in range(n):
            for v in range(u + 1, n):
                if self.intersect(properties[u], properties[v]) >= k:
                    adj[u].append(v)
                    adj[v].append(u)
        
        visited = [False] * n
        count = 0
        
        for i in range(n):
            if not visited[i]:
                count += 1
                queue = deque([i])
                visited[i] = True
                while queue:
                    current = queue.popleft()
                    for neighbor in adj[current]:
                        if not visited[neighbor]:
                            visited[neighbor] = True
                            queue.append(neighbor)
        
        return count
```

['Breadth-First Search', 'Queue', 'C++', 'Java', 'Python3']