Skip to main content
Back to problems
Leetcode
Medium
Graphs
Graph Connectivity
Count The Number Of Complete Components

Count how many connected components in an undirected graph are complete graphs.

Acceptance 100%
Problem Statement

You are given an undirected graph with nn vertices labeled from $0toton-1$ and a list of edges. A connected component is called complete if every pair of distinct vertices inside that component is directly connected by an edge.

Your task is to return the number of connected components that are complete.

In other words, for each connected component, check whether it forms a clique: if the component has kk vertices, then it must contain exactly k(k1)2\frac{k(k-1)}{2} edges to be complete.

Input Format

  • An integer nn representing the number of vertices.
  • A list of undirected edges, where each edge is a pair [u,v][u, v].

Vertices are labeled from $0toton-1$.

Output Format

  • Return an integer: the number of connected components that are complete.

Constraints

  • 1n1 \le n
  • The graph is undirected.
  • No self-loops are assumed.
  • Parallel edges are not required for the standard interpretation of this problem.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 6
edges = [[0,1],[0,2],[1,2],[3,4]]

Output

2

Explanation

The graph has three connected components: {0,1,2}, {3,4}, and {5}. The first is complete because all 3 pairs are connected. The second is also complete because a 2-node connected component needs exactly 1 edge. The isolated vertex {5} is complete by definition. So the answer is 3.

Note: if using the standard LeetCode interpretation, every isolated vertex is a complete component.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.