K26

Number of Connected Components

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: NUMERIC

Problem Statement & Objective

Given n nodes and a list of undirected edges, return the total number of connected components in the graph.

Function Signature (Python)

number_of_connected_components(n: int, edges: list[list[int]]) -> int

Input & Output Contract

Output Rule:Bağlı bileşen sayısı (tam sayı).

System Constraints

1 <= n <= 2000
0 <= kenarlar.length <= 5000

Example Execution Walkthrough

n = 5, kenarlar = [[0, 1], [1, 2], [3, 4]]
Bileşen 1: {0, 1, 2}, Bileşen 2: {3, 4} -> cevap: 2

Example Test Scenarios (3)

Example 1
Input:[5,[[0,1],[1,2],[3,4]]]
Expected Output:2
Example 2: Tam Bağlı
Input:[5,[[0,1],[1,2],[2,3],[3,4]]]
Expected Output:1
Example 3: Kenarsız
Input:[4,[]]
Expected Output:4
Loading Python Editor...
Input Parameters
[
  5,
  [
    [
      0,
      1
    ],
    [
      1,
      2
    ],
    [
      3,
      4
    ]
  ]
]
Expected Output
2