LeetCode Problem

How to Solve Couples Holding Hands

In this problem, you need to minimize the number of swaps required to make couples sit together. You can treat the problem as a graph traversal task, leveraging depth-first search (DFS) to model couples' connections. The key is efficiently identifying the minimal swap solution based on their seating arrangement.

GhostInterview Help

Need help with Couples Holding Hands without spending extra time grinding it?

GhostInterview can read Couples Holding Hands from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #765Graph traversal with depth-first searchReviewed 2026-03-07
Difficulty
Hard
Primary pattern
Graph traversal with depth-first search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

In this problem, you need to minimize the number of swaps required to make couples sit together. You can treat the problem as a graph traversal task, leveraging depth-first search (DFS) to model couples' connections. The key is efficiently identifying the minimal swap solution based on their seating arrangement.

Problem Statement

In the Couples Holding Hands problem, n couples are seated in a row of 2n seats. The people and seats are represented by an integer array row, where each person has an ID. Couples are numbered sequentially, with the first couple being (0, 1), the second couple being (2, 3), and so on. Your task is to return the minimum number of swaps required to ensure that each couple is seated side by side.

A swap involves choosing two people, having them stand up, and then swapping their seats. The goal is to determine the fewest swaps needed so that every couple is seated next to each other. For instance, in the case of row = [0, 2, 1, 3], only one swap is needed to align the couples properly.

Examples

Example 1

Input: row = [0,2,1,3]

Output: 1

We only need to swap the second (row[1]) and third (row[2]) person.

Example 2

Input: row = [3,2,0,1]

Output: 0

All couples are already seated side by side.

Constraints

  • 2n == row.length
  • 2 <= n <= 30
  • n is even.
  • 0 <= row[i] < 2n
  • All the elements of row are unique.

Solution Approach

Graph Traversal with DFS

The problem can be viewed as a graph, where each seat and person is a node. For each couple, create an edge between the two seats they should occupy. The minimal swaps needed to arrange the couples next to each other can be identified by using depth-first search (DFS) to find connected components. Each component will represent a couple that can be rearranged with one swap.

Greedy Approach for Pairing

A greedy approach can be used where each time you find two people who are not seated next to each other, you swap them. By traversing through the row and making local optimal swaps (minimizing the number of misplacements), you can solve the problem efficiently.

Union-Find Approach

Union-find (also known as disjoint set union, DSU) can be used to manage and track the seating arrangement. By treating each pair as a group and applying union-find, you can identify which people are in the same group and determine how to swap them until all pairs are together.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the approach chosen. For DFS, it is O(n), where n is the number of people. The space complexity is O(n) due to the need to store the connections between individuals. Union-find has a time complexity of O(α(n)) for each union and find operation, where α is the inverse Ackermann function, and a space complexity of O(n).

What Interviewers Usually Probe

  • Look for understanding of graph traversal and the ability to apply DFS in a seating arrangement problem.
  • Assess the candidate's ability to use greedy algorithms in a constrained optimization scenario.
  • Evaluate knowledge of union-find and its application to solve seating problems efficiently.

Common Pitfalls or Variants

Common pitfalls

  • Failing to identify the problem as a graph traversal task can lead to inefficient solutions.
  • Not considering all possible swap configurations, leading to suboptimal results.
  • Using the wrong data structures, such as simple arrays instead of graph-related structures, can complicate the solution.

Follow-up variants

  • Instead of using swaps, solve the problem using only adjacent seat moves.
  • Modify the problem to consider a situation where multiple people can swap at once.
  • Instead of ensuring only couples are next to each other, generalize to a larger group problem, where people need to be grouped with others of specific criteria.

How GhostInterview Helps

  • GhostInterview helps by breaking down the problem into graph-related subproblems, ensuring clarity on how to minimize swaps with DFS.
  • It encourages testing various graph traversal strategies and their applications, guiding towards the most efficient solution.
  • With its comprehensive solution approaches, GhostInterview aids in understanding multiple methods to solve the problem, boosting confidence and efficiency in interviews.

Topic Pages

FAQ

What is the core pattern used in solving the Couples Holding Hands problem?

The problem is primarily solved through graph traversal, especially using depth-first search (DFS) to model the couples as connected components in the graph.

How do swaps work in the Couples Holding Hands problem?

A swap involves choosing two people who are not sitting next to each other and swapping their positions until all couples are seated together.

Can a greedy approach be applied to the Couples Holding Hands problem?

Yes, a greedy approach can work by making local optimal swaps at each step, such as swapping people until each couple is seated next to each other.

Is Union-Find a good approach for the Couples Holding Hands problem?

Yes, Union-Find is effective for tracking and grouping people who should sit together, efficiently handling swaps needed to group each couple.

What is the time complexity of the Couples Holding Hands problem?

The time complexity depends on the approach: DFS is O(n), and Union-Find has a time complexity of O(α(n)) for each union-find operation.

GhostInterview Solver

Need direct help with Couples Holding Hands instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Couples Holding Hands from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.