The Wedding Reception Seating Challenge

Imagine you're organizing a wedding reception where n couples want to sit together and hold hands during the ceremony. Unfortunately, the guests have already taken their seats randomly in a row of 2n chairs, and now you need to help them rearrange so every couple can sit side by side!

You're given an integer array row where row[i] represents the ID of the person sitting in the i-th seat. The couples are numbered systematically: couple 1 consists of persons (0, 1), couple 2 consists of persons (2, 3), and so on, with the last couple being (2n-2, 2n-1).

Your goal is to find the minimum number of swaps needed so that every couple sits together. In each swap, you can choose any two people and have them exchange seats.

Example: If we have [0, 2, 1, 3], person 0 needs to sit next to person 1, and person 2 needs to sit next to person 3. We can swap positions 1 and 2 to get [0, 1, 2, 3] with just 1 swap!

Input & Output

example_1.py โ€” Simple Case
$ Input: [0, 2, 1, 3]
โ€บ Output: 1
๐Ÿ’ก Note: We need to swap person 2 and person 1. Person 0 wants to sit with person 1 (couple 0), and person 2 wants to sit with person 3 (couple 1). After swapping positions 1 and 2, we get [0,1,2,3] where both couples are together.
example_2.py โ€” No Swaps Needed
$ Input: [3, 2, 0, 1]
โ€บ Output: 0
๐Ÿ’ก Note: Couples are already sitting together: (3,2) form couple 1 and (0,1) form couple 0. No swaps needed since 3//2 = 2//2 = 1 and 0//2 = 1//2 = 0.
example_3.py โ€” Complex Cycle
$ Input: [1, 4, 2, 5, 3, 0]
โ€บ Output: 2
๐Ÿ’ก Note: Multiple couples are misplaced. Person 1 needs person 0, person 4 needs person 5, etc. This creates a cycle that requires 2 swaps to resolve: swap to get couples (0,1), (2,3), (4,5) sitting together.

Visualization

Tap to expand
Wedding Reception Seating Challenge02Not a couple!13Swap partners01Couple 0 โœ“23Couple 1 โœ“Algorithm Steps1. Track position of each person2. For each seat pair, check if couple3. If not, find partner using XOR: p^14. Swap partner into correct position5. Update position map and continueKey Insight: Partner Formulapartner = person XOR 1Works because couples are consecutive: (0,1), (2,3), (4,5)...
Understanding the Visualization
1
Survey the Room
Walk around and note where each person is sitting, creating a mental map of positions
2
Check Each Table
Go to each pair of seats and see if the people sitting there are actually a couple
3
Find the Partner
If they're not a couple, locate where the correct partner is sitting using your position map
4
Make the Swap
Politely ask the misplaced person to swap seats with the correct partner
5
Update Your Map
Update your mental map of where everyone is now sitting after the swap
Key Takeaway
๐ŸŽฏ Key Insight: The optimal solution treats couple arrangement as a position-swapping problem. By maintaining a position map and using the XOR trick to find partners, we can solve this in O(n) time with minimal swaps!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n!)

We may need to try all possible arrangements in worst case, leading to factorial complexity

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth can go up to n levels

n
2n
โšก Linearithmic Space

Constraints

  • 2n == row.length
  • 2 <= n <= 30
  • n is even
  • 0 <= row[i] < 2n
  • All the values of row are unique
  • The couples are numbered consecutively starting from 0
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
73.9K Views
Medium Frequency
~25 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