Check for Zero Sub-Matrix in 2D Grid
Last Updated :
09 Jan, 2024
Given a 2D array Grid[][] of N rows and M columns along with two integers X and Y, then your task is to output YES, If there exists a sub-matrix of dimensions X and Y containing 0’s only else NO.
Examples:
Input: N = 3, M = 4, grid[][] = {{0, 0, 1, 1}, {0, 0, 1, 1}, {0, 0, 0, 0}}, X = 2, Y = 2
Output: YES
Explanation: The sub-matrix formed by first two rows and columns contains all 0s and has dimensions as 2*2. Thus, output is YES.
Input: N = 1, M = 1, grid[][] = {{1}}, X = 1, Y = 1
Output: NO
Explanation: It can be verified that there is no sub-matrix of size 1*1 in given matrix, which contains all 0s.
Approach: To solve the problem follow the below idea:
This can be solved by counting the number of 1’s at each point in a new grid. For each X and Y space we can iterate in a grid, If value comes out to be 0. Then grid of X*Y dimensions containing all 0’s exists. We can solve this problem in O(NxM) using Prefix Sum.
Below are the steps involved:
- Create a new grid let say G[][] of size [N + 1][M + 1].
- As the idea is to count the number of 1’s at each point, Therefore, follow the below formula:
- G [i][j] = grid[i – 1][j – 1] + G[i – 1][j] + G[i][j – 1] – G[i – 1][j – 1].
- For checking the possibility of the existence of a grid, We can again iterate in Prefix Sum Grid and check for 1’s in each X*Y submatrix:
- Formula to check number of 1’s in the submatrix: G[i + X][j + Y] – G[i + X][j] – G[i][j + Y] + G[i][j].
- If the value for above equation comes out to be 0
- Else
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
bool findSpace(vector<vector< int > > grid, int N, int M,
int X, int Y)
{
int G[N + 1][M + 1];
for ( int i = 1; i <= N; i++)
for ( int j = 1; j <= M; j++)
G[i][j] = grid[i - 1][j - 1] + G[i - 1][j]
+ G[i][j - 1] - G[i - 1][j - 1];
for ( int i = 0; i + X <= N; i++)
for ( int j = 0; j + Y <= M; j++) {
int ones = G[i + X][j + Y] - G[i + X][j]
- G[i][j + Y] + G[i][j];
if (ones == 0)
return true ;
}
return false ;
}
int main()
{
int N = 4;
int M = 4;
vector<vector< int > > grid = { { 0, 0, 1, 1 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
int X = 2;
int Y = 4;
bool result = findSpace(grid, N, M, X, Y);
cout << boolalpha << result;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static boolean findSpace( int [][] grid, int N, int M,
int X, int Y)
{
int [][] G = new int [N + 1 ][M + 1 ];
for ( int i = 1 ; i <= N; i++)
for ( int j = 1 ; j <= M; j++)
G[i][j] = grid[i - 1 ][j - 1 ] + G[i - 1 ][j]
+ G[i][j - 1 ] - G[i - 1 ][j - 1 ];
for ( int i = 0 ; i + X <= N; i++)
for ( int j = 0 ; j + Y <= M; j++) {
int ones = G[i + X][j + Y] - G[i + X][j]
- G[i][j + Y] + G[i][j];
if (ones == 0 )
return true ;
}
return false ;
}
public static void main(String[] args)
{
int N = 4 ;
int M = 4 ;
int [][] grid = { { 0 , 0 , 1 , 1 },
{ 0 , 0 , 1 , 1 },
{ 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 0 } };
int X = 2 ;
int Y = 4 ;
boolean result = findSpace(grid, N, M, X, Y);
System.out.println(result);
}
}
|
Python3
def find_space(grid, N, M, X, Y):
G = [[ 0 ] * (M + 1 ) for _ in range (N + 1 )]
for i in range ( 1 , N + 1 ):
for j in range ( 1 , M + 1 ):
G[i][j] = grid[i - 1 ][j - 1 ] + G[i - 1 ][j] + G[i][j - 1 ] - G[i - 1 ][j - 1 ]
for i in range (N - X + 1 ):
for j in range (M - Y + 1 ):
ones = G[i + X][j + Y] - G[i + X][j] - G[i][j + Y] + G[i][j]
if ones = = 0 :
return True
return False
if __name__ = = "__main__" :
N = 4
M = 4
grid = [[ 0 , 0 , 1 , 1 ],
[ 0 , 0 , 1 , 1 ],
[ 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ]]
X = 2
Y = 4
result = find_space(grid, N, M, X, Y)
print (result)
|
C#
using System;
public class GFG {
static bool FindSpace( int [, ] grid, int N, int M, int X,
int Y)
{
int [, ] G = new int [N + 1, M + 1];
for ( int i = 1; i <= N; i++) {
for ( int j = 1; j <= M; j++) {
G[i, j] = grid[i - 1, j - 1] + G[i - 1, j]
+ G[i, j - 1] - G[i - 1, j - 1];
}
}
for ( int i = 0; i + X <= N; i++) {
for ( int j = 0; j + Y <= M; j++) {
int ones = G[i + X, j + Y] - G[i + X, j]
- G[i, j + Y] + G[i, j];
if (ones == 0) {
return true ;
}
}
}
return false ;
}
static void Main()
{
int N = 4;
int M = 4;
int [, ] grid = { { 0, 0, 1, 1 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
int X = 2;
int Y = 4;
bool result = FindSpace(grid, N, M, X, Y);
Console.WriteLine(result);
}
}
|
Javascript
function findSpace(grid, N, M, X, Y) {
let G = new Array(N + 1).fill( null ).map(() => new Array(M + 1).fill(0));
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= M; j++) {
G[i][j] = grid[i - 1][j - 1] + G[i - 1][j] + G[i][j - 1] - G[i - 1][j - 1];
}
}
for (let i = 0; i + X <= N; i++) {
for (let j = 0; j + Y <= M; j++) {
let ones = G[i + X][j + Y] - G[i + X][j] - G[i][j + Y] + G[i][j];
if (ones === 0) {
return true ;
}
}
}
return false ;
}
let N = 4;
let M = 4;
let grid = [
[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
let X = 2;
let Y = 4;
let result = findSpace(grid, N, M, X, Y);
console.log(result);
|
Time Complexity: O(N * M), As two nested loops are there.
Auxiliary Space: O(N * M), As an extra 2D matrix G[][] of dimensions N+1 and M+1 is used.
Share your thoughts in the comments
Please Login to comment...