## Couple Holding Hands Problem in JavaIt is very interesting problem frequently asked in interviews of top IT companies like ## Problem StatementIn this problem, we have given n number of couples sitting on 2n number of seats (arranged in row) and wants to hold each other's hands. We have to find the minimum number of swaps (exchange of seats) in such a way every couple can sit side by side. We can choose any two people for swap and they can stand up and exchange their seats. ## Note: The array must be of even length, otherwise one person will be left out of being a couple.Let's understand it through examples. ## Example 1:
## Example 2:
## Solution to the ProblemIn order to solve the problem, we will define an integer array row. Array with index represent the couple sitting on i-th seat. People and seats are represented by integers from 0 to 2N-1. The couples are numbered in order, the first pair is (0, 1), the second pair is (2, 3), and so on, and the last pair is (2N- 2, 2N-1). The initial seat row[i] of these couples is determined by the person who initially sat in the i-th seat. The problem can be solved in many ways by using union-find, greedy, or cycle decomposition of permutation graph. Let's discuss each in detail. ## Solution 1: Using Permutation Graph and Graph DecompositionThe permutation graph theory privileges a given a pair of permutation say In order to transform the G2 into G1, we required to perform swapping so that the edges between two vertices can be removed. In simple words, make the vertices as self-connected edges. If we observe any cycle in the graph, the minimal step to remove all non-self-connected edges for a cycle is _{1}, C_{2}, C_{3}, …. C_{k} are the length of the connected components in the graph. We can also minimize the total number of swaps by decomposing the graph into different cycles so that each cycle become small as possible (Because the number of edges is fixed, but if the more cycles we ended with, the more times #swap is minus by 1). ## StepsFirst, determine the number of connected parts in the graph. For the same we have used a variable named connectedNumber. Suppose, all the couples are sit together, in such a case, the number of connected parts is The variable connectedNumber will incremented by 1 every time when we do a right swap to make a couple. Do the swapping process until the variable connectedNumber becomes equal to row.length/2. So the answer of this problem is
1 ## Solution 2: Greedy ApproachIf we look carefully at the graph (above) there is a greedy approach. There is only one way to decompose the graph in such a way that any swap that helps match at least one couple is a greedy action. In this approach, we use a HashMap that will compute the position of each person. After that, loop over the row, again. If we found a pair of people do not match, we find the corresponding match is in the position and swap them. We keep performing the greedy action and count the number of swaps.
2 ## ComplexityThe time and space complexity of all the above approaches is |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]