Valid Arrangement of Pairs - Problem

Imagine you have a collection of paired connections that need to be arranged in a specific order to form a valid chain. Each pair has a start point and an end point, and the challenge is to arrange them so that one pair's end connects perfectly with the next pair's start.

You are given a 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.

Goal: Return any valid arrangement of the given pairs that forms a continuous chain.

Example: If you have pairs [[5,1],[4,5],[11,9],[9,4]], one valid arrangement could be [[11,9],[9,4],[4,5],[5,1]] because 11→9→4→5→1 forms a continuous path.

The problem guarantees that a valid arrangement always exists, making this a classic Eulerian Path problem in graph theory!

Input & Output

example_1.py — Basic Chain
$ Input: pairs = [[5,1],[4,5],[11,9],[9,4]]
Output: [[11,9],[9,4],[4,5],[5,1]]
💡 Note: The arrangement forms a valid chain: 11→9→4→5→1. Each pair's end value matches the next pair's start value.
example_2.py — Simple Path
$ Input: pairs = [[1,3],[3,2],[2,1]]
Output: [[1,3],[3,2],[2,1]]
💡 Note: This forms a cycle: 1→3→2→1. The arrangement is already valid as given.
example_3.py — Single Pair
$ Input: pairs = [[1,2]]
Output: [[1,2]]
💡 Note: With only one pair, the arrangement is trivially valid.

Visualization

Tap to expand
Eulerian Path: Valid Arrangement of PairsInput: [[5,1],[4,5],[11,9],[9,4]]Step 1: Model as Directed Graph11start9451endStep 2: Degree AnalysisNode | In-Deg | Out-Deg | Diff11 | 0 | 1 | +1 ← START9 | 1 | 1 | 04 | 1 | 1 | 05 | 1 | 1 | 01 | 1 | 0 | -1 ← ENDStep 3: DFS Path ConstructionDFS from node 11: 11 → 9 → 4 → 5 → 1Path uses each edge exactly once (Eulerian Path)Step 4: Result AssemblyOutput: [[11,9], [9,4], [4,5], [5,1]] ✅🎯 Key Insight: Every valid arrangement corresponds to an Eulerian path in the directed graph!
Understanding the Visualization
1
Graph Modeling
Transform pairs into directed graph with adjacency list
2
Degree Analysis
Calculate in-degree and out-degree for each node to find start
3
Path Construction
Use DFS with Hierholzer's algorithm to build Eulerian path
4
Result Assembly
Convert the node sequence back into pairs format
Key Takeaway
🎯 Key Insight: This problem is elegantly solved by recognizing it as an Eulerian Path problem. The key insight is to start from a node with out-degree > in-degree and use Hierholzer's algorithm to construct the path efficiently in O(E + V) time.

Time & Space Complexity

Time Complexity
⏱️
O(n! × n)

O(n!) to generate all permutations and O(n) to validate each permutation

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for storing the current permutation and recursion stack

n
2n
Linearithmic Space

Constraints

  • 1 ≤ pairs.length ≤ 105
  • pairs[i].length == 2
  • 0 ≤ starti, endi ≤ 109
  • starti ≠ endi
  • No two pairs are identical
  • There exists a valid arrangement of pairs
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
24.5K Views
Medium Frequency
~25 min Avg. Time
890 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