# Number of submatrices with OR value 1

Given a **N*N** binary matrix, task is to find the count of rectangular sub-matrices with OR value 1.

**Examples:**

Input :arr[][] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}Output :0Explanation: All the submatrices will have an OR value 0. Thus, ans = 0.Input :arr[][] = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}Output :16

A **Simple Solution** will be to generate all the possible sub-matrices and then check if any of the values inside them are 1. If for a sub-matrix, atleast a single element is one, we increment the value of final answer for one. The time complexity of above approach is O(n^{6}).

**Better approach**: Lets have a look at this problem in an other way. We will now try to find the number of submatrices with all 0s. And for the final answer, we will substract this value from the total number of submatrices.

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 0s 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 0, then in p_arr[R][C], we will store the number of 0s to the right of the cell(R, C) along row ‘R’ before we encounter ‘1’ or end of the array plus one.
- If arr[R][C] equals 1, the p_arr[R][C] equals zero.

For creating this matrix, we will use the following recurrence relation.

IF arr[R][C] is 0 p_arr[R][C] = p_arr[R][C+1] + 1 ELSE p_arr[R][C] = 0 arr[][] = {{1, 0, 0, 0}, {0, 1, 0, 1}, {0, 1, 0, 0}, {0, 0, 0, 0}} p_arr[][] for above will look like {{0, 3, 2, 1}, {1, 0, 1, 0}, {1, 0, 2, 1}, {4, 3, 2, 1}}

Once, we have the required matrix **p_arr**, we will start processing the matrix ‘p_arr’ column-wise. If we are processing j^{th} 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 cell **(i, j)** with all 0s.

For this, we can use stack data structure.

**Algorithm**:

- Initialize a stack ‘q’ to store the value of the elements getting pushed along with the count(C
_{ij}) of the number of elements that were pushed in the stack with a value**strictly greater**than the value of the current element. We will use 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 0s starting from the element being pushed at that step. Thus, using ‘to_sum’, we update the count of number of submatrices with all 0s at each step. - For a column ‘j’, at any step ‘i’, we will prepare to push p_arr[i][j] in the stack. Let Q
_{t}represent the topmost element of the stack and C_{t}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 += (C**. Let C_{t}+ 1) * (Q_{t}– p_arr[i][j])_{i, j}represent the number of elements greater than the current element that were pushed in this stack previously. We also need to keep a track of C_{i, j}. Thus, before popping an element, we update C_{i, j}as C_{i, j}+= C_{t}along with to_sum. - We update the number of submatrices with all zeros as count_zero_submatrices += to_sum.
- Finally, we push that element in the stack after pairing it with C
_{i, j}.

Total number of sub-matrices in a N*N matrix equals:

(N^{2}* (N + 1)^{2})/4

Thus, the final answer will be:

ans = (N^{2}* (N + 1)^{2})/4 - count_zero_submatrices

We create the prefix-array in O(N^{2}) 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(N^{2}).

Below is the implementation of the above approach:

## C++

`// C++ program to count number of submatrices ` `// with OR value 1 ` ` ` `#include <iostream> ` `#include <stack> ` `#define n 3 ` `using` `namespace` `std; ` ` ` `// Function to find required prefix-count for each row ` `// from right to left ` `void` `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 find the count of submatrices ` `// with OR value 1 ` `int` `matrixOrValueOne(` `bool` `arr[][n]) ` `{ ` ` ` `// Array to store prefix count of zeros from ` ` ` `// right to left for boolean array ` ` ` `int` `p_arr[n][n] = { 0 }; ` ` ` ` ` `findPrefixCount(p_arr, arr); ` ` ` ` ` `// Variable to store the count of ` ` ` `// submatrices with OR value 0 ` ` ` `int` `count_zero_submatrices = 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 0s ` ` ` `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]; ` ` ` ` ` `count_zero_submatrices += to_sum; ` ` ` ` ` `q.push({ p_arr[i][j], c }); ` ` ` ` ` `i--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final answer ` ` ` `return` `(n * (n + 1) * n * (n + 1)) / 4 ` ` ` `- count_zero_submatrices; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `bool` `arr[][n] = { { 0, 0, 0 }, ` ` ` `{ 0, 1, 0 }, ` ` ` `{ 0, 0, 0 } }; ` ` ` ` ` `cout << matrixOrValueOne(arr); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to count number

# of submatrices with OR value 1

# Function to find required prefix-count

# for each row from right to left

def findPrefixCount(p_arr, arr):

for i in range(0, n):

for j in range(n – 1, -1, -1):

if arr[i][j]:

continue

if j != n – 1:

p_arr[i][j] += p_arr[i][j + 1]

p_arr[i][j] += int(not arr[i][j])

# Function to find the count

# of submatrices with OR value 1

def matrixOrValueOne(arr):

# Array to store prefix count of zeros

# 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 count of

# submatrices with OR value 0

count_zero_submatrices = 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 0s

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.pop()[1] + 1

to_sum += p_arr[i][j]

count_zero_submatrices += to_sum

q.append((p_arr[i][j], c))

i -= 1

# Return the final answer

return ((n * (n + 1) * n * (n + 1)) //

4 – count_zero_submatrices)

# Driver Code

if __name__ == “__main__”:

n = 3

arr = [[0, 0, 0],

[0, 1, 0],

[0, 0, 0]]

print(matrixOrValueOne(arr))

# This code is contributed by Rituraj Jain

**Output:**

16

## Recommended Posts:

- Number of submatrices with all 1s
- Sum of bitwise AND of all submatrices
- Sum of Bitwise-OR of all Submatrices
- Sum of all Submatrices of a Given Matrix
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Number of ways to calculate a target number using only array elements
- Given number of matches played, find number of teams in tournament
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Querying maximum number of divisors that a number in a given range has
- Number of leading zeros in binary representation of a given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.