Count of ways to select K consecutive empty cells from a given Matrix
Last Updated :
31 May, 2021
Given a binary matrix V[][] of dimensions N * M, wherein each cell is either empty or blocked marked by a 0 and 1 respectively, the task is to count the number of ways to select K consecutive empty cells from the same row or column.
Examples:
Input: V[][] = {{1, 1, 0}, {0, 0, 0}}, K = 2
Output: 3
Explanation:
Considering 1-based indexing, 2 consecutive empty cells can be selected in the following ways:
[(1, 3), (2, 3)], [(2, 1), (2, 2)], [(2, 2), (2, 3)]
Input: V[][] = {{1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, K = 1
Output: 9
Explanation:
It is possible to select all the cells since every cell is empty.
Approach:
The idea is to traverse the matrix row-wise and for each row, count the total number of empty consecutive cells.
Follow the steps below to solve the problem:
- Traverse the matrix row-wise and count the number of consecutive cells. If the count becomes equal to or exceeds K, increase the count by 1.
- Every time a blocked cell is encountered, reset the count of consecutive empty cells to 0.
- Repeat the above steps while traversing the matrix column-wise as well if K ? 1.
- Return the final count of cells obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int rowWise( char * v, int n,
int m, int k)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int countcons = 0;
for ( int j = 0; j < m; j++) {
if (*(v + i * m + j) == '1' ) {
countcons = 0;
}
else {
countcons++;
}
if (countcons >= k) {
ans++;
}
}
}
return ans;
}
int colWise( char * v, int n,
int m, int k)
{
int ans = 0;
for ( int i = 0; i < m; i++) {
int countcons = 0;
for ( int j = 0; j < n; j++) {
if (*(v + j * n + i) == '1' ) {
countcons = 0;
}
else {
countcons++;
}
if (countcons >= k) {
ans++;
}
}
}
return ans;
}
int main()
{
int n = 3, m = 3, k = 1;
char v[n][m] = { '0' , '0' , '0' ,
'0' , '0' , '0' ,
'0' , '0' , '0' };
if (k == 1) {
cout << rowWise(v[0], n, m, k);
}
else {
cout << colWise(v[0], n, m, k)
+ rowWise(v[0], n,
m, k);
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static int rowWise( char [][]v, int n,
int m, int k)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
int countcons = 0 ;
for ( int j = 0 ; j < m; j++)
{
if (v[i][j] == '1' )
{
countcons = 0 ;
}
else
{
countcons++;
}
if (countcons >= k)
{
ans++;
}
}
}
return ans;
}
static int colWise( char [][]v, int n,
int m, int k)
{
int ans = 0 ;
for ( int i = 0 ; i < m; i++)
{
int countcons = 0 ;
for ( int j = 0 ; j < n; j++)
{
if (v[j][i] == '1' )
{
countcons = 0 ;
}
else
{
countcons++;
}
if (countcons >= k)
{
ans++;
}
}
}
return ans;
}
public static void main(String[] args)
{
int n = 3 , m = 3 , k = 1 ;
char v[][] = { { '0' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
if (k == 1 )
{
System.out.print(rowWise(v, n, m, k));
}
else
{
System.out.print(colWise(v, n, m, k) +
rowWise(v, n, m, k));
}
}
}
|
Python3
def rowWise(v, n, m, k):
ans = 0
for i in range (n):
countcons = 0
for j in range (m):
if (v[i][j] = = '1' ):
countcons = 0
else :
countcons + = 1
if (countcons > = k):
ans + = 1
return ans
def colWise(v, n, m, k):
ans = 0
for i in range (m):
countcons = 0
for j in range (n):
if (v[j][i] = = '1' ):
countcons = 0
else :
countcons + = 1
if (countcons > = k):
ans + = 1
return ans
if __name__ = = "__main__" :
n = 3
m = 3
k = 1
v = [[ '0' , '0' , '0' ],
[ '0' , '0' , '0' ],
[ '0' , '0' , '0' ]]
if (k = = 1 ):
print (rowWise(v, n, m, k))
else :
print (colWise(v, n, m, k) +
rowWise(v, n, m, k))
|
C#
using System;
class GFG{
static int rowWise( char [,]v, int n,
int m, int k)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
int countcons = 0;
for ( int j = 0; j < m; j++)
{
if (v[i, j] == '1' )
{
countcons = 0;
}
else
{
countcons++;
}
if (countcons >= k)
{
ans++;
}
}
}
return ans;
}
static int colWise( char [,]v, int n,
int m, int k)
{
int ans = 0;
for ( int i = 0; i < m; i++)
{
int countcons = 0;
for ( int j = 0; j < n; j++)
{
if (v[j, i] == '1' )
{
countcons = 0;
}
else
{
countcons++;
}
if (countcons >= k)
{
ans++;
}
}
}
return ans;
}
public static void Main(String[] args)
{
int n = 3, m = 3, k = 1;
char [,]v = { { '0' , '0' , '0' },
{ '0' , '0' , '0' },
{ '0' , '0' , '0' } };
if (k == 1)
{
Console.Write(rowWise(v, n, m, k));
}
else
{
Console.Write(colWise(v, n, m, k) +
rowWise(v, n, m, k));
}
}
}
|
Javascript
<script>
function rowWise(v, n, m, k)
{
var ans = 0;
for ( var i = 0; i < n; i++) {
var countcons = 0;
for ( var j = 0; j < m; j++) {
if ((v + i * m + j) == '1' ) {
countcons = 0;
}
else {
countcons++;
}
if (countcons >= k) {
ans++;
}
}
}
return ans;
}
function colWise(v, n, m, k)
{
var ans = 0;
for ( var i = 0; i < m; i++) {
var countcons = 0;
for ( var j = 0; j < n; j++) {
if ((v + j * n + i) == '1' ) {
countcons = 0;
}
else {
countcons++;
}
if (countcons >= k) {
ans++;
}
}
}
return ans;
}
var n = 3, m = 3, k = 1;
var v = [ '0' , '0' , '0' ,
'0' , '0' , '0' ,
'0' , '0' , '0' ];
if (k == 1) {
document.write( rowWise(v[0], n, m, k));
}
else {
document.write( colWise(v[0], n, m, k)
+ rowWise(v[0], n,
m, k));
}
</script>
|
Time Complexity: O(N * M)
Space Complexity: O(N * M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...