Watch 10 video solutions for Maximum Number of K-Divisible Components, a hard level problem involving Tree, Depth-First Search. This walkthrough by NeetCodeIO has 8,917 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the ith node, and an integer k.
A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by k, where the value of a connected component is the sum of the values of its nodes.
Return the maximum number of components in any valid split.
Example 1:
Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6 Output: 2 Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because: - The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12. - The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6. It can be shown that no other valid split has more than 2 connected components.
Example 2:
Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3 Output: 3 Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because: - The value of the component containing node 0 is values[0] = 3. - The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9. - The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6. It can be shown that no other valid split has more than 3 connected components.
Constraints:
1 <= n <= 3 * 104edges.length == n - 1edges[i].length == 20 <= ai, bi < nvalues.length == n0 <= values[i] <= 1091 <= k <= 109values is divisible by k.edges represents a valid tree.Problem Overview: You are given a tree with n nodes where each node has a value. The goal is to remove some edges so that every resulting connected component has a total sum divisible by k. Return the maximum number of such components you can form.
Approach 1: Brute Force Edge Removal + Component Sum Check (O(n^2) time, O(n) space)
One straightforward idea is to try removing edges and verify whether the resulting components have sums divisible by k. For each candidate edge removal, run a traversal such as DFS to compute the sum of nodes in each component. If both resulting sums are divisible by k, the cut is valid. However, recomputing component sums repeatedly leads to redundant traversals across the same nodes.
This approach relies heavily on repeated traversals of the tree, making it inefficient for large inputs. With up to n-1 edges and potentially full DFS traversals each time, the time complexity grows toward O(n^2). It mainly helps build intuition about why subtree sums are useful.
Approach 2: DFS with Subtree Sums (O(n) time, O(n) space)
The optimal approach uses a single postorder Depth-First Search traversal to compute subtree sums. Starting from any root (commonly node 0), recursively calculate the sum of each subtree. If the total sum of a subtree is divisible by k, you can treat that subtree as a separate component by conceptually cutting the edge connecting it to its parent.
The key insight: when a subtree sum is divisible by k, you increment the component count and return 0 upward instead of the actual sum. This prevents the parent subtree from including those nodes again. If the sum is not divisible by k, return the remainder sum so it contributes to the parent calculation.
Each node is visited exactly once and each edge is processed once, giving O(n) time complexity. The recursion stack and adjacency list together require O(n) space. This approach efficiently leverages subtree aggregation and modular arithmetic to maximize the number of valid components.
Recommended for interviews: Interviewers expect the DFS subtree-sum approach. It shows you understand postorder traversal on trees, modular arithmetic reasoning, and how to greedily cut valid subtrees during aggregation. Mentioning the brute force idea first demonstrates problem exploration, but implementing the linear-time DFS solution shows strong algorithmic maturity.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Edge Removal + DFS | O(n^2) | O(n) | Conceptual understanding of how edge removal affects component sums |
| DFS with Subtree Sums | O(n) | O(n) | Optimal solution for large trees and typical interview expectations |