37. Number of Operations to Make Network Connected | Code
Medium

Problem Statement

You are given an initial computer network connections. Return the minimum number of times you need to extract and place cables to make all computers connected. If it's not possible, return -1.

Examples

1Example 1
Input:
{ "n": 4, "connections": [ [ 0, 1 ], [ 0, 2 ], [ 1, 2 ] ] }
Output:
1
2Example 2
Input:
{ "n": 6, "connections": [ [ 0, 1 ], [ 0, 2 ], [ 0, 3 ], [ 1, 2 ], [ 1, 3 ] ] }
Output:
2
3Example 3
Input:
{ "n": 6, "connections": [ [ 0, 1 ], [ 0, 2 ], [ 0, 3 ], [ 1, 2 ] ] }
Output:
-1
Loading...

Sign in to Run Code and Submit

    37. Number of Operations to Make Network Connected | Code – DSA Problem Solution | DSAPrime