2Brute2Best
🔥0

Number of Operations to Make Network Connected

Medium·LeetCode #1319 Pro OnlySlight Variation
LeetCode
01

Problem Statement

There are n computers, numbered 0 to n − 1. Some pairs are joined by a cable. You get the cables as connections, where connections[i] = [a, b] joins computers a and b directly. Two computers can talk if a chain of cables joins them.

You can unplug any cable and plug it into a different pair of computers. Return the minimum number of moves (cables unplugged and plugged in again) so that every computer can talk to every other one. If it cannot be done, return -1.

Input

n, the number of computers, and connections, the list of cables.

Output

The minimum number of cable moves, or -1 if it is not possible.

Example 1
Input: n = 4, connections = [[0, 1], [0, 2], [1, 2]]
Output: 1

Computers 0, 1 and 2 are linked, and computer 3 is alone. Move cable 1–2 (it closes a loop, so nothing breaks) to join 3 to the group.

Example 2
Input: n = 6, connections = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3]]
Output: 2

Computers 0 to 3 form one group, and 4 and 5 are alone. The spare cables 1–2 and 1–3 close loops, so each can join one of the lone computers: 2 moves.

Example 3
Input: n = 6, connections = [[0, 1], [0, 2], [0, 3], [1, 2]]
Output: -1

Connecting 6 computers needs at least 5 cables, and there are only 4.

Constraints
  • • 1 ≤ n ≤ 10^5
  • • 1 ≤ connections.length ≤ min(n · (n − 1) / 2, 10^5)
  • • 0 ≤ aᵢ, bᵢ < n
  • • aᵢ ≠ bᵢ
  • • No two computers are connected by more than one cable.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“connections between computers”
→ Cables merge groups, so the question is how many groups are left.
“minimum number of operations”
→ Each move joins at most two groups, so count the groups, not the spare cables.
“return -1 if impossible”
→ Fewer than `n − 1` cables in total can never connect everything.
03

Approach Complexity Ladder

Brute Force — relabel the whole group on every joinO(C · N)O(N)

Keep a group label per computer; each join renames one whole group

Optimal — Union-Find: count the groupsO(C · α(N))O(N)

Union each cable; every successful union removes one group; the answer is groups − 1

Brute Force Intuition

Give every computer a group label, starting with its own number. For each cable whose ends have different labels, rename every computer with one label to the other label, and count one fewer group. Every join costs a full pass over all n computers.

Recognition rule for this sub-pattern:

If both ends of an edge already share a root, this edge closes a cycle.
Remember this · Union-Find (DSU)
Trigger
Edges arrive one by one, "are these connected?", merge equivalent items, count components, extra edge creating a cycle.
Think
Am I repeatedly merging groups and asking whether two things are in the same group?
Key question
What are the elements, and what does it mean to union two of them?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.