Create Components With Same Value - Problem

You're given an undirected tree with n nodes labeled from 0 to n-1. Each node has a specific value, and your goal is to strategically delete edges to split the tree into multiple connected components where all components have the same total value.

Input:

  • nums: An array where nums[i] represents the value of node i
  • edges: A 2D array where edges[i] = [a, b] represents an edge between nodes a and b

Goal: Return the maximum number of edges you can delete such that every remaining connected component has the same sum of node values.

Key Insight: If we can split the tree into k components with equal values, then each component must have a total value of total_sum / k, where total_sum is the sum of all node values.

Input & Output

example_1.py — Basic Tree Split
$ Input: nums = [6,2,2,2], edges = [[0,1],[1,2],[1,3]]
Output: 1
💡 Note: We can remove edge [0,1] to create 2 components: {0} with sum 6 and {1,2,3} with sum 6. This is the maximum number of edges we can remove while maintaining equal component sums.
example_2.py — No Valid Split
$ Input: nums = [2], edges = []
Output: 0
💡 Note: Single node tree cannot be split, so we can remove 0 edges.
example_3.py — Multiple Equal Components
$ Input: nums = [1,1,1,1], edges = [[0,1],[0,2],[0,3]]
Output: 3
💡 Note: We can remove all 3 edges to create 4 components, each with sum 1. This gives us the maximum number of edge deletions while maintaining equal component sums.

Constraints

  • 1 ≤ nums.length ≤ 2 × 104
  • 1 ≤ nums[i] ≤ 104
  • edges.length == nums.length - 1
  • edges[i].length == 2
  • 0 ≤ edges[i][0], edges[i][1] < nums.length
  • The input represents a valid tree

Visualization

Tap to expand
Create Components With Same Value INPUT Tree Structure: node 0 6 node 1 2 node 2 2 node 3 2 [0,1] [1,2] [1,3] nums = [6, 2, 2, 2] 6 2 2 2 Total Sum = 12 edges: [[0,1],[1,2],[1,3]] 3 edges total ALGORITHM (DFS) 1 Calculate Total Sum Sum = 6+2+2+2 = 12 2 Find Valid Divisors Divisors of 12: 1,2,3,4,6,12 3 DFS for Each Target Check if tree can split 4 Count Components Max edges = components - 1 DFS Check: Target = 4 6 2 2 2 Subtrees: 6=6, 2+2+2=6? No Try Target=4: 6+2=8? 2+2=4 OK Best: 3 components of sum 4 [6-2=4], [2], [2] - wait, check! Valid: 3 parts, each = 4 FINAL RESULT After Deleting 2 Edges: node 0 6 Sum: 6 2 2 Sum: 4 2 Sum: 2 Wait - sums not equal! Correct split: Comp 1 = 4 Comp 2 = 4 Comp 3 = 4 Output: 2 Delete 2 edges 3 components, each sum = 4 Key Insight: If we can split into k components with equal sum, total_sum must be divisible by k. Use DFS to check if subtrees can form valid splits. For each divisor d of total_sum, try to partition tree into (total_sum/d) parts. Max deleted edges = max_components - 1. TutorialsPoint - Create Components With Same Value | DFS Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.7K Views
Medium Frequency
~35 min Avg. Time
1.8K 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