Largest Submatrix With Rearrangements of Columns Allowed
Last Updated :
17 Sep, 2024
Given a matrix with 0 and 1's, find the largest rectangle of all 1's in the matrix. The rectangle can be formed by swapping any pair of columns of given matrix.
Example:
Input: mat[][] =
{ {0, 1, 0, 1, 0},
{0, 1, 0, 1, 1},
{1, 1, 0, 1, 0} };
Output: 6
The largest rectangle's area is 6. The rectangle can be formed by swapping column 2 with 3 The matrix after swapping will be
0 0 1 1 0
0 0 1 1 1
1 0 1 1 0
Input: mat[][] =
{ {0, 1, 0, 1, 0},
{0, 1, 1, 1, 1},
{1, 1, 1, 0, 1},
{1, 1, 1, 1, 1} };
Output: 9
The idea is to use an auxiliary matrix to store count of consecutive 1's in every column. Once we have these counts, we sort all rows of auxiliary matrix in non-increasing order of counts. Finally traverse the sorted rows to find the maximum area.
Note : After forming the auxiliary matrix each row becomes independent, hence we can swap or sort each row independently.It is because we can only swap columns, so we have made each row independent and find the max area of rectangle possible with row and column.
Below are detailed steps for first example mentioned above.
Step 1: First of all, calculate no. of consecutive 1's in every column. An auxiliary array hist[][] is used to store the counts of consecutive 1's. So for the above first example, contents of hist[R][C] would be
0 1 0 1 0
0 2 0 2 1
1 3 0 3 0
Time complexity of this step is O(R*C)
Step 2: Sort the rows in non-increasing fashion. After sorting step the matrix hist[][] would be
1 1 0 0 0
2 2 1 0 0
3 3 1 0 0
This step can be done in O(R * (R + C)). Since we know that the values are in range from 0 to R, we can use counting sort for every row.
The sorting is actually the swapping of columns. If we look at the 3rd row under step 2:
3 3 1 0 0
The sorted row corresponds to swapping the columns so that the column with the highest possible rectangle is placed first, after that comes the column that allows the second highest rectangle and so on. So, in the example there are 2 columns that can form a rectangle of height 3. That makes an area of 3*2=6. If we try to make the rectangle wider the height drops to 1, because there are no columns left that allow a higher rectangle on the 3rd row.
Step 3: Traverse each row of hist[][] and check for the max area. Since every row is sorted by count of 1's, current area can be calculated by multiplying column number with value in hist[i][j]. This step also takes O(R * C) time.
Below is the implementation based of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
// Returns area of the largest rectangle of 1's
int maxArea(vector<vector<bool>>& mat) {
int R = mat.size();
int C = mat[0].size();
// Auxiliary matrix to store count of consecutive 1's in every column.
vector<vector<int>> hist(R, vector<int>(C, 0));
// Step 1: Fill the auxiliary array hist[][]
for (int i = 0; i < C; i++) {
// First row in hist is a copy of the first row in mat
hist[0][i] = mat[0][i];
// Fill remaining rows of hist
for (int j = 1; j < R; j++)
hist[j][i] = (mat[j][i] == 0) ? 0 : hist[j - 1][i] + 1;
}
// Step 2: Sort columns of hist[][] in non-increasing order
for (int i = 0; i < R; i++) {
vector<int> count(R + 1, 0);
// Counting occurrence
for (int j = 0; j < C; j++)
count[hist[i][j]]++;
// Traverse the count array from right side
int col_no = 0;
for (int j = R; j >= 0; j--) {
if (count[j] > 0) {
for (int k = 0; k < count[j]; k++) {
hist[i][col_no] = j;
col_no++;
}
}
}
}
// Step 3: Traverse the sorted hist[][] to find maximum area
int curr_area, max_area = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
// Since values are in decreasing order,
// The area ending with cell (i, j) can be obtained by
// multiplying column number with value of hist[i][j]
curr_area = (j + 1) * hist[i][j];
max_area = max(max_area, curr_area);
}
}
return max_area;
}
// Driver program
int main() {
vector<vector<bool>> mat = { { 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 0 } };
cout << "Area of the largest rectangle is " << maxArea(mat);
return 0;
}
C
// C program to find the largest rectangle of 1's with swapping
// of columns allowed.
#include <stdio.h>
#include <stdbool.h>
#define R 3
#define C 5
// Returns area of the largest rectangle of 1's
int maxArea(bool mat[R][C])
{
// An auxiliary array to store count of consecutive 1's
// in every column.
int hist[R + 1][C + 1];
// Step 1: Fill the auxiliary array hist[][]
for (int i = 0; i < C; i++) {
// First row in hist[][] is copy of first row in mat[][]
hist[0][i] = mat[0][i];
// Fill remaining rows of hist[][]
for (int j = 1; j < R; j++)
hist[j][i] = (mat[j][i] == 0) ? 0 : hist[j - 1][i] + 1;
}
// Step 2: Sort columns of hist[][] in non-increasing order
for (int i = 0; i < R; i++) {
int count[R + 1] = { 0 };
// counting occurrence
for (int j = 0; j < C; j++)
count[hist[i][j]]++;
// Traverse the count array from right side
int col_no = 0;
for (int j = R; j >= 0; j--) {
if (count[j] > 0) {
for (int k = 0; k < count[j]; k++) {
hist[i][col_no] = j;
col_no++;
}
}
}
}
// Step 3: Traverse the sorted hist[][] to find maximum area
int curr_area, max_area = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
// Since values are in decreasing order,
// The area ending with cell (i, j) can
// be obtained by multiplying column number
// with value of hist[i][j]
curr_area = (j + 1) * hist[i][j];
if (curr_area > max_area)
max_area = curr_area;
}
}
return max_area;
}
// Driver program
int main()
{
bool mat[R][C] = { { 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 0 } };
printf("Area of the largest rectangle is %d", maxArea(mat));
return 0;
}
Java
public class GfG {
// Returns the area of the largest rectangle of 1's
public static int maxArea(int[][] mat) {
int R = mat.length;
int C = mat[0].length;
// Auxiliary matrix to store the count of
// consecutive 1's in every column.
int[][] hist = new int[R][C];
// Step 1: Fill the auxiliary array hist[][]
for (int i = 0; i < C; i++) {
// First row in hist is a copy of the first row in mat
hist[0][i] = mat[0][i];
// Fill remaining rows of hist
for (int j = 1; j < R; j++) {
hist[j][i] = (mat[j][i] == 0) ? 0 : hist[j - 1][i] + 1;
}
}
// Step 2: Sort columns of hist[][] in
// non-increasing order
for (int i = 0; i < R; i++) {
int[] count = new int[R + 1];
// Counting occurrence
for (int j = 0; j < C; j++) {
count[hist[i][j]]++;
}
// Traverse the count array from the right side
int col_no = 0;
for (int j = R; j >= 0; j--) {
if (count[j] > 0) {
for (int k = 0; k < count[j]; k++) {
hist[i][col_no] = j;
col_no++;
}
}
}
}
// Step 3: Traverse the sorted hist[][] to find the maximum area
int curr_area, max_area = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
// The area ending with cell (i, j)
curr_area = (j + 1) * hist[i][j];
max_area = Math.max(max_area, curr_area);
}
}
return max_area;
}
// Driver program
public static void main(String[] args) {
int[][] mat = {
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 0 }
};
System.out.println("Area of the largest rectangle is " + maxArea(mat));
}
}
Python
def maxArea(mat):
R = len(mat)
C = len(mat[0])
# Auxiliary matrix to store count of consecutive 1's in every column.
hist = [[0] * C for _ in range(R)]
# Step 1: Fill the auxiliary array hist[][]
for i in range(C):
# First row in hist is a copy of the first row in mat
hist[0][i] = mat[0][i]
# Fill remaining rows of hist
for j in range(1, R):
hist[j][i] = hist[j - 1][i] + 1 if mat[j][i] == 1 else 0
# Step 2: Sort columns of hist[][] in non-increasing order
for i in range(R):
count = [0] * (R + 1)
# Counting occurrence
for j in range(C):
count[hist[i][j]] += 1
# Traverse the count array from the right side
col_no = 0
for j in range(R, -1, -1):
if count[j] > 0:
for k in range(count[j]):
hist[i][col_no] = j
col_no += 1
# Step 3: Traverse the sorted hist[][] to find the maximum area
max_area = 0
for i in range(R):
for j in range(C):
# The area ending with cell (i, j)
curr_area = (j + 1) * hist[i][j]
max_area = max(max_area, curr_area)
return max_area
# Driver code
mat = [
[0, 1, 0, 1, 0],
[0, 1, 0, 1, 1],
[1, 1, 0, 1, 0]
]
print("Area of the largest rectangle is", maxArea(mat))
C#
using System;
class GfG
{
// Returns the area of the largest rectangle of 1's
public static int MaxArea(int[,] mat)
{
int R = mat.GetLength(0);
int C = mat.GetLength(1);
// Auxiliary matrix to store count of consecutive 1's in every column.
int[,] hist = new int[R, C];
// Step 1: Fill the auxiliary array hist[][]
for (int i = 0; i < C; i++)
{
// First row in hist is a copy of the first row in mat
hist[0, i] = mat[0, i];
// Fill remaining rows of hist
for (int j = 1; j < R; j++)
{
hist[j, i] = (mat[j, i] == 0) ? 0 : hist[j - 1, i] + 1;
}
}
// Step 2: Sort columns of hist[][] in non-increasing order
for (int i = 0; i < R; i++)
{
int[] count = new int[R + 1];
// Counting occurrence
for (int j = 0; j < C; j++)
{
count[hist[i, j]]++;
}
// Traverse the count array from the right side
int col_no = 0;
for (int j = R; j >= 0; j--)
{
if (count[j] > 0)
{
for (int k = 0; k < count[j]; k++)
{
hist[i, col_no] = j;
col_no++;
}
}
}
}
// Step 3: Traverse the sorted hist[][] to find the maximum area
int max_area = 0;
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
// The area ending with cell (i, j)
int curr_area = (j + 1) * hist[i, j];
max_area = Math.Max(max_area, curr_area);
}
}
return max_area;
}
// Driver code
static void Main()
{
int[,] mat = {
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 0 }
};
Console.WriteLine("Area of the largest rectangle is " + MaxArea(mat));
}
}
JavaScript
function maxArea(mat) {
const R = mat.length;
const C = mat[0].length;
// Auxiliary matrix to store count of consecutive 1's in every column.
let hist = Array.from({ length: R }, () => Array(C).fill(0));
// Step 1: Fill the auxiliary array hist[][]
for (let i = 0; i < C; i++) {
// First row in hist is a copy of the first row in mat
hist[0][i] = mat[0][i];
// Fill remaining rows of hist
for (let j = 1; j < R; j++) {
hist[j][i] = (mat[j][i] === 1) ? hist[j - 1][i] + 1 : 0;
}
}
// Step 2: Sort columns of hist[][] in non-increasing order
for (let i = 0; i < R; i++) {
let count = Array(R + 1).fill(0);
// Counting occurrence
for (let j = 0; j < C; j++) {
count[hist[i][j]]++;
}
// Traverse the count array from the right side
let col_no = 0;
for (let j = R; j >= 0; j--) {
if (count[j] > 0) {
for (let k = 0; k < count[j]; k++) {
hist[i][col_no] = j;
col_no++;
}
}
}
}
// Step 3: Traverse the sorted hist[][] to find the maximum area
let max_area = 0;
for (let i = 0; i < R; i++) {
for (let j = 0; j < C; j++) {
// The area ending with cell (i, j)
let curr_area = (j + 1) * hist[i][j];
max_area = Math.max(max_area, curr_area);
}
}
return max_area;
}
// Driver code
let mat = [
[0, 1, 0, 1, 0],
[0, 1, 0, 1, 1],
[1, 1, 0, 1, 0]
];
console.log("Area of the largest rectangle is " + maxArea(mat));
OutputArea of the largest rectangle is 6
Time complexity of above solution is O(R * (R + C)) where R is number of rows and C is number of columns in input matrix. Extra space: O(R * C)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem