Queries to count minimum flips required to fill a binary submatrix with 0s only
Last Updated :
01 Jun, 2021
Given a binary matrix mat[][] of size M * N and Q queries of the form {pi, pj, qi, qj}, the task for each query is to count the number of 0s in the submatrix from the cell (pi, pj) to (qi, qj).
Examples:
Input: mat[][] = {{0, 1, 0, 1, 1, 1, 0}, {1, 0, 1, 1, 1, 0, 1}, {1, 1, 0, 0, 1, 1, 0}, {1, 1, 1, 1, 1, 0, 1}, {0, 0, 1, 0, 1, 1, 1}, {1, 1, 0, 1, 1, 0, 1 }}, Q[] = {{0, 1, 3, 2}, {2, 2, 4, 5}, {4, 3, 5, 6}}
Output: 3 4 2
Explanation:
The submatrix from indices (0, 1) to (3, 2) contains 3 0s.
The submatrix from indices (2, 2) to (4, 5) contains 4 0s.
The submatrix from indices (4, 3) to (5, 6) contains 2 0s.
Input: mat[][] = {{0, 1, 0}, {1, 0, 1}}, Q[] = {{0, 0, 2, 2}}
Output: 3
Naive Approach: The simplest approach to solve the problem is to count the number of 0s for each query by iterating the submatrix and print the count for each query.
Time Complexity: O(M*N*Q)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to preprocess the given matrix mat[][]. Create a count matrix, say prefixCnt[M][N], such that prefix_cnt[i][j] stores the count of 0s in the submatrix within indices (0, 0) to (i, j). Follow the steps below to compute prefixCnt[][]:
- Initialize prefixCnt[i][j] with 1 if mat[i][j] is 0. Otherwise, initialize with 0.
- Find the prefix sum for each row and column and update the matrix accordingly.
Once, matrix prefixCnt[][] is computed the count of 0s in any sub-matrix can be evaluated in O(1) time. Below is the expression to count the 0s in each submatrix from (pi, pj) to (qi, qj) can be calculated by:
cnt = prefixCnt[qi][qj] – prefixCnt[pi-1][qj] – prefixCnt[qi][pj – 1] + prefixCnt[pi – 1][pj – 1]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 6
#define N 7
void preCompute( int mat[M][N],
int prefixCnt[M][N])
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
if (mat[i][j] == 0) {
prefixCnt[i][j] = 1;
}
else {
prefixCnt[i][j] = 0;
}
}
}
for ( int i = 0; i < M; i++)
for ( int j = 1; j < N; j++)
prefixCnt[i][j] += prefixCnt[i][j - 1];
for ( int i = 1; i < M; i++)
for ( int j = 0; j < N; j++)
prefixCnt[i][j] += prefixCnt[i - 1][j];
}
int countQuery( int prefixCnt[M][N],
int pi, int pj,
int qi, int qj)
{
int cnt = prefixCnt[qi][qj];
if (pi > 0)
cnt -= prefixCnt[pi - 1][qj];
if (pj > 0)
cnt -= prefixCnt[qi][pj - 1];
if (pi > 0 && pj > 0)
cnt += prefixCnt[pi - 1][pj - 1];
return cnt;
}
void count0s( int mat[M][N], int Q[][4],
int sizeQ)
{
int prefixCnt[M][N];
preCompute(mat, prefixCnt);
for ( int i = 0; i < sizeQ; i++) {
cout << countQuery(prefixCnt, Q[i][0],
Q[i][1], Q[i][2],
Q[i][3])
<< ' ' ;
}
}
int main()
{
int mat[M][N] = {
{ 0, 1, 0, 1, 1, 1, 0 },
{ 1, 0, 1, 1, 1, 0, 1 },
{ 1, 1, 0, 0, 1, 1, 0 },
{ 1, 1, 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 0, 1 }
};
int Q[][4] = { { 0, 1, 3, 2 },
{ 2, 2, 4, 5 },
{ 4, 3, 5, 6 } };
int sizeQ = sizeof (Q) / sizeof (Q[0]);
count0s(mat, Q, sizeQ);
return 0;
}
|
Java
class GFG{
final static int M = 6 ;
final static int N = 7 ;
static void preCompute( int mat[][], int prefixCnt[][])
{
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
if (mat[i][j] == 0 )
{
prefixCnt[i][j] = 1 ;
}
else
{
prefixCnt[i][j] = 0 ;
}
}
}
for ( int i = 0 ; i < M; i++)
for ( int j = 1 ; j < N; j++)
prefixCnt[i][j] += prefixCnt[i][j - 1 ];
for ( int i = 1 ; i < M; i++)
for ( int j = 0 ; j < N; j++)
prefixCnt[i][j] += prefixCnt[i - 1 ][j];
}
static int countQuery( int prefixCnt[][],
int pi, int pj,
int qi, int qj)
{
int cnt = prefixCnt[qi][qj];
if (pi > 0 )
cnt -= prefixCnt[pi - 1 ][qj];
if (pj > 0 )
cnt -= prefixCnt[qi][pj - 1 ];
if (pi > 0 && pj > 0 )
cnt += prefixCnt[pi - 1 ][pj - 1 ];
return cnt;
}
static void count0s( int mat[][], int Q[][],
int sizeQ)
{
int prefixCnt[][] = new int [M][N];
preCompute(mat, prefixCnt);
for ( int i = 0 ; i < sizeQ; i++)
{
System.out.print(countQuery(prefixCnt, Q[i][ 0 ],
Q[i][ 1 ],
Q[i][ 2 ],
Q[i][ 3 ]) + " " );
}
}
public static void main (String[] args)
{
int mat[][] = { { 0 , 1 , 0 , 1 , 1 , 1 , 0 },
{ 1 , 0 , 1 , 1 , 1 , 0 , 1 },
{ 1 , 1 , 0 , 0 , 1 , 1 , 0 },
{ 1 , 1 , 1 , 1 , 1 , 0 , 1 },
{ 0 , 0 , 1 , 0 , 1 , 1 , 1 },
{ 1 , 1 , 0 , 1 , 1 , 0 , 1 } };
int Q[][] = { { 0 , 1 , 3 , 2 },
{ 2 , 2 , 4 , 5 },
{ 4 , 3 , 5 , 6 } };
int sizeQ = Q.length;
count0s(mat, Q, sizeQ);
}
}
|
Python3
M = 6
N = 7
def preCompute(mat, prefixCnt):
for i in range (M):
for j in range (N):
if (mat[i][j] = = 0 ):
prefixCnt[i][j] = 1
else :
prefixCnt[i][j] = 0
for i in range (M):
for j in range ( 1 , N):
prefixCnt[i][j] + = prefixCnt[i][j - 1 ]
for i in range ( 1 , M):
for j in range (N):
prefixCnt[i][j] + = prefixCnt[i - 1 ][j]
return prefixCnt
def countQuery(prefixCnt, pi, pj, qi, qj):
cnt = prefixCnt[qi][qj]
if (pi > 0 ):
cnt - = prefixCnt[pi - 1 ][qj]
if (pj > 0 ):
cnt - = prefixCnt[qi][pj - 1 ]
if (pi > 0 and pj > 0 ):
cnt + = prefixCnt[pi - 1 ][pj - 1 ]
return cnt
def count0s(mat, Q, sizeQ):
prefixCnt = [[ 0 for i in range (N)] for i in range (M)]
prefixCnt = preCompute(mat, prefixCnt)
for i in range (sizeQ):
print (countQuery(prefixCnt, Q[i][ 0 ], Q[i][ 1 ], Q[i][ 2 ], Q[i][ 3 ]), end = " " )
if __name__ = = '__main__' :
mat = [[ 0 , 1 , 0 , 1 , 1 , 1 , 0 ],
[ 1 , 0 , 1 , 1 , 1 , 0 , 1 ],
[ 1 , 1 , 0 , 0 , 1 , 1 , 0 ],
[ 1 , 1 , 1 , 1 , 1 , 0 , 1 ],
[ 0 , 0 , 1 , 0 , 1 , 1 , 1 ],
[ 1 , 1 , 0 , 1 , 1 , 0 , 1 ] ]
Q = [ [ 0 , 1 , 3 , 2 ],
[ 2 , 2 , 4 , 5 ],
[ 4 , 3 , 5 , 6 ] ]
sizeQ = len (Q)
count0s(mat, Q, sizeQ)
|
C#
using System;
class GFG
{
static int M = 6;
static int N = 7;
static void preCompute( int [,]mat, int [,]prefixCnt)
{
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
if (mat[i, j] == 0)
{
prefixCnt[i, j] = 1;
}
else
{
prefixCnt[i, j] = 0;
}
}
}
for ( int i = 0; i < M; i++)
for ( int j = 1; j < N; j++)
prefixCnt[i, j] += prefixCnt[i, j - 1];
for ( int i = 1; i < M; i++)
for ( int j = 0; j < N; j++)
prefixCnt[i, j] += prefixCnt[i - 1, j];
}
static int countQuery( int [,]prefixCnt,
int pi, int pj,
int qi, int qj)
{
int cnt = prefixCnt[qi, qj];
if (pi > 0)
cnt -= prefixCnt[pi - 1, qj];
if (pj > 0)
cnt -= prefixCnt[qi, pj - 1];
if (pi > 0 && pj > 0)
cnt += prefixCnt[pi - 1, pj - 1];
return cnt;
}
static void count0s( int [,]mat, int [,]Q,
int sizeQ)
{
int [,]prefixCnt = new int [M, N];
preCompute(mat, prefixCnt);
for ( int i = 0; i < sizeQ; i++)
{
Console.Write(countQuery(prefixCnt, Q[i, 0],
Q[i, 1],
Q[i, 2],
Q[i, 3]) + " " );
}
}
public static void Main( string [] args)
{
int [,]mat = { { 0, 1, 0, 1, 1, 1, 0 },
{ 1, 0, 1, 1, 1, 0, 1 },
{ 1, 1, 0, 0, 1, 1, 0 },
{ 1, 1, 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 0, 1 } };
int [,]Q = { { 0, 1, 3, 2 },
{ 2, 2, 4, 5 },
{ 4, 3, 5, 6 } };
int sizeQ = Q.GetLength(0);
count0s(mat, Q, sizeQ);
}
}
|
Javascript
<script>
let M = 6;
let N = 7;
function preCompute(mat, prefixCnt)
{
for (let i = 0; i < M; i++)
{
for (let j = 0; j < N; j++)
{
if (mat[i][j] == 0)
{
prefixCnt[i][j] = 1;
}
else
{
prefixCnt[i][j] = 0;
}
}
}
for (let i = 0; i < M; i++)
for (let j = 1; j < N; j++)
prefixCnt[i][j] += prefixCnt[i][j - 1];
for (let i = 1; i < M; i++)
for (let j = 0; j < N; j++)
prefixCnt[i][j] += prefixCnt[i - 1][j];
}
function countQuery(prefixCnt,
pi, pj,
qi, qj)
{
let cnt = prefixCnt[qi][qj];
if (pi > 0)
cnt -= prefixCnt[pi - 1][qj];
if (pj > 0)
cnt -= prefixCnt[qi][pj - 1];
if (pi > 0 && pj > 0)
cnt += prefixCnt[pi - 1][pj - 1];
return cnt;
}
function count0s(mat, Q,
sizeQ)
{
let prefixCnt = new Array(M);
for ( var i = 0; i < prefixCnt.length; i++) {
prefixCnt[i] = new Array(2);
}
preCompute(mat, prefixCnt);
for (let i = 0; i < sizeQ; i++)
{
document.write(countQuery(prefixCnt, Q[i][0],
Q[i][1],
Q[i][2],
Q[i][3]) + " " );
}
}
let mat = [[ 0, 1, 0, 1, 1, 1, 0 ],
[ 1, 0, 1, 1, 1, 0, 1 ],
[ 1, 1, 0, 0, 1, 1, 0 ],
[ 1, 1, 1, 1, 1, 0, 1 ],
[ 0, 0, 1, 0, 1, 1, 1 ],
[ 1, 1, 0, 1, 1, 0, 1 ]];
let Q = [[ 0, 1, 3, 2 ],
[ 2, 2, 4, 5 ],
[ 4, 3, 5, 6 ]];
let sizeQ = Q.length;
count0s(mat, Q, sizeQ);
</script>
|
Time Complexity: O(M*N + Q)
Auxiliary Space: O(M*N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...