Divide Nodes Into the Maximum Number of Groups - Problem

You're given an undirected graph with n nodes labeled from 1 to n, and your mission is to organize these nodes into groups with a special constraint.

Think of it like organizing people at a party where friends must be in adjacent groups - if two people are friends (connected by an edge), they can only be in groups that differ by exactly 1 (like group 2 and group 3, but not group 1 and group 3).

Your goal: Find the maximum number of groups you can create while satisfying this constraint. If it's impossible to group the nodes this way, return -1.

Key constraint: For every edge [a, b], if node a is in group x and node b is in group y, then |y - x| = 1.

This is essentially asking: what's the longest path in each connected component of the graph, since nodes along a path can be arranged in consecutive groups!

Input & Output

example_1.py โ€” Linear Chain
$ Input: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,5]]
โ€บ Output: 4
๐Ÿ’ก Note: The graph has a longest path of length 3 (like 6โ†’2โ†’1โ†’4), so we can create 4 groups: Group 1: [6], Group 2: [2], Group 3: [1,5], Group 4: [4,3]. Adjacent nodes are in consecutive groups.
example_2.py โ€” Triangle
$ Input: n = 3, edges = [[1,2],[2,3],[3,1]]
โ€บ Output: -1
๐Ÿ’ก Note: This forms a triangle (3-cycle). Since it's an odd cycle, the graph is not bipartite. No valid grouping exists because you can't assign consecutive group numbers to nodes in a triangle without violating the constraint.
example_3.py โ€” Disconnected Components
$ Input: n = 4, edges = [[1,2],[3,4]]
โ€บ Output: 2
๐Ÿ’ก Note: Two disconnected edges. Each edge forms a component with longest path = 1, so each needs 2 groups. The maximum across components is 2. Groups: [1,3] and [2,4].

Visualization

Tap to expand
๐ŸŽญ Theater Row AssignmentStep 1: Friend ConnectionsA1A2A3A4Friends must be in adjacent rowsStep 2: Row Assignment (BFS Distance)Row 1A1Row 2A2Row 3A3Row 4A4โœ“ Valid: 4 rows neededStep 3: Conflict DetectionB1B2B3CONFLICT!โœ— Triangle: Impossible!B2 and B3 are friends butboth need same row color๐ŸŽฏ Key: Blue/Red alternating rows ensure friends are adjacent. Triangles create impossible conflicts!
Understanding the Visualization
1
Build Graph
Create connections between friend pairs (actors who must be in adjacent rows)
2
Check Components
For each group of connected actors, verify no conflicts exist (odd cycles)
3
Find Longest Chain
Determine the maximum chain length in each component using BFS
4
Calculate Rows
The longest chain determines minimum rows needed for that theater section
Key Takeaway
๐ŸŽฏ Key Insight: The problem is equivalent to finding the longest path in a bipartite graph. BFS gives us both the bipartite check and the longest path in optimal O(V + E) time!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(V + E)

Visit each vertex and edge once during BFS traversal of all components

n
2n
โœ“ Linear Growth
Space Complexity
O(V + E)

Space for adjacency list, visited array, and BFS queue

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 500
  • 0 โ‰ค edges.length โ‰ค 104
  • edges[i].length == 2
  • 1 โ‰ค ai, bi โ‰ค n
  • ai != bi
  • No duplicate edges in the input
Asked in
Google 42 Meta 38 Amazon 35 Microsoft 28
28.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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