Number of Connected Components Problem
Number of Connected Components Problem — ExecCode Medium DSA Practice
Solve the Number of Connected Components problem on ExecCode. Free online medium DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given n nodes labeled from 0 to n - 1 and a list of undirected edges, return the number of connected components in the graph.
Examples
Input n = 7, edges = [[0, 1], [1, 2], [2, 0], [3, 4], [5, 6]]; Output 3. Input n = 5, edges = [[0, 1], [1, 2], [2, 3], [3, 4]]; Output 1. Input n = 4, edges = []; Output 4
Constraints
1 ≤ n ≤ 2000 0 ≤ edges.length ≤ 5000 edges[i].length == 2 0 ≤ ai, bi < n ai != bi There are no repeated edges.
Practice Number of Connected Components free on ExecCode. Browse DSA problems, topic map, and placement guides.