Tap here to switch tabs
Problem
Submissions

Problem: Couples Holding Hands

Couples Holding Hands gives a row of people from numbered couples and asks for the minimum swaps needed to seat every couple side by side.

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

Couples Holding Hands gives a row of people from numbered couples and asks for the minimum swaps needed to seat every couple side by side.

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.