Ctrl K

Busy Server Pairs

Hard
A data center has servers machines numbered from 1 to servers, joined by cables: cables[i] = [u, v] is a cable between servers u and v. The same two servers may be joined by several cables. The **load** of a pair of servers (a, b) is the number of cables attached to at least one of them. A cable running between a and b themselves is counted once. For each value thresholds[j], count the pairs a < b whose load is strictly greater than thresholds[j]. Return the answers in the same order.
Example 1
servers=4, cables=[[1, 2], [2, 3], [2, 4], [3, 4]], thresholds=[2, 3] → [6, 2]

Servers 1, 2, 3, 4 have 1, 3, 2 and 2 cables. The pairs (2, 3) and (2, 4) carry 3 + 2 − 1 = 4, the other four pairs carry 3. All 6 pairs exceed 2, and two of them exceed 3.

Example 2
servers=3, cables=[[1, 2], [2, 1], [1, 2]], thresholds=[0, 2] → [3, 3]

Three parallel cables join 1 and 2: the pair (1, 2) carries 3, and so do (1, 3) and (2, 3), since server 3 has no cables at all. All three pairs exceed 2.

Constraints
  • 2 ≤ servers ≤ 2·10⁴
  • 1 ≤ len(cables) ≤ 10⁵
  • 1 ≤ u, v ≤ servers, u ≠ v
  • 1 ≤ len(thresholds) ≤ 20
  • 0 ≤ thresholds[j] < len(cables)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsK-th Smallest Pair Gap HardScooter Trips Under a Road Limit HardFlowerbed Beauty on a Budget HardRobots Collecting Coins HardTriples Under a Limit Medium
loading Python…
Input
servers=4, cables=[[1, 2], [2, 3], [2, 4], [3, 4]], thresholds=[2, 3]
Expected
[6, 2]

Run checks the first 3 tests in your browser. Submit checks all 47 tests on the server, hidden ones included.