Given an ‘n x m’ matrix composed solely of ‘0’s and ‘1’s, the task is to determine the count of ‘1’s that are enclosed by an even number (greater than zero) of neighboring ‘0’s. The surroundings of a matrix cell encompass the eight neighboring cells, including those diagonally adjacent.
Examples:
Input: matrix = {{1, 0, 0}, {1, 1, 0}, {0, 1, 0}} Output: 1 Explanation: 1 that occurs in the 1st row and 1st column, has 3 surrounding elements 0,1 and 1. The occurrence of zero is odd. 1 that occurs in the 2nd row and 1st column has 5 surrounding elements 1,0,1,1 and 0. The occurrence of zero is even. 1 that occurs in the 2nd row and the 2nd column has 8 surrounding elements. The occurrence of 0 is odd. Similarly, for the 1 that occurs in the 3rd row and 2nd column, the occurrence of zero in its 5 surrounding elements is odd. Hence, the output is 1.
Input: matrix = {{1}} Output: 0 Explanation: There is only 1 element in the matrix. Hence, it has no surroundings, so its count for even 0’s is 0 for the whole matrix. 0 is even but we want the occurrence of a zero in the surrounding at least once. Hence, the output is 0.
Approach: To solve the problem follow the below steps:
- For each cell in the matrix:
a. If the cell contains a 1, proceed to analyze its surroundings. b. Create a counter variable `zeroCount` to keep track of the number of surrounding 0’s.
- For each of the eight surrounding cells (above, below, left, right, diagonals):
a. Check if the surrounding cell exists within the matrix boundaries. b. If the surrounding cell’s value is 0, increment the `zeroCount`.
- After analyzing the surroundings, check two conditions:
a. If `zeroCount` is even. b. If `zeroCount` is greater than 0.
- If both conditions are satisfied, increment the `result` counter.
- Finally, return the `result` count, which represents the number of 1’s that meet the specified condition.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public :
int Count(vector<vector< int > >& matrix)
{
int numRows = matrix.size();
int numCols = matrix[0].size();
int result = 0;
for ( int row = 0; row < numRows; ++row) {
for ( int col = 0; col < numCols; ++col) {
if (matrix[row][col]) {
int zeroCount = 0;
if (row - 1 >= 0)
zeroCount
+= matrix[row - 1][col] == 0;
if (row + 1 < numRows)
zeroCount
+= matrix[row + 1][col] == 0;
if (col - 1 >= 0)
zeroCount
+= matrix[row][col - 1] == 0;
if (col + 1 < numCols)
zeroCount
+= matrix[row][col + 1] == 0;
if (row - 1 >= 0 && col - 1 >= 0)
zeroCount
+= matrix[row - 1][col - 1]
== 0;
if (row - 1 >= 0 && col + 1 < numCols)
zeroCount
+= matrix[row - 1][col + 1]
== 0;
if (row + 1 < numRows && col - 1 >= 0)
zeroCount
+= matrix[row + 1][col - 1]
== 0;
if (row + 1 < numRows
&& col + 1 < numCols)
zeroCount
+= matrix[row + 1][col + 1]
== 0;
if (!(zeroCount & 1) && zeroCount)
result++;
}
}
}
return result;
}
};
int main()
{
int n = 3, m = 3;
vector<vector< int > > matrix
= { { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 } };
Solution ob;
int ans = ob.Count(matrix);
cout << ans << "\n";
return 0;
}
|
Java
import java.util.*;
class Solution {
public int count( int [][] matrix)
{
int numRows = matrix.length;
int numCols = matrix[ 0 ].length;
int result = 0 ;
for ( int row = 0 ; row < numRows; ++row) {
for ( int col = 0 ; col < numCols; ++col) {
if (matrix[row][col]
== 1 ) {
int zeroCount = 0 ;
if (row - 1 >= 0 )
zeroCount
+= matrix[row - 1 ][col] == 0
? 1
: 0 ;
if (row + 1 < numRows)
zeroCount
+= matrix[row + 1 ][col] == 0
? 1
: 0 ;
if (col - 1 >= 0 )
zeroCount
+= matrix[row][col - 1 ] == 0
? 1
: 0 ;
if (col + 1 < numCols)
zeroCount
+= matrix[row][col + 1 ] == 0
? 1
: 0 ;
if (row - 1 >= 0 && col - 1 >= 0 )
zeroCount
+= matrix[row - 1 ][col - 1 ] == 0
? 1
: 0 ;
if (row - 1 >= 0 && col + 1 < numCols)
zeroCount
+= matrix[row - 1 ][col + 1 ] == 0
? 1
: 0 ;
if (row + 1 < numRows && col - 1 >= 0 )
zeroCount
+= matrix[row + 1 ][col - 1 ] == 0
? 1
: 0 ;
if (row + 1 < numRows
&& col + 1 < numCols)
zeroCount
+= matrix[row + 1 ][col + 1 ] == 0
? 1
: 0 ;
if ((zeroCount % 2 == 0 )
&& zeroCount
> 0 )
result++;
}
}
}
return result;
}
}
public class Main {
public static void main(String[] args)
{
int tc = 1 ;
while (tc-- > 0 ) {
int n = 3 , m = 3 ;
int [][] matrix
= { { 1 , 0 , 0 }, { 1 , 1 , 0 }, { 0 , 1 , 0 } };
Solution ob = new Solution();
int ans = ob.count(
matrix);
System.out.println(ans);
}
}
}
|
Python
class Solution:
def count( self , matrix):
numRows = len (matrix)
numCols = len (matrix[ 0 ])
result = 0
for row in range (numRows):
for col in range (numCols):
if matrix[row][col]:
zeroCount = 0
if row - 1 > = 0 :
zeroCount + = matrix[row - 1 ][col] = = 0
if row + 1 < numRows:
zeroCount + = matrix[row + 1 ][col] = = 0
if col - 1 > = 0 :
zeroCount + = matrix[row][col - 1 ] = = 0
if col + 1 < numCols:
zeroCount + = matrix[row][col + 1 ] = = 0
if row - 1 > = 0 and col - 1 > = 0 :
zeroCount + = matrix[row - 1 ][col - 1 ] = = 0
if row - 1 > = 0 and col + 1 < numCols:
zeroCount + = matrix[row - 1 ][col + 1 ] = = 0
if row + 1 < numRows and col - 1 > = 0 :
zeroCount + = matrix[row + 1 ][col - 1 ] = = 0
if row + 1 < numRows and col + 1 < numCols:
zeroCount + = matrix[row + 1 ][col + 1 ] = = 0
if zeroCount % 2 = = 0 and zeroCount ! = 0 :
result + = 1
return result
if __name__ = = "__main__":
tc = 1
while tc > 0 :
n, m = 3 , 3
matrix = [[ 1 , 0 , 0 ], [ 1 , 1 , 0 ], [ 0 , 1 , 0 ]]
ob = Solution()
ans = ob.count(matrix)
print (ans)
tc - = 1
|
C#
using System;
class Solution
{
public int Count( int [][] matrix)
{
int numRows = matrix.Length;
int numCols = matrix[0].Length;
int result = 0;
for ( int row = 0; row < numRows; ++row)
{
for ( int col = 0; col < numCols; ++col)
{
if (matrix[row][col] == 1)
{
int zeroCount = 0;
if (row - 1 >= 0)
zeroCount += matrix[row - 1][col] == 0 ? 1 : 0;
if (row + 1 < numRows)
zeroCount += matrix[row + 1][col] == 0 ? 1 : 0;
if (col - 1 >= 0)
zeroCount += matrix[row][col - 1] == 0 ? 1 : 0;
if (col + 1 < numCols)
zeroCount += matrix[row][col + 1] == 0 ? 1 : 0;
if (row - 1 >= 0 && col - 1 >= 0)
zeroCount += matrix[row - 1][col - 1] == 0 ? 1 : 0;
if (row - 1 >= 0 && col + 1 < numCols)
zeroCount += matrix[row - 1][col + 1] == 0 ? 1 : 0;
if (row + 1 < numRows && col - 1 >= 0)
zeroCount += matrix[row + 1][col - 1] == 0 ? 1 : 0;
if (row + 1 < numRows && col + 1 < numCols)
zeroCount += matrix[row + 1][col + 1] == 0 ? 1 : 0;
if (zeroCount % 2 == 0 && zeroCount > 0)
result++;
}
}
}
return result;
}
}
class MainClass
{
public static void Main( string [] args)
{
int tc = 1;
while (tc-- > 0)
{
int [][] matrix = { new [] { 1, 0, 0 }, new [] { 1, 1, 0 }, new [] { 0, 1, 0 } };
Solution ob = new Solution();
int ans = ob.Count(matrix);
Console.WriteLine(ans);
}
}
}
|
Javascript
class Solution {
count(matrix) {
const numRows = matrix.length;
const numCols = matrix[0].length;
let result = 0;
for (let row = 0; row < numRows; ++row) {
for (let col = 0; col < numCols; ++col) {
if (matrix[row][col]) {
let zeroCount = 0;
if (row - 1 >= 0)
zeroCount += matrix[row - 1][col] == 0;
if (row + 1 < numRows)
zeroCount += matrix[row + 1][col] == 0;
if (col - 1 >= 0)
zeroCount += matrix[row][col - 1] == 0;
if (col + 1 < numCols)
zeroCount += matrix[row][col + 1] == 0;
if (row - 1 >= 0 && col - 1 >= 0)
zeroCount += matrix[row - 1][col - 1] == 0;
if (row - 1 >= 0 && col + 1 < numCols)
zeroCount += matrix[row - 1][col + 1] == 0;
if (row + 1 < numRows && col - 1 >= 0)
zeroCount += matrix[row + 1][col - 1] == 0;
if (row + 1 < numRows && col + 1 < numCols)
zeroCount += matrix[row + 1][col + 1] == 0;
if (!(zeroCount & 1) && zeroCount > 0)
result++;
}
}
}
return result;
}
}
const n = 3, m = 3;
const matrix = [[1, 0, 0], [1, 1, 0], [0, 1, 0]];
const ob = new Solution();
const ans = ob.count(matrix);
console.log(ans);
|
Time Complexity: O(N*M) Auxillary Space: O(1)
|