Skip to main content

Possible Bipartition - Solution & Explanation

MediumDepth-First SearchBreadth-First SearchUnion FindGraph17 min readAsked at: Amazon, Microsoft, Samsung +8
Practice this problem

Problem Statement

We want to split a group of n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group.

Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way.

 

Example 1:

Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: The first group has [1,4], and the second group has [2,3].

Example 2:

Input: n = 3, dislikes = [[1,2],[1,3],[2,3]]
Output: false
Explanation: We need at least 3 groups to divide them. We cannot put them in two groups.

 

Constraints:

  • 1 <= n <= 2000
  • 0 <= dislikes.length <= 104
  • dislikes[i].length == 2
  • 1 <= ai < bi <= n
  • All the pairs of dislikes are unique.

Approach Overview

Problem Overview: You are given n people and a list of dislike pairs. Each pair means those two people cannot be in the same group. The task is to check whether you can split everyone into two groups so that no pair of people who dislike each other end up in the same group.

The structure formed by dislikes is a graph where each person is a node and every dislike pair is an undirected edge. The problem reduces to checking whether this graph is bipartite. A graph is bipartite if its nodes can be colored using two colors such that no adjacent nodes share the same color.

Approach 1: Graph Coloring Using BFS (O(n + m) time, O(n + m) space)

Build an adjacency list from the dislikes array so you can quickly access all neighbors of a person. Maintain a color array where each node is assigned either 1 or -1. Iterate through all nodes because the graph may have multiple disconnected components. For every uncolored node, start a Breadth-First Search. Assign the starting node a color, then push it into a queue. While processing the queue, give each neighbor the opposite color. If you encounter a neighbor that already has the same color as the current node, the graph cannot be bipartite, so the partition is impossible. BFS works well here because it explores level by level and naturally alternates colors across edges.

Approach 2: Graph Coloring Using DFS (O(n + m) time, O(n + m) space)

The same bipartite check can be implemented using Depth-First Search. After constructing the adjacency list, recursively traverse the graph and assign alternating colors to neighbors. Each DFS call colors the current node and then explores all adjacent nodes. If a neighbor already has a conflicting color, you immediately return false. DFS tends to produce shorter code because the recursion naturally handles traversal, while BFS uses an explicit queue. Both methods rely on the same underlying graph traversal idea and produce identical time complexity.

In both solutions, the key insight is that dislike relationships form edges in a graph, and the two groups correspond to the two colors of a bipartite graph. The algorithm simply attempts to color the graph while respecting those constraints.

Recommended for interviews: The BFS graph-coloring solution is commonly expected because it clearly demonstrates understanding of bipartite graphs and iterative traversal. DFS is equally correct and sometimes shorter to implement. Interviewers mainly look for the insight that this is a bipartite graph problem and that coloring during traversal detects conflicts in O(n + m) time.

Approach 1: Approach 1: Graph Coloring Using BFS

This approach uses a BFS traversal to determine if the graph is bipartite. A graph is bipartite if you can color it using two colors such that no two adjacent nodes have the same color. We'll represent people as nodes and dislikes as edges.

The C solution uses a BFS approach to check if the graph can be bipartite. The graph is represented using an adjacency list, and BFS is performed from each uncolored node. If we find a conflict in colors, we return false.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges.
Space Complexity: O(V + E) for storing the graph structure.

Try this approach in the editor →

Approach 2: Approach 2: Graph Coloring Using DFS

This approach uses a DFS traversal to determine if the graph is bipartite. Similar to BFS, DFS attempts to color the graph using two colors to check if a valid bipartition is possible.

The C solution uses a recursive DFS approach to attempt coloring with two colors. If two adjacent nodes have the same color during the traversal, the graph is not bipartite.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges.
Space Complexity: O(V + E) for graph representation.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Graph Coloring Using BFS

Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges.
Space Complexity: O(V + E) for storing the graph structure.

Approach 2: Graph Coloring Using DFS

Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges.
Space Complexity: O(V + E) for graph representation.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Graph Coloring with BFSO(n + m)O(n + m)Preferred iterative solution; easy to reason about level-by-level coloring
Graph Coloring with DFSO(n + m)O(n + m)Good when recursion is comfortable; results in shorter code for bipartite checks

Video Solution

Possible Bipartition | Bipartite graph | Graph coloring | Leetcode #886 • Techdose • 55,536 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Possible Bipartition easy or hard?
Possible Bipartition is considered a Medium difficulty problem. The main challenge is recognizing that the dislike relationships form a graph and that the problem reduces to checking whether the graph is bipartite using BFS or DFS coloring.
Possible Bipartition Python/Java solution
Both Python and Java implementations follow the same idea: build the adjacency list, maintain a color array, and run BFS or DFS for each unvisited node. During traversal, assign opposite colors to neighbors and detect conflicts. The algorithm remains O(n + m) regardless of language.
How to solve Possible Bipartition in O(n)?
Build an adjacency list from the dislike pairs and run BFS or DFS to color the graph. Assign one color to the starting node and alternate colors for neighbors. If a neighbor already has the same color as the current node, return false. Since each node and edge is processed once, the runtime is O(n + m).
What is the best approach for Possible Bipartition?
The standard approach is to treat the dislikes list as a graph and check whether the graph is bipartite. You color nodes using two colors while traversing with BFS or DFS. If any edge connects two nodes with the same color, the bipartition is impossible. This solution runs in O(n + m) time where m is the number of dislike pairs.
Is Possible Bipartition asked at Google/Amazon/Meta?
Bipartite graph detection problems appear frequently in interviews at companies like Amazon, Google, and Meta. Variations include checking if a graph is bipartite, course scheduling constraints, and grouping problems with conflicts. This problem specifically tests graph traversal and coloring techniques.
What data structure is used in Possible Bipartition?
The solution uses an adjacency list to represent the graph and an array or map to store node colors. A queue is used for BFS implementations, while recursion or a stack is used for DFS traversal.
What is the time complexity of Possible Bipartition?
The optimal BFS or DFS graph traversal runs in O(n + m) time. Each person (node) is processed once and each dislike pair (edge) is examined at most twice in an undirected adjacency list. The space complexity is also O(n + m) due to the adjacency list and color array.

Ready to solve this problem?

Practice Possible Bipartition with our built-in code editor and test cases.

Practice on FleetCode