Minimum swaps of similar indexed elements required to make all elements of one of the two given arrays equal
Last Updated :
22 Apr, 2021
Given two arrays A[] and B[] of size N, the task is to count the minimum number of swaps of similar indexed elements required to make one of the two given arrays equal. If it is not possible to make all elements of an array equal, then print -1.
Examples:
Input: A[] = {3, 4, 1, 3, 1, 3, 4}, B[] = {1, 3, 3, 1, 3, 4, 3}
Output: 3
Explanation:
Swapping (A[0], B[0]) modifies A[] to {1, 4, 1, 3, 1, 3, 4} and B[] to {3, 3, 3, 1, 3, 4, 3}.
Swapping (A[3], B[3]) modifies A[] to {1, 4, 1, 1, 1, 3, 4} and B[] to {3, 3, 3, 3, 3, 4, 3}.
swapping (A[5], B[5]) modifies A[] to {1, 4, 1, 1, 1, 4, 4} and B[] to {3, 3, 3, 3, 3, 3, 3}.
Since the array B[] contains only a single distinct element, the required output is 3.
Input: A[] = {1, 2, 4, 1, 6, 5, 10}, B[] = {2, 3, 5, 4, 1, 8, 2}
Output: -1
Approach: The problem can be solved using Hashing. Follow the steps below to solve the problem:
- Initialize a variable, say minSwaps, to store the minimum number of swaps required such that at least one of the array contains only a single distinct value.
- Initialize another variable, say swapsRequired, to store the count of swaps required such that at least one of the array contains only a single distinct value.
- Initialize an array, say countA[], to store the frequency of each distinct element of the array A[].
- Initialize an array, say countB[], to store the frequency of each distinct element of the array B[].
- Initialize an array, say countAB[], to store the frequency of each distinct element which is present at the same indices of A[] and B[].
- Traverse both the arrays using variable i, and check if the value of (countA[i] + countB[i] – countAB[i] == N) is true or not. If found to be true, then update swapsRequired = min(countA[i], countB[i]) – countAB[i] and update minSwaps = min(minSwaps, swapsRequired).
- Finally, print the value of minSwaps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumNumOfSwap( int A[], int B[], int N)
{
int MaxA = *max_element(A, A + N);
int MaxB = *max_element(B, B + N);
int M = max(MaxA, MaxB);
int countA[M + 1] = { 0 };
int countB[M + 1] = { 0 };
int equalAB[M + 1] = { 0 };
int swapsRequired;
int minSwaps = N;
for ( int i = 0; i < N; i++) {
countA[A[i]]++;
countB[B[i]]++;
if (A[i] == B[i]) {
equalAB[A[i]]++;
}
}
for ( int i = 1; i <= M; i++) {
if ((countA[i] + countB[i]
- equalAB[i])
== N) {
swapsRequired
= min(countA[i], countB[i])
- equalAB[i];
minSwaps = min(minSwaps,
swapsRequired);
}
}
if (minSwaps == N) {
return -1;
}
else {
return minSwaps;
}
}
int main()
{
int A[] = { 3, 4, 1, 3, 1, 3, 4 };
int B[] = { 1, 3, 3, 1, 3, 4, 3 };
int N = sizeof (A) / sizeof (A[0]);
cout << minimumNumOfSwap(A, B, N);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static int minimumNumOfSwap( int A[], int B[], int N)
{
int MaxA = Arrays.stream(A).max().getAsInt();
int MaxB = Arrays.stream(B).max().getAsInt();
int M = Math.max(MaxA, MaxB);
int [] countA = new int [M + 1 ];
int [] countB = new int [M + 1 ];
int [] equalAB = new int [M + 1 ];
int swapsRequired;
int minSwaps = N;
for ( int i = 0 ; i < N; i++)
{
countA[A[i]]++;
countB[B[i]]++;
if (A[i] == B[i])
{
equalAB[A[i]]++;
}
}
for ( int i = 1 ; i <= M; i++)
{
if ((countA[i] + countB[i]
- equalAB[i])
== N)
{
swapsRequired
= Math.min(countA[i], countB[i])
- equalAB[i];
minSwaps = Math.min(minSwaps,
swapsRequired);
}
}
if (minSwaps == N)
{
return - 1 ;
}
else
{
return minSwaps;
}
}
public static void main(String[] args)
{
int A[] = { 3 , 4 , 1 , 3 , 1 , 3 , 4 };
int B[] = { 1 , 3 , 3 , 1 , 3 , 4 , 3 };
int N = A.length;
System.out.println(minimumNumOfSwap(A, B, N));
}
}
|
Python3
def minimumNumOfSwap(A, B, N) :
MaxA = max (A)
MaxB = max (B)
M = max (MaxA, MaxB)
countA = [ 0 ] * (M + 1 )
countB = [ 0 ] * (M + 1 )
equalAB = [ 0 ] * (M + 1 )
swapsRequired = 0
minSwaps = N
for i in range (N) :
countA[A[i]] + = 1
countB[B[i]] + = 1
if (A[i] = = B[i]) :
equalAB[A[i]] + = 1
for i in range ( 1 , M + 1 ) :
if ((countA[i] + countB[i] - equalAB[i]) = = N) :
swapsRequired = min (countA[i], countB[i]) - equalAB[i]
minSwaps = min (minSwaps, swapsRequired)
if (minSwaps = = N) :
return - 1
else :
return minSwaps
A = [ 3 , 4 , 1 , 3 , 1 , 3 , 4 ]
B = [ 1 , 3 , 3 , 1 , 3 , 4 , 3 ]
N = len (A)
print (minimumNumOfSwap(A, B, N))
|
C#
using System;
using System.Linq;
class GFG{
public static int minimumNumOfSwap( int []A, int []B,
int N)
{
int MaxA = A.Max();
int MaxB = B.Max();
int M = Math.Max(MaxA, MaxB);
int [] countA = new int [M + 1];
int [] countB = new int [M + 1];
int [] equalAB = new int [M + 1];
int swapsRequired;
int minSwaps = N;
for ( int i = 0; i < N; i++)
{
countA[A[i]]++;
countB[B[i]]++;
if (A[i] == B[i])
{
equalAB[A[i]]++;
}
}
for ( int i = 1; i <= M; i++)
{
if ((countA[i] + countB[i] - equalAB[i]) == N)
{
swapsRequired = Math.Min(countA[i],
countB[i]) -
equalAB[i];
minSwaps = Math.Min(minSwaps,
swapsRequired);
}
}
if (minSwaps == N)
{
return -1;
}
else
{
return minSwaps;
}
}
public static void Main( string [] args)
{
int []A = { 3, 4, 1, 3, 1, 3, 4 };
int []B = { 1, 3, 3, 1, 3, 4, 3 };
int N = A.Length;
Console.WriteLine(minimumNumOfSwap(A, B, N));
}
}
|
Javascript
<script>
function minimumNumOfSwap(A, B, N)
{
let MaxA = 0;
for (let i = 0; i < A.length; i++)
{
MaxA = Math.max(MaxA, A[i]);
}
let MaxB = 0;
for (let i = 0; i < B.length; i++)
{
MaxB = Math.max(MaxB, B[i]);
}
let M = Math.max(MaxA, MaxB);
let countA = new Array(M + 1);
countA.fill(0);
let countB = new Array(M + 1);
countB.fill(0);
let equalAB = new Array(M + 1);
equalAB.fill(0);
let swapsRequired;
let minSwaps = N;
for (let i = 0; i < N; i++)
{
countA[A[i]]++;
countB[B[i]]++;
if (A[i] == B[i])
{
equalAB[A[i]]++;
}
}
for (let i = 1; i <= M; i++)
{
if ((countA[i] + countB[i] - equalAB[i]) == N)
{
swapsRequired = Math.min(countA[i],
countB[i]) -
equalAB[i];
minSwaps = Math.min(minSwaps,
swapsRequired);
}
}
if (minSwaps == N)
{
return -1;
}
else
{
return minSwaps;
}
}
let A = [ 3, 4, 1, 3, 1, 3, 4 ];
let B = [ 1, 3, 3, 1, 3, 4, 3 ];
let N = A.length;
document.write(minimumNumOfSwap(A, B, N));
</script>
|
Time Complexity: O(M + N), where M is the largest element in A[] and B[].
Auxiliary Space:O(M)
Share your thoughts in the comments
Please Login to comment...