Tap here to switch tabs
Problem
Submissions

Problem: Couples Holding Hands

med
30 min
Try to solve the Couples Holding Hands problem.

Statement

There are nn couples sitting in 2n2n seats arranged in a row, who want to hold hands.

The people and seats are represented by an integer array row of length 2n2n, where row[i] is the label of the person sitting in the ii-th seat. The couples are ordered sequentially, meaning the first couple is (0,1)(0, 1), the second couple is (2,3)(2, 3), and so on, up to the final couple (2n−2,2n−1)(2n - 2, 2n - 1).

Return the minimum number of swaps needed so that every couple is seated side by side. A single swap consists of choosing any two individuals and having them exchange their seats.

Constraints:

  • row.length ==2n== 2n

  • 2≤n≤302 \leq n \leq 30

  • 0≤0 \leq row[i] <2n< 2n

  • All the elements of row are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Couples Holding Hands

med
30 min
Try to solve the Couples Holding Hands problem.

Statement

There are nn couples sitting in 2n2n seats arranged in a row, who want to hold hands.

The people and seats are represented by an integer array row of length 2n2n, where row[i] is the label of the person sitting in the ii-th seat. The couples are ordered sequentially, meaning the first couple is (0,1)(0, 1), the second couple is (2,3)(2, 3), and so on, up to the final couple (2n−2,2n−1)(2n - 2, 2n - 1).

Return the minimum number of swaps needed so that every couple is seated side by side. A single swap consists of choosing any two individuals and having them exchange their seats.

Constraints:

  • row.length ==2n== 2n

  • 2≤n≤302 \leq n \leq 30

  • 0≤0 \leq row[i] <2n< 2n

  • All the elements of row are unique.