Find Maximum side length of square in a Matrix

Given a square matrix of odd order N. The task is to find the side-length of the largest square formed in the matrix. A square in matrix is said to be formed if all elements on its perimeter is same and its centre is centre of matrix. Centre of matrix is also a square with side length 1.

Examples:

Input : matrix[][] = {2, 3, 0,
                      0, 2, 0,
                      0, 1, 1}
Output : 1

Input : matrix[][] = {2, 2, 2,
                      2, 1, 2,
                      2, 2, 2}
Output : 3

The centre of any odd order matrix is element with index i = j = n/2. Now, for finding largest square in matrix, check all sorrounding elements from centre which are at same distance. For a square which is i-unit away from centre check all the element which are in the n/2-i and n/2+i row and column also difference of n/2 and either of index of element does-not exceed i. Please find the format of a square with different possible side length in below example.

x x x x x
x y y y x
x y c y x
x y y y x
x x x x x

Points to care about :

Algorithm :



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find max possible side-length
// of a square in a given matrix
  
#include <bits/stdc++.h>
#define n 5
using namespace std;
  
// Function to return side-length of square
int largestSideLen(int matrix[][n])
{
    int result = 1;
  
    // Traverse the matrix
    for (int i = 0; i < n / 2; i++) {
  
        int element = matrix[i][i];
        int isSquare = 1;
  
        for (int j = i; j < n - i; j++) {
            // for row i
            if (matrix[i][j] != element)
                isSquare = 0;
            // for row n-i-1
            if (matrix[n - i - 1][j] != element)
                isSquare = 0;
            // for column i
            if (matrix[j][i] != element)
                isSquare = 0;
            // for column n-i-1
            if (matrix[j][n - i - 1] != element)
                isSquare = 0;
        }
  
        if (isSquare)
            return n - 2 * i;
    }
  
    // Return result
    return result;
}
  
// Driver program
int main()
{
    int matrix[n][n] = { 1, 1, 1, 1, 1,
                         1, 2, 2, 2, 1,
                         1, 2, 1, 2, 1,
                         1, 2, 2, 2, 1,
                         1, 1, 1, 1, 1 };
  
    cout << largestSideLen(matrix);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find max possible side-length
// of a square in a given matrix
  
import java.io.*;
  
class GFG {
static int n = 5;
    // Function to return side-length of square
static int largestSideLen(int matrix[][])
{
    int result = 1;
  
    // Traverse the matrix
    for (int i = 0; i < (n / 2); i++) {
  
        int element = matrix[i][i];
        int isSquare = 1;
  
        for (int j = i; j < n - i; j++) {
            // for row i
            if (matrix[i][j] != element)
                isSquare = 0;
            // for row n-i-1
            if (matrix[n - i - 1][j] != element)
                isSquare = 0;
            // for column i
            if (matrix[j][i] != element)
                isSquare = 0;
            // for column n-i-1
            if (matrix[j][n - i - 1] != element)
                isSquare = 0;
        }
  
        if (isSquare > 0)
            return n - (2 * i);
    }
  
    // Return result
    return result;
}
  
// Driver program
      
    public static void main (String[] args) {
        int matrix[][] = {{ 1, 1, 1, 1, 1},
                        {1, 2, 2, 2, 1},
                        {1, 2, 1, 2, 1},
                        {1, 2, 2, 2, 1},
                        {1, 1, 1, 1, 1 }};
  
    System.out.println (largestSideLen(matrix));
          
          
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find max possible 
# side-length of a square in a given matrix
  
n = 5
  
# Function to return side-length of square
def largestSideLen(matrix):
    result = 1
  
    # Traverse the matrix
    for i in range(int(n / 2)):
        element = matrix[i][i]
        isSquare = 1
  
        for j in range(i, n - i):
              
            # for row i
            if (matrix[i][j] != element):
                isSquare = 0
                  
            # for row n-i-1
            if (matrix[n - i - 1][j] != element):
                isSquare = 0
                  
            # for column i
            if (matrix[j][i] != element):
                isSquare = 0
                  
            # for column n-i-1
            if (matrix[j][n - i - 1] != element):
                isSquare = 0
  
        if (isSquare):
            return n - 2 * i
  
    # Return result
    return result
  
# Driver Code
if __name__ == '__main__':
    matrix = [[1, 1, 1, 1, 1],
              [1, 2, 2, 2, 1],
              [1, 2, 1, 2, 1],
              [1, 2, 2, 2, 1],
              [1, 1, 1, 1, 1]]
  
    print(largestSideLen(matrix))
  
# This code is contributed by
# Surendra_Gangwar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C#  program to find max possible side-length
// of a square in a given matrix
using System;
  
public class GFG{
    static int n = 5;
    // Function to return side-length of square
static int largestSideLen(int [,]matrix)
{
    int result = 1;
  
    // Traverse the matrix
    for (int i = 0; i < (n / 2); i++) {
  
        int element = matrix[i,i];
        int isSquare = 1;
  
        for (int j = i; j < n - i; j++) {
            // for row i
            if (matrix[i,j] != element)
                isSquare = 0;
            // for row n-i-1
            if (matrix[n - i - 1,j] != element)
                isSquare = 0;
            // for column i
            if (matrix[j,i] != element)
                isSquare = 0;
            // for column n-i-1
            if (matrix[j,n - i - 1] != element)
                isSquare = 0;
        }
  
        if (isSquare > 0)
            return n - (2 * i);
    }
  
    // Return result
    return result;
}
  
// Driver program
      
      
    static public void Main (){
        int [,]matrix = {{ 1, 1, 1, 1, 1},
                        {1, 2, 2, 2, 1},
                        {1, 2, 1, 2, 1},
                        {1, 2, 2, 2, 1},
                        {1, 1, 1, 1, 1 }};
  
           Console.WriteLine(largestSideLen(matrix));
          
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find max possible
// side-length of a square in a given matrix 
  
// Function to return side-length of square 
function largestSideLen($matrix
    $n = 5; 
    $result = 1; 
  
    // Traverse the matrix 
    for ($i = 0; $i < ($n / 2); $i++)
    
  
        $element = $matrix[$i][$i]; 
        $isSquare = 1; 
  
        for ($j = $i; $j < ($n - $i); $j++) 
        
            // for row i 
            if ($matrix[$i][$j] != $element
                $isSquare = 0; 
                  
            // for row n-i-1 
            if ($matrix[$n - $i - 1][$j] != $element
                $isSquare = 0; 
                  
            // for column i 
            if ($matrix[$j][$i] != $element
                $isSquare = 0; 
                  
            // for column n-i-1 
            if ($matrix[$j][$n - $i - 1] != $element
                $isSquare = 0; 
        
  
        if ($isSquare
            return ($n - 2 * $i); 
    
  
    // Return result 
    return $result
  
// Driver Code 
$matrix = array( 1, 1, 1, 1, 1, 
                 1, 2, 2, 2, 1, 
                 1, 2, 1, 2, 1, 
                 1, 2, 2, 2, 1, 
                 1, 1, 1, 1, 1 ); 
  
echo largestSideLen($matrix); 
  
// This code is contributed by Sachin
?>
chevron_right

Output:
5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Sach_Code, SURENDRA_GANGWAR

Article Tags :
Practice Tags :