Count 1s in binary matrix having remaining indices of its row and column filled with 0s
Last Updated :
31 May, 2022
Given a binary matrix, mat[][] of size M * N, the task is to count the number of 1s from the given binary matrix whose corresponding row and column consists of 0s only in the remaining indices.
Examples:
Input: mat[][] = {{1, 0, 0}, {0, 0, 1}, {0, 0, 0}}
Output: 2
Explanation:
The only two cells satisfying the conditions are (0, 0) and (1, 2).
Therefore, the count is 2.
Input: mat[][] = {{1, 0}, {1, 1}}
Output: 0
Naive Approach: The simplest approach is to iterate over the matrix and check the given condition for all 1s present in the given matrix by traversing its corresponding row and column. Increase count of all 1s satisfying the condition. Finally, print the count as the required answer.
Time Complexity: O(M*N2)
Auxiliary Space: O(M + N)
Efficient Approach: The above approach can be optimized based on the idea that the sum of such rows and columns will be only 1. Follow the steps below to solve the problem:
- Initialize two arrays, rows[] and cols[], to store the sum of each row and each column of the matrix respectively.
- Initialize a variable, say cnt, to store the count of 1s satisfying given condition.
- Traverse the matrix for every mat[i][j] = 1, check if rows[i] and cols[j] is 1.If found to be true, then increment cnt.
- After completing the above steps, print the final value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numSpecial(vector<vector< int > >& mat)
{
int m = mat.size(), n = mat[0].size();
int rows[m];
int cols[n];
int i, j;
for (i = 0; i < m; i++) {
rows[i] = 0;
for (j = 0; j < n; j++)
rows[i] += mat[i][j];
}
for (i = 0; i < n; i++) {
cols[i] = 0;
for (j = 0; j < m; j++)
cols[i] += mat[j][i];
}
int cnt = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (mat[i][j] == 1 && rows[i] == 1
&& cols[j] == 1)
cnt++;
}
}
return cnt;
}
int main()
{
vector<vector< int > > mat
= { { 1, 0, 0 }, { 0, 0, 1 }, { 0, 0, 0 } };
cout << numSpecial(mat) << endl;
return 0;
}
|
Java
class GFG{
static int numSpecial( int [][]mat)
{
int m = mat.length;
int n = mat[ 0 ].length;
int []rows = new int [m];
int []cols = new int [n];
int i, j;
for (i = 0 ; i < m; i++)
{
rows[i] = 0 ;
for (j = 0 ; j < n; j++)
rows[i] += mat[i][j];
}
for (i = 0 ; i < n; i++)
{
cols[i] = 0 ;
for (j = 0 ; j < m; j++)
cols[i] += mat[j][i];
}
int cnt = 0 ;
for (i = 0 ; i < m; i++)
{
for (j = 0 ; j < n; j++)
{
if (mat[i][j] == 1 &&
rows[i] == 1 &&
cols[j] == 1 )
cnt++;
}
}
return cnt;
}
public static void main(String[] args)
{
int [][]mat = { { 1 , 0 , 0 },
{ 0 , 0 , 1 },
{ 0 , 0 , 0 } };
System.out.print(numSpecial(mat) + "\n" );
}
}
|
Python3
def numSpecial(mat):
m = len (mat)
n = len (mat[ 0 ])
rows = [ 0 ] * m
cols = [ 0 ] * n
i, j = 0 , 0
for i in range (m):
rows[i] = 0
for j in range (n):
rows[i] + = mat[i][j]
for i in range (n):
cols[i] = 0
for j in range (m):
cols[i] + = mat[j][i]
cnt = 0
for i in range (m):
for j in range (n):
if (mat[i][j] = = 1 and
rows[i] = = 1 and
cols[j] = = 1 ):
cnt + = 1
return cnt
if __name__ = = '__main__' :
mat = [ [ 1 , 0 , 0 ],
[ 0 , 0 , 1 ],
[ 0 , 0 , 0 ] ]
print (numSpecial(mat))
|
C#
using System;
class GFG{
static int numSpecial( int [,]mat)
{
int m = mat.GetLength(0);
int n = mat.GetLength(1);
int []rows = new int [m];
int []cols = new int [n];
int i, j;
for (i = 0; i < m; i++)
{
rows[i] = 0;
for (j = 0; j < n; j++)
rows[i] += mat[i, j];
}
for (i = 0; i < n; i++)
{
cols[i] = 0;
for (j = 0; j < m; j++)
cols[i] += mat[j, i];
}
int cnt = 0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i, j] == 1 &&
rows[i] == 1 &&
cols[j] == 1)
cnt++;
}
}
return cnt;
}
public static void Main(String[] args)
{
int [,]mat = { { 1, 0, 0 },
{ 0, 0, 1 },
{ 0, 0, 0 } };
Console.Write(numSpecial(mat) + "\n" );
}
}
|
Javascript
<script>
function numSpecial(mat)
{
var m = mat.length;
var n = mat[0].length;
var rows = Array.from({length: m}, (_, i) => 0);
var cols = Array.from({length: n}, (_, i) => 0);
var i, j;
for (i = 0; i < m; i++)
{
rows[i] = 0;
for (j = 0; j < n; j++)
rows[i] += mat[i][j];
}
for (i = 0; i < n; i++)
{
cols[i] = 0;
for (j = 0; j < m; j++)
cols[i] += mat[j][i];
}
var cnt = 0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (mat[i][j] == 1 &&
rows[i] == 1 &&
cols[j] == 1)
cnt++;
}
}
return cnt;
}
var mat = [ [ 1, 0, 0 ],
[ 0, 0, 1 ],
[ 0, 0, 0 ] ];
document.write(numSpecial(mat) + "\n" );
</script>
|
Time Complexity: O(N*M), as we are using nested loops to traverse N*M times.
Auxiliary Space: O(N+M), as we are using extra space for two arrays row and col.
Share your thoughts in the comments
Please Login to comment...