Watch 10 video solutions for Valid Arrangement of Pairs, a hard level problem involving Depth-First Search, Graph, Eulerian Circuit. This walkthrough by codestorywithMIK has 15,592 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed 2D integer array pairs where pairs[i] = [starti, endi]. An arrangement of pairs is valid if for every index i where 1 <= i < pairs.length, we have endi-1 == starti.
Return any valid arrangement of pairs.
Note: The inputs will be generated such that there exists a valid arrangement of pairs.
Example 1:
Input: pairs = [[5,1],[4,5],[11,9],[9,4]] Output: [[11,9],[9,4],[4,5],[5,1]] Explanation: This is a valid arrangement since endi-1 always equals starti. end0 = 9 == 9 = start1 end1 = 4 == 4 = start2 end2 = 5 == 5 = start3
Example 2:
Input: pairs = [[1,3],[3,2],[2,1]] Output: [[1,3],[3,2],[2,1]] Explanation: This is a valid arrangement since endi-1 always equals starti. end0 = 3 == 3 = start1 end1 = 2 == 2 = start2 The arrangements [[2,1],[1,3],[3,2]] and [[3,2],[2,1],[1,3]] are also valid.
Example 3:
Input: pairs = [[1,2],[1,3],[2,1]] Output: [[1,2],[2,1],[1,3]] Explanation: This is a valid arrangement since endi-1 always equals starti. end0 = 2 == 2 = start1 end1 = 1 == 1 = start2
Constraints:
1 <= pairs.length <= 105pairs[i].length == 20 <= starti, endi <= 109starti != endipairs.Problem Overview: You receive pairs[i] = [start, end]. Rearrange the pairs so that the end of one pair equals the start of the next. Every pair must be used exactly once. The task reduces to finding an ordering of directed edges that forms a continuous chain.
Approach 1: Backtracking / Permutation Search (O(n!))
Treat each pair as a directed edge and attempt to build the sequence using brute-force backtracking. Start from any pair, then recursively choose another unused pair whose start matches the current end. Track visited pairs and build the path step by step. This guarantees correctness but explores up to n! permutations in the worst case, with O(n) recursion space. This approach only works for very small inputs and mainly helps recognize the structure of the chaining requirement.
Approach 2: Eulerian Path in Directed Graph (Hierholzer’s Algorithm) (O(n))
Interpret each pair as a directed edge in a graph. The problem becomes finding an Eulerian path that uses every edge exactly once. Maintain an adjacency list mapping start → list of ends and compute in-degrees and out-degrees. A valid arrangement exists when either every node has equal in/out degree (Eulerian circuit) or exactly one node has out = in + 1 (start node) and one has in = out + 1. Use Depth-First Search with Hierholzer’s algorithm: repeatedly follow edges, removing them as you traverse, and append nodes during backtracking. The reversed traversal order forms the valid edge sequence. Time complexity is O(n) since each edge is processed once, and space complexity is O(n) for adjacency storage and recursion.
Approach 3: Iterative Hierholzer Using Stack (O(n))
The same Eulerian-path logic can be implemented iteratively using a stack instead of recursion. Push the start node onto the stack and keep traversing unused outgoing edges while pushing nodes. When a node has no remaining edges, pop it and append it to the path. This produces the Eulerian traversal in reverse order. The algorithm still processes each edge once, so time complexity remains O(n) with O(n) auxiliary space. Some engineers prefer this approach to avoid recursion limits.
Recommended for interviews: The Eulerian path solution using Hierholzer’s algorithm is the expected answer. Interviewers want you to recognize that the chaining constraint maps directly to an Eulerian Circuit / Path problem in a directed graph. Mentioning the brute-force idea shows you understand the search space, but implementing the linear-time graph solution demonstrates strong algorithmic insight.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking / Permutation Search | O(n!) | O(n) | Conceptual baseline or very small inputs |
| DFS Hierholzer (Eulerian Path) | O(n) | O(n) | Optimal approach for directed graph edge ordering |
| Iterative Hierholzer with Stack | O(n) | O(n) | Preferred when avoiding recursion depth limits |