ORIGINAL CODING QUESTION
Disconnected network groups
Count connected components of the undirected graph, including isolated vertices.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
n m, then m vertex pairs (u,v). Vertices are zero-based.
Constraints
1 ≤ n ≤ 30; 0 ≤ m ≤ 100; vertex IDs in [0,n).
Example 1
Input
4 2 0 1 2 3
Output
2
Two disconnected pairs exist.
Example 2
Input
3 0
Output
3
Each isolated vertex is its own group.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Every unvisited vertex starts a component.
Hint 2
Traverse all vertices reachable from it.
Hint 3
Union-find provides another direct approach.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →