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.

&nbsp;
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.


&nbsp;
Constraints:


	2n == row.length
	2 &lt;= n &lt;= 30
	n is even.
	0 &lt;= row[i] &lt; 2n
	All the elements of row are unique.


class Solution:
    def minSwapsCouples(self, row: List[int]) -> int:
        count = 0
        for i in range(0, len(row)-2, 2):
            idx = i+1
            if(row[i]%2 == 0):
                n = (row[i] + 2)//2
                newNo = 2*n - 1
            else:
                n = (row[i] + 1) //2;
                newNo = 2*n - 2
            for j in range(i, len(row)):
                if row[j] == newNo:
                    idx = j
                    break
            if idx != i+1:
                count += 1
            row[idx], row[i+1] = row[i+1], row[idx]
        return count
		```
class Solution {
    public int minSwapsCouples(int[] row) { // Union -Find pairs for 2
        Map<Integer,Integer> parents=new HashMap<>();
        int count=0;
        for(int i=0;i<row.length;i+=2){
            int parent=Math.min(row[i],row[i+1]);
            int child=Math.max(row[i],row[i+1]);
            parents.put(parent,child);
        }
        for(int i=0;i<row.length;i+=2){
            if((parents.containsKey(i) && parents.get(i)==(i+1)) 
               || (parents.containsKey(i-1) && parents.get(i-1)==i))
                continue;
            count+=1;
            int curChild=parents.get(i);
            int correctChildsChild = parents.get(i+1);
            parents.remove(i+1); // remove mapping of 1 in eg) //0,2 ; 1,3
            parents.put(Math.min(curChild,correctChildsChild),Math.max(curChild,correctChildsChild));
            // add mapping 2->7 , also place smaller number as parent for eg)//0,4 ; 1,3
            parents.put(i,i+1);
        }
        return count;
    }
}
class Solution {
public:
	int minSwapsCouples(vector<int>& row) {
		int cnt=0,n=row.size();
		// first of all making row element equal  ex for [0,1],[2,3] --> [0,0] ,[2,2]
		for(auto &x : row){
			if(x&1){
				x--;
			}
		}

		for( int i=0; i<n; i+=2){
			int ele=row[i];
			int j=i+1;
			// for every num find location of equal one,
			while(j<n and row[j]!=ele) j++;
			if(j!=i+1){
						// not in same couch then cnt++ , and swap
				swap(row[j], row[i+1]);
				cnt++;
			}
		}
		return cnt;
	}
};
/**
 * @param {number[]} row
 * @return {number}
 */
var minSwapsCouples = function(row) {
    let currentPositions = [];

    for(let i=0;i<row.length;i++){
        currentPositions[row[i]]=i;
    }

    let swapCount = 0;

     for(let j=0;j<row.length;j+=2) // Looping through the couches
         {

    let leftPartner = row[j];
    let correctRightPartner = getPartner(leftPartner);
        if(row[j+1] !== correctRightPartner)
        {
            console.log("unhappy");
            //Swap Positions of positions - Bring the Correct Right Partner in the Couch
            let tempValue = row[j+1];
            row[j+1]=correctRightPartner;
            let tempPosition = currentPositions[correctRightPartner];
            row[tempPosition]=tempValue;

            //Correct the Hash Table keeping the currentPositions of each Person.
            currentPositions[correctRightPartner]=j+1;
            currentPositions[tempValue]=tempPosition;

            swapCount+=1;
        }
        else
        {

        console.log("happy");
        }
    }

    console.log("currentPositions", currentPositions,"Correct Row", row);

    return swapCount;

};
function getPartner(x){
    if(x%2 ===0)
    {
        return x+1;
    }
    else
        return x-1;
}
Couples Holding Hands