Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
Last Updated :
03 May, 2021
Given a matrix, mat[][] of size N × N, the task is to minimize the count of adjacent row swaps to convert the given matrix to a lower triangular matrix. If it is not possible to convert the given matrix to a lower triangular matrix, then print -1.
Note: A lower triangular matrix contains 0s at all the indices above the main diagonal.
Examples:
Input: mat[][] = {{0, 0, 2}, {3, 1, 0}, {4, 0, 0}}
Output: 3
Explanation:
Swap 1st row and 2nd row {{3, 1, 0}, {0, 0, 2}, {4, 0, 0}}
Swap 2nd row and 3rd row {{3, 1, 0}, {4, 0, 0}, {0, 0, 2}}
Swap 1st row and 2nd row {{4, 0, 0}, {3, 1, 0}, {0, 0, 2}}
Therefore, the required output is 3.
Input: mat[][] = {{0, 2, 3, 0}, {0, 2, 4, 0}, {0, 5, 1, 0}, {0, 1, 1, 0}}
Output: -1
Approach: This problem can be solved using the Greedy technique. The idea to first find the count of zeros present in each row and store it in an integer array. Then, count the minimum number of adjacent swaps required to sort the array based on the number of 0s in descending order. Follow the steps below to solve the problem:
- Initialize an array, say cntZero[] to store the count of 0s present in each row.
- For ith row, traverse the cntZero[] array from (i + 1)th index and find the first index, say First where ctnZero[First] >= (N – i -1).
- Swap all the adjacent elements from the ith index to First index of cntZero[] array and increment the count.
- Finally, return the count of adjacent swaps required to convert the given matrix to the lower triangular matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minAdjSwaps(vector<vector< int > >& mat)
{
int N = mat.size();
vector< int > cntZero(N, 0);
for ( int i = 0; i < N; i++) {
for ( int j = N - 1;
j >= 0 && mat[i][j] == 0;
j--) {
cntZero[i]++;
}
}
int cntSwaps = 0;
for ( int i = 0; i < N; i++) {
if (cntZero[i]
< (N - i - 1)) {
int First = i;
while (First < N
&& cntZero[First]
< (N - i - 1)) {
First++;
}
if (First == N) {
return -1;
}
while (First > i) {
swap(cntZero[First],
cntZero[First - 1]);
First--;
cntSwaps++;
}
}
}
return cntSwaps;
}
int main()
{
vector<vector< int > > mat
= { { 0, 0, 2 },
{ 3, 1, 0 },
{ 4, 0, 0 } };
cout << minAdjSwaps(mat);
}
|
Java
import java.util.*;
class GFG{
static int minAdjSwaps( int [][]mat)
{
int N = mat.length;
int []cntZero = new int [N];
for ( int i = 0 ; i < N; i++)
{
for ( int j = N - 1 ;
j >= 0 && mat[i][j] == 0 ;
j--)
{
cntZero[i]++;
}
}
int cntSwaps = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (cntZero[i] < (N - i - 1 ))
{
int First = i;
while (First < N && cntZero[First] <
(N - i - 1 ))
{
First++;
}
if (First == N)
{
return - 1 ;
}
while (First > i)
{
cntZero = swap(cntZero, First,
First - 1 );
First--;
cntSwaps++;
}
}
}
return cntSwaps;
}
static int [] swap( int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void main(String[] args)
{
int [][]mat = { { 0 , 0 , 2 },
{ 3 , 1 , 0 },
{ 4 , 0 , 0 } };
System.out.print(minAdjSwaps(mat));
}
}
|
Python3
def minAdjSwaps(mat):
N = len (mat)
cntZero = [ 0 ] * (N)
for i in range (N):
for j in range (N - 1 , - 1 , - 1 ):
if mat[i][j] ! = 0 :
break
cntZero[i] + = 1
cntSwaps = 0
for i in range (N):
if (cntZero[i] < (N - i - 1 )):
First = i
while (First < N and
cntZero[First] < (N - i - 1 )):
First + = 1
if (First = = N):
return - 1
while (First > i):
cntZero[First] = cntZero[First - 1 ]
cntZero[First - 1 ] = cntZero[First]
First - = 1
cntSwaps + = 1
return cntSwaps
if __name__ = = '__main__' :
mat = [ [ 0 , 0 , 2 ],
[ 3 , 1 , 0 ],
[ 4 , 0 , 0 ] ]
print (minAdjSwaps(mat))
|
C#
using System;
class GFG{
static int minAdjSwaps( int [,]mat)
{
int N = mat.GetLength(0);
int []cntZero = new int [N];
for ( int i = 0; i < N; i++)
{
for ( int j = N - 1;
j >= 0 && mat[i, j] == 0;
j--)
{
cntZero[i]++;
}
}
int cntSwaps = 0;
for ( int i = 0; i < N; i++)
{
if (cntZero[i] < (N - i - 1))
{
int First = i;
while (First < N &&
cntZero[First] < (N - i - 1))
{
First++;
}
if (First == N)
{
return -1;
}
while (First > i)
{
cntZero = swap(cntZero,
First, First - 1);
First--;
cntSwaps++;
}
}
}
return cntSwaps;
}
static int [] swap( int []arr,
int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void Main(String[] args)
{
int [,]mat = {{0, 0, 2},
{3, 1, 0},
{4, 0, 0}};
Console.Write(minAdjSwaps(mat));
}
}
|
Javascript
<script>
function minAdjSwaps(mat) {
var N = mat.length;
var cntZero = Array(N).fill(0);
for (i = 0; i < N; i++) {
for (j = N - 1; j >= 0 &&
mat[i][j] == 0; j--)
{
cntZero[i]++;
}
}
var cntSwaps = 0;
for (i = 0; i < N; i++) {
if (cntZero[i] < (N - i - 1)) {
var First = i;
while (First < N && cntZero[First] <
(N - i - 1))
{
First++;
}
if (First == N) {
return -1;
}
while (First > i) {
cntZero = swap(cntZero, First,
First - 1);
First--;
cntSwaps++;
}
}
}
return cntSwaps;
}
function swap(arr , i , j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
var mat = [ [ 0, 0, 2 ],
[ 3, 1, 0 ],
[ 4, 0, 0 ] ];
document.write(minAdjSwaps(mat));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...