Couples Holding Hands LeetCode solution in C++
1class Solution {2public:3int minSwapsCouples(vector<int> &row) {4int cnt = 0, n = row.size();5// first of all making row element equal ex for [0,1],[2,3] --> [0,0]6// ,[2,2]7for (auto &x : row) {8if (x & 1) {9x--;10}11}1213for (int i = 0; i < n; i += 2) {14int ele = row[i];15int j = i + 1;16// for every num find location of equal one,17while (j < n and row[j] != ele) j++;18if (j != i + 1) {19// not in same couch then cnt++ , and swap20swap(row[j], row[i + 1]);21cnt++;22}23}24return cnt;25}26};