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 firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum number of ones in a N*N matrix with given constraints
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Find perimeter of shapes formed with 1s in binary matrix
- Maximum size rectangle binary sub-matrix with all 1s
- 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
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Maximum size of square such that all submatrices of that size have sum less than K
- Largest Square in a Binary Matrix with at most K 1s for multiple Queries
- Largest sub-matrix with all equal elements
- Maximum size square sub-matrix with all 1s
- Size of all connected non-empty cells of a Matrix
- Smallest element from all square submatrices of size K from a given Matrix
- Program to check if a matrix is Binary matrix or not
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Given a matrix of 'O' and 'X', find the largest subsquare surrounded by 'X'
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Find smallest and largest element from square matrix diagonals
- Find the original matrix when largest element in a row and a column are given