Largest sub-matrix with all equal elements

Given a binary matrix of size N * M, the task is to find the largest area sub-matrix such that all elements in it are same i.e. either all are 0 or all are 1. Print the largest possible area of such matrix.

Examples:

Input: mat[][] = {
{1, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 1},
{1, 0, 0, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 1, 1, 0, 0},
{1, 0, 1, 1, 1, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 1, 1} }
Output: 10
Largest submatrix with all equal elements starts from
mat[4][2] (top-left corner) and ends mat[5][6] (borrom-right corner).



Input: mat[][] = {
{1, 0},
{0, 1}}
Output: 1

Approach:

  1. Try to find the largest sub-matrix with all 1s and the same can be applied to find the largest submatrix with all 0s.
  2. Maintain a matrix dp[N][M], where dp[i][j] represents the number of consecutive 1s present in the jth column starting from the ith row till the last row. This matrix can be easily filled by traversing each column from bottom to top.
  3. Now utilize the fact that for each row i, dp[i][j] represents the largest consecutive 1s till last row in the jth column. This problem is now same as the problem of finding the largest area rectangle present in the histogram which has been discussed in this article.
  4. The approach mentioned in the previous approach has to be applied for every row of the matrix to find the maximum area sub-matrix.

The same can be applied for finding the maximum area sub-matrix with all 0s.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
#define row 6
#define col 8
using namespace std;
  
// Function to find the maximum rectangular
// area under given histogram with n bars
int cal(int hist[], int n)
{
    // Create an empty stack. The stack holds indexes
    // of hist[] array. The bars stored in stack are
    // always in increasing order of their heights.
    stack<int> s;
  
    // Initalize max area
    int max_area = 0;
  
    // To store top of the stack
    int tp;
  
    // To store area with top bar
    int area_with_top;
    // as the smallest bar
  
    // Run through all bars of given histogram
    int i = 0;
    while (i < n) {
  
        // If this bar is higher than the bar on top
        // stack, push it to stack
        if (s.empty() || hist[s.top()] <= hist[i])
            s.push(i++);
  
        // If this bar is lower than top of stack,
        // then calculate area of rectangle with stack
        // top as the smallest (or minimum height) bar.
        // 'i' is 'right index' for the top and element
        // before top in stack is 'left index'
        else {
  
            // Store the top index
            tp = s.top();
  
            // Pop the top
            s.pop();
  
            // Calculate the area with hist[tp] stack
            // as smallest bar
            area_with_top = hist[tp]
                            * (s.empty() ? i : i - s.top() - 1);
  
            // Update max area, if needed
            if (max_area < area_with_top)
                max_area = area_with_top;
        }
    }
  
    // Now pop the remaining bars from stack and calculate
    // area with every popped bar as the smallest bar
    while (s.empty() == false) {
        tp = s.top();
        s.pop();
        area_with_top = hist[tp]
                        * (s.empty() ? i : i - s.top() - 1);
  
        if (max_area < area_with_top)
            max_area = area_with_top;
    }
  
    return max_area;
}
  
// Function to find largest sub matrix
// with all equal elements
int largestMatrix(int a[][col])
{
    // To find largest sub matrix
    // with all elements 1
    int dp[row][col];
  
    // Fill dp[][] by traversing each
    // column from bottom to up
    for (int i = 0; i < col; i++) {
        int cnt = 0;
        for (int j = row - 1; j >= 0; j--) {
            dp[j][i] = 0;
            if (a[j][i] == 1) {
                cnt++;
                dp[j][i] = cnt;
            }
            else {
                cnt = 0;
            }
        }
    }
  
    int ans = -1;
  
    for (int i = 0; i < row; i++) {
  
        // Maintain the histogram array
        int hist[col];
        for (int j = 0; j < col; j++) {
            hist[j] = dp[i][j];
        }
  
        // Find maximum area rectangle in Histogram
        ans = max(ans, cal(hist, col));
    }
  
    // To fill dp[][] for finding largest
    // sub matrix with all elements 0
    for (int i = 0; i < col; i++) {
        int cnt = 0;
        for (int j = row - 1; j >= 0; j--) {
            dp[j][i] = 0;
            if (a[j][i] == 0) {
                cnt++;
                dp[j][i] = cnt;
            }
            else {
                cnt = 0;
            }
        }
    }
  
    for (int i = 0; i < row; i++) {
  
        // Maintain the histogram array
        int hist[col];
        for (int j = 0; j < col; j++) {
            hist[j] = dp[i][j];
        }
  
        // Find maximum area rectangle in Histogram
        ans = max(ans, cal(hist, col));
    }
  
    return ans;
}
  
// Driver code
int main()
{
  
    int a[row][col] = { { 1, 1, 0, 1, 0, 0, 0, 0 },
                        { 0, 1, 1, 1, 1, 0, 0, 1 },
                        { 1, 0, 0, 1, 1, 1, 0, 0 },
                        { 0, 1, 1, 0, 1, 1, 0, 0 },
                        { 1, 0, 1, 1, 1, 1, 1, 0 },
                        { 0, 0, 1, 1, 1, 1, 1, 1 } };
  
    cout << largestMatrix(a);
  
    return 0;
}

chevron_right


Output:

10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.