Possible Bipartition
Problem Statement
There are n people, numbered from 1 to n. Some pairs of people do not like each other. The list dislikes gives those pairs, where [a, b] means person a does not like person b.
You want to split everyone into two groups. Two people who dislike each other must not be in the same group. Groups may have any size.
Return true if such a split exists, otherwise false.
n, the number of people, and dislikes, a list of pairs [a, b].
true if the people can be split into two groups with no dislike inside one group, otherwise false.
Put persons 1 and 4 in one group, and persons 2 and 3 in the other. Every dislike joins the two groups.
All three people dislike each other. With only two groups, two of them must share a group.
- • 1 ≤ n ≤ 2000
- • 0 ≤ dislikes.length ≤ 10^4
- • dislikes[i].length = 2
- • 1 ≤ a_i < b_i ≤ n
- • All the pairs in dislikes are unique.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try all 2^n splits; accept the first one where no dislike pair shares a group
One edge per dislike, added both ways; BFS two-colouring; a clash means false
Give every person group A or B, for all 2^n splits. A split works when no dislike pair has both people in the same group. Each person has two choices, so the brute force tries 2^n splits, and that is only practical for small n.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.