Number of Provinces

Asked at Expedia · Coding Questions · Difficulty: Medium · Last asked Jul 15, 2026

Given an `n x n` matrix representing connectivity between `n` cities. If city `i` is directly connected to city `j`, `matrix[i][j] = 1`. If city `i` is not directly connected to city `j`, `matrix[i][j] = 0`. Return the number of connected components (i.e., number of provinces).

Loading the problem…