Seat Every Couple Together with Minimum Swaps

Quick Overview

Seat Every Couple Together with Minimum Swaps. The 2n entries of row are distinct person IDs. Couples are (0,1), (2,3), and so forth.

Seat Every Couple Together with Minimum Swaps

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem The 2n entries of row are distinct person IDs. Couples are (0,1), (2,3), and so forth. Seats are grouped as consecutive pairs (0,1), (2,3), etc. One swap exchanges any two people. Return the fewest swaps needed so each seat pair contains a couple. ```cpp class Solution { public: int minSwapsCouples(vector<int>& row); }; ``` ### Examples **Example 1:** ```text row = [0,3,2,1] Output: 1 Explanation: Swap IDs 3 and 1 to obtain [0,1,2,3]. ``` **Example 2:** ```text row = [1,0,3,2] Output: 0 Explanation: Every seat pair already contains partners. ``` ### Constraints - The array has exactly 2n entries with 2 <= n <= 30. - It is a permutation of the IDs from 0 through 2n-1. ### Contract considerations Count minimum swaps; the original callable returns an integer and does not request a sequence. ```hint Read the boundary condition How many swaps does one cycle of mixed couples require? ``` ### Boundary reminders Count minimum swaps; the original callable returns an integer and does not request a sequence.

Overview: Seat Every Couple Together with Minimum Swaps. The 2n entries of row are distinct person IDs. Couples are (0,1), (2,3), and so forth.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The 2n entries of row are distinct person IDs. Couples are (0,1), (2,3), and so forth. Seats are grouped as consecutive pairs (0,1), (2,3), etc. One swap exchanges any two people. Return the fewest swaps needed so each seat pair contains a couple.

class Solution {
public:
    int minSwapsCouples(vector<int>& row);
};

Examples

Example 1:

row = [0,3,2,1]
Output: 1
Explanation: Swap IDs 3 and 1 to obtain [0,1,2,3].

Example 2:

row = [1,0,3,2]
Output: 0
Explanation: Every seat pair already contains partners.

Constraints

  • The array has exactly 2n entries with 2 <= n <= 30.
  • It is a permutation of the IDs from 0 through 2n-1.

Contract considerations

Count minimum swaps; the original callable returns an integer and does not request a sequence.

Boundary reminders

Count minimum swaps; the original callable returns an integer and does not request a sequence.