Find size of the largest '+' formed by all ones in a binary matrix - GeeksforGeeks

08 Apr, 2021
Given a N X N binary matrix, find the size of the largest ‘+’ formed by all 1s.Example:  
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 – 
C++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the size of the largest '+'// formed by all 1's in given binary matrix#include <bits/stdc++.h>using namespace std; // size of binary square matrix#define N 10 // Function to find the size of the largest '+'// formed by all 1's in given binary matrixint findLargestPlus(int mat[N][N]){    // left[j][j], right[i][j], top[i][j] and    // bottom[i][j] store maximum number of    // consecutive 1's present to the left,    // right, top and bottom of mat[i][j] including    // cell(i, j) respectively    int left[N][N], right[N][N], top[N][N],        bottom[N][N];     // initialize above four matrix    for (int i = 0; i < N; i++)    {        // initialize first row of top        top[0][i] = mat[0][i];         // initialize last row of bottom        bottom[N - 1][i] = mat[N - 1][i];         // initialize first column of left        left[i][0] = mat[i][0];         // initialize last column of right        right[i][N - 1] = mat[i][N - 1];    }     // fill all cells of above four matrix    for (int i = 0; i < N; i++)    {        for (int j = 1; j < N; j++)        {            // calculate left matrix (filled left to right)            if (mat[i][j] == 1)                left[i][j] = left[i][j - 1] + 1;            else                left[i][j] = 0;             // calculate top matrix            if (mat[j][i] == 1)                top[j][i] = top[j - 1][i] + 1;            else                top[j][i] = 0;             // calculate new value of j to calculate            // value of bottom(i, j) and right(i, j)            j = N - 1 - j;             // calculate bottom matrix            if (mat[j][i] == 1)                bottom[j][i] = bottom[j + 1][i] + 1;            else                bottom[j][i] = 0;             // calculate right matrix            if (mat[i][j] == 1)                right[i][j] = right[i][j + 1] + 1;            else                right[i][j] = 0;             // revert back to old j            j = N - 1 - j;        }    }     // n stores length of longest + found so far    int n = 0;     // compute longest +    for (int i = 0; i < N; i++)    {        for (int j = 0; j < N; j++)        {            // find minimum of left(i, j), right(i, j),            // top(i, j), bottom(i, j)            int len = min(min(top[i][j], bottom[i][j]),                          min(left[i][j], right[i][j]));             // largest + would be formed by a cell that            // has maximum value            if(len > n)                n = len;        }    }     // 4 directions of length n - 1 and 1 for middle cell    if (n)       return 4 * (n - 1) + 1;     // matrix contains all 0's    return 0;} /* Driver function to test above functions */int main(){    // Binary Matrix of size N    int mat[N][N] =    {        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },        { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },        { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },        { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },        { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },        { 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 },        { 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 },        { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 },        { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }    };     cout << findLargestPlus(mat);     return 0;}
// Java program to find the size of the largest '+'// formed by all 1's in given binary matrix import java.io.*; class GFG {         // size of binary square matrix    static int N = 10;     // Function to find the size of the largest '+'    // formed by all 1's in given binary matrix    static int findLargestPlus(int mat[][])    {                 // left[j][j], right[i][j], top[i][j] and        // bottom[i][j] store maximum number of        // consecutive 1's present to the left,        // right, top and bottom of mat[i][j]        // including cell(i, j) respectively        int left[][] = new int[N][N];        int right[][] = new int[N][N];        int top[][] = new int[N][N];        int bottom[][] = new int[N][N];         // initialize above four matrix        for (int i = 0; i < N; i++) {                         // initialize first row of top            top[0][i] = mat[0][i];             // initialize last row of bottom            bottom[N - 1][i] = mat[N - 1][i];             // initialize first column of left            left[i][0] = mat[i][0];             // initialize last column of right            right[i][N - 1] = mat[i][N - 1];        }         // fill all cells of above four matrix        for (int i = 0; i < N; i++) {            for (int j = 1; j < N; j++) {                                 // calculate left matrix                // (filled left to right)                if (mat[i][j] == 1)                    left[i][j] = left[i][j - 1] + 1;                else                    left[i][j] = 0;                 // calculate top matrix                if (mat[j][i] == 1)                    top[j][i] = top[j - 1][i] + 1;                else                    top[j][i] = 0;                 // calculate new value of j to                // calculate value of bottom(i, j)                // and right(i, j)                j = N - 1 - j;                 // calculate bottom matrix                if (mat[j][i] == 1)                    bottom[j][i] = bottom[j + 1][i] + 1;                else                    bottom[j][i] = 0;                 // calculate right matrix                if (mat[i][j] == 1)                    right[i][j] = right[i][j + 1] + 1;                else                    right[i][j] = 0;                 // revert back to old j                j = N - 1 - j;            }        }         // n stores length of longest + found so far        int n = 0;         // compute longest +        for (int i = 0; i < N; i++) {            for (int j = 0; j < N; j++) {                // find minimum of left(i, j),                // right(i, j), top(i, j),                // bottom(i, j)                int len = Math.min(Math.min(top[i][j],                    bottom[i][j]),Math.min(left[i][j],                                        right[i][j]));                 // largest + would be formed by a                // cell that has maximum value                if (len > n)                    n = len;            }        }         // 4 directions of length n - 1 and 1 for        // middle cell        if (n > 0)            return 4 * (n - 1) + 1;         // matrix contains all 0's        return 0;    }     /* Driver function to test above functions */    public static void main(String[] args)    {                 // Binary Matrix of size N        int mat[][] = {            { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },            { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },            { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },            { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },            { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },            { 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 },            { 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 },            { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 },            { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }        };        System.out.println(findLargestPlus(mat));    }} // This code is contributed by vt_m.
# Python 3 program to find the size# of the largest '+' formed by all# 1's in given binary matrix # size of binary square matrixN = 10 # Function to find the size of the# largest '+' formed by all 1's in# given binary matrixdef findLargestPlus(mat):     # left[j][j], right[i][j], top[i][j] and    # bottom[i][j] store maximum number of    # consecutive 1's present to the left,    # right, top and bottom of mat[i][j] including    # cell(i, j) respectively    left = [[0 for x in range(N)]               for y in range(N)]    right = [[0 for x in range(N)]                for y in range(N)]    top = [[0 for x in range(N)]              for y in range(N)]    bottom = [[0 for x in range(N)]                 for y in range(N)]     # initialize above four matrix    for i in range(N):                 # initialize first row of top        top[0][i] = mat[0][i]         # initialize last row of bottom        bottom[N - 1][i] = mat[N - 1][i]         # initialize first column of left        left[i][0] = mat[i][0]         # initialize last column of right        right[i][N - 1] = mat[i][N - 1]     # fill all cells of above four matrix    for i in range(N):        for j in range(1, N):                         # calculate left matrix (filled            # left to right)            if (mat[i][j] == 1):                left[i][j] = left[i][j - 1] + 1            else:                left[i][j] = 0             # calculate top matrix            if (mat[j][i] == 1):                top[j][i] = top[j - 1][i] + 1            else:                top[j][i] = 0             # calculate new value of j to calculate            # value of bottom(i, j) and right(i, j)            j = N - 1 - j             # calculate bottom matrix            if (mat[j][i] == 1):                bottom[j][i] = bottom[j + 1][i] + 1            else:                bottom[j][i] = 0             # calculate right matrix            if (mat[i][j] == 1):                right[i][j] = right[i][j + 1] + 1            else:                right[i][j] = 0             # revert back to old j            j = N - 1 - j     # n stores length of longest '+'    # found so far    n = 0     # compute longest +    for i in range(N):        for j in range(N):                         # find minimum of left(i, j),            # right(i, j), top(i, j), bottom(i, j)            l = min(min(top[i][j], bottom[i][j]),                    min(left[i][j], right[i][j]))             # largest + would be formed by            # a cell that has maximum value            if(l > n):                n = l     # 4 directions of length n - 1 and 1    # for middle cell    if (n):        return 4 * (n - 1) + 1     # matrix contains all 0's    return 0 # Driver Codeif __name__=="__main__":         # Binary Matrix of size N    mat = [ [ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ],            [ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 ],            [ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 ],            [ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 ],            [ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 ],            [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],            [ 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 ],            [ 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 ],            [ 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 ],            [ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 ]]     print(findLargestPlus(mat)) # This code is contributed by ChitraNayal
// C# program to find the size of the largest '+'// formed by all 1's in given binary matrixusing System; class GFG {         // size of binary square matrix    static int N = 10;     // Function to find the size of the largest '+'    // formed by all 1's in given binary matrix    static int findLargestPlus(int [,] mat)    {                 // left[j][j], right[i][j], top[i][j] and        // bottom[i][j] store maximum number of        // consecutive 1's present to the left,        // right, top and bottom of mat[i][j]        // including cell(i, j) respectively        int [,] left = new int[N,N];        int [,] right = new int[N,N];        int [,] top = new int[N,N];        int [,] bottom = new int[N,N];         // initialize above four matrix        for (int i = 0; i < N; i++) {                         // initialize first row of top            top[0,i] = mat[0,i];             // initialize last row of bottom            bottom[N - 1,i] = mat[N - 1,i];             // initialize first column of left            left[i,0] = mat[i,0];             // initialize last column of right            right[i,N - 1] = mat[i,N - 1];        }         // fill all cells of above four matrix        for (int i = 0; i < N; i++) {            for (int j = 1; j < N; j++) {                                 // calculate left matrix                // (filled left to right)                if (mat[i,j] == 1)                    left[i,j] = left[i,j - 1] + 1;                else                    left[i,j] = 0;                 // calculate top matrix                if (mat[j,i] == 1)                    top[j,i] = top[j - 1,i] + 1;                else                    top[j,i] = 0;                 // calculate new value of j to                // calculate value of bottom(i, j)                // and right(i, j)                j = N - 1 - j;                 // calculate bottom matrix                if (mat[j,i] == 1)                    bottom[j,i] = bottom[j + 1,i] + 1;                else                    bottom[j,i] = 0;                 // calculate right matrix                if (mat[i,j] == 1)                    right[i,j] = right[i,j + 1] + 1;                else                    right[i,j] = 0;                 // revert back to old j                j = N - 1 - j;            }        }         // n stores length of longest + found so far        int n = 0;         // compute longest +        for (int i = 0; i < N; i++) {            for (int j = 0; j < N; j++) {                 // find minimum of left(i, j),                // right(i, j), top(i, j),                // bottom(i, j)                int len = Math.Min(Math.Min(top[i,j],                    bottom[i,j]),Math.Min(left[i,j],                                        right[i,j]));                 // largest + would be formed by a                // cell that has maximum value                if (len > n)                    n = len;            }        }         // 4 directions of length n - 1 and 1 for        // middle cell        if (n > 0)            return 4 * (n - 1) + 1;         // matrix contains all 0's        return 0;    }     /* Driver function to test above functions */    public static void Main()    {                 // Binary Matrix of size N        int [,]mat = {            { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },            { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },            { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },            { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },            { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },            { 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 },            { 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 },            { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 },            { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }        };         Console.Write(findLargestPlus(mat));    }} // This code is contributed by KRV.
<?php// PHP program to find the size of the// largest '+' formed by all 1's in// given binary matrix // size of binary square matrix$N = 10; // Function to find the size of the largest '+'// formed by all 1's in given binary matrixfunction findLargestPlus($mat){    global $N ;         // left[j][j], right[i][j], top[i][j] and    // bottom[i][j] store maximum number of    // consecutive 1's present to the left,    // right, top and bottom of mat[i][j]    // including cell(i, j) respectively    $left[$N][$N] = array();    $right[$N][$N] = array();    $top[$N][$N] = array();    $bottom[$N][$N] = array();     // initialize above four matrix    for ($i = 0; $i < $N; $i++)    {        // initialize first row of top        $top[0][$i] = $mat[0][$i];         // initialize last row of bottom        $bottom[$N - 1][$i] = $mat[$N - 1][$i];         // initialize first column of left        $left[$i][0] = $mat[$i][0];         // initialize last column of right        $right[$i][$N - 1] = $mat[$i][$N - 1];    }     // fill all cells of above four matrix    for ( $i = 0; $i < $N; $i++)    {        for ($j = 1; $j < $N; $j++)        {            // calculate left matrix (filled left to right)            if ($mat[$i][$j] == 1)                $left[$i][$j] = $left[$i][$j - 1] + 1;            else                $left[$i][$j] = 0;             // calculate top matrix            if ($mat[$j][$i] == 1)                $top[$j][$i] = $top[$j - 1][$i] + 1;            else                $top[$j][$i] = 0;             // calculate new value of j to calculate            // value of bottom(i, j) and right(i, j)            $j = $N - 1 - $j;             // calculate bottom matrix            if ($mat[$j][$i] == 1)                $bottom[$j][$i] = $bottom[$j + 1][$i] + 1;            else                $bottom[$j][$i] = 0;             // calculate right matrix            if ($mat[$i][$j] == 1)                $right[$i][$j] = $right[$i][$j + 1] + 1;            else                $right[$i][$j] = 0;             // revert back to old j            $j = $N - 1 - $j;        }    }     // n stores length of longest + found so far    $n = 0;     // compute longest +    for ($i = 0; $i < $N; $i++)    {        for ($j = 0; $j < $N; $j++)        {            // find minimum of left(i, j), right(i, j),            // top(i, j), bottom(i, j)            $len = min(min($top[$i][$j], $bottom[$i][$j]),                       min($left[$i][$j], $right[$i][$j]));             // largest + would be formed by a            // cell that has maximum value            if($len > $n)                $n = $len;        }    }     // 4 directions of length n - 1 and 1    // for middle cell    if ($n)    return 4 * ($n - 1) + 1;     // matrix contains all 0's    return 0;} // Driver Code // Binary Matrix of size N$mat = array(array(1, 0, 1, 1, 1, 1, 0, 1, 1, 1),             array(1, 0, 1, 0, 1, 1, 1, 0, 1, 1),             array(1, 1, 1, 0, 1, 1, 0, 1, 0, 1),             array(0, 0, 0, 0, 1, 0, 0, 1, 0, 0),             array(1, 1, 1, 0, 1, 1, 1, 1, 1, 1),             array(1, 1, 1, 1, 1, 1, 1, 1, 1, 0),             array(1, 0, 0, 0, 1, 0, 0, 1, 0, 1),             array(1, 0, 1, 1, 1, 1, 0, 0, 1, 1),             array(1, 1, 0, 0, 1, 0, 1, 0, 0, 1),             array(1, 0, 1, 1, 1, 1, 0, 1, 0, 0)); echo findLargestPlus($mat); // This code is contributed by Sach_Code?>
<script>// Javascript program to find the size of the largest '+'// formed by all 1's in given binary matrix     // size of binary square matrix    let N = 10;         // Function to find the size of the largest '+'    // formed by all 1's in given binary matrix    function findLargestPlus(mat)    {        // left[j][j], right[i][j], top[i][j] and        // bottom[i][j] store maximum number of        // consecutive 1's present to the left,        // right, top and bottom of mat[i][j]        // including cell(i, j) respectively        let left = new Array(N);        for(let i = 0; i < N; i++)        {            left[i] = new Array(N);        }                 let right = new Array(N);        for(let i = 0; i < N; i++)        {            right[i] = new Array(N);        }                 let top = new Array(N);        for(let i = 0; i < N; i++)        {            top[i] = new Array(N);        }                 let bottom = new Array(N);        for(let i = 0; i < N; i++)        {            bottom[i] = new Array(N);        }                 for(let i = 0; i < N; i++)        {            for(let j = 0; j < N; j++)            {                left[i][j] = 0;                right[i][j] = 0;                top[i][j] = 0;                bottom[i][j] = 0;            }        }                 // initialize above four matrix        for (let i = 0; i < N; i++) {                           // initialize first row of top            top[0][i] = mat[0][i];               // initialize last row of bottom            bottom[N - 1][i] = mat[N - 1][i];               // initialize first column of left            left[i][0] = mat[i][0];               // initialize last column of right            right[i][N - 1] = mat[i][N - 1];        }                 // fill all cells of above four matrix        for (let i = 0; i < N; i++) {            for (let j = 1; j < N; j++) {                                   // calculate left matrix                // (filled left to right)                if (mat[i][j] == 1)                    left[i][j] = left[i][j - 1] + 1;                else                    left[i][j] = 0;                   // calculate top matrix                if (mat[j][i] == 1)                    top[j][i] = top[j - 1][i] + 1;                else                    top[j][i] = 0;                   // calculate new value of j to                // calculate value of bottom(i, j)                // and right(i, j)                j = N - 1 - j;                   // calculate bottom matrix                if (mat[j][i] == 1)                    bottom[j][i] = bottom[j + 1][i] + 1;                else                    bottom[j][i] = 0;                   // calculate right matrix                if (mat[i][j] == 1)                    right[i][j] = right[i][j + 1] + 1;                else                    right[i][j] = 0;                   // revert back to old j                j = N - 1 - j;            }        }           // n stores length of longest + found so far        let n = 0;           // compute longest +        for (let i = 0; i < N; i++) {            for (let j = 0; j < N; j++) {                // find minimum of left(i, j),                // right(i, j), top(i, j),                // bottom(i, j)                let len = Math.min(Math.min(top[i][j],                    bottom[i][j]),Math.min(left[i][j],                                        right[i][j]));                   // largest + would be formed by a                // cell that has maximum value                if (len > n)                    n = len;            }        }           // 4 directions of length n - 1 and 1 for        // middle cell        if (n > 0)            return 4 * (n - 1) + 1;           // matrix contains all 0's        return 0;    }         /* Driver function to test above functions */    let mat = [ [ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ],            [ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 ],            [ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 ],            [ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 ],            [ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 ],            [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],            [ 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 ],            [ 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 ],            [ 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 ],            [ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 ]]    document.write(findLargestPlus(mat));         // This code is contributed by avanitrachhadiya2155</script>
Output:  
17
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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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. 
KRV
ukasp
Sach_Code
avanitrachhadiya2155
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Program to multiply two matrices
Python program to multiply two matrices
The Celebrity Problem
Min Cost Path | DP-6
Printing all solutions in N-Queen Problem
Count all possible paths from top left to bottom right of a mXn matrix
Multiplication of two Matrices in Single line using Numpy in Python
Search in a row wise and column wise sorted matrix
Program to find transpose of a matrix