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 ith 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.
Example 1:
Input: row = [0,2,1,3] Output: 1 Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
Example 2:
Input: row = [3,2,0,1] Output: 0 Explanation: All couples are already seated side by side.
Constraints:
2n == row.length2 <= n <= 30n is even.0 <= row[i] < 2nrow are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a group of people sitting in a row where partners are separated from each other. The brute force approach involves looking at every possible way to swap people around until every partner is sitting right next to their loved one.
Here's how the algorithm would work step-by-step:
def min_swaps_couples(row_arrangement):
total_swaps_made = 0
# Iterate through every pair of seats in the row to ensure each couple sits together
for current_index in range(0, len(row_arrangement), 2):
expected_person = row_arrangement[current_index]
# Determine the unique identifier for the expected partner based on even-odd pairing rules
if expected_person % 2 == 0:
expected_partner = expected_person + 1
else:
expected_partner = expected_person - 1
if row_arrangement[current_index + 1] == expected_partner:
continue
# Search through the remaining seats to locate the correct partner who is separated
for search_index in range(current_index + 2, len(row_arrangement)):
if row_arrangement[search_index] == expected_partner:
row_arrangement[current_index + 1], row_arrangement[search_index] = row_arrangement[search_index], row_arrangement[current_index + 1]
total_swaps_made += 1
break
return total_swaps_madeInstead of trying every possible seating arrangement, we can think of the problem like connecting pairs of people who want to sit together. By looking at who is sitting where and following the chain of partners, we can figure out the minimum number of swaps needed to unite everyone.
Here's how the algorithm would work step-by-step:
def min_swaps_couples(row_of_seats):
total_swaps_count = 0
person_to_index_map = {person: index for index, person in enumerate(row_of_seats)}
# We step by twos because couples occupy pairs of adjacent seats.
for current_seat_index in range(0, len(row_of_seats), 2):
first_person = row_of_seats[current_seat_index]
expected_partner = first_person + 1 if first_person % 2 == 0 else first_person - 1
second_person_index = current_seat_index + 1
# If the expected partner is not beside the first person, we must swap.
if row_of_seats[second_person_index] != expected_partner:
partner_current_index = person_to_index_map[expected_partner]
displaced_person = row_of_seats[second_person_index]
# We update the tracking map to reflect the new positions after swapping.
row_of_seats[second_person_index], row_of_seats[partner_current_index] = row_of_seats[partner_current_index], row_of_seats[second_person_index]
person_to_index_map[expected_partner] = second_person_index
person_to_index_map[displaced_person] = partner_current_index
total_swaps_count += 1
return total_swaps_count| Case | How to Handle |
|---|---|
| Empty or null input array | Return zero swaps immediately since no seating arrangements are necessary. |
| Minimum valid input of length two | Return zero swaps because a single couple is already seated correctly by definition. |
| Maximum-sized valid inputs with thousands of elements | Use an efficient disjoint set union or greedy indexing approach that runs in linear time to avoid timeouts. |
| Couples already sitting perfectly side-by-side | Iterate through the array and find zero mismatches, resulting in a total swap count of zero. |
| Couples in reverse or completely interleaved order | Form graph cycles where the greedy pointer approach correctly calculates the minimum swaps as size minus number of connected components. |
| Invalid problem constraints such as odd array lengths | Assume valid problem constraints where the input length is always even and forms complete pairs. |
| Disjoint cycles in the swap graph | Traverse each unvisited node to resolve independent cycles separately without duplicate processing. |
| Large integer values for couple IDs | Map IDs to normalized indices using a hash map to handle arbitrarily large or non-sequential identifiers safely. |