Given a N x N binary matrix (elements in matrix can be either 1 or 0) where each row and column of the matrix is sorted in ascending order, count number of 0s present in it.
Expected time complexity is O(N).
Examples:
Input:
[0, 0, 0, 0, 1]
[0, 0, 0, 1, 1]
[0, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
Output: 8
Input:
[0, 0]
[0, 0]
Output: 4
Input:
[1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
Output: 0
The idea is very simple. We start from the bottom-left corner of the matrix and repeat below steps until we find the top or right edge of the matrix.
- Decrement row index until we find a 0.
- Add number of 0s in current column i.e. current row index + 1 to the result and move right to next column (Increment col index by 1).
The above logic will work since the matrix is row-wise and column-wise sorted. The logic will also work for any matrix containing non-negative integers.
Below is the implementation of above idea :
C++
#include <iostream>
using namespace std;
#define N 5
int countZeroes( int mat[N][N])
{
int row = N - 1, col = 0;
int count = 0;
while (col < N)
{
while (mat[row][col])
if (--row < 0)
return count;
count += (row + 1);
col++;
}
return count;
}
int main()
{
int mat[N][N] =
{
{ 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 1 },
{ 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
cout << countZeroes(mat);
return 0;
}
|
C
#include <stdio.h>
#define N 5
int countZeroes( int mat[N][N])
{
int row = N - 1, col = 0;
int count = 0;
while (col < N)
{
while (mat[row][col])
if (--row < 0)
return count;
count += (row + 1);
col++;
}
return count;
}
int main()
{
int mat[N][N] =
{
{ 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 1 },
{ 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
printf ( "%d" ,countZeroes(mat));
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int N = 5 ;
static int countZeroes( int mat[][])
{
int row = N - 1 , col = 0 ;
int count = 0 ;
while (col < N)
{
while (mat[row][col] > 0 )
if (--row < 0 )
return count;
count += (row + 1 );
col++;
}
return count;
}
public static void main (String[] args)
{
int mat[][] = { { 0 , 0 , 0 , 0 , 1 },
{ 0 , 0 , 0 , 1 , 1 },
{ 0 , 1 , 1 , 1 , 1 },
{ 1 , 1 , 1 , 1 , 1 },
{ 1 , 1 , 1 , 1 , 1 } };
System.out.println(countZeroes(mat));
}
}
|
Python
def countZeroes(mat):
N = 5 ;
row = N - 1 ;
col = 0 ;
count = 0 ;
while (col < N):
while (mat[row][col]):
if (row < 0 ):
return count;
row = row - 1 ;
count = count + (row + 1 );
col = col + 1 ;
return count;
mat = [[ 0 , 0 , 0 , 0 , 1 ],
[ 0 , 0 , 0 , 1 , 1 ],
[ 0 , 1 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 1 , 1 ]];
print ( countZeroes(mat));
|
C#
using System;
class GFG
{
public static int N = 5;
static int countZeroes( int [,] mat)
{
int row = N - 1, col = 0;
int count = 0;
while (col < N)
{
while (mat[row,col] > 0)
if (--row < 0)
return count;
count += (row + 1);
col++;
}
return count;
}
public static void Main ()
{
int [,] mat = { { 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 1 },
{ 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 } };
Console.WriteLine(countZeroes(mat));
}
}
|
PHP
<?php
function countZeroes( $mat )
{
$N = 5;
$row = $N - 1;
$col = 0;
$count = 0;
while ( $col < $N )
{
while ( $mat [ $row ][ $col ])
if (-- $row < 0)
return $count ;
$count += ( $row + 1);
$col ++;
}
return $count ;
}
$mat = array ( array (0, 0, 0, 0, 1),
array (0, 0, 0, 1, 1),
array (0, 1, 1, 1, 1),
array (1, 1, 1, 1, 1),
array (1, 1, 1, 1, 1));
echo countZeroes( $mat );
?>
|
Javascript
<script>
let N = 5;
function countZeroes(mat)
{
let row = N - 1, col = 0;
let count = 0;
while (col < N)
{
while (mat[row][col] > 0)
if (--row < 0)
return count;
count += (row + 1);
col++;
}
return count;
}
let mat = [[ 0, 0, 0, 0, 1 ],
[ 0, 0, 0, 1, 1 ],
[ 0, 1, 1, 1, 1 ],
[ 1, 1, 1, 1, 1 ],
[ 1, 1, 1, 1, 1 ]];
document.write(countZeroes(mat));
</script>
|
Time complexity of above solution is O(n) since the solution follows single path from bottom-left corner to top or right edge of the matrix.
Auxiliary space used by the program is O(1). since no extra space has been taken.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Aug, 2022
Like Article
Save Article