Minimum Connection Changes
Updated
Problem statement
There are n computers labeled from 1 through n. The array connections describes undirected wires, where connections[i] = [a, b] means computers a and b are directly connected.
In one operation, you may remove one existing wire and reconnect that same wire between any two computers. Return the minimum number of operations needed to make every computer reachable from every other computer. Return -1 when this is impossible.
The input contains no self-connections and no duplicate wires.
Function
minimumConnectionChanges(n: int, connections: int[][]) → intExamples
Example 1
n = 4connections = [[1,2],[1,3],[2,3]]return = 1Computers 1, 2, and 3 already contain a redundant wire. Move one such wire to connect computer 4 to that component.
Example 2
n = 4connections = [[1,2],[3,4]]return = -1Connecting four computers requires at least three wires, but only two are available.
Example 3
n = 6connections = [[1,2],[1,3],[1,4],[2,3],[2,4]]return = 2The component containing computers 1 through 4 has two redundant wires. Move those two wires to attach computers 5 and 6.
Constraints
1 ≤ n ≤ 10^50 ≤ connections.length ≤ min(10^5, n × (n - 1) / 2)connections[i].length = 21 ≤ connections[i][0], connections[i][1] ≤ nconnections[i][0] ≠ connections[i][1]- No two entries describe the same undirected wire.
More InMobi Online Assessment questions
- Validate a Bracket SequenceOA · Seen Oct 2026
- Count Substrings Matching with One Adjacent SwapOA · Seen Oct 2026
- Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
- Minimum Service Cold StartsOA · Seen Oct 2026
- Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
- Connected Components with Divisor ThresholdOA · Seen Aug 2026
- Maximize Impression ValueOA · Seen Aug 2026
- Supply Path OptimizationOA · Seen Aug 2026
- Delivery ServiceOA · Seen Jun 2026
- Shared InterestOA · Seen Jun 2026
Similar questions at other companies
- Minimum Unreachable WarehousesVisa · OAVisa interview questions
- Visible Profiles CountMicrosoft · OAMicrosoft interview questions
- Fewest Pointer Changes to Make Every Node GoodIndeed · OAIndeed interview questions
- Evaluate DivisionPlaud · ONSITE INTERVIEW
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Determine Whether Two Horses Are Genetically RelatedGoogle · ONSITE INTERVIEWGoogle interview questions
- Most Stones Removed with Same Row or ColumnSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Count the Number of Complete ComponentsAmazon · PHONE SCREEN
- Count Connected Components in an Undirected GraphNutanix · ONSITE INTERVIEWNutanix interview questions