-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathCount zeros in a sorted matrix.cpp
53 lines (43 loc) · 1.11 KB
/
Count zeros in a sorted matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/ C++ program to count number of 0s in the given
// row-wise and column-wise sorted binary matrix.
#include <iostream>
using namespace std;
// define size of square matrix
#define N 5
// Function to count number of 0s in the given
// row-wise and column-wise sorted binary matrix.
int countZeroes(int mat[N][N])
{
// start from bottom-left corner of the matrix
int row = N - 1, col = 0;
// stores number of zeroes in the matrix
int count = 0;
while (col < N)
{
// move up until you find a 0
while (mat[row][col])
// if zero is not found in current column,
// we are done
if (--row < 0)
return count;
// add 0s present in current column to result
count += (row + 1);
// move right to next column
col++;
}
return count;
}
// Driver Program to test above functions
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;
}