Count of ways to select K consecutive empty cells from a given Matrix

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++
// C++ program to find no of ways// to select K consecutive empty// cells from a row or column#include <bits/stdc++.h>using namespace std;Â
// Function to Traverse// the matrix row wiseint rowWise(char* v, int n,            int m, int k){    // Initialize ans    int ans = 0;Â
    // Traverse row wise    for (int i = 0; i < n; i++) {Â
        // Initialize no of        // consecutive empty        // cells        int countcons = 0;Â
        for (int j = 0; j < m; j++) {Â
            // Check if blocked cell is            // encountered then reset            // countcons to 0            if (*(v + i * m + j) == '1') {                countcons = 0;            }Â
            // Check if empty cell is            // encountered, then            // increment countcons            else {                countcons++;            }Â
            // Check if number of empty            // consecutive cells            // is greater or equal            // to K, increment the ans            if (countcons >= k) {                ans++;            }        }    }Â
    // Return the count    return ans;}Â
// Function to Traverse the// matrix column wiseint colWise(char* v, int n,            int m, int k){    // Initialize ans    int ans = 0;Â
    // Traverse column wise    for (int i = 0; i < m; i++) {Â
        // Initialize no of        // consecutive empty cells        int countcons = 0;Â
        for (int j = 0; j < n; j++) {Â
            // Check if blocked cell is            // encountered then reset            // countcons to 0            if (*(v + j * n + i) == '1') {                countcons = 0;            }Â
            // Check if empty cell is            // encountered, increment            // countcons            else {                countcons++;            }Â
            // Check if number of empty            // consecutive cells            // is greater than or equal            // to K, increment the ans            if (countcons >= k) {                ans++;            }        }    }Â
    // Return the count    return ans;}Â
// Driver Codeint main(){Â
    int n = 3, m = 3, k = 1;Â
    char v[n][m] = { '0', '0', '0',                     '0', '0', '0',                     '0', '0', '0' };Â
    // If k = 1 only traverse row wise    if (k == 1) {        cout << rowWise(v[0], n, m, k);    }Â
    // Traverse both row and column wise    else {        cout << colWise(v[0], n, m, k)                    + rowWise(v[0], n,                              m, k);    }Â
    return 0;} |
Java
// Java program to find no of ways// to select K consecutive empty// cells from a row or columnimport java.util.*;Â
class GFG{Â
// Function to Traverse// the matrix row wisestatic int rowWise(char [][]v, int n,                        int m, int k){         // Initialize ans    int ans = 0;Â
    // Traverse row wise    for(int i = 0; i < n; i++)    {Â
        // Initialize no of        // consecutive empty        // cells        int countcons = 0;Â
        for(int j = 0; j < m; j++)         {Â
            // Check if blocked cell is            // encountered then reset            // countcons to 0            if (v[i][j] == '1')             {                countcons = 0;            }Â
            // Check if empty cell is            // encountered, then            // increment countcons            else            {                countcons++;            }Â
            // Check if number of empty            // consecutive cells            // is greater or equal            // to K, increment the ans            if (countcons >= k)            {                ans++;            }        }    }Â
    // Return the count    return ans;}Â
// Function to Traverse the// matrix column wisestatic int colWise(char [][]v, int n,                        int m, int k){         // Initialize ans    int ans = 0;Â
    // Traverse column wise    for(int i = 0; i < m; i++)     {Â
        // Initialize no of        // consecutive empty cells        int countcons = 0;Â
        for(int j = 0; j < n; j++)        {                         // Check if blocked cell is            // encountered then reset            // countcons to 0            if (v[j][i] == '1')            {                countcons = 0;            }Â
            // Check if empty cell is            // encountered, increment            // countcons            else            {                countcons++;            }Â
            // Check if number of empty            // consecutive cells            // is greater than or equal            // to K, increment the ans            if (countcons >= k)            {                ans++;            }        }    }Â
    // Return the count    return ans;}Â
// Driver Codepublic 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 only traverse row wise    if (k == 1)     {        System.out.print(rowWise(v, n, m, k));    }Â
    // Traverse both row and column wise    else    {        System.out.print(colWise(v, n, m, k) +                         rowWise(v, n, m, k));    }}}Â
// This code is contributed by amal kumar choubey |
Python3
# Python 3 program to find no of ways# to select K consecutive empty# cells from a row or columnÂ
# Function to Traverse# the matrix row wisedef rowWise(v, n, m, k):Â
    # Initialize ans    ans = 0Â
    # Traverse row wise    for i in range (n):Â
        # Initialize no of        # consecutive empty        # cells        countcons = 0Â
        for j in range (m):Â
            # Check if blocked cell is            # encountered then reset            # countcons to 0            if (v[i][j] == '1'):                countcons = 0                       # Check if empty cell is            # encountered, then            # increment countcons            else:                countcons += 1Â
            # Check if number of empty            # consecutive cells            # is greater or equal            # to K, increment the ans            if (countcons >= k):                ans += 1        # Return the count    return ansÂ
# Function to Traverse the# matrix column wisedef colWise(v, n, m, k):Â
    # Initialize ans    ans = 0Â
    # Traverse column wise    for i in range (m):Â
        # Initialize no of        # consecutive empty cells        countcons = 0                 for j in range (n):Â
            # Check if blocked cell is            # encountered then reset            # countcons to 0            if (v[j][i] == '1'):                countcons = 0                        # Check if empty cell is            # encountered, increment            # countcons            else:                countcons += 1                        # Check if number of empty            # consecutive cells            # is greater than or equal            # to K, increment the ans            if (countcons >= k):                ans += 1                # Return the count    return ansÂ
# Driver Codeif __name__ == "__main__":Â
    n = 3    m = 3    k = 1Â
    v = [['0', '0', '0'],         ['0', '0', '0'],         ['0', '0', '0']]Â
    # If k = 1 only     # traverse row wise    if (k == 1):        print (rowWise(v, n, m, k))        # Traverse both row     # and column wise    else:        print (colWise(v, n, m, k) +               rowWise(v, n, m, k))     # This code is contributed by Chitranayal |
C#
// C# program to find no of ways // to select K consecutive empty // cells from a row or column using System;Â
class GFG{ Â
// Function to Traverse // the matrix row wise static int rowWise(char [,]v, int n,                        int m, int k) {          // Initialize ans     int ans = 0; Â
    // Traverse row wise     for(int i = 0; i < n; i++)     { Â
        // Initialize no of         // consecutive empty         // cells         int countcons = 0; Â
        for(int j = 0; j < m; j++)         { Â
            // Check if blocked cell is             // encountered then reset             // countcons to 0             if (v[i, j] == '1')             {                 countcons = 0;             } Â
            // Check if empty cell is             // encountered, then             // increment countcons             else            {                 countcons++;             } Â
            // Check if number of empty             // consecutive cells             // is greater or equal             // to K, increment the ans             if (countcons >= k)             {                 ans++;             }         }     } Â
    // Return the count     return ans; } Â
// Function to Traverse the // matrix column wise static int colWise(char [,]v, int n,                        int m, int k) {          // Initialize ans     int ans = 0; Â
    // Traverse column wise     for(int i = 0; i < m; i++)     { Â
        // Initialize no of         // consecutive empty cells         int countcons = 0; Â
        for(int j = 0; j < n; j++)         {                          // Check if blocked cell is             // encountered then reset             // countcons to 0             if (v[j, i] == '1')             {                 countcons = 0;             } Â
            // Check if empty cell is             // encountered, increment             // countcons             else            {                 countcons++;             } Â
            // Check if number of empty             // consecutive cells             // is greater than or equal             // to K, increment the ans             if (countcons >= k)             {                 ans++;             }         }     } Â
    // Return the count     return ans; } Â
// Driver Code 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 only traverse row wise     if (k == 1)     {         Console.Write(rowWise(v, n, m, k));     } Â
    // Traverse both row and column wise     else    {         Console.Write(colWise(v, n, m, k) +                       rowWise(v, n, m, k));     } } } Â
// This code is contributed by amal kumar choubey |
Javascript
<script>Â
// Javascript program to find no of ways// to select K consecutive empty// cells from a row or columnÂ
// Function to Traverse// the matrix row wisefunction rowWise(v, n, m, k){    // Initialize ans    var ans = 0;Â
    // Traverse row wise    for (var i = 0; i < n; i++) {Â
        // Initialize no of        // consecutive empty        // cells        var countcons = 0;Â
        for (var j = 0; j < m; j++) {Â
            // Check if blocked cell is            // encountered then reset            // countcons to 0            if ((v + i * m + j) == '1') {                countcons = 0;            }Â
            // Check if empty cell is            // encountered, then            // increment countcons            else {                countcons++;            }Â
            // Check if number of empty            // consecutive cells            // is greater or equal            // to K, increment the ans            if (countcons >= k) {                ans++;            }        }    }Â
    // Return the count    return ans;}Â
// Function to Traverse the// matrix column wisefunction colWise(v, n, m, k){    // Initialize ans    var ans = 0;Â
    // Traverse column wise    for (var i = 0; i < m; i++) {Â
        // Initialize no of        // consecutive empty cells        var countcons = 0;Â
        for (var j = 0; j < n; j++) {Â
            // Check if blocked cell is            // encountered then reset            // countcons to 0            if ((v + j * n + i) == '1') {                countcons = 0;            }Â
            // Check if empty cell is            // encountered, increment            // countcons            else {                countcons++;            }Â
            // Check if number of empty            // consecutive cells            // is greater than or equal            // to K, increment the ans            if (countcons >= k) {                ans++;            }        }    }Â
    // Return the count    return ans;}Â
// Driver Codevar n = 3, m = 3, k = 1;var v = ['0', '0', '0',                 '0', '0', '0',                 '0', '0', '0'];                  // If k = 1 only traverse row wiseif (k == 1) {    document.write( rowWise(v[0], n, m, k));  }Â
// Traverse both row and column wiseelse {    document.write( colWise(v[0], n, m, k)                + rowWise(v[0], n,                          m, k));}Â
// This code is contributed by itsok.</script> |
9
Â
Time Complexity: O(N * M)Â
Space Complexity: O(N * M)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



