✅ Do as they say ✅
IntuitionTo solve this problem, I need to build a graph where nodes are connected based on the number of common elements they share. Then I need to count the co
# Intuition
To solve this problem, I need to build a graph where nodes are connected based on the number of common elements they share. Then I need to count the connected components in this graph.

# Approach
1. Build an adjacency list representation of the graph:
   - For each pair of arrays, calculate the number of distinct common elements using a set intersection
   - If the intersection count is at least k, add an edge between the corresponding nodes
2. Count connected components using BFS:
   - Use a boolean array to keep track of visited nodes
   - For each unvisited node, perform BFS and mark all reachable nodes as visited
   - Increment the component count each time BFS is performed

# Complexity
- Time complexity: O(n² × m), where n is the number of arrays and m is the maximum length of any array. We compare each pair of arrays (O(n²)) and calculating the intersection takes O(m) time.
- Space complexity: O(n² + nm) for the adjacency list (O(n²) in worst case) and set operations (O(nm)).

# 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++) {
            adj.add(new ArrayList<>());
        }

        // Build the graph
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                int intersectionCount = intersect(properties[i], properties[j]);
                if(intersectionCount >= k) {
                    adj.get(i).add(j);
                    adj.get(j).add(i);
                }
            }
        }

        // Count connected components
        int componentCount = 0;
        boolean[] vis = new boolean[n];
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                bfs(i, adj, vis);
                componentCount++;
            }
        }
        return componentCount;
    }

    // BFS to traverse all nodes in a connected component
    public void bfs(int node, List<List<Integer>> adj, boolean[] vis) {
        Queue<Integer> q = new LinkedList<>();
        q.add(node);
        vis[node] = true;

        while(!q.isEmpty()) {
            int curNode = q.remove();
            for(int neighbour : adj.get(curNode)) {
                if(!vis[neighbour]) {
                    q.add(neighbour);
                    vis[neighbour] = true;
                }
            }
        }
    }

    // Count distinct common elements between two arrays
    public int intersect(int[] a, int[] b) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for(int num : a) set1.add(num);
        for(int num : b) set2.add(num);
        set1.retainAll(set2); 
        return set1.size();
    }
}
```
![fa14ea89-3c60-41c2-94ef-92e1e1c20d13_1732668720.141056.webp](https://assets.leetcode.com/users/images/71e52715-b723-4d94-8fa7-6472059c3133_1742711169.4951062.webp)

['Hash Table', 'Breadth-First Search', 'Graph', 'Java']