Divide Nodes Into the Maximum Number of Groups Problem
Description
LeetCode Problem 2493.
You are given a positive integer n representing the number of nodes in an undirected graph. The nodes are labeled from 1 to n.
You are also given a 2D integer array edges, where edges[i] = [a_i,b_i] indicates that there is a bidirectional edge between nodes a_i and b_i. Notice that the given graph may be disconnected.
Divide the nodes of the graph into m groups (1-indexed) such that:
- Each node in the graph belongs to exactly one group.
- For every pair of nodes in the graph that are connected by an edge
[a_i,b_i], ifa_ibelongs to the group with indexx, andb_ibelongs to the group with indexy, then|y - x| = 1.
Return the maximum number of groups (i.e., maximum m) into which you can divide the nodes. Return -1 if it is impossible to group the nodes with the given conditions.
Example 1:

Input: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
Output: 4
Explanation: As shown in the image we:
- Add node 5 to the first group.
- Add node 1 to the second group.
- Add nodes 2 and 4 to the third group.
- Add nodes 3 and 6 to the fourth group.
We can see that every edge is satisfied.
It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
Example 2:
Input: n = 3, edges = [[1,2],[2,3],[3,1]]
Output: -1
Explanation: If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
It can be shown that no grouping is possible.
Constraints:
1 <= n <= 5001 <= edges.length <= 10^4edges[i].length == 21 <= a_i, b_i <= na_i != b_i- There is at most one edge between any pair of vertices.
Solution
Approach: BFS from every node
Nodes joined by an edge must land in groups whose indices differ by exactly one. That is only possible when each connected component is bipartite; an odd cycle makes the answer -1.
Components are independent, so the answer is the sum of the best group count of each component. For one component, the best split starts BFS from some node: the BFS layers are the groups, and the number of layers is the group count. Try every node as the start, run BFS, and stop with -1 whenever an edge connects two nodes whose distances do not differ by exactly one. Keep, per component (identified by its smallest node), the largest layer count found, then add those up.
Complexity: time O(n(n + m)); space O(n + m).
from collections import defaultdict, deque
class Solution:
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
g = [[] for _ in range(n)]
for a, b in edges:
g[a - 1].append(b - 1)
g[b - 1].append(a - 1)
d = defaultdict(int)
for i in range(n):
q = deque([i])
dist = [0] * n
dist[i] = mx = 1
root = i
while q:
a = q.popleft()
root = min(root, a)
for b in g[a]:
if dist[b] == 0:
dist[b] = dist[a] + 1
mx = max(mx, dist[b])
q.append(b)
elif abs(dist[b] - dist[a]) != 1:
return -1
d[root] = max(d[root], mx)
return sum(d.values())