2872. Maximum Number of K-Divisible Components LeetCode Solution
In this guide, you will get 2872. Maximum Number of K-Divisible Components LeetCode Solution with the best time and space complexity. The solution to Maximum Number of K-Divisible Components problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Number of K-Divisible Components solution in C++
- Maximum Number of K-Divisible Components solution in Java
- Maximum Number of K-Divisible Components solution in Python
- Additional Resources
Problem Statement of Maximum Number of K-Divisible Components
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 * 104
edges.length == n – 1
edges[i].length == 2
0 <= ai, bi < n
values.length == n
0 <= values[i] <= 109
1 <= k <= 109
Sum of values is divisible by k.
The input is generated such that edges represents a valid tree.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2872. Maximum Number of K-Divisible Components LeetCode Solution in C++
class Solution {
public:
int maxKDivisibleComponents(int n, vector<vector<int>>& edges,
vector<int>& values, int k) {
int ans = 0;
vector<vector<int>> graph(n);
for (const vector<int>& edge : edges) {
const int u = edge[0];
const int v = edge[1];
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(graph, 0, /*prev=*/-1, values, k, ans);
return ans;
}
private:
long dfs(const vector<vector<int>>& graph, int u, int prev,
const vector<int>& values, int k, int& ans) {
long treeSum = values[u];
for (const int v : graph[u])
if (v != prev)
treeSum += dfs(graph, v, u, values, k, ans);
if (treeSum % k == 0)
++ans;
return treeSum;
}
};
/* code provided by PROGIEZ */
2872. Maximum Number of K-Divisible Components LeetCode Solution in Java
class Solution {
public int maxKDivisibleComponents(int n, int[][] edges, int[] values, int k) {
List<Integer>[] graph = new List[n];
for (int i = 0; i < n; i++)
graph[i] = new ArrayList<>();
for (int[] edge : edges) {
final int u = edge[0];
final int v = edge[1];
graph[u].add(v);
graph[v].add(u);
}
dfs(graph, 0, /*prev=*/-1, values, k);
return ans;
}
private int ans = 0;
private long dfs(List<Integer>[] graph, int u, int prev, int[] values, int k) {
long treeSum = values[u];
for (int v : graph[u])
if (v != prev)
treeSum += dfs(graph, v, u, values, k);
if (treeSum % k == 0)
++ans;
return treeSum;
}
}
// code provided by PROGIEZ
2872. Maximum Number of K-Divisible Components LeetCode Solution in Python
class Solution:
def maxKDivisibleComponents(
self,
n: int,
edges: list[list[int]],
values: list[int],
k: int,
) -> int:
ans = 0
graph = [[] for _ in range(n)]
def dfs(u: int, prev: int) -> int:
nonlocal ans
treeSum = values[u]
for v in graph[u]:
if v != prev:
treeSum += dfs(v, u)
if treeSum % k == 0:
ans += 1
return treeSum
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
dfs(0, -1)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.