
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find minimum number of swaps needed to arrange all pair of socks together in C++
Suppose we have a list of numbers called row and this is representing socks lying in a row. They are not sorted, but we want to rearrange them so that each pair of socks are side by side like (0, 1), (2, 3), (4, 5), and so on. We have to find the minimum number of swaps required to rearrange them.
So, if the input is like row = [0, 5, 6, 2, 1, 3, 7, 4], then the output will be 2, as the row orders are
[0, 5, 6, 2, 1, 3, 7, 4]
[0, 1, 6, 2, 5, 3, 7, 4]
[0, 1, 3, 2, 5, 6, 7, 4]
[0, 1, 3, 2, 5, 4, 7, 6]
To solve this, we will follow these steps −
Define an array p and another array sz
Define a function find(), this will take u,
return (if p[u] is same as u, then u, otherwise find(p[u]) and p[u] := find(p[u]))
Define a function join(), this will take u, v,
pu := find((u), pv := find(v))
if pu is same as pv, then −
return
if sz[pu] >= sz[pv], then −
p[pv] := pu
sz[pu] := sz[pu] + sz[pv]
Otherwise
p[pu] := pv
sz[pv] := sz[pv] + sz[pu]
From the main method do the following −
n := size of arr
p := an array of size n
for initialize i := 0, when i < n, update (increase i by 1), do −
p[i] := i
sz := an array of size n and fill with 1
for initialize i := 0, when i < n, update (increase i by 1), do −
u := arr[i/2] / 2
v := arr[(i/2) OR 1] / 2
join(u, v)
ans := 0
for initialize i := 0, when i < n, update (increase i by 1), do −
if find(i) is same as i, then −
ans := ans + sz[i] − 1
return ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; vector<int> p, sz; int find(int u) { return p[u] == u ? u : p[u] = find(p[u]); } void join(int u, int v) { int pu = find(u), pv = find(v); if (pu == pv) return; if (sz[pu] >= sz[pv]) { p[pv] = pu; sz[pu] += sz[pv]; }else { p[pu] = pv; sz[pv] += sz[pu]; } } int solve(vector<int>& arr) { int n = arr.size() / 2; p = vector<int>(n); for (int i = 0; i < n; ++i) p[i] = i; sz = vector<int>(n, 1); for (int i = 0; i < n; ++i) { int u = arr[i << 1] / 2; int v = arr[i << 1 | 1] / 2; join(u, v); } int ans = 0; for (int i = 0; i < n; ++i) if (find(i) == i) ans += sz[i] − 1; return ans; } int main(){ vector<int> v = {0, 5, 6, 2, 1, 3, 7, 4}; cout << solve(v); }
Input
{2, 4, 6, 3}
Output
23
- Related Articles
- Program to find minimum swaps needed to group all 1s together in Python
- Program to count number of swaps required to group all 1s together in Python
- Program to find minimum swaps to arrange a binary grid using Python
- Minimum Swaps to Group All 1's Together in Python
- Minimum Swaps required to group all 1’s together in C++
- Program to find minimum number of rocketships needed for rescue in Python
- Minimum swaps required to bring all elements less than or equal to k together in C++
- C++ program to find minimum number of operations needed to make all cells at r row c columns black
- C++ program to find minimum number of steps needed to move from start to end
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to find how many swaps needed to sort an array in Python
- Program to find minimum amount needed to be paid all good performers in Python
- C++ program to find minimum number of punches are needed to make way to reach target
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to find minimum how many operations needed to make number 0
