Count number of 1’s at specific position in a Matrix
Last Updated :
24 Apr, 2023
Given a binary matrix mat[][] of size m*n (0-based indexing), find the number of 1’s such that 1 is located at a specific position where the same row and same column don’t have any other 1’s.
Examples:
Input: mat[][] = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
Output: 3
Explanation: All 1’s in the matrix satisfy the given condition.
Input: mat[][] = [[1, 0, 1], [0, 1, 0], [1, 0, 0]]
Output: 1
Explanation: mat[1][1] satisfy the given condition.
Approach: The approach is based on matrix traversal.
Below are the steps for the above approach:
- Initialize auxiliary array rows[] and cols[] to track how many 1’s are in the corresponding row or column.
- Initialize a variable say, ans to store the number of 1’s that satisfy the given condition.
- Now traverse through the matrix mat[][] and check if mat[i][j]==1 then increment rows[i]++(for row) and increment cols[j]++(for column).
- Run a loop from i = 0 to i < m and check if row[i] == 1,
- Run a loop from j = 0 to j < n, and check if the element at the current position is 1 in mat[][] if mat[i][j] == 1
- Check if cols[j] == 1, increment ans, else break.
- Return ans.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNumberOfOnes(vector<vector< char > >& mat)
{
const int m = mat.size();
const int n = mat[0].size();
int ans = 0;
vector< int > rows(m);
vector< int > cols(n);
for ( int i = 0; i < m; ++i)
for ( int j = 0; j < n; ++j)
if (mat[i][j] == 1) {
++rows[i];
++cols[j];
}
for ( int i = 0; i < m; ++i)
if (rows[i] == 1)
for ( int j = 0; j < n; ++j)
if (mat[i][j] == 1) {
if (cols[j] == 1)
++ans;
break ;
}
return ans;
}
int main()
{
vector<vector< char > > mat
= { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 } };
cout << findNumberOfOnes(mat);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findLonelyColor( char [][] painting)
{
int m = painting.length, n = painting[ 0 ].length;
int [] rows = new int [m];
int [] cols = new int [n];
for ( int i = 0 ; i < m; ++i) {
for ( int j = 0 ; j < n; ++j) {
if (painting[i][j] == 'B' ) {
rows[i]++;
cols[j]++;
}
}
}
int ans = 0 ;
for ( int i = 0 ; i < m; ++i) {
if (rows[i] == 1 ) {
for ( int j = 0 ; j < n; ++j) {
if (painting[i][j] == 'B'
&& cols[j] == 1 ) {
ans++;
break ;
}
}
}
}
return ans;
}
public static void main(String[] args)
{
char [][] painting = { { 'W' , 'W' , 'B' },
{ 'W' , 'B' , 'W' },
{ 'B' , 'W' , 'W' } };
int ans = findLonelyColor(painting);
System.out.println(ans);
}
}
|
Python3
from typing import List
def findLonelyColor( self , painting: List [ List [ str ]]) - > int :
m, n = len (painting), len (painting[ 0 ])
rows, cols = [ 0 ] * m, [ 0 ] * n
for i in range (m):
for j in range (n):
if painting[i][j] = = 'B' :
rows[i] + = 1
cols[j] + = 1
ans = 0
for i in range (m):
if rows[i] = = 1 :
for j in range (n):
if painting[i][j] = = 'B' and cols[j] = = 1 :
ans + = 1
break
return ans
if __name__ = = "__main__" :
painting = [[ 'W' , 'W' , 'B' ],
[ 'W' , 'B' , 'W' ],
[ 'B' , 'W' , 'W' ]]
print (findLonelyColor(__name__, painting))
|
C#
using System;
public class GFG {
public static int FindLonelyColor( char [, ] painting)
{
int m = painting.GetLength(0);
int n = painting.GetLength(1);
int [] rows = new int [m];
int [] cols = new int [n];
for ( int i = 0; i < m; ++i) {
for ( int j = 0; j < n; ++j) {
if (painting[i, j] == 'B' ) {
rows[i]++;
cols[j]++;
}
}
}
int ans = 0;
for ( int i = 0; i < m; ++i) {
if (rows[i] == 1) {
for ( int j = 0; j < n; ++j) {
if (painting[i, j] == 'B'
&& cols[j] == 1) {
ans++;
break ;
}
}
}
}
return ans;
}
public static void Main()
{
char [, ] painting = { { 'W' , 'W' , 'B' },
{ 'W' , 'B' , 'W' },
{ 'B' , 'W' , 'W' } };
int ans = FindLonelyColor(painting);
Console.WriteLine(ans);
}
}
|
Javascript
function findNumberOfOnes(mat) {
const m = mat.length;
const n = mat[0].length;
let ans = 0;
const rows = new Array(m).fill(0);
const cols = new Array(n).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] === 1) {
++rows[i];
++cols[j];
}
}
}
for (let i = 0; i < m; ++i) {
if (rows[i] === 1) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] === 1) {
if (cols[j] === 1) {
++ans;
}
break ;
}
}
}
}
return ans;
}
const mat = [
[0, 0, 1],
[0, 1, 0],
[1, 0, 0],
];
console.log(findNumberOfOnes(mat));
|
Output :
3
Time Complexity: O(m*n + m*n), Where m and n are the sizes of rows and columns
Auxiliary Space: O(m + n), used auxiliary array rows and cols of size m and n respectively
Share your thoughts in the comments
Please Login to comment...