Maximum Size Rectangle Sub-matrix With All 1's
You are given an 'N' * 'M' sized binary-valued matrix 'MAT, where 'N' is the number of rows and 'M' is the number of columns. You need to return the maximum size (area) of the submatrix which consists of all 1’s i.e. the maximum area of a submatrix in which each cell has only the value ‘1’.
In the above image, areas in green, red, and violet color are all submatrices of the original 4x4 matrix.
Note:
1. Binary valued matrix has only two values in each cell : 0 and 1.
2. A submatrix is a matrix formed by selecting certain rows and columns from a larger matrix.
3. The area of a matrix with 'h' rows and 'w' columns is equal to 'h' * 'w'.
Input Format:
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains two space-separated integers 'N' and 'M', where 'N' = the number of rows in the given matrix and 'M' = the number of columns in the given matrix.
Then 'N' lines follow for each test case. Each line contains 'M' space-separated integers (either 1 or 0) denoting matrix elements.
Output Format:
For each test case print in a single line the area of maximum size submatrix of all 1’s in the given matrix on a new line.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'T' <= 50
1 <= 'N', 'M' <= 100
Time Limit: 1 sec
AnswerBot
1y
The task is to find the maximum size of a submatrix consisting of all 1's in a given binary-valued matrix.
Iterate through each cell of the matrix
For each cell, calculate the maximum size of a submatri...read more
CodingNinjas
author
2y
Dynamic Programming.
- We start from the first row and move downwards.
- We create three 1-dimensional arrays HEIGHT[], LEFT[], RIGHT[].
- ‘HEIGHT’[i]: stores the number of current continuous 1’s in column i.
- L...read more
CodingNinjas
author
2y
Stack, Dynamic Programming, Largest rectangle in a histogram.
- Let’s first understand how do we calculate the largest area in a histogram.
- Let’s take an example image:
In the above example, the largest r...read more
Add answer anonymously...
Top Novartis Full Stack Engineer interview questions & answers
Popular interview questions of Full Stack Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app