765. Couples Holding Hands LeetCode Solution
In this guide, you will get 765. Couples Holding Hands LeetCode Solution with the best time and space complexity. The solution to Couples Holding Hands problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Couples Holding Hands solution in C++
- Couples Holding Hands solution in Java
- Couples Holding Hands solution in Python
- Additional Resources

Problem Statement of Couples Holding Hands
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.length
2 <= n <= 30
n is even.
0 <= row[i] < 2n
All the elements of row are unique.
Complexity Analysis
- Time Complexity:
- Space Complexity:
765. Couples Holding Hands LeetCode Solution in C++
class UnionFind {
public:
UnionFind(int n) : count(n), id(n), rank(n) {
iota(id.begin(), id.end(), 0);
}
void unionByRank(int u, int v) {
const int i = find(u);
const int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j]) {
id[i] = j;
} else if (rank[i] > rank[j]) {
id[j] = i;
} else {
id[i] = j;
++rank[j];
}
--count;
}
int getCount() const {
return count;
}
private:
int count;
vector<int> id;
vector<int> rank;
int find(int u) {
return id[u] == u ? u : id[u] = find(id[u]);
}
};
class Solution {
public:
int minSwapsCouples(vector<int>& row) {
const int n = row.size() / 2;
UnionFind uf(n);
for (int i = 0; i < n; ++i) {
const int a = row[2 * i];
const int b = row[2 * i + 1];
uf.unionByRank(a / 2, b / 2);
}
return n - uf.getCount();
}
};
/* code provided by PROGIEZ */
765. Couples Holding Hands LeetCode Solution in Java
class UnionFind {
public UnionFind(int n) {
count = n;
id = new int[n];
rank = new int[n];
for (int i = 0; i < n; ++i)
id[i] = i;
}
public void unionByRank(int u, int v) {
final int i = find(u);
final int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j]) {
id[i] = j;
} else if (rank[i] > rank[j]) {
id[j] = i;
} else {
id[i] = j;
++rank[j];
}
--count;
}
public int getCount() {
return count;
}
private int count;
private int[] id;
private int[] rank;
private int find(int u) {
return id[u] == u ? u : (id[u] = find(id[u]));
}
}
class Solution {
public int minSwapsCouples(int[] row) {
final int n = row.length / 2;
UnionFind uf = new UnionFind(n);
for (int i = 0; i < n; ++i) {
final int a = row[2 * i];
final int b = row[2 * i + 1];
uf.unionByRank(a / 2, b / 2);
}
return n - uf.getCount();
}
}
// code provided by PROGIEZ
765. Couples Holding Hands LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.