LeetCode Entry

2685. Count the Number of Complete Components

11.07.2026 medium 2026 kotlin rust

Fully connected components

2685. Count the Number of Complete Components medium substack youtube

https://dmitrysamoylenko.com/leetcode/

11.07.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1417

Problem TLDR

Fully connected components

Intuition

  1. Union-Find to find connected components
  2. Count incoming edges for each node
  3. Fully connected group size is the number of edges for each node +1

Approach

  • 50 elements can fit into long variable
  • adjacency matrix would look the same for each group

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code

    fun countCompleteComponents(n: Int, e: Array<IntArray>) = e
        .fold(LongArray(n){1L shl it}){ m, (a, b) -> m[a] += 1L shl b; m[b] += 1L shl a; m }
        .groupBy { it }.count { (k, v) -> v.size == k.countOneBits() }
6

Comments