You are given a 2D integer array properties having dimensions n x m and an integer k.

Define a function intersect(a, b) that returns the number of distinct integers common to both arrays a and b.

Construct an undirected graph where each index i corresponds to properties[i]. There is an edge between node i and node j if and only if intersect(properties[i], properties[j]) >= k, where i and j are in the range [0, n - 1] and i != j.

Return the number of connected components in the resulting graph.

Input & Output

Example 1 — Basic Graph Formation
$ Input: properties = [[1,2,3],[1,3],[2,3,4]], k = 2
Output: 2
💡 Note: Intersections: properties[0] ∩ properties[1] = {1,3} (size 2 ≥ k), properties[0] ∩ properties[2] = {2,3} (size 2 ≥ k), properties[1] ∩ properties[2] = {3} (size 1 < k). Edges: 0-1, 0-2. Components: {0,1,2} and {} → But node 2 connects to 0, so actually {0,1,2} is one component. Wait, let me recalculate: 0 connects to 1 and 2, so all three are connected → 1 component.
Example 2 — No Connections
$ Input: properties = [[1,2],[3,4],[5,6]], k = 2
Output: 3
💡 Note: No two arrays have 2 or more common elements, so no edges exist. Each node forms its own component: 3 components total.
Example 3 — All Connected
$ Input: properties = [[1,2,3,4],[1,2,5,6],[2,3,7,8]], k = 2
Output: 1
💡 Note: All pairs have at least 2 common elements: (0,1) share {1,2}, (0,2) share {2,3}, (1,2) share {2}. Wait, (1,2) only share {2}, which is size 1 < k=2. So edges are 0-1 and 0-2, making one component {0,1,2}.

Constraints

  • 1 ≤ properties.length ≤ 103
  • 1 ≤ properties[i].length ≤ 103
  • 1 ≤ properties[i][j] ≤ 105
  • 1 ≤ k ≤ 103

Visualization

Tap to expand
Properties Graph: From Arrays to Connected ComponentsInput Arrays[1,2,3][1,3][2,3,4]k = 2Intersection Check(0,1): {1,3} → 2 ≥ 2 ✓(0,2): {2,3} → 2 ≥ 2 ✓(1,2): {3} → 1 < 2 ✗Graph Formation012Edges: 0-1, 0-2ResultComponent 1{0, 1, 2}Answer: 1 connected componentAll nodes are connected through node 0, forming one large component
Understanding the Visualization
1
Input Properties
Each array becomes a node in the graph
2
Find Intersections
Connect nodes with intersection size ≥ k
3
Count Components
Find connected components in the resulting graph
Key Takeaway
🎯 Key Insight: Transform array similarity into graph connectivity - nodes with enough common elements form connected components
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
12.0K Views
Medium Frequency
~25 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen