Javatpoint Logo
Javatpoint Logo

Couple Holding Hands Problem in Java

It is very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve couple holding hands problem in Java with different approaches and logic. Also, we will create Java programs for the same.

Couple Holding Hands Problem in Java

Problem Statement

In 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:

Input: couple = [4, 1, 5, 2]

Output: 1

Explanation: Here, we required only one swap (1 with 5) to make couples, we get [4, 5, 1, 2]. Here, the order of people does not matter. For example, [5, 4, 2, 1] and [4, 5, 1, 2] are the same.

Example 2:

Input: couple = [1, 2, 8, 9]

Output: 0

Explanation: Here, no need to exchange seats, all couples already holds each other's hands.

Solution to the Problem

In 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 Decomposition

The permutation graph theory privileges a given a pair of permutation say G1: (W, X, W, Y, W, Z, Y) and G2: (X, Y, Z, W, W, Y, W). By connecting the edges from G1 to G2, we can represent the pair of permutation by a graph. Based on observation, if at any position [i], we have G1[i]=G2[i], then the vertex should have a self-connected edge.

Couple Holding Hands Problem in Java

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 edge-1. Therefore, the total number of swaps will be ∑(Ck-1), where C1, C2, C3, …. Ck 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).

Steps

First, 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 row.length/2.

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 row.length/2-connectedNumber .

CoupleHoldingHands1.java

Output:

1

Solution 2: Greedy Approach

If 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.

CoupleHoldingHands2.java

Output:

2

Complexity

The time and space complexity of all the above approaches is O(n), where n is the number of couples.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA