Beginner-Friendly || JAVA
IntuitionThe problem involves finding connected components based on shared properties between items. We can represent this as a graph problem, where nodes repre
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves finding connected components based on shared properties between items. We can represent this as a graph problem, where nodes represent items, and edges exist between nodes if they share at least k common properties.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Build the Graph:

    - Each item is treated as a node.

    - Use a helper function intersect() to count common elements between two property lists.

    - If two items share at least k common properties, an edge is created between them.

    - We store these edges in an adjacency list representation of the graph.

2. Find Connected Components:

    - Use Depth-First Search (DFS) to traverse the graph and count the number of connected components.

    - Maintain a visited array to track nodes that have been explored.

    - For each unvisited node, start a DFS traversal and mark all reachable nodes as visited.

    - Each DFS traversal represents discovering a new connected component.
# Complexity
- Time complexity: O(n^2*m)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n^2)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {

    public int intersect(int[] a,int[] b){
        Set<Integer> A = new HashSet<>();
        Set<Integer> B = new HashSet<>();
        for(int i : a) A.add(i);
        for(int i : b) B.add(i);

        A.retainAll(B);
        return A.size();
    }
    
    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<>());
        }

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

        boolean visited[] = new boolean[n];  
        int c = 0;

        for(int i=0;i<n;i++){
            if(!visited[i]){
                c++;
                dfs(i,adj,visited);
            }
        }
        return c;
    }
    public void dfs(int node ,List<List<Integer>> adj,boolean[] visited){
        visited[node] = true;
        for(int nei : adj.get(node)){
            if(!visited[nei]){
                dfs(nei,adj,visited);
            }
        }
    }
}
```
['Depth-First Search', 'Graph', 'Java']