Restore the Array From Adjacent Pairs - Problem

Imagine you had a perfect sequence of unique numbers, but somehow it got scrambled! ๐Ÿ˜ฑ All you have left are clues about which numbers were neighbors in the original array.

You're given a 2D array adjacentPairs where each adjacentPairs[i] = [u, v] tells you that numbers u and v were sitting right next to each other in the original array. Your mission? Restore the original sequence!

๐Ÿ” The Challenge: The pairs can be given in any order, and each pair [u, v] could represent either u followed by v OR v followed by u in the original array.

Example: If adjacentPairs = [[2,1],[3,4],[3,2]], the original array could be [1,2,3,4] because:
โ€ข 1 and 2 are adjacent
โ€ข 2 and 3 are adjacent
โ€ข 3 and 4 are adjacent

Think of it like solving a jigsaw puzzle where you only know which pieces touch each other!

Input & Output

example_1.py โ€” Basic Chain
$ Input: adjacentPairs = [[2,1],[3,4],[3,2]]
โ€บ Output: [1,2,3,4]
๐Ÿ’ก Note: The pairs tell us: 2โ†”1, 3โ†”4, 3โ†”2. Connecting these gives us the chain 1-2-3-4, so the original array is [1,2,3,4].
example_2.py โ€” Reverse Order
$ Input: adjacentPairs = [[4,3],[1,2],[3,2]]
โ€บ Output: [4,3,2,1]
๐Ÿ’ก Note: Same pairs as example 1 but in different order. We can reconstruct as [4,3,2,1] (or [1,2,3,4]). Both are valid since we can start from either end.
example_3.py โ€” Two Elements
$ Input: adjacentPairs = [[100,200]]
โ€บ Output: [100,200]
๐Ÿ’ก Note: With only one pair [100,200], the original array must be [100,200] or [200,100]. Both numbers are endpoints since each appears in only one pair.

Constraints

  • nums.length == n
  • adjacentPairs.length == n - 1
  • adjacentPairs[i].length == 2
  • -105 โ‰ค adjacentPairs[i][0], adjacentPairs[i][1] โ‰ค 105
  • There exists some nums that satisfies adjacentPairs
  • All elements in nums are unique

Visualization

Tap to expand
๐Ÿ”— Broken Chain Necklace AnalogyStep 1: Scattered Chain LinksWe found these connected pairs: [2โ†”1], [3โ†”4], [3โ†”2]1234Scattered piecesStep 2: Find the EndpointLink 1 and Link 4 each connect to only one other link - they're endpoints!1START234ENDStep 3: Rebuild the ChainFollow connections: 1โ†’2โ†’3โ†’41234Complete Chain!๐ŸŽฏ Key Algorithm Insights๐Ÿ” Endpoint Detection:Find nodes with degree 1๐Ÿšถ Chain Walking:Follow connections while avoiding backtrackโšก Efficiency:O(n) time, single traversal needed
Understanding the Visualization
1
Identify the Pieces
Each adjacent pair represents two chain links that were connected
2
Find the Starting Point
Look for a link with only one connection - this must be an endpoint
3
Follow the Chain
Starting from the endpoint, follow each connection to the next link
4
Rebuild Complete
Continue until you reach the other endpoint with no more connections
Key Takeaway
๐ŸŽฏ Key Insight: In any linear chain, exactly two nodes have degree 1 (the endpoints). Start from either endpoint and follow the unique path through the chain to reconstruct the original sequence efficiently in O(n) time.
Asked in
Meta 15 Google 12 Amazon 8 Microsoft 6
89.0K Views
Medium Frequency
~15 min Avg. Time
1.8K 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