KatInterview

770. Couples Holding Hands

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the minimum number of swaps to arrange n couples in 2n seats so that each couple sits side-by-side. The seats are arranged in pairs (0, 1), (2, 3), ..., (2n-2, 2n-1). A couple is defined by (2k, 2k+1). A person with ID p has a partner with ID p ^ 1 (bitwise XOR), which elegantly handles both even and odd p.

Solution

Couples Holding Hands in Python

class Solution:
    def minSwapsCouples(self, row: list[int]) -> int:
        n = len(row)
        # pos[person_id] = seat_index
        pos = {val: i for i, val in enumerate(row)}
        
        swaps = 0
        # Iterate through the seat pairs (sofas)
        for i in range(0, n, 2):
            # Person in the first seat of the pair
            p1 = row[i]
            # The required partner for p1
            partner = p1 ^ 1
            
            # If the partner is already in the second seat, this couple is fine.
            if row[i + 1] == partner:
                continue
            
            # If not, a swap is needed.
            swaps += 1
            
            # Person currently in the partner's seat
            p2 = row[i + 1]
            # Find the actual position of the partner
            partner_pos = pos[partner]
            
            # Perform the swap in the row array
            row[i + 1], row[partner_pos] = row[partner_pos], row[i + 1]
            
            # Update the positions in the pos map for the two swapped people
            pos[p2] = partner_pos
            pos[partner] = i + 1
            
        return swaps

Couples Holding Hands in Java

class Solution {
    public int minSwapsCouples(int[] row) {
        int n = row.length;
        // pos[person_id] = seat_index
        int[] pos = new int[n];
        for (int i = 0; i < n; i++) {
            pos[row[i]] = i;
        }
        
        int swaps = 0;
        // Iterate through the seat pairs (sofas)
        for (int i = 0; i < n; i += 2) {
            // Person in the first seat of the pair
            int p1 = row[i];
            // The required partner for p1
            int partner = p1 ^ 1;
            
            // If the partner is already in the second seat, this couple is fine.
            if (row[i + 1] != partner) {
                // If not, a swap is needed.
                swaps++;
                
                // Person currently in the partner's seat
                int p2 = row[i + 1];
                // Find the actual position of the partner
                int partnerPos = pos[partner];
                
                // Perform the swap in the row array
                // First, update the row
                row[i + 1] = partner;
                row[partnerPos] = p2;
                
                // Then, update the positions in the pos map for the two swapped people
                pos[partner] = i + 1;
                pos[p2] = partnerPos;
            }
        }
        return swaps;
    }
}

Couples Holding Hands in C++

class Solution {
public:
    int minSwapsCouples(vector<int>& row) {
        int n = row.size();
        // pos[person_id] = seat_index
        vector<int> pos(n);
        for (int i = 0; i < n; ++i) {
            pos[row[i]] = i;
        }
        
        int swaps = 0;
        // Iterate through the seat pairs (sofas)
        for (int i = 0; i < n; i += 2) {
            // Person in the first seat of the pair
            int p1 = row[i];
            // The required partner for p1
            int partner = p1 ^ 1;
            
            // If the partner is already in the second seat, this couple is fine.
            if (row[i + 1] != partner) {
                // If not, a swap is needed.
                swaps++;
                
                // Person currently in the partner's seat
                int p2 = row[i + 1];
                // Find the actual position of the partner
                int partner_pos = pos[partner];
                
                // Perform the swap in the row array
                row[i + 1] = partner;
                row[partner_pos] = p2;
                
                // Update the positions in the pos map for the two swapped people
                pos[partner] = i + 1;
                pos[p2] = partner_pos;
            }
        }
        return swaps;
    }
};

Couples Holding Hands in TypeScript

function minSwapsCouples(row: number[]): number {
    const n = row.length;
    // pos[person_id] = seat_index
    const pos: number[] = new Array(n);
    for (let i = 0; i < n; i++) {
        pos[row[i]] = i;
    }

    let swaps = 0;
    // Iterate through the seat pairs (sofas)
    for (let i = 0; i < n; i += 2) {
        // Person in the first seat of the pair
        const p1 = row[i];
        // The required partner for p1
        const partner = p1 ^ 1;

        // If the partner is already in the second seat, this couple is fine.
        if (row[i + 1] !== partner) {
            // If not, a swap is needed.
            swaps++;
            
            // Person currently in the partner's seat
            const p2 = row[i + 1];
            // Find the actual position of the partner
            const partnerPos = pos[partner];

            // Perform the swap in the row array
            row[i + 1] = partner;
            row[partnerPos] = p2;

            // Update the positions in the pos map for the two swapped people
            pos[partner] = i + 1;
            pos[p2] = partnerPos;
        }
    }
    return swaps;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.