Given a N X N binary matrix, find the size of the largest ‘+’ formed by all 1s.
For above matrix, largest ‘+’ would be formed by highlighted part of size 17.
The idea is to maintain four auxiliary matrices left, right, top, bottom to store consecutive 1’s in every direction. For each cell (i, j) in the input matrix, we store below information in these four matrices –
left(i, j) stores maximum number of consecutive 1's to the left of cell (i, j) including cell (i, j). right(i, j) stores maximum number of consecutive 1's to the right of cell (i, j) including cell (i, j). top(i, j) stores maximum number of consecutive 1's at top of cell (i, j) including cell (i, j). bottom(i, j) stores maximum number of consecutive 1's at bottom of cell (i, j) including cell (i, j).
After computing value for each cell of above matrices, the largest + would be formed by a cell of input matrix that has maximum value by considering minimum of (left(i, j), right(i, j), top(i, j), bottom(i, j) )
We can use Dynamic Programming to compute the total amount of consecutive 1’s in every direction.
if mat(i, j) == 1 left(i, j) = left(i, j - 1) + 1 else left(i, j) = 0 if mat(i, j) == 1 top(i, j) = top(i - 1, j) + 1; else top(i, j) = 0; if mat(i, j) == 1 bottom(i, j) = bottom(i + 1, j) + 1; else bottom(i, j) = 0; if mat(i, j) == 1 right(i, j) = right(i, j + 1) + 1; else right(i, j) = 0;
Below is the implementation of above idea –
Time complexity of above solution is O(n2).
Auxiliary space used by the program is O(n2).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find perimeter of shapes formed with 1s in binary matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Maximum size rectangle binary sub-matrix with all 1s
- Largest Square in a Binary Matrix with at most K 1s for multiple Queries
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Given a matrix of 'O' and 'X', find the largest subsquare surrounded by 'X'
- Find the original matrix when largest element in a row and a column are given
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Find smallest and largest element from square matrix diagonals
- Find if there is a rectangle in binary matrix with corners as 1
- Find duplicate rows in a binary matrix
- Find maximum path length in a binary matrix
- Find if a binary matrix exists with given row and column sums
- Python Counter| Find duplicate rows in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Find row number of a binary matrix having maximum number of 1s
- Program to check if a matrix is Binary matrix or not