Minimum row or column swaps required to make every pair of adjacent cell of a Binary Matrix distinct
Last Updated :
23 Nov, 2021
Given a binary matrix M[][] of dimensions N x N, the task is to make every pair of adjacent cells in the same row or column of the given matrix distinct by swapping the minimum number of rows or columns.
Examples
Input: M[][] = {{0, 1, 1, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}}, N = 4
Output: 2
Explanation:
Step 1: Swapping the 2nd and 3rd rows modifies matrix to the following representation:
M[][] = { { 0, 1, 1, 0},
{ 1, 0, 0, 1},
{ 0, 1, 1, 0},
{ 1, 0, 0, 1} }
Step 1: Swapping the 1st and 2nd columns modifies matrix to the following representation:
M[][] = { { 1, 0, 1, 0},
{ 0, 1, 0, 1},
{ 1, 0, 1, 0},
{ 0, 1, 0, 1} }
Input: M[][] = {{0, 1, 1}, {1, 1, 0}, {1, 0, 0}, {1, 1, 1}}, N = 3
Output: -1
Approach: The given problem can be solved based on the following observations:
- In the desired matrix, any submatrix starting from a corner must have Bitwise XOR of all cells equal to 0.
- It can also be observed that there should be at most two types of sequences that should be present in a row or column, i.e. {0, 1, 0, 1} and {1, 0, 1, 1}. Therefore, one sequence can be generated from the other by swapping with the XOR value of that sequence with 1.
- Therefore, by making only the first column and first row according to the required format, the total swaps required will be minimized.
Follow the steps below to solve the problem:
- Traverse in the matrix M[][] and check if bitwise xor of all elements M[i][0], M[0][j], M[0][0], and M[i][j] is 1 then return -1.
- Initialize variables rowSum, colSum, rowSwap, and colSwap with 0.
- Traverse in the range [0, N-1] and increment rowSum by M[i][0], colSum by M[0][i] and increment rowSwap by 1 if M[i][0] is equal to i%2 and colSwap by 1 if M[0][i] is equal to i%2.
- If rowSum is not equal to either of N/2 or (N+1)/2 then return -1.
- If colSum is not equal to either of N/2 or (N+1)/2 then return -1.
- Assign colSwap = N – colSwap if, N%2 and colSwap%2 both are not equal to 0 and rowSwap = N – rowSwap if, N%2 and rowSwap%2 both are not equal to 0.
- Assign colSwap equal to the minimum of colSwap and N-colSwap, and rowSwap equal to the minimum of rowSwap and N-rowSwap.
- Finally, print the result as (rowSum+colSum)/2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSwaps(vector<vector< int > >& b)
{
int n = b.size();
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (b[0][0] ^ b[0][j] ^ b[i][0] ^ b[i][j])
return -1;
}
}
int rowSum = 0;
int colSum = 0;
int rowSwap = 0;
int colSwap = 0;
for ( int i = 0; i < n; i++) {
rowSum += b[i][0];
colSum += b[0][i];
rowSwap += b[i][0] == i % 2;
colSwap += b[0][i] == i % 2;
}
if (rowSum != n / 2 && rowSum != (n + 1) / 2)
return -1;
if (colSum != n / 2 && colSum != (n + 1) / 2)
return -1;
if (n % 2 == 1) {
if (colSwap % 2)
colSwap = n - colSwap;
if (rowSwap % 2)
rowSwap = n - rowSwap;
}
else {
colSwap = min(colSwap, n - colSwap);
rowSwap = min(rowSwap, n - rowSwap);
}
return (rowSwap + colSwap) / 2;
}
int main()
{
vector<vector< int > > M = { { 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 1, 0, 0, 1 },
{ 1, 0, 0, 1 } };
int ans = minSwaps(M);
cout << ans;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int minSwaps( int [][] b)
{
int n = b.length;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if ((b[ 0 ][ 0 ] ^ b[ 0 ][j] ^ b[i][ 0 ] ^ b[i][j]) == 1 )
{
return - 1 ;
}
}
}
int rowSum = 0 ;
int colSum = 0 ;
int rowSwap = 0 ;
int colSwap = 0 ;
for ( int i = 0 ; i < n; i++)
{
rowSum += b[i][ 0 ];
colSum += b[ 0 ][i];
int cond1 = 0 ;
int cond2 = 0 ;
if (b[i][ 0 ] == i % 2 )
cond1 = 1 ;
if (b[ 0 ][i] == i % 2 )
cond2 = 1 ;
rowSwap += cond1;
colSwap += cond2;
}
if (rowSum != n / 2 && rowSum != (n + 1 ) / 2 )
return - 1 ;
if (colSum != n / 2 && colSum != (n + 1 ) / 2 )
return - 1 ;
if (n % 2 == 1 )
{
if ((colSwap % 2 ) == 1 )
colSwap = n - colSwap;
if ((rowSwap % 2 ) == 1 )
rowSwap = n - rowSwap;
}
else
{
colSwap = Math.min(colSwap, n - colSwap);
rowSwap = Math.min(rowSwap, n - rowSwap);
}
return (rowSwap + colSwap) / 2 ;
}
public static void main (String[] args)
{
int [][] M = { { 0 , 1 , 1 , 0 },
{ 0 , 1 , 1 , 0 },
{ 1 , 0 , 0 , 1 },
{ 1 , 0 , 0 , 1 } };
int ans = minSwaps(M);
System.out.println(ans);
}
}
|
Python3
def minSwaps(b):
n = len (b)
for i in range (n):
for j in range (n):
if (b[ 0 ][ 0 ] ^ b[ 0 ][j] ^
b[i][ 0 ] ^ b[i][j]):
return - 1
rowSum = 0
colSum = 0
rowSwap = 0
colSwap = 0
for i in range (n):
rowSum + = b[i][ 0 ]
colSum + = b[ 0 ][i]
rowSwap + = b[i][ 0 ] = = i % 2
colSwap + = b[ 0 ][i] = = i % 2
if (rowSum ! = n / / 2 and
rowSum ! = (n + 1 ) / / 2 ):
return - 1
if (colSum ! = n / / 2 and
colSum ! = (n + 1 ) / / 2 ):
return - 1
if (n % 2 = = 1 ):
if (colSwap % 2 ):
colSwap = n - colSwap
if (rowSwap % 2 ):
rowSwap = n - rowSwap
else :
colSwap = min (colSwap, n - colSwap)
rowSwap = min (rowSwap, n - rowSwap)
return (rowSwap + colSwap) / / 2
if __name__ = = "__main__" :
M = [ [ 0 , 1 , 1 , 0 ],
[ 0 , 1 , 1 , 0 ],
[ 1 , 0 , 0 , 1 ],
[ 1 , 0 , 0 , 1 ] ]
ans = minSwaps(M)
print (ans)
|
C#
using System;
class GFG
{
public static int minSwaps( int [,] b)
{
int n = b.GetLength(0);
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if ((b[0, 0] ^ b[0, j] ^ b[i, 0] ^ b[i, j]) == 1)
{
return -1;
}
}
}
int rowSum = 0;
int colSum = 0;
int rowSwap = 0;
int colSwap = 0;
for ( int i = 0; i < n; i++)
{
rowSum += b[i, 0];
colSum += b[0, i];
int cond1 = 0;
int cond2 = 0;
if (b[i, 0] == i % 2)
cond1 = 1;
if (b[0, i] == i % 2)
cond2 = 1;
rowSwap += cond1;
colSwap += cond2;
}
if (rowSum != n / 2 && rowSum != (n + 1) / 2)
return -1;
if (colSum != n / 2 && colSum != (n + 1) / 2)
return -1;
if (n % 2 == 1)
{
if ((colSwap % 2) == 1)
colSwap = n - colSwap;
if ((rowSwap % 2) == 1)
rowSwap = n - rowSwap;
}
else
{
colSwap = Math.Min(colSwap, n - colSwap);
rowSwap = Math.Min(rowSwap, n - rowSwap);
}
return (rowSwap + colSwap) / 2;
}
public static void Main(String[] args)
{
int [,] M = { { 0, 1, 1, 0 },
{ 0, 1, 1, 0 },
{ 1, 0, 0, 1 },
{ 1, 0, 0, 1 } };
int ans = minSwaps(M);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function minSwaps(b)
{
var n = b.length;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if ((b[0][0] ^ b[0][j] ^ b[i][0] ^ b[i][j]) == 1)
{
return -1;
}
}
}
var rowSum = 0;
var colSum = 0;
var rowSwap = 0;
var colSwap = 0;
for (i = 0; i < n; i++)
{
rowSum += b[i][0];
colSum += b[0][i];
var cond1 = 0;
var cond2 = 0;
if (b[i][0] == i % 2)
cond1 = 1;
if (b[0][i] == i % 2)
cond2 = 1;
rowSwap += cond1;
colSwap += cond2;
}
if (rowSum != n / 2 && rowSum != (n + 1) / 2)
return -1;
if (colSum != n / 2 && colSum != (n + 1) / 2)
return -1;
if (n % 2 == 1)
{
if ((colSwap % 2) == 1)
colSwap = n - colSwap;
if ((rowSwap % 2) == 1)
rowSwap = n - rowSwap;
}
else
{
colSwap = Math.min(colSwap, n - colSwap);
rowSwap = Math.min(rowSwap, n - rowSwap);
}
return (rowSwap + colSwap) / 2;
}
var M = [[ 0, 1, 1, 0 ],
[ 0, 1, 1, 0 ],
[ 1, 0, 0, 1 ],
[ 1, 0, 0, 1 ] ];
var ans = minSwaps(M);
document.write(ans);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...