Number of submatrices with all 1s

22 Jun, 2022
Given an N*N matrix containing only 0s and 1s, the task is to count the number of submatrices containing all 1s.
Examples:  
Input : arr[][] = {{1, 1, 1},
                   {1, 1, 1},
                   {1, 1, 1}}
Output : 36
Explanation: All the possible submatrices will have only 1s.
Since, there are 36 submatrices in total, ans = 36

Input : {{1, 1, 1},
                   {1, 0, 1},
                   {1, 1, 1}}
Output : 20 
For simplicity, we will say a sub-matrix starts from an index (R, C) if that particular index is its top-left corner.A Simple Solution will be to generate all the possible sub-matrices and then check if all the values inside them are 1. If for a sub-matrix, all the elements are one, we increment the value of the final answer for one. The time complexity of the above approach is O(n6).
Better Approach: To optimize the process, for every index of the matrix, we will try to find the number of submatrices starting from that index having all 1s in it.Our first step towards solving this problem is creating a matrix ‘p_arr’. For each index (R, C), if arr[R][C] equals 1, then in p_arr[R][C], we will store the number of 1s to the right of the cell(R, C) along row ‘R’ before we encounter first zero or end of the array plus 1. If arr[R][C] equals zero, the p_arr[R][C] also equals zero.For creating this matrix, we will use the following recurrence relation. 
IF arr[R][C] is not 0
    p_arr[R][C] = p_arr[R][C+1] + 1
ELSE 
    p_arr[R][C] = 0

arr[][] = {{1, 0, 1, 1},
           {0, 1, 0, 1},
           {1, 1, 1, 0},
           {1, 0, 1, 1}}
p_arr[][] for above will look like
          {{1, 0, 2, 1},
           {0, 1, 0, 1},
           {3, 2, 1, 0},
           {1, 0, 2, 1}}
Once, we have the required matrix p_arr, we will proceed towards the next step. Look at the matrix ‘p_arr’ column-wise. If we are processing the jth column of the matrix p_arr, then for each element ‘i’ of this column, we will try to find the number of sub-matrices starting from the cell (i, j) with all 1s.For this, we can use the stack data structure.
Algorithm: 
Initialize a stack ‘q’ to store the value of the elements getting pushed along with the count(Cij) of the number of elements that were pushed in this stack with a value strictly greater than the value of the current element. We will use a pair to tie up the two data together. Initialize a variable ‘to_sum’ with 0. At each step, this variable is updated to store the number of submatrices with all 1s starting from the element being pushed at that step. Thus, using ‘to_sum’, we update the count of the number of submatrices with all 1s at each step.For a column ‘j’, at any step ‘i’, we will prepare to push p_arr[i][j] in the stack. Let Qt represent the topmost element of the stack and Ct represent the number of elements previously pushed in the stack with a value greater than the top-most element of the stack. Before pushing an element ‘p_arr[i][j]’ in the stack, while the stack is not empty or topmost element is greater than the number to be pushed, keep popping the topmost element of the stack and at the same time update to_sum as to_sum += (Ct + 1) * (Qt – p_arr[i][j]). Let Ci, represent the number of elements greater than the current element that was pushed in this stack previously. We also need to keep a track of Ci, j. Thus, before popping an element, we update Ci, j as Ci, j += Ct along with to_sum.We update the answer as ans += to_sum.Finally, we push that element in the stack after pairing it with Ci, j.
Initialize a stack ‘q’ to store the value of the elements getting pushed along with the count(Cij) of the number of elements that were pushed in this stack with a value strictly greater than the value of the current element. We will use a pair to tie up the two data together. Initialize a variable ‘to_sum’ with 0. At each step, this variable is updated to store the number of submatrices with all 1s starting from the element being pushed at that step. Thus, using ‘to_sum’, we update the count of the number of submatrices with all 1s at each step.
For a column ‘j’, at any step ‘i’, we will prepare to push p_arr[i][j] in the stack. Let Qt represent the topmost element of the stack and Ct represent the number of elements previously pushed in the stack with a value greater than the top-most element of the stack. Before pushing an element ‘p_arr[i][j]’ in the stack, while the stack is not empty or topmost element is greater than the number to be pushed, keep popping the topmost element of the stack and at the same time update to_sum as to_sum += (Ct + 1) * (Qt – p_arr[i][j]). Let Ci, represent the number of elements greater than the current element that was pushed in this stack previously. We also need to keep a track of Ci, j. Thus, before popping an element, we update Ci, j as Ci, j += Ct along with to_sum.
We update the answer as ans += to_sum.
Finally, we push that element in the stack after pairing it with Ci, j.
Create the prefix-array in O(n2) and for each column, we push an element in the stack or pop it out only once. Thus, the time complexity of this algorithm is O(n2).
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to count number of// sub-matrices with all 1s #include <iostream>#include <stack> using namespace std; #define n 3 // Function to find required prefix-count for// each row from right to leftvoid findPrefixCount(int p_arr[][n], bool arr[][n]){    for (int i = 0; i < n; i++) {        for (int j = n - 1; j >= 0; j--) {             if (!arr[i][j])                continue;             if (j != n - 1)                p_arr[i][j] += p_arr[i][j + 1];             p_arr[i][j] += (int)arr[i][j];        }    }} // Function to count the number of// sub-matrices with all 1sint matrixAllOne(bool arr[][n]){    // Array to store required prefix count of    // 1s from right to left for boolean array    int p_arr[n][n] = { 0 };     findPrefixCount(p_arr, arr);     // variable to store the final answer    int ans = 0;     /*  Loop to evaluate each column of        the prefix matrix uniquely.        For each index of a column we will try to        determine the number of sub-matrices        starting from that index        and has all 1s */    for (int j = 0; j < n; j++) {         int i = n - 1;         /*  Stack to store elements and the count            of the numbers they popped                         First part of pair will be the            value of inserted element.            Second part will be the count            of the number of elements pushed            before with a greater value */        stack<pair<int, int> > q;         // variable to store the number of        // submatrices with all 1s        int to_sum = 0;         while (i >= 0) {             int c = 0;             while (q.size() != 0 and q.top().first > p_arr[i][j]) {                 to_sum -= (q.top().second + 1) *                            (q.top().first - p_arr[i][j]);                 c += q.top().second + 1;                q.pop();            }             to_sum += p_arr[i][j];             ans += to_sum;             q.push({ p_arr[i][j], c });             i--;        }    }     return ans;} // Driver Codeint main(){    bool arr[][n] = { { 1, 1, 0 },                      { 1, 0, 1 },                      { 0, 1, 1 } };     cout << matrixAllOne(arr);     return 0;}
// Java program to count number of// sub-matrices with all 1simport java.util.*;class GFG{     static int n = 3;static class pair{    int first, second;    public pair(int first, int second)    {        this.first = first;        this.second = second;    }} // Function to find required prefix-count for// each row from right to leftstatic void findPrefixCount(int p_arr[][],                            boolean arr[][]){    for (int i = 0; i < n; i++)    {        for (int j = n - 1; j >= 0; j--)        {            if (!arr[i][j])                continue;             if (j != n - 1)                p_arr[i][j] += p_arr[i][j + 1];             p_arr[i][j] += arr[i][j] == true ? 1 : 0;        }    }} // Function to count the number of// sub-matrices with all 1sstatic int matrixAllOne(boolean arr[][]){    // Array to store required prefix count of    // 1s from right to left for boolean array    int [][]p_arr = new int[n][n];     findPrefixCount(p_arr, arr);     // variable to store the final answer    int ans = 0;     /* Loop to evaluate each column of        the prefix matrix uniquely.        For each index of a column we will try to        determine the number of sub-matrices        starting from that index        and has all 1s */    for (int j = 0; j < n; j++)    {        int i = n - 1;         /* Stack to store elements and the count            of the numbers they popped                         First part of pair will be the            value of inserted element.            Second part will be the count            of the number of elements pushed            before with a greater value */        Stack<pair> q = new Stack<pair>();         // variable to store the number of        // submatrices with all 1s        int to_sum = 0;         while (i >= 0)        {            int c = 0;             while (q.size() != 0 &&                   q.peek().first > p_arr[i][j])            {                to_sum -= (q.peek().second + 1) *                          (q.peek().first - p_arr[i][j]);                 c += q.peek().second + 1;                q.pop();            }             to_sum += p_arr[i][j];             ans += to_sum;             q.add(new pair(p_arr[i][j], c));             i--;        }    }    return ans;} // Driver Codepublic static void main(String[] args){    boolean arr[][] = { { true, true, false },                        { true, false, true },                        { false, true, true } };     System.out.println(matrixAllOne(arr));}} // This code is contributed by PrinciRaj1992
# Python3 program to count the number# of sub-matrices with all 1s # Function to find required prefix-count# for each row from right to leftdef findPrefixCount(p_arr, arr):     for i in range(0, n):        for j in range(n - 1, -1, -1):             if not arr[i][j]:                continue             if j != n - 1:                p_arr[i][j] += p_arr[i][j + 1]             p_arr[i][j] += arr[i][j]         # Function to count the number of# sub-matrices with all 1sdef matrixAllOne(arr):     # Array to store required prefix count of    # 1s from right to left for boolean array    p_arr = [[0 for i in range(n)] for j in range(n)]     findPrefixCount(p_arr, arr)     # variable to store the final answer    ans = 0     # Loop to evaluate each column of    # the prefix matrix uniquely.    # For each index of a column we will try to    # determine the number of sub-matrices    # starting from that index and has all 1s    for j in range(0, n):         i = n - 1         # Stack to store elements and the count        # of the numbers they popped                     # First part of pair will be the        # value of inserted element.        # Second part will be the count        # of the number of elements pushed        # before with a greater value */        q = []         # variable to store the number of        # submatrices with all 1s        to_sum = 0         while i >= 0:             c = 0            while len(q) != 0 and q[-1][0] > p_arr[i][j]:                 to_sum -= (q[-1][1] + 1) * \                            (q[-1][0] - p_arr[i][j])                 c += q[-1][1] + 1                q.pop()                         to_sum += p_arr[i][j]            ans += to_sum             q.append((p_arr[i][j], c))            i -= 1             return ans # Driver Codeif __name__ == "__main__":     arr = [[1, 1, 0], [1, 0, 1], [0, 1, 1]]                     n = 3    print(matrixAllOne(arr)) # This code is contributed by Rituraj Jain
// C# program to count number of// sub-matrices with all 1susing System;using System.Collections.Generic;     class GFG{static int n = 3;class pair{    public int first, second;    public pair(int first, int second)    {        this.first = first;        this.second = second;    }} // Function to find required prefix-count for// each row from right to leftstatic void findPrefixCount(int [,]p_arr,                            Boolean [,]arr){    for (int i = 0; i < n; i++)    {        for (int j = n - 1; j >= 0; j--)        {            if (!arr[i, j])                continue;             if (j != n - 1)                p_arr[i, j] += p_arr[i, j + 1];             p_arr[i, j] += arr[i, j] == true ? 1 : 0;        }    }} // Function to count the number of// sub-matrices with all 1sstatic int matrixAllOne(Boolean [,]arr){    // Array to store required prefix count of    // 1s from right to left for boolean array    int [,]p_arr = new int[n, n];     findPrefixCount(p_arr, arr);     // variable to store the final answer    int ans = 0;     /*  Loop to evaluate each column of        the prefix matrix uniquely.        For each index of a column we will try to        determine the number of sub-matrices        starting from that index        and has all 1s */    for (int j = 0; j < n; j++)    {        int i = n - 1;         /*  Stack to store elements and the count            of the numbers they popped                         First part of pair will be the            value of inserted element.            Second part will be the count            of the number of elements pushed            before with a greater value */        Stack<pair> q = new Stack<pair>();         // variable to store the number of        // submatrices with all 1s        int to_sum = 0;         while (i >= 0)        {            int c = 0;             while (q.Count != 0 &&                   q.Peek().first > p_arr[i, j])            {                to_sum -= (q.Peek().second + 1) *                          (q.Peek().first - p_arr[i, j]);                 c += q.Peek().second + 1;                q.Pop();            }             to_sum += p_arr[i, j];             ans += to_sum;             q.Push(new pair(p_arr[i, j], c));             i--;        }    }    return ans;} // Driver Codepublic static void Main(String[] args){    Boolean [,]arr = {{ true, true, false },                      { true, false, true },                      { false, true, true }};     Console.WriteLine(matrixAllOne(arr));}} // This code is contributed by 29AjayKumar
<script>  // Javascript program to count number of// sub-matrices with all 1s var n = 3; // Function to find required prefix-count for// each row from right to leftfunction findPrefixCount(p_arr, arr){    for (var i = 0; i < n; i++) {        for (var j = n - 1; j >= 0; j--) {             if (!arr[i][j])                continue;             if (j != n - 1)                p_arr[i][j] += p_arr[i][j + 1];             p_arr[i][j] += arr[i][j];        }    }} // Function to count the number of// sub-matrices with all 1sfunction matrixAllOne(arr){    // Array to store required prefix count of    // 1s from right to left for boolean array    var p_arr = Array.from(Array(n), ()=>Array(n).fill(0));     findPrefixCount(p_arr, arr);     // variable to store the final answer    var ans = 0;     /*  Loop to evaluate each column of        the prefix matrix uniquely.        For each index of a column we will try to        determine the number of sub-matrices        starting from that index        and has all 1s */    for (var j = 0; j < n; j++) {         var i = n - 1;         /*  Stack to store elements and the count            of the numbers they popped                         First part of pair will be the            value of inserted element.            Second part will be the count            of the number of elements pushed            before with a greater value */        var q = [];         // variable to store the number of        // submatrices with all 1s        var to_sum = 0;         while (i >= 0) {             var c = 0;             while (q.length != 0 && q[q.length-1][0] > p_arr[i][j]) {                 to_sum -= (q[q.length-1][1] + 1) *                            (q[q.length-1][0] - p_arr[i][j]);                 c += q[q.length-1][1] + 1;                q.pop();            }             to_sum += p_arr[i][j];             ans += to_sum;             q.push([ p_arr[i][j], c ]);             i--;        }    }     return ans;} // Driver Codevar arr = [ [ 1, 1, 0 ],                  [ 1, 0, 1 ],                  [ 0, 1, 1 ] ];document.write( matrixAllOne(arr)); </script>
10
 
Time Complexity: O(N2)Auxiliary Space: O(N2), since N2 extra space has been taken.
rituraj_jain
princiraj1992
29AjayKumar
noob2000
pankajsharmagfg
rishavnitro
Constructive Algorithms
Algorithms
Combinatorial
Mathematical
Matrix
Stack
Mathematical
Stack
Matrix
Combinatorial
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
What is Hashing | A Complete Tutorial
Understanding Time Complexity with Simple Examples
CPU Scheduling in Operating Systems
Write a program to print all permutations of a given string
Permutation and Combination in Python
Factorial of a large number
Count of subsets with sum equal to X
itertools.combinations() module in Python to print all possible combinations