Ctrl K

Isolated Groups in a Network

Medium
A data center has node_count servers numbered 0 to node_count - 1. Some of them are wired together: each entry links[i] = [a, b] is a two-way cable between servers a and b. Two servers can exchange data if there is a path of cables between them, possibly through other servers. This splits the network into groups: within a group anyone can reach anyone, while servers in different groups cannot reach each other. A server with no cables is a group of its own. Return the number of groups.
Example 1
node_count=7, links=[[0, 3], [3, 5], [1, 2], [6, 4]] → 3

Servers 0, 3 and 5 form one group, 1 and 2 another, and 4 and 6 a third.

Example 2
node_count=4, links=[[0, 1]] → 3

Servers 0 and 1 are linked, while 2 and 3 stand alone: three groups in total.

Example 3
node_count=3, links=[[0, 1], [1, 2]] → 1
Constraints
  • 1 ≤ node_count ≤ 2000
  • 0 ≤ links.length ≤ 5000
  • links[i].length = 2
  • 0 ≤ a, b < node_count, a ≠ b
  • Any pair of servers is joined by at most one cable
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDo the Links Form a Tree MediumRatio Chain Queries MediumClusters of Linked Servers MediumThe Extra Cable MediumSplit Into Two Teams Medium
loading Python…
Input
node_count=7, links=[[0, 3], [3, 5], [1, 2], [6, 4]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.