21. Surround the 1's
The problem can be found at the following link: Question Link
My Approach
This is a simple brute force approach, where it iterates through each cell, and check its surrounding eight cells, and increments a counter if the count of zeros in the surroundings is divisible by 2. Steps for implementing this solution.
Initialize arrays
dx
anddy
to represent the possible directions: (-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), and (1, 1).Define a function
isValid
to check if a given position is within the boundaries of the matrix.Iterate through each cell in the matrix.
If the cell value is 1, calculate the count
c
of adjacent 0's.If
c
is non-zero and even, increment thecnt
variable.
Return the
cnt
variable as the result.
Explanation with Example
Consider the following matrix:
Cell (0, 1) has three neighboring 0's.
Cell (1, 1) has five neighboring 0's.
Cell (1, 2) has four neighboring 0's.
Cell (1, 4) has four neighboring 0's.
Cell (2, 2) has five neighboring 0's.
Cell (2, 3) has five neighboring 0.
Since the counts are even for (1, 2), and (1, 4) we increment cnt
by 2.
Time and Auxiliary Space Complexity
Time Complexity: The code iterates through each cell once and checks its neighboring cells, resulting in
O(n * m)
, wheren
is the number of rows andm
is the number of columns in the matrix.Auxiliary Space Complexity: The code uses a constant amount of extra space for the
dx
anddy
arrays, as well as a few integer variables. Hence, the auxiliary space complexity isO(1)
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated