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 K-Divisible Tree ComponentsSplit tree to maximize components with sums divisible by kInput Treevalues=[1,8,1,4,4], k=620134DFS AnalysisFind subtrees with k-divisible sums20134Final Result2 components found02134Sum=1Sum=16Output: 2 components maximumGreen edges show optimal cuts for k-divisible components
Understanding the Visualization
1
Input Tree
Tree with node values and divisor k
2
Find Cuts
Identify edges to remove for optimal splitting
3
Count Components
Maximum components with k-divisible sums
Key Takeaway
🎯 Key Insight: Use DFS to greedily cut subtrees when their sum is divisible by k - this maximizes the number of valid components
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