Open In App

Counting 1’s Surrounded by Even 0’s

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
#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;
 
        // Iterate through each cell in the matrix
        for (int row = 0; row < numRows; ++row) {
            for (int col = 0; col < numCols; ++col) {
 
                // Check if the current cell is
                // 1 Count of adjacent cells
                // containing 0
                if (matrix[row][col]) {
 
                    int zeroCount = 0;
 
                    // Check the surrounding 8
                    // cells for zeroes
 
                    // Check top cell
                    if (row - 1 >= 0)
                        zeroCount
                            += matrix[row - 1][col] == 0;
 
                    // Check bottom cell
                    if (row + 1 < numRows)
                        zeroCount
                            += matrix[row + 1][col] == 0;
 
                    // Check left cell
                    if (col - 1 >= 0)
                        zeroCount
                            += matrix[row][col - 1] == 0;
 
                    // Check right cell
                    if (col + 1 < numCols)
                        zeroCount
                            += matrix[row][col + 1] == 0;
 
                    // Check top-left cell
                    if (row - 1 >= 0 && col - 1 >= 0)
                        zeroCount
                            += matrix[row - 1][col - 1]
                               == 0;
 
                    // Check top-right cell
                    if (row - 1 >= 0 && col + 1 < numCols)
                        zeroCount
                            += matrix[row - 1][col + 1]
                               == 0;
 
                    // Check bottom-left
                    // cell
                    if (row + 1 < numRows && col - 1 >= 0)
                        zeroCount
                            += matrix[row + 1][col - 1]
                               == 0;
 
                    // Check bottom-right
                    // cell
                    if (row + 1 < numRows
                        && col + 1 < numCols)
                        zeroCount
                            += matrix[row + 1][col + 1]
                               == 0;
 
                    // If adjacent zeros
                    // are even and
                    // nonzero
                    if (!(zeroCount & 1) && zeroCount)
 
                        // Increment the result
                        // count
                        result++;
                }
            }
        }
 
        // Return the final count of desired
        // cells
        return result;
    }
};
 
// Drivers code
int main()
{
 
    int n = 3, m = 3;
    vector<vector<int> > matrix
        = { { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 } };
 
    Solution ob;
 
    // Calculate the result using
    // the Solution class
    int ans = ob.Count(matrix);
 
    // Output the result
    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;
 
        // Iterate through each cell in the matrix
        for (int row = 0; row < numRows; ++row) {
            for (int col = 0; col < numCols; ++col) {
                if (matrix[row][col]
                    == 1) { // Check if the current cell is
                            // 1
                    int zeroCount = 0; // Count of adjacent
                                       // cells containing 0
 
                    // Check the surrounding 8 cells for
                    // zeros
                    if (row - 1 >= 0)
                        zeroCount
                            += matrix[row - 1][col] == 0
                                   ? 1
                                   : 0; // Check top cell
                    if (row + 1 < numRows)
                        zeroCount
                            += matrix[row + 1][col] == 0
                                   ? 1
                                   : 0; // Check bottom cell
                    if (col - 1 >= 0)
                        zeroCount
                            += matrix[row][col - 1] == 0
                                   ? 1
                                   : 0; // Check left cell
                    if (col + 1 < numCols)
                        zeroCount
                            += matrix[row][col + 1] == 0
                                   ? 1
                                   : 0; // Check right cell
                    if (row - 1 >= 0 && col - 1 >= 0)
                        zeroCount
                            += matrix[row - 1][col - 1] == 0
                                   ? 1
                                   : 0; // Check top-left
                                        // cell
                    if (row - 1 >= 0 && col + 1 < numCols)
                        zeroCount
                            += matrix[row - 1][col + 1] == 0
                                   ? 1
                                   : 0; // Check top-right
                                        // cell
                    if (row + 1 < numRows && col - 1 >= 0)
                        zeroCount
                            += matrix[row + 1][col - 1] == 0
                                   ? 1
                                   : 0; // Check bottom-left
                                        // cell
                    if (row + 1 < numRows
                        && col + 1 < numCols)
                        zeroCount
                            += matrix[row + 1][col + 1] == 0
                                   ? 1
                                   : 0; // Check
                                        // bottom-right cell
 
                    if ((zeroCount % 2 == 0)
                        && zeroCount
                               > 0) // If adjacent zeros are
                                    // even and nonzero
                        result++; // Increment the result
                                  // count
                }
            }
        }
 
        return result; // Return the final count of desired
                       // cells
    }
}
 
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); // Calculate the result using the
                         // Solution class
            System.out.println(ans); // Output the result
        }
    }
}


Python




class Solution:
    def count(self, matrix):
        numRows = len(matrix)
        numCols = len(matrix[0])
        result = 0
 
        # Iterate through each cell in the matrix
        for row in range(numRows):
            for col in range(numCols):
                if matrix[row][col]:  # Check if the current cell is 1
                    zeroCount = 0  # Count of adjacent cells containing 0
 
                    # Check the surrounding 8 cells for zeros
                    if row - 1 >= 0:
                        # Check top cell
                        zeroCount += matrix[row - 1][col] == 0
                    if row + 1 < numRows:
                        # Check bottom cell
                        zeroCount += matrix[row + 1][col] == 0
                    if col - 1 >= 0:
                        # Check left cell
                        zeroCount += matrix[row][col - 1] == 0
                    if col + 1 < numCols:
                        # Check right cell
                        zeroCount += matrix[row][col + 1] == 0
                    if row - 1 >= 0 and col - 1 >= 0:
                        # Check top-left cell
                        zeroCount += matrix[row - 1][col - 1] == 0
                    if row - 1 >= 0 and col + 1 < numCols:
                        # Check top-right cell
                        zeroCount += matrix[row - 1][col + 1] == 0
                    if row + 1 < numRows and col - 1 >= 0:
                        # Check bottom-left cell
                        zeroCount += matrix[row + 1][col - 1] == 0
                    if row + 1 < numRows and col + 1 < numCols:
                        # Check bottom-right cell
                        zeroCount += matrix[row + 1][col + 1] == 0
 
                    if zeroCount % 2 == 0 and zeroCount != 0# If adjacent zeros are even and nonzero
                        result += 1  # Increment the result count
 
        return result  # Return the final count of desired cells
 
 
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)  # Calculate the result using the Solution class
        print(ans)  # Output the result
        tc -= 1


C#




using System;
 
class Solution
{
    public int Count(int[][] matrix)
    {
        int numRows = matrix.Length;
        int numCols = matrix[0].Length;
        int result = 0;
 
        // Iterate through each cell in the matrix
        for (int row = 0; row < numRows; ++row)
        {
            for (int col = 0; col < numCols; ++col)
            {
                if (matrix[row][col] == 1) // Check if the current cell is 1
                {
                    int zeroCount = 0; // Count of adjacent cells containing 0
 
                    // Check the surrounding 8 cells for zeros
                    if (row - 1 >= 0)
                        zeroCount += matrix[row - 1][col] == 0 ? 1 : 0; // Check top cell
                    if (row + 1 < numRows)
                        zeroCount += matrix[row + 1][col] == 0 ? 1 : 0; // Check bottom cell
                    if (col - 1 >= 0)
                        zeroCount += matrix[row][col - 1] == 0 ? 1 : 0; // Check left cell
                    if (col + 1 < numCols)
                        zeroCount += matrix[row][col + 1] == 0 ? 1 : 0; // Check right cell
                    if (row - 1 >= 0 && col - 1 >= 0)
                        zeroCount += matrix[row - 1][col - 1] == 0 ? 1 : 0; // Check top-left cell
                    if (row - 1 >= 0 && col + 1 < numCols)
                        zeroCount += matrix[row - 1][col + 1] == 0 ? 1 : 0; // Check top-right cell
                    if (row + 1 < numRows && col - 1 >= 0)
                        zeroCount += matrix[row + 1][col - 1] == 0 ? 1 : 0; // Check bottom-left cell
                    if (row + 1 < numRows && col + 1 < numCols)
                        zeroCount += matrix[row + 1][col + 1] == 0 ? 1 : 0; // Check bottom-right
                                                                            // cell
 
                    if (zeroCount % 2 == 0 && zeroCount > 0) // If adjacent zeros are even
                                                             // and nonzero
                        result++; // Increment the result count
                }
            }
        }
 
        return result; // Return the final count of desired cells
    }
}
 
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); // Calculate the result using the Solution class
            Console.WriteLine(ans); // Output the result
        }
    }
}


Javascript




class Solution {
    count(matrix) {
        const numRows = matrix.length;
        const numCols = matrix[0].length;
        let result = 0;
 
        // Iterate through each cell in the matrix
        for (let row = 0; row < numRows; ++row) {
            for (let col = 0; col < numCols; ++col) {
 
                // Check if the current cell is 1
                // Count adjacent cells containing 0
                if (matrix[row][col]) {
 
                    let zeroCount = 0;
 
                    // Check the surrounding 8 cells for zeroes
 
                    // Check top cell
                    if (row - 1 >= 0)
                        zeroCount += matrix[row - 1][col] == 0;
 
                    // Check bottom cell
                    if (row + 1 < numRows)
                        zeroCount += matrix[row + 1][col] == 0;
 
                    // Check left cell
                    if (col - 1 >= 0)
                        zeroCount += matrix[row][col - 1] == 0;
 
                    // Check right cell
                    if (col + 1 < numCols)
                        zeroCount += matrix[row][col + 1] == 0;
 
                    // Check top-left cell
                    if (row - 1 >= 0 && col - 1 >= 0)
                        zeroCount += matrix[row - 1][col - 1] == 0;
 
                    // Check top-right cell
                    if (row - 1 >= 0 && col + 1 < numCols)
                        zeroCount += matrix[row - 1][col + 1] == 0;
 
                    // Check bottom-left cell
                    if (row + 1 < numRows && col - 1 >= 0)
                        zeroCount += matrix[row + 1][col - 1] == 0;
 
                    // Check bottom-right cell
                    if (row + 1 < numRows && col + 1 < numCols)
                        zeroCount += matrix[row + 1][col + 1] == 0;
 
                    // If adjacent zeros are even and nonzero
                    if (!(zeroCount & 1) && zeroCount > 0)
 
                        // Increment the result count
                        result++;
                }
            }
        }
 
        // Return the final count of desired cells
        return result;
    }
}
 
// Driver code
const n = 3, m = 3;
const matrix = [[1, 0, 0], [1, 1, 0], [0, 1, 0]];
 
const ob = new Solution();
 
// Calculate the result using the Solution class
const ans = ob.count(matrix);
 
// Output the result
console.log(ans);


Output

1




Time Complexity: O(N*M)
Auxillary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads