Easy cpp and Java✅
IntuitionThe problem involves finding the number of connected components in a graph where nodes represent arrays, and edges exist between nodes if they share at
# Intuition
The problem involves finding the number of connected components in a graph where nodes represent arrays, and edges exist between nodes if they share at least k distinct common integers. The key insight is to model this as a graph problem and use Depth-First Search (DFS) to count the connected components.

# Approach
1. Graph Construction:
* Each array is treated as a node in the graph.
* An edge is added between two nodes if the number of distinct common integers between their corresponding arrays is at least k.

2. Intersection Calculation:
* A helper function intersect calculates the number of distinct common integers between two arrays using sets.

3. DFS Traversal:
* DFS is used to traverse the graph and mark all reachable nodes from a starting node, effectively identifying a connected component.
* The process is repeated for all unvisited nodes to count the total number of connected components.

# Complexity
- Time complexity:
O(n^2⋅m+n+e)

- Space complexity:
O(n⋅m+n+e)

# Code
```java []
import java.util.*;

class Solution {
    // Helper function to calculate the number of distinct common integers between two arrays
    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);
        
        setA.retainAll(setB); // Retain only common elements
        return setA.size();
    }

    // DFS to traverse the graph and mark visited nodes
    private void dfs(int node, Map<Integer, List<Integer>> graph, Set<Integer> visited) {
        visited.add(node);
        for (int neighbor : graph.getOrDefault(node, new ArrayList<>())) {
            if (!visited.contains(neighbor)) {
                dfs(neighbor, graph, visited);
            }
        }
    }

    public int numberOfComponents(int[][] properties, int k) {
        int n = properties.length;
        
        // Build the graph
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (intersect(properties[i], properties[j]) >= k) {
                    // Add edge between i and j
                    graph.computeIfAbsent(i, x -> new ArrayList<>()).add(j);
                    graph.computeIfAbsent(j, x -> new ArrayList<>()).add(i);
                }
            }
        }
        
        // Count connected components using DFS
        Set<Integer> visited = new HashSet<>();
        int components = 0;
        for (int i = 0; i < n; i++) {
            if (!visited.contains(i)) {
                dfs(i, graph, visited);
                components++;
            }
        }
        
        return components;
    }
}
```

```c++ []
class Solution {
public:
    vector<vector<int>> build_graph(const vector<vector<int>>& p, int k) {
        int n = p.size();
        vector<vector<int>> adj_list(n);
        vector<unordered_set<int>> s(n);
        
        // Convert properties to sets for fast lookup
        for (int i = 0; i < n; i++) {
            for (auto &x: p[i])
                s[i].insert(x);
        }

        // Check all pairs of nodes for at least k common properties
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int common = 0;
                for (auto &x: s[j]) {
                    if (s[i].count(x)) common++;
                    if (common >= k) break;
                }
                if (common >= k) {
                    adj_list[i].push_back(j);
                    adj_list[j].push_back(i);
                }
            }
        }
        return adj_list;
    }
    
    void dfs(int node, const vector<vector<int>>& adj_list, vector<bool>& visited) {
        visited[node] = true;
        
        for (int neigh: adj_list[node]) {
            if (!visited[neigh]) {
                dfs(neigh, adj_list, visited);
            }
        }
    }
    
    int numberOfComponents(vector<vector<int>>& properties, int k) {
        vector<vector<int>> adj = build_graph(properties, k);
        int n = properties.size();
        vector<bool> visited(n, false);
       
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                count++;
                dfs(i, adj, visited);
            }
        }
        return count;
    }
};
```
['Ordered Map', 'Ordered Set', 'C++', 'Java']