Maximum Number of K-Divisible Components - Problem

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.

Input & Output

Example 1 — Basic Tree Split
$ Input: n = 5, edges = [[0,2],[1,2],[1,3],[1,4]], values = [1,8,1,4,4], k = 6
Output: 2
💡 Note: We can split the tree by removing the edge between nodes 1 and 2. This creates two components: {0,2} with sum 1+1=2 (not divisible by 6), and {1,3,4} with sum 8+4+4=16 (not divisible by 6). But we can remove edges (1,3) and (1,4) to get components {0,2,1} with sum 10 (not divisible) and {3},{4} with sums 4,4. The optimal split gives us 2 components: {1,4,3} sum=16 and {0,2} sum=2, but since we need divisible sums, we get components {1,3,4} sum=16 and {0,2} sum=2. Actually, the correct split removes edge (0,2) to get {0} sum=1, {2,1,3,4} sum=17. The optimal solution finds 2 valid components.
Example 2 — Single Node
$ Input: n = 1, edges = [], values = [10], k = 5
Output: 1
💡 Note: Single node with value 10 is divisible by 5, so we get 1 component.
Example 3 — No Valid Split
$ Input: n = 3, edges = [[0,1],[1,2]], values = [1,2,3], k = 7
Output: 0
💡 Note: Total sum is 6, which is not divisible by 7. No matter how we split, we cannot get components with sums divisible by 7.

Constraints

  • 1 ≤ n ≤ 3 × 104
  • edges.length == n - 1
  • 0 ≤ ai, bi < n
  • values.length == n
  • 0 ≤ values[i] ≤ 109
  • 1 ≤ k ≤ 109
  • Sum of values[i] is divisible by k

Visualization

Tap to expand
Maximum Number of K-Divisible Components INPUT 0 v=1 2 v=1 1 v=8 3 v=4 4 v=4 n = 5, k = 6 edges = [[0,2],[1,2],[1,3],[1,4]] values = [1,8,1,4,4] Total sum = 18 (18 % 6 = 0) ALGORITHM STEPS 1 Start DFS from root Pick node 0, traverse tree 2 Calculate subtree sums Sum each node + children 3 Check divisibility by k If sum % k == 0, cut edge 4 Count components Each cut adds 1 component Subtree Sums: Node 3: sum=4 (4%6!=0) Node 4: sum=4 (4%6!=0) Node 1: 8+4+4=16 (16%6!=0) Node 2: 1+16=17 (17%6!=0) Node 0: 1+17=18 (18%6=0) OK Subtree{3,4,1}: 12 (12%6=0) OK --> Can split into 2 parts! FINAL RESULT Component 1 (sum=6) 0 v=1 2 v=1 CUT HERE Component 2 (sum=12) 1 v=8 3 v=4 4 v=4 Output: 2 Maximum components Key Insight: Use DFS to compute subtree sums bottom-up. When a subtree sum is divisible by k, we can "cut" that edge and start fresh (return 0 to parent). Each valid cut increases components by 1. Total sum divisible by k guarantees at least one valid split. Time: O(n), Space: O(n) TutorialsPoint - Maximum Number of K-Divisible Components | DFS with Subtree Sum Checking
Asked in
Google 15 Meta 12 Amazon 8
8.5K Views
Medium Frequency
~25 min Avg. Time
234 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