> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Couples Holding Hands Python Solution

> Tested Python solution for LeetCode 765 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 765, [Hard](/catalog/hard). Topics: [Greedy](/catalog/topics/greedy), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Union-Find](/catalog/topics/union-find), [Graph Theory](/catalog/topics/graph-theory). [View on LeetCode](https://leetcode.com/problems/couples-holding-hands/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 765   # by problem number
lcpy gen -s couples_holding_hands   # by problem name
```

## Problem

There are `n` couples sitting in `2n` seats arranged in a row and want to hold hands.

The people and seats are represented by an integer array `row` where `row[i]` is the ID of the person sitting in the `i<sup>th</sup>` seat. The couples are numbered in order, the first couple being `(0, 1)`, the second couple being `(2, 3)`, and so on with the last couple being `(2n - 2, 2n - 1)`.

Return *the minimum number of swaps so that every couple is sitting side by side*. A swap consists of choosing any two people, then they stand up and switch seats.

### Examples

```
Input: row = [0,2,1,3]
Output: 1
Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
```

```
Input: row = [3,2,0,1]
Output: 0
Explanation: All couples are already seated side by side.
```

### Constraints

* 2n == row\.length
* 2 \<= n \<= 30
* 0 \<= row\[i] \< 2n
* All the elements of row are unique.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/couples_holding_hands/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/couples_holding_hands/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def min_swaps_couples(self, row: list[int]) -> int:
        arr = list(row)
        pos = {person: i for i, person in enumerate(arr)}
        swaps = 0
        for i in range(0, len(arr), 2):
            partner = arr[i] ^ 1
            if arr[i + 1] != partner:
                j = pos[partner]
                other = arr[i + 1]
                arr[i + 1], arr[j] = partner, other
                pos[partner] = i + 1
                pos[other] = j
                swaps += 1
        return swaps
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.