
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximal Square in C++
Suppose we have a 2D binary matrix filled with 0's and 1's. We have to find the largest square containing only 1's and return its area. So if the matrix is like −
1 | 0 | 1 | 0 | 0 |
1 | 0 | 1 | 1 | 0 |
1 | 1 | 1 | 1 | 1 |
1 | 0 | 0 | 1 | 0 |
Then the output will be 4
To solve this, we will follow these steps −
ans := 0, n := number of rows, c := number of rows
if n is 0, then return 0
Create another matrix of order (n x c)
for i in range 0 to n – 1
for j in range 0 to c – 1
m[i, j] := matrix[i, j]
ans := maximum of m[i, j] and ans
for j in range 0 to c – 1
if m[i, j] is not 0, then
m[i, j] := 1 + minimum of m[i + 1, j], m[i, j-1], m[i + 1, j-1],
ans := maximum of m[i, j] and ans
return ans*ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { int ans = 0; int n = matrix.size(); if(!n)return 0; int c = matrix[0].size(); vector<vector<int>> m(n, vector <int> (c)); for(int i =0;i<n;i++){ for(int j = 0; j<c;j++){ m[i][j] = matrix[i][j] - '0'; ans = max(m[i][j],ans); } } for(int i =n-2;i>=0;i--){ for(int j =1;j<c;j++){ if(m[i][j]){ m[i][j] = 1 + min({m[i+1][j],m[i][j-1],m[i+1][j-1]}); } ans = max(ans,m[i][j]); } } return ans*ans; } }; main(){ vector<vector<char>> v = {{'1','0','1','0','0'},{'1','0','1','1','1'},{'1','1','1','1','1'}, {'1','0','0','1','0'}}; Solution ob; cout << ((ob.maximalSquare(v))); }
Input
[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output
4
- Related Articles
- Maximal Rectangle in C++
- Maximal Disjoint Intervals in C++
- What are Maximal Frequent Itemsets?
- Program to find maximal network rank in Python
- Counting Maximal Value Roots in Binary Tree in C++
- Get maximal GPS precision on mobile browser
- C++ Program to find maximal achievable diversity of music notes
- In the following APs, find the missing terms in the boxes:(i) $2, \square, 26$(ii) $\square, 13, \square, 3$(iii) $5, \square, \square, 9\frac{1}{2}$(iv) $-4, \square, \square, \square, \square, 6$(v) $\square, 38, \square, \square, \square, -22$
- Square and Square root in Arduino
- In the following APs, find the missing terms in the boxes:$-4, \square, \square, \square, \square, 6$
- In the following APs, find the missing terms in the boxes:$\square, 38, \square, \square, \square, -22$
- Program to print Square inside a Square in C
- Count square and non-square numbers before n in C++
- Fill in the blanks:(a) \( 369 \div \square=369 \)(b) \( (-75) \div \square=-1 \)(c) \( (-206) \div \square=1 \)(d) \( -87 \div \square=87 \)(e) \( \square \div -1=-87 \)(f) \( \square \div 48=-1 \)(g) \( 20 \div \square=-2 \)(h) \( \square \div(4)=-3 \)
- Latin Square in Python

Advertisements