Number of Possible Sets of Closing Branches - Problem

There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.

The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (possibly none). However, they want to ensure that the remaining branches have a distance of at most maxDistance from each other.

The distance between two branches is the minimum total traveled length needed to reach one branch from another.

You are given integers n, maxDistance, and a 0-indexed 2D array roads, where roads[i] = [u_i, v_i, w_i] represents the undirected road between branches u_i and v_i with length w_i.

Return the number of possible sets of closing branches, so that any branch has a distance of at most maxDistance from any other.

Note that, after closing a branch, the company will no longer have access to any roads connected to it. Note that, multiple roads are allowed.

Input & Output

Example 1 — Basic Case
$ Input: n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,1]]
Output: 7
💡 Note: Valid subsets: {0}, {1}, {2}, {0,1}, {0,2}, {1,2}, {0,1,2}. After Floyd-Warshall, dist[1][2] = 3 (via path 1→0→2), so all distances are ≤ 5.
Example 2 — All Connected Within Distance
$ Input: n = 3, maxDistance = 10, roads = [[0,1,1],[1,2,1],[2,0,1]]
Output: 7
💡 Note: All 7 possible subsets are valid since max distance is 2, which is ≤ 10.
Example 3 — Linear Chain
$ Input: n = 4, maxDistance = 4, roads = [[0,1,3],[1,2,3],[2,3,3]]
Output: 7
💡 Note: Single branches and adjacent pairs are valid: {0}, {1}, {2}, {3}, {0,1}, {1,2}, {2,3}. Longer chains exceed maxDistance of 4.

Constraints

  • 2 ≤ n ≤ 15
  • 1 ≤ maxDistance ≤ 109
  • 0 ≤ roads.length ≤ 1000
  • roads[i].length == 3
  • 0 ≤ ui, vi ≤ n-1
  • ui ≠ vi
  • 1 ≤ wi ≤ 109
  • All branches are initially reachable from each other

Visualization

Tap to expand
Number of Possible Sets of Closing Branches INPUT 0 1 2 w=2 w=1 w=10 n = 3 maxDistance = 5 roads = [ [0,1,2], [1,2,10], [0,2,1] ] ALGORITHM STEPS 1 Precompute Distances Floyd-Warshall for all pairs 2 Enumerate Subsets 2^n = 8 possible subsets 3 Check Each Subset Verify max dist <= 5 4 Count Valid Sets Sum all valid subsets Valid Subsets: {} - empty set - OK {0} - single node - OK {1} - single node - OK {2} - single node - OK {0,2} - dist=1 <= 5 - OK {0,1,2} dist(1,2)=3 <= 5 FINAL RESULT All Valid Closing Sets: {} {0} {1} {2} {0, 2} Output: 5 5 valid branch closing configurations found OK Key Insight: Precompute all-pairs shortest paths using Floyd-Warshall O(n^3). Then enumerate all 2^n subsets of branches. For each subset, check if all pairwise distances are within maxDistance. The "closed" branches are those NOT in the subset. Total complexity: O(n^3 + 2^n * n^2). Works efficiently since n is small (n <= 10). TutorialsPoint - Number of Possible Sets of Closing Branches | Optimized with Precomputed Distances
Asked in
Amazon 25 Microsoft 18 Google 12
12.0K Views
Medium Frequency
~35 min Avg. Time
285 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen