Largest Plus or '+' formed by all ones in a binary square matrix
Last Updated :
12 Mar, 2025
Given an n × n binary matrix mat consisting of 0s and 1s. Your task is to find the size of the largest ‘+’ shape that can be formed using only 1s.
A ‘+’ shape consists of a center cell with four arms extending in all four directions (up, down, left, and right) while remaining within the matrix boundaries. The size of a ‘+’ is defined as the total number of cells forming it, including the center and all arms.
The task is to return the maximum size of any valid ‘+’ in mat. If no ‘+’ can be formed, return 0.
Examples:
Input: mat = [ [0, 1, 1, 0, 1], [0, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 0, 1], [0, 1, 1, 1, 0] ]
Output: 9
Explanation: A ‘+’ with an arm length of 2 (2 cells in each direction + 1 center) can be formed at the center of mat.
0 1 1 0 1
0 0 1 1 1
1 1 1 1 1
1 1 1 0 1
0 1 1 1 0
Total size = (2 × 4) + 1 = 9
Input: mat = [ [0, 1, 1], [0, 0, 1], [1, 1, 1,] ]
Output: 1
Explanation: A ‘+’ with an arm length of 0 (0 cells in each direction + 1 center) can be formed with any of the 1's.
Input: mat = [ [0] ]
Output: 0
Explanation: No ‘+’ sign can be formed.
[Naive Approach] - Consider Every Point as Center - O(n^4) Time and O(n^4) Space
Traverse through the matrix cells one by one. Consider every traversed point as center of a plus and find the size of the +. For every element we traverse left, right, bottom and up. The worst case in this solution happens when we have all 1s.
[Expected Approach] - Precompute 4 Arrays - O(n^2) Time and O(n^2) Space
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 approach:
C++
// C++ program to find the largest '+' in a binary matrix
// using Dynamic Programming
#include <bits/stdc++.h>
using namespace std;
int findLargestPlus(vector<vector<int>> &mat) {
int n = mat.size();
vector<vector<int>> left(n, vector<int>(n, 0));
vector<vector<int>> right(n, vector<int>(n, 0));
vector<vector<int>> top(n, vector<int>(n, 0));
vector<vector<int>> bottom(n, vector<int>(n, 0));
// Fill left and top matrices
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
left[i][j] = (j == 0) ? 1 : left[i][j - 1] + 1;
top[i][j] = (i == 0) ? 1 : top[i - 1][j] + 1;
}
}
}
// Fill right and bottom matrices
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (mat[i][j] == 1) {
right[i][j] = (j == n - 1) ? 1 : right[i][j + 1] + 1;
bottom[i][j] = (i == n - 1) ? 1 : bottom[i + 1][j] + 1;
}
}
}
int maxPlusSize = 0;
// Compute the maximum '+' size
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
int armLength = min({left[i][j], right[i][j],
top[i][j], bottom[i][j]});
maxPlusSize = max(maxPlusSize,
(4 * (armLength - 1)) + 1);
}
}
}
return maxPlusSize;
}
int main() {
// Hardcoded input matrix
vector<vector<int>> mat = {
{0, 1, 1, 0, 1},
{0, 0, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 0, 1},
{0, 1, 1, 1, 0}
};
cout << findLargestPlus(mat) << endl;
return 0;
}
Java
// Java program to find the largest '+' in a binary matrix
// using Dynamic Programming
class GfG {
static int findLargestPlus(int[][] mat) {
int n = mat.length;
int[][] left = new int[n][n];
int[][] right = new int[n][n];
int[][] top = new int[n][n];
int[][] bottom = new int[n][n];
// Fill left and top matrices
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
left[i][j] = (j == 0) ? 1 : left[i][j - 1] + 1;
top[i][j] = (i == 0) ? 1 : top[i - 1][j] + 1;
}
}
}
// Fill right and bottom matrices
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (mat[i][j] == 1) {
right[i][j] = (j == n - 1) ? 1 : right[i][j + 1] + 1;
bottom[i][j] = (i == n - 1) ? 1 : bottom[i + 1][j] + 1;
}
}
}
int maxPlusSize = 0;
// Compute the maximum '+' size
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
int armLength = Math.min(Math.min(left[i][j], right[i][j]),
Math.min(top[i][j], bottom[i][j]));
maxPlusSize = Math.max(maxPlusSize,
(4 * (armLength - 1)) + 1);
}
}
}
return maxPlusSize;
}
public static void main(String[] args) {
// Hardcoded input matrix
int[][] mat = {
{0, 1, 1, 0, 1},
{0, 0, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 0, 1},
{0, 1, 1, 1, 0}
};
System.out.println(findLargestPlus(mat));
}
}
Python
# Python program to find the largest '+' in a binary matrix
# using Dynamic Programming
def findLargestPlus(mat):
n = len(mat)
left = [[0] * n for i in range(n)]
right = [[0] * n for i in range(n)]
top = [[0] * n for i in range(n)]
bottom = [[0] * n for i in range(n)]
# Fill left and top matrices
for i in range(n):
for j in range(n):
if mat[i][j] == 1:
left[i][j] = 1 if j == 0 else left[i][j - 1] + 1
top[i][j] = 1 if i == 0 else top[i - 1][j] + 1
# Fill right and bottom matrices
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
if mat[i][j] == 1:
right[i][j] = 1 if j == n - 1 else right[i][j + 1] + 1
bottom[i][j] = 1 if i == n - 1 else bottom[i + 1][j] + 1
maxPlusSize = 0
# Compute the maximum '+' size
for i in range(n):
for j in range(n):
if mat[i][j] == 1:
armLength = min(left[i][j], right[i][j],
top[i][j], bottom[i][j])
maxPlusSize = max(maxPlusSize,
(4 * (armLength - 1)) + 1)
return maxPlusSize
if __name__ == "__main__":
# Hardcoded input matrix
mat = [
[0, 1, 1, 0, 1],
[0, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 0, 1],
[0, 1, 1, 1, 0]
]
print(findLargestPlus(mat))
C#
// C# program to find the largest '+' in a binary matrix
// using Dynamic Programming
using System;
class GfG {
static int FindLargestPlus(int[,] mat) {
int n = mat.GetLength(0);
int[,] left = new int[n, n];
int[,] right = new int[n, n];
int[,] top = new int[n, n];
int[,] bottom = new int[n, n];
// Fill left and top matrices
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i, j] == 1) {
left[i, j] = (j == 0) ? 1 : left[i, j - 1] + 1;
top[i, j] = (i == 0) ? 1 : top[i - 1, j] + 1;
}
}
}
// Fill right and bottom matrices
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (mat[i, j] == 1) {
right[i, j] = (j == n - 1) ? 1 : right[i, j + 1] + 1;
bottom[i, j] = (i == n - 1) ? 1 : bottom[i + 1, j] + 1;
}
}
}
int maxPlusSize = 0;
// Compute the maximum '+' size
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i, j] == 1) {
int armLength = Math.Min(Math.Min(left[i, j], right[i, j]),
Math.Min(top[i, j], bottom[i, j]));
maxPlusSize = Math.Max(maxPlusSize,
(4 * (armLength - 1)) + 1);
}
}
}
return maxPlusSize;
}
public static void Main() {
// Hardcoded input matrix
int[,] mat = {
{0, 1, 1, 0, 1},
{0, 0, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 0, 1},
{0, 1, 1, 1, 0}
};
Console.WriteLine(FindLargestPlus(mat));
}
}
JavaScript
// JavaScript program to find the largest '+' in a binary matrix
// using Dynamic Programming
function findLargestPlus(mat) {
let n = mat.length;
let left = Array.from({ length: n }, () => Array(n).fill(0));
let right = Array.from({ length: n }, () => Array(n).fill(0));
let top = Array.from({ length: n }, () => Array(n).fill(0));
let bottom = Array.from({ length: n }, () => Array(n).fill(0));
// Fill left and top matrices
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j] === 1) {
left[i][j] = (j === 0) ? 1 : left[i][j - 1] + 1;
top[i][j] = (i === 0) ? 1 : top[i - 1][j] + 1;
}
}
}
// Fill right and bottom matrices
for (let i = n - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
if (mat[i][j] === 1) {
right[i][j] = (j === n - 1) ? 1 : right[i][j + 1] + 1;
bottom[i][j] = (i === n - 1) ? 1 : bottom[i + 1][j] + 1;
}
}
}
let maxPlusSize = 0;
// Compute the maximum '+' size
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j] === 1) {
let armLength = Math.min(left[i][j], right[i][j],
top[i][j], bottom[i][j]);
maxPlusSize = Math.max(maxPlusSize,
(4 * (armLength - 1)) + 1);
}
}
}
return maxPlusSize;
}
// Hardcoded input matrix
let mat = [
[0, 1, 1, 0, 1],
[0, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 0, 1],
[0, 1, 1, 1, 0]
];
console.log(findLargestPlus(mat));
Time Complexity: O(n²), due to four passes to compute the directional matrices and one final pass to determine the largest '+'. Each pass takes O(n²) time, leading to an overall complexity of O(n²).
Space Complexity: O(n²), due to four auxiliary matrices (left, right, top, bottom) consuming O(n²) extra space.
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem