- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find Maximum side length of square in a Matrix in C++

In this problem, we are given a 2-D matrix mat[][] of size n, n being an odd number. Our task is to Find Maximum side length of a square in a Matrix.

**Problem Description** − We need to find the length of the square matrix whose perimeter values are the same and it shares the same center as the matrix.

**Let’s take an example to understand the problem,**

## Input

mat[][] = { {2, 4, 6, 6, 5}, {1, 7, 7, 7, 3}, {5, 7, 0, 7, 1}, {3, 7, 7, 7, 1}, {2, 0, 1, 3, 2} }

## Output

3

## Solution Approach

A simple solution to the problem is by finding the center element of the matrix as it is an odd matrix, the center element would be at index (n/2, n/2). After finding the center we will find all the 2D sub-matrices around it which will check if all its elements are the same.

The submatrix at index i away from the center will have an element of row (n/2 - 1) and (n/2 + 1) from index (n/2 - 1) to (n/2 + 1). Also elements of column (n/2 - 1) and (n/2 + 1) from index (n/2 - 1) to (n/2 + 1) are included at its perimeter. We need to check if the sub-matrix for any value of i from 0 to n/2 has all perimeter elements the same.

**Program to illustrate the working of our solution,**

## Example

#include <iostream> #define n 5 using namespace std; int findMaxSideSquare(int matrix[][n]) { int squareLen = 1; for (int i = 0; i < n / 2; i++) { int sideVal = matrix[i][i]; bool isSquare = true; for (int j = i; j < n - i; j++) { if (matrix[i][j] != sideVal) isSquare = false; if (matrix[n - i - 1][j] != sideVal) isSquare = false; if (matrix[j][i] != sideVal) isSquare = false; if (matrix[j][n - i - 1] != sideVal) isSquare = false; } if (isSquare) squareLen = n - 2 * i; } return squareLen; } int main() { int mat[n][n] = { {2, 4, 6, 6, 5}, {1, 7, 7, 7, 3}, {5, 7, 0, 7, 1}, {3, 7, 7, 7, 1}, {2, 0, 1, 3, 2} }; cout<<"The maximum side length of square in a Matrix is "<<findMaxSideSquare(mat); return 0; }

## Output

The maximum side length of square in a Matrix is 3

- Related Questions & Answers
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++
- Find maximum path length in a binary matrix in Python
- Maximum and Minimum in a square matrix in C++
- Program to find length of longest matrix path length in Python
- Print maximum sum square sub-matrix of given size in C Program.
- Program to find maximum length of k ribbons of same length in Python
- Area of a square from diagonal length in C++
- Find maximum element of each column in a matrix in C++
- Find maximum element of each row in a matrix in C++
- Find maximum average subarray of k length in C++
- Program to find area of largest square of 1s in a given matrix in python
- C++ program to find the side of the Octagon inscribed within the square
- Find maximum length Snake sequence in Python
- Find maximum length Snake sequence in C++
- Python – Sort Matrix by Maximum String Length