Given two arrays a[] and b[] of the same length, containing the same values but in different orders (with no duplicates).
The task is to make b[] identical to a[] using the minimum number of swaps.
Examples:
Input: a[] = [3, 6, 4, 8], b[] = [4, 6, 8, 3]
Output: 2
Explanation: To make b[] identical to a[], we need 2 swaps:
1. Swap 4 with 8, resulting in b[] = [8, 6, 4, 3].
2. Swap 8 with 3, resulting in b[] = [3, 6, 4, 8].Input: a[] = [1, 2, 3], b[] = [1, 3, 2]
Output: 1
Explanation: To make b[] identical to a[], we need 1 swap:
1. Swap 3 with 2, resulting in b[] = [1, 2, 3].Input: a[] = [5, 2, 6, 9], b[] = [5, 2, 6, 9]
Output: 0
Explanation: b[] is already identical to a[], so no swaps are needed.
Table of Content
[Naive Approach] Using Nested Loops - O(n^2) Time and O(1) Space
The idea is compare each element a[i] and if the element doesn't match, search the correct one in the rest of b[] and swap. This approach ensures each mismatch is corrected one at a time, even if it takes multiple passes. Since elements are distinct and positions known, swapping will eventually align both arrays
// C++ program to make an array same to another
// with minimum number of swaps using Naive Approach
#include <bits/stdc++.h>
using namespace std;
int minSwaps(vector<int>& a, vector<int>& b) {
int swaps = 0;
int n = a.size();
// Iterate through each index
for (int i = 0; i < n; i++) {
// If elements don't match at index i
if (a[i] != b[i]) {
// Find correct element in the rest of array 'b'
for (int j = i + 1; j < n; j++) {
if (b[j] == a[i]) {
// Swap to bring correct element at position i
swap(b[i], b[j]);
swaps++;
break;
}
}
}
}
return swaps;
}
int main() {
// Define input arrays
vector<int> a = {3, 6, 4, 8};
vector<int> b = {4, 6, 8, 3};
// Output the minimum swaps required
cout << minSwaps(a, b) << endl;
return 0;
}
// Java program to make an array same to another
// with minimum number of swaps using Naive Approach
public class GfG {
public static int minSwaps(int[] a, int[] b) {
int swaps = 0;
int n = a.length;
// Iterate through each index
for (int i = 0; i < n; i++) {
// If elements don't match at index i
if (a[i] != b[i]) {
// Find correct element in the rest of array 'b'
for (int j = i + 1; j < n; j++) {
if (b[j] == a[i]) {
// Swap to bring correct element at position i
int temp = b[i];
b[i] = b[j];
b[j] = temp;
swaps++;
break;
}
}
}
}
return swaps;
}
public static void main(String[] args) {
// Define input arrays
int[] a = {3, 6, 4, 8};
int[] b = {4, 6, 8, 3};
// Output the minimum swaps required
System.out.println(minSwaps(a, b));
}
}
# Python program to make an array same to another
# with minimum number of swaps using Naive Approach
def minSwaps(a, b):
swaps = 0
n = len(a)
# Iterate through each index
for i in range(n):
# If elements don't match at index i
if a[i] != b[i]:
# Find correct element in the rest of array 'b'
for j in range(i + 1, n):
if b[j] == a[i]:
# Swap to bring correct element at position i
b[i], b[j] = b[j], b[i]
swaps += 1
break
return swaps
if __name__ == "__main__":
# Define input arrays
a = [3, 6, 4, 8]
b = [4, 6, 8, 3]
# Output the minimum swaps required
print(minSwaps(a, b))
// C# program to make an array same to another
// with minimum number of swaps using Naive Approach
using System;
public class GfG {
public static int minSwaps(int[] a, int[] b) {
int swaps = 0;
int n = a.Length;
// Iterate through each index
for (int i = 0; i < n; i++) {
// If elements don't match at index i
if (a[i] != b[i]) {
// Find correct element in the rest of array 'b'
for (int j = i + 1; j < n; j++) {
if (b[j] == a[i]) {
// Swap to bring correct element at position i
int temp = b[i];
b[i] = b[j];
b[j] = temp;
swaps++;
break;
}
}
}
}
return swaps;
}
public static void Main(string[] args) {
// Define input arrays
int[] a = {3, 6, 4, 8};
int[] b = {4, 6, 8, 3};
// Output the minimum swaps required
Console.WriteLine(minSwaps(a, b));
}
}
// JavaScript program to make an array same to another
// with minimum number of swaps using Naive Approach
function minSwaps(a, b) {
let swaps = 0;
let n = a.length;
// Iterate through each index
for (let i = 0; i < n; i++) {
// If elements don't match at index i
if (a[i] != b[i]) {
// Find correct element in the rest of array 'b'
for (let j = i + 1; j < n; j++) {
if (b[j] == a[i]) {
// Swap to bring correct element at position i
let temp = b[i];
b[i] = b[j];
b[j] = temp;
swaps++;
break;
}
}
}
}
return swaps;
}
// Define input arrays
let a = [3, 6, 4, 8];
let b = [4, 6, 8, 3];
// Output the minimum swaps required
console.log(minSwaps(a, b));
Output
2
Time Complexity: O(n^2), due to two nested loops for each mismatch check.
Space Complexity: O(1), as no extra space used beyond variables.
[Expected Approach] Using Hashing - O(n) Time and O(n) Space
The idea is to use a hash map for quick index lookups. First, we store the positions of elements in a in a hash map, allowing constant-time retrieval of their correct positions. Then, we iterate through b, and whenever an element is out of place, we swap it with the element at its correct position using the hashmap. This ensures that each swap moves an element closer to its target position. If a swap occurs, we recheck the current index to ensure correctness before proceeding.
// C++ program to make an array same to another
// with minimum number of swaps using Map
#include <bits/stdc++.h>
using namespace std;
int minSwaps(vector<int>& a, vector<int>& b) {
// Store index of elements in 'a'
unordered_map<int, int> pos;
int n = a.size();
// Populate the hashmap with indices of
// elements in 'a'
for(int i = 0; i < n; i++) {
pos[a[i]] = i;
}
int swaps = 0;
// Traverse array 'b' to match it with 'a'
for(int i = 0; i < n; i++) {
// If element is not in the correct
// position, swap it
if(a[i] != b[i]) {
swap(b[i], b[pos[b[i]]]);
swaps++;
// Re-evaluate current index to
// check correctness
i--;
}
}
return swaps;
}
int main() {
// Define input arrays
vector<int> a = {3, 6, 4, 8};
vector<int> b = {4, 6, 8, 3};
// Output the minimum swaps required
cout << minSwaps(a, b) << endl;
return 0;
}
// Java program to make an array same as another
// with minimum number of swaps using Map
import java.util.*;
class GfG {
// Method to find minimum swaps required
static int minSwaps(int[] a, int[] b) {
// Store index of elements in 'a'
Map<Integer, Integer> pos = new HashMap<>();
int n = a.length;
// Populate the hashmap with indices
// of elements in 'a'
for(int i = 0; i < n; i++) {
pos.put(a[i], i);
}
int swaps = 0;
// Traverse array 'b' to match it with 'a'
for(int i = 0; i < n; i++) {
// If element is not in the correct
// position, swap it
if(a[i] != b[i]) {
int temp = b[i];
b[i] = b[pos.get(b[i])];
b[pos.get(temp)] = temp;
swaps++;
// Re-evaluate current index to check
// correctness
i--;
}
}
return swaps;
}
public static void main(String[] args) {
// Define input arrays
int[] a = {3, 6, 4, 8};
int[] b = {4, 6, 8, 3};
// Output the minimum swaps required
System.out.println(minSwaps(a, b));
}
}
# Python program to make an array same as another
# with minimum number of swaps using Dictionary
def minSwaps(a, b):
# Store index of elements in 'a'
pos = {a[i]: i for i in range(len(a))}
swaps = 0
i = 0
n = len(a)
while i < n:
# If element is not in the correct position, swap it
if a[i] != b[i]:
swap_idx = pos[b[i]]
# Swap elements in 'b'
b[i], b[swap_idx] = b[swap_idx], b[i]
swaps += 1
# Re-evaluate current index to check correctness
i -= 1
i += 1
return swaps
if __name__ == "__main__":
# Define input arrays
a = [3, 6, 4, 8]
b = [4, 6, 8, 3]
# Output the minimum swaps required
print(minSwaps(a, b))
// C# program to make an array same as another
// with minimum number of swaps using Dictionary
using System;
using System.Collections.Generic;
class GfG {
// Method to find minimum swaps required
static int minSwaps(int[] a, int[] b) {
// Store index of elements in 'a'
Dictionary<int, int> pos = new Dictionary<int, int>();
int n = a.Length;
// Populate the dictionary with indices
// of elements in 'a'
for(int i = 0; i < n; i++) {
pos[a[i]] = i;
}
int swaps = 0;
// Traverse array 'b' to match it with 'a'
for(int i = 0; i < n; i++) {
// If element is not in the correct
// position, swap it
if(a[i] != b[i]) {
int temp = b[i];
b[i] = b[pos[b[i]]];
b[pos[temp]] = temp;
swaps++;
// Re-evaluate current index to check
// correctness
i--;
}
}
return swaps;
}
public static void Main(string[] args) {
// Define input arrays
int[] a = {3, 6, 4, 8};
int[] b = {4, 6, 8, 3};
// Output the minimum swaps required
Console.WriteLine(minSwaps(a, b));
}
}
// JavaScript program to make an array same as another
// with minimum number of swaps using Map
function minSwaps(a, b) {
// Store index of elements in 'a'
let pos = new Map();
for (let i = 0; i < a.length; i++) {
pos.set(a[i], i);
}
let swaps = 0;
let i = 0;
let n = a.length;
while (i < n) {
// If element is not in the correct position, swap it
if (a[i] !== b[i]) {
let swapIdx = pos.get(b[i]);
// Swap elements in 'b'
[b[i], b[swapIdx]] = [b[swapIdx], b[i]];
swaps++;
// Re-evaluate current index to check correctness
i--;
}
i++;
}
return swaps;
}
// Define input arrays
let a = [3, 6, 4, 8];
let b = [4, 6, 8, 3];
// Output the minimum swaps required
console.log(minSwaps(a, b));
Output:
2Time Complexity: O(n), as each element is processed once, and swaps are performed in constant time.
Space Complexity: O(n), due to hash map is used to store element indices, requiring additional space.