Find row with maximum and minimum number of zeroes in given Matrix
Given a 2D matrix containing only zeroes and ones, where each row is sorted. The task is to find the row with the maximum number of 0s and the row with minimum number of 0s.
Example:
Input: mat[][] = {
{0, 1, 1, 1},
{0, 0, 1, 1},
{1, 1, 1, 1},
{0, 0, 0, 0}}
Output:
Row with min zeroes: 3
Row with max zeroes: 4
Input: mat[][] = {
{0, 1, 1, 1},
{0, 0, 1, 1},
{0, 0, 0, 1},
{0, 0, 0, 0}}
Output:
Row with min zeroes: 1
Row with max zeroes: 4
Simple approach: A simple method is to do a row-wise traversal of the matrix, count the number of 0s in each row, and compare the count with max and min. Finally, return the index of row with maximum 0s and minimum 0s. The time complexity of this method is O(M*N) where M is a number of rows and N is a number of columns in matrix.
Efficient approach: Since each row is sorted, we can use Binary Search to find the count of 0s in each row. The idea is to find the index of first instance of 1 in each row.
The count of 0s in that row will be:
- If 1 exists in the row, then count of 0s will be equal to the index of first 1 in the row considering zero-based indexing.
- If 1 does not exist in the row, then count of 0s will be N which is the total number of columns in the matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
int first( bool arr[], int low, int high)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || arr[mid - 1] == 0) && arr[mid] == 1)
return mid;
else if (arr[mid] == 0)
return first(arr, (mid + 1), high);
else
return first(arr, low, (mid - 1));
}
return -1;
}
void rowWith0s( bool mat[R][C])
{
int max_row_index = 0, max = INT_MIN;
int min_row_index = 0, min = INT_MAX;
int i, index;
for (i = 0; i < R; i++) {
index = first(mat[i], 0, C - 1);
int cntZeroes = 0;
if (index == -1) {
cntZeroes = C;
}
else {
cntZeroes = index;
}
if (max < cntZeroes) {
max = cntZeroes;
max_row_index = i;
}
if (min > cntZeroes) {
min = cntZeroes;
min_row_index = i;
}
}
cout << "Row with min 0s: " << min_row_index + 1;
cout << "\nRow with max 0s: " << max_row_index + 1;
}
int main()
{
bool mat[R][C] = { { 0, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 } };
rowWith0s(mat);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int R = 4 ;
static int C = 4 ;
static int first( int arr[], int low, int high)
{
if (high >= low)
{
int mid = low + (high - low) / 2 ;
if ((mid == 0 || arr[mid - 1 ] == 0 ) && arr[mid] == 1 )
return mid;
else if (arr[mid] == 0 )
return first(arr, (mid + 1 ), high);
else
return first(arr, low, (mid - 1 ));
}
return - 1 ;
}
static void rowWith0s( int mat[][])
{
int max_row_index = 0 , max = Integer.MIN_VALUE;
int min_row_index = 0 , min = Integer.MAX_VALUE;
int i, index;
for (i = 0 ; i < R; i++)
{
index = first(mat[i], 0 , C - 1 );
int cntZeroes = 0 ;
if (index == - 1 )
{
cntZeroes = C;
}
else
{
cntZeroes = index;
}
if (max < cntZeroes)
{
max = cntZeroes;
max_row_index = i;
}
if (min > cntZeroes)
{
min = cntZeroes;
min_row_index = i;
}
}
System.out.println ( "Row with min 0s: " + (min_row_index + 1 ));
System.out.println ( "Row with max 0s: " + (max_row_index + 1 ));
}
public static void main (String[] args)
{
int mat[][] = { { 0 , 0 , 0 , 1 },
{ 0 , 1 , 1 , 1 },
{ 1 , 1 , 1 , 1 },
{ 0 , 0 , 0 , 0 } };
rowWith0s(mat);
}
}
|
Python3
import sys
R = 4
C = 4
def first(arr, low, high) :
if (high > = low) :
mid = low + (high - low) / / 2 ;
if ((mid = = 0 or arr[mid - 1 ] = = 0 ) and arr[mid] = = 1 ) :
return mid;
elif (arr[mid] = = 0 ) :
return first(arr, (mid + 1 ), high);
else :
return first(arr, low, (mid - 1 ));
return - 1 ;
def rowWith0s(mat) :
row_index = 0 ; max = - (sys.maxsize - 1 );
min_row_index = 0 ; min = sys.maxsize;
for i in range (R) :
index = first(mat[i], 0 , C - 1 );
cntZeroes = 0 ;
if (index = = - 1 ) :
cntZeroes = C;
else :
cntZeroes = index;
if ( max < cntZeroes) :
max = cntZeroes;
max_row_index = i;
if ( min > cntZeroes) :
min = cntZeroes;
min_row_index = i;
print ( "Row with min 0s:" ,min_row_index + 1 );
print ( "Row with max 0s:" , max_row_index + 1 );
if __name__ = = "__main__" :
mat = [
[ 0 , 0 , 0 , 1 ],
[ 0 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 1 ],
[ 0 , 0 , 0 , 0 ]
];
rowWith0s(mat);
|
C#
using System;
class GFG
{
static int R = 4;
static int C = 4;
static int first( int []arr, int low, int high)
{
if (high >= low)
{
int mid = low + (high - low) / 2;
if ((mid == 0 || arr[mid - 1] == 0) && arr[mid] == 1)
return mid;
else if (arr[mid] == 0)
return first(arr, (mid + 1), high);
else
return first(arr, low, (mid - 1));
}
return -1;
}
static void rowWith0s( int [,]mat)
{
int max_row_index = 0, max = int .MinValue;
int min_row_index = 0, min = int .MaxValue;
int i, index;
for (i = 0; i < R; i++)
{
index = first(GetRow(mat,i), 0, C - 1);
int cntZeroes = 0;
if (index == -1)
{
cntZeroes = C;
}
else
{
cntZeroes = index;
}
if (max < cntZeroes)
{
max = cntZeroes;
max_row_index = i;
}
if (min > cntZeroes)
{
min = cntZeroes;
min_row_index = i;
}
}
Console.WriteLine ( "Row with min 0s: " + (min_row_index + 1));
Console.WriteLine ( "Row with max 0s: " + (max_row_index + 1));
}
public static int [] GetRow( int [,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
public static void Main (String[] args)
{
int [,]mat = { { 0, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 0 } };
rowWith0s(mat);
}
}
|
Javascript
<script>
var R = 4;
var C = 4;
function first(arr , low , high)
{
if (high >= low)
{
var mid = low + parseInt((high - low) / 2);
if ((mid == 0 || arr[mid - 1] == 0) &&
arr[mid] == 1)
return mid;
else if (arr[mid] == 0)
return first(arr, (mid + 1), high);
else
return first(arr, low, (mid - 1));
}
return -1;
}
function rowWith0s(mat)
{
var max_row_index = 0, max = Number.MIN_VALUE;
var min_row_index = 0, min = Number.MAX_VALUE;
var i, index;
for (i = 0; i < R; i++)
{
index = first(mat[i], 0, C - 1);
var cntZeroes = 0;
if (index == -1)
{
cntZeroes = C;
}
else
{
cntZeroes = index;
}
if (max < cntZeroes)
{
max = cntZeroes;
max_row_index = i;
}
if (min > cntZeroes)
{
min = cntZeroes;
min_row_index = i;
}
}
document.write( "Row with min 0s: " +
(min_row_index + 1)+ "<br/>" );
document.write( "Row with max 0s: " +
(max_row_index + 1));
}
var mat = [ [ 0, 0, 0, 1 ],
[ 0, 1, 1, 1 ],
[ 1, 1, 1, 1 ],
[ 0, 0, 0, 0 ] ];
rowWith0s(mat);
</script>
|
Output:
Row with min 0s: 3
Row with max 0s: 4
Time Complexity: O(R*logC), as we are using a loop to traverse R times and in each traversal we are calling the function first which will cost O(logC) time as we are using binary search.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...