Largest square formed in a matrix | Practice | GeeksforGeeks

Given a binary matrix mat of size n * m, find out the maximum size square sub-matrix with all 1s.
Example 1:
Input: n = 2, m = 2
mat = {{1, 1}, 
       {1, 1}}
Output: 2
Explaination: The maximum size of the square
sub-matrix is 2. The matrix itself is the 
maximum sized sub-matrix in this case.
Example 2:
Input: n = 2, m = 2
mat = {{0, 0}, 
       {0, 0}}
Output: 0
Explaination: There is no 1 in the matrix.
Your Task:
You do not need to read input or print anything. Your task is to complete the function maxSquare() which takes n, m and mat as input parameters and returns the size of the maximum square sub-matrix of given matrix.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n, m ≤ 50
0 ≤ mat[i][j] ≤ 1 
0
vickyupadhyay361 day ago
giving error in one test case while submitting, but passing for same test case in custom input
int maxSquare(int n, int m, vector<vector<int>> mat){
       // code here
       vector<vector<int>> t(n,vector<int>(m,0));
       int mx=0;
       for(int i=n-1;i>=0;i--){
           for(int j=m-1;j>=0;j--){
               if(mat[i][j]==1){
                   if(i==n-1 || j==n-1){//last row or  last column
                        t[i][j]=1;
                   }
                   else{
                       t[i][j]=1+min(t[i][j+1],min(t[i+1][j+1],t[i+1][j]));
                   }
               }
               mx=max(mx,t[i][j]);
           }
       }
       
       return mx;
   }
0
tanishqspike1143 weeks ago
C++| Simple DP Logic
class Solution{
public:
    int maxSquare(int n, int m, vector<vector<int>> mat){
        vector<vector<int>> dp(n+1,vector<int>(m+1));
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mat[i-1][j-1]==1)
                    dp[i][j]=min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]))+1;
                ans=max(ans,dp[i][j]);
            }
        }
        return ans;
    }
};
 
0
sharadyaduvanshi3 weeks ago
 int maxSquare(int n, int m, vector<vector<int>> mat){       // code here              // for(int i = 0 ; i < n; i ++)       // {       //     for(int j = 0; j < m; j ++)       //     {       //          mat[i][j] = 1 - mat[i][j];       //     }       // }              int **dp = new int*[n+1];       for(int i = 0 ; i< n; i ++)       {           dp[i] = new int[m+1];           for(int j = 0; j < m; j ++)           {               // if(i == n-1 || j == m-1)               // dp[i][j] = mat[i][j];               // if(mat[i][j] == 0)               dp[i][j] = mat[i][j];           }       }        for(int i = n-2 ; i>= 0; i --)       {           //dp[i] = new int[m+1];           for(int j = m-2; j >=0 ; j --)           {               if(j+1 < m && i+1<n &&mat[i][j] == 1)               dp[i][j] = dp[i][j]+min(dp[i][j+1],min(dp[i+1][j],dp[i+1][j+1]));           }       }       int ans = INT_MIN;       for(int i = 0; i < n; i ++)       {           for(int j = 0; j < m; j ++)           {               ans = max(ans,dp[i][j]);           }       }       return ans;          }
0
jainmuskan5653 weeks ago
// int helpSolve(int n,int m, vector<vector<int>> mat, vector<vector<int>>& dp){   //     int r= n, c= m;   //     // if(n>=r || m>=c){   //     //     return 0;   //     // }   //     // if(dp[n][m]!= -1){   //     //     return dp[n][m];   //     // }   //     // int right= helpSolve(n,m+1,mat,dp); // dp[n][m+1];   //     // int down= helpSolve(n+1,m,mat,dp); // dp[n+1][m];   //     // int diag= helpSolve(n+1,m+1,mat,dp); // dp[n+1][m+1];          //     // dp[n][m]=0;   //     // if(mat[n][m]==1){   //     //     dp[n][m]=1+min({right,down,diag});   //     // }          //     // // int ans=INT_MIN;   //     // // for(int i=0;i<n;i++){   //     // //     for(int j=0;j<m;j++){   //     // //         ans=max(ans,dp[i][j]);   //     // //     }   //     // // }   //     return dp[n][m];   // }   int maxSquare(int n, int m, vector<vector<int>> mat){       vector<vector<int>>dp(n+1,vector<int>(m+1));       int r=n, c=m;       for(int i=0;i<=r;i++){           for(int j=0;j<=c;j++){               if(i==0||j==0){                   dp[i][j]=0;               }           }       }       for(int i=1;i<=r;i++){           for(int j=1;j<=c;j++){               if(mat[i-1][j-1]==1){                   dp[i][j]= 1+min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]});               }               else if(mat[i-1][j-1]==0){                   dp[i][j]=0;               }           }       }       int ans= dp[n][m];              for(int i=1;i<=n;i++){           for(int j=1;j<=m;j++){               ans=max(ans,dp[i][j]);           }       }       return ans;   }
0
khushiaggarwal09021 month ago
//Easiest approch
(According to Aditya Verma approch)
int maxSquare(int n, int m, vector<vector<int>> mat)   {       int dp[n+1][m+1];       int ans=INT_MIN;       for(int i=0;i<=n;i++)       {                  for(int j=0;j<=m;j++)           {               if(i==0 || j==0)               {                    dp[i][j]=0;                }           }       }                     for(int i=1;i<n+1;i++)      for(int j=1;j<m+1;j++)      {               if(mat[i-1][j-1]==1)               {                   dp[i][j] = 1+min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]});               }               else if(mat[i-1][j-1]==0)               {                   dp[i][j]=0;               }                        }                      for(int i=1;i<n+1;i++)      for(int j=1;j<m+1;j++)      {          if(dp[i][j]>ans)          {              ans=dp[i][j];          }      }              return ans;   }
0
aasif23641 month ago
class Solution{public:   int maxSquare(int n, int m, vector<vector<int>> mat){       vector<vector<int>>dp(n , vector<int>(m , 0));       int res=0;       for(int i=n-1 ; i>=0 ; i--){           for(int j=m-1 ; j>=0 ; j--){               if(i==n-1 and j==m-1){                   dp[i][j]=mat[i][j];               }               else if(i==n-1){                   dp[i][j]=mat[i][j];               }               else if(j==m-1){                   dp[i][j]=mat[i][j];               }               else if(mat[i][j]==1){                   dp[i][j]=min({dp[i][j+1] , dp[i+1][j] , dp[i+1][j+1]}) + 1;               }                              res=max(res , dp[i][j]);           }       }              return res;   }};
0
yashgehi3982 months ago
class Solution{
public:
    int maxSquare(int n, int m, vector<vector<int>> mat){
        // code here
        vector<vector<int>> dp(n+1,vector<int>(m+1,0));
        int size =0;
        for(int i{1};i<=n;i++){
            for(int j{1};j<=m;j++){
                if(mat[i-1][j-1] == 0) dp[i][j] =0;
                else dp[i][j] = 1+min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));
                size = max(size,dp[i][j]);
            }
        }
        return size;
    }
};

0
artistdarkangel2 months ago
JAVA Solution -
Time Complexity: O(n*m)Auxiliary Space: O(1)
class Solution{   static int maxSquare(int n, int m, int mat[][]){       int max=0;       if(n==0 || m==0)       return 0;       for(int i=n-1; i>=0; i--)       {           for(int j=m-1; j>=0; j--)           {               if(mat[i][j]==1)               {                   max=1;                   break;               }           }              if(max==1)           break;       }              for(int i=n-2; i>=0; i--)       {           for(int j=m-2; j>=0; j--)           {               if(mat[i][j]==1)               {                   mat[i][j]=Math.min(Math.min(mat[i+1][j], mat[i][j+1]),mat[i+1][j+1])+1;                   if(mat[i][j]>max)                   max=mat[i][j];               }           }          }       return max;   }}
 
0
sharma_nitin_262 months ago
class Solution{
public:
    int maxSquare(int n, int m, vector<vector<int>> v){
        int ans=0;
        int a[n][m];
        
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                if(i==0 || j==0){
                    a[i][j]=v[i][j];
                    if(ans<a[i][j])
                        ans=a[i][j];
                }
                else if(v[i][j]==1){
                    a[i][j]=min({a[i][j-1],a[i-1][j],a[i-1][j-1]})+1;
                    if(ans<a[i][j])
                        ans=a[i][j];
                }
                else
                    a[i][j]=0;
            }
        return ans;
    }
};
0
lindan1232 months ago
int maxSquare(int n, int m, vector<vector<int>> mat){
        int dp[n+1][m+1];
        int ans=INT_MIN;
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=m;j++)
            {
                if(i==0 || j==0)
                {
                     dp[i][j]=0;
                }
                else if(mat[i-1][j-1]==1)
                {
                    dp[i][j] = 1+min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]});
                }
                else if(mat[i-1][j-1]==0)
                {
                    dp[i][j]=0;
                }
                ans = max(ans,dp[i][j]);
            }
        }
        return ans;
    }
Time Taken : 0.0
Cpp
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.