FastPrepCount the Number of Complete Components
Problem · Graph

Count the Number of Complete Components

Learn this problem
MediumAmazon logoAmazonNEW GRADPHONE SCREEN
See Amazon hiring insights

Problem statement

You are given an integer n and an undirected graph whose vertices are numbered from 0 through n - 1. The array edges contains each undirected edge [u, v].

A connected component is complete when every pair of distinct vertices in that component is joined by an edge.

Return the number of complete connected components. A component containing one vertex is complete.

Function

countCompleteComponents(n: int, edges: int[][]) → int

Examples

Example 1

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

The components are {0,1,2}, {3,4}, and {5}. Each contains every possible internal edge, so all three are complete.

Example 2

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

The component {0,1,2} is complete. The component {3,4,5} is missing edge [4,5], so it is not complete.

Example 3

n = 1edges = []return = 1

The only vertex forms a one-vertex complete component.

More Amazon problems

drafts saved locally
public int countCompleteComponents(int n, int[][] edges) {
  // write your code here
}
n6
edges[[0,1],[0,2],[1,2],[3,4]]
expected3
checking account