
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum size rectangle binary sub-matrix with all 1s in C++
In this tutorial, we will be discussing a program to find maximum size rectangle binary sub-matrix with all 1s.
For this we will be provided with 2D matrix containing zeroes and ones. Our task is to find the largest 2D matrix subset containing only ones.
Example
#include <bits/stdc++.h> using namespace std; #define R 4 #define C 4 //finding the maximum area int maxHist(int row[]) { stack<int> result; int top_val; int max_area = 0; int area = 0; int i = 0; while (i < C) { if (result.empty() || row[result.top()] <= row[i]) result.push(i++); else { top_val = row[result.top()]; result.pop(); area = top_val * i; if (!result.empty()) area = top_val * (i - result.top() - 1); max_area = max(area, max_area); } } while (!result.empty()) { top_val = row[result.top()]; result.pop(); area = top_val * i; if (!result.empty()) area = top_val * (i - result.top() - 1); max_area = max(area, max_area); } return max_area; } //returning area of largest required subset int maxRectangle(int A[][C]) { int result = maxHist(A[0]); for (int i = 1; i < R; i++) { for (int j = 0; j < C; j++) if (A[i][j]) A[i][j] += A[i - 1][j]; result = max(result, maxHist(A[i])); } return result; } int main() { int A[][C] = { { 0, 1, 1, 0 },{ 1, 1, 1, 1 },{ 1, 1, 1, 1 },{ 1, 1, 0, 0 }, }; cout << "Area of maximum rectangle is " << maxRectangle(A); return 0; }
Output
Area of maximum rectangle is 8
- Related Articles
- Maximum size rectangle binary sub-matrix with all 1s in C++ Program
- Maximum size square submatrix with all 1s
- Finding the maximum square sub-matrix with all equal elements in C++
- Print maximum sum square sub-matrix of given size in C Program.
- Count all 0s which are blocked by 1s in binary matrix in C++
- Find row number of a binary matrix having maximum number of 1s in C++
- Find perimeter of shapes formed with 1s in binary matrix in C++
- Program to count substrings with all 1s in binary string in Python
- Maximum sum rectangle in a 2D matrix
- Maximum trace possible for any sub-matrix of the given matrix in C++
- Find size of the largest ‘+’ formed by all ones in a binary matrix in C++
- Count numbers have all 1s together in binary representation in C++
- Find if there is a rectangle in binary matrix with corners as 1 in C++
- JavaScript Program for Counting sets of 1s and 0s in a binary matrix
- Maximum product quadruple (sub-sequence of size 4) in array in C++

Advertisements