Given an array of 2 * N positive integers where each array element lies between 1 to N and appears exactly twice in the array. The task is to find the minimum number of adjacent swaps required to arrange all similar array elements together.

**Note**: It is not necessary that the final array (after performing swaps) should be sorted.

**Examples:**

Input: arr[] = { 1, 2, 3, 3, 1, 2 }

Output: 5

After first swapping, array will be arr[] = { 1, 2, 3, 1, 3, 2 },

after second arr[] = { 1, 2, 1, 3, 3, 2 }, after third arr[] = { 1, 1, 2, 3, 3, 2 },

after fourth arr[] = { 1, 1, 2, 3, 2, 3 }, after fifth arr[] = { 1, 1, 2, 2, 3, 3 }Input: arr[] = { 1, 2, 1, 2 }

Output: 1

arr[2] can be swapped with arr[1] to get the required position.

**Approach** : This problem can be solved using greedy approach. Following are the steps :

- Keep an array
*visited[]*which tells that visited[curr_ele] is false if swap operation has not been performed on curr_ele. - Traverse through the original array and if the current array element has not been visited yet i.e.
*visited[arr[curr_ele]] = false*, set it to true and iterate over another loop starting from the current position to the end of array. - Initialize a variable count which will determine the number of swaps required to place the current element’s partner at its correct position.
- In nested loop, increment count only if the visited[curr_ele] is false (since if it is true, means curr_ele has already been placed at its correct position).
- If the current element’s partner is found in the nested loop, add up the value of count to the total answer.

Below is the implementation of above approach:

`// C++ Program to find the minimum number of ` `// adjacent swaps to arrange similar items together ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find minimum swaps ` `int` `findMinimumAdjacentSwaps(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// visited array to check if value is seen already ` ` ` `bool` `visited[N + 1]; ` ` ` ` ` `int` `minimumSwaps = 0; ` ` ` `memset` `(visited, ` `false` `, ` `sizeof` `(visited)); ` ` ` ` ` `for` `(` `int` `i = 0; i < 2 * N; i++) { ` ` ` ` ` `// If the arr[i] is seen first time ` ` ` `if` `(visited[arr[i]] == ` `false` `) { ` ` ` `visited[arr[i]] = ` `true` `; ` ` ` ` ` `// stores the number of swaps required to ` ` ` `// find the correct position of current ` ` ` `// element's partner ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `j = i + 1; j < 2 * N; j++) { ` ` ` ` ` `// Increment count only if the current ` ` ` `// element has not been visited yet (if is ` ` ` `// visited, means it has already been placed ` ` ` `// at its correct position) ` ` ` `if` `(visited[arr[j]] == ` `false` `) ` ` ` `count++; ` ` ` ` ` `// If current element's partner is found ` ` ` `else` `if` `(arr[i] == arr[j]) ` ` ` `minimumSwaps += count; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `minimumSwaps; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 3, 1, 2 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `N /= 2; ` ` ` ` ` `cout << findMinimumAdjacentSwaps(arr, N) << endl; ` ` ` `return` `0; ` `} ` |

**Output:**

5

**Time Complexity:** O(N^{2})

## Recommended Posts:

- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum adjacent swaps to move maximum and minimum to corners
- Number of swaps to sort when only adjacent swapping allowed
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Minimum swaps required to bring all elements less than or equal to k together
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Minimum product in a grid of adjacent elements
- Minimum absolute difference of adjacent elements in a circular array
- Minimum number of swaps required to sort an array
- Minimum number of swaps required to sort an array | Set 2
- Generate permutations with only adjacent swaps allowed
- Number of ways to form an array with distinct adjacent elements
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Biggest number by arranging numbers in certain order
- Immediate smallest number after re-arranging the digits of a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.