Given a matrix mat[][] of dimensions N*M, the task is to find the size of the largest square submatrix such that the sum of all rows, columns, diagonals in that submatrix are equal.
Examples:
Input: N = 3, M = 4, mat[][] = [[5, 1, 3, 1], [9, 3, 3, 1], [1, 3, 3, 8]]
Output: 2
Explanation:
The submatrix which satisfies all the given conditions is shown in bold
5 1 3 1
9 3 3 1
1 3 3 8
Therefore, the size of the submatrix is 2.Input: N = 4, M = 5, mat[][] = [[7, 1, 4, 5, 6], [2, 5, 1, 6, 4], [1, 5, 4, 3, 2], [1, 2, 7, 3, 4]]
Output: 3
Explanation:
The submatrix which satisfies all the given conditions is shown in bold
7 1 4 5 6
2 5 1 6 4
1 5 4 3 2
1 2 7 3 4
Therefore, the size of the submatrix is 3.
Approach: The given problem can be solved by finding the Prefix Sum of all the rows and the columns and then iterate for all possible sizes of the square submatrix from each cell of the matrix and if there exists any such square matrix that satisfies the given criteria then print that size of a square matrix. Follow the below steps to solve the problem:
- Maintain two prefix sum arrays prefixSumRow[] and prefixSumColumn[] and store the prefix sum of rows and columns of the given matrix respectively.
- Perform the following steps to check if any square matrix starting from the cell (i, j) of size K satisfy the given criteria or not:
- Find the sum of elements of primary diagonal of the submatrix mat[i][j] to mat[i + K][j + K] and store it in the variable, say sum.
- If the value of the sum is the same as the value mentioned below then return true. Otherwise, return false.
- The prefix sum of all the rows i.e., the value of prefixSumRow[k][j + K] – prefixSumRow[k][j] for all values of k over then range [i, i + K].
- The prefix sum of all the columns i.e., the value of prefixSumColumn[i + K][j] – prefixSumColumn[i][k] for all values of k over then range [j, j + K].
- The prefix sum of anti-diagonal elements.
- Now, iterate for all possible sizes of the square matrix that can be formed over the range [min(N, M), 1] and if there exist any possible satisfies the given criteria using the steps in the above steps, then print that size of a square matrix.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Define the prefix sum arrays globally int prefix_sum_row[50][51];
int prefix_sum_col[51][50];
bool is_valid( int r, int c, int size,
vector<vector< int > >& grid)
{ int r_end = r + size, c_end = c + size;
// Diagonal sum
int sum = 0;
for ( int i = r, j = c; i < r_end; i++, j++) {
sum += grid[i][j];
}
// Check each row
for ( int i = r; i < r_end; i++) {
if (prefix_sum_row[i][c_end]
- prefix_sum_row[i]
!= sum) {
return false ;
}
}
// Check each column
for ( int i = c; i < c_end; i++) {
if (prefix_sum_col[r_end][i]
- prefix_sum_col[r][i]
!= sum) {
return false ;
}
}
// Check anti-diagonal
int ad_sum = 0;
for ( int i = r, j = c_end - 1; i < r_end;
i++, j--) {
ad_sum += grid[i][j];
}
return ad_sum == sum;
} int largestSquareValidMatrix(
vector<vector< int > >& grid)
{ // Store the size of the given grid
int m = grid.size(), n = grid[0].size();
// Compute the prefix sum for the rows
for ( int i = 0; i < m; i++) {
for ( int j = 1; j <= n; j++) {
prefix_sum_row[i][j]
= prefix_sum_row[i][j - 1]
+ grid[i][j - 1];
}
}
// Compute the prefix sum for the columns
for ( int i = 1; i <= m; i++) {
for ( int j = 0; j < n; j++) {
prefix_sum_col[i][j]
= prefix_sum_col[i - 1][j]
+ grid[i - 1][j];
}
}
// Check for all possible square submatrix
for ( int size = min(m, n); size > 1; size--) {
for ( int i = 0; i <= m - size; i++) {
for ( int j = 0; j <= n - size; j++) {
if (is_valid(i, j, size, grid)) {
return size;
}
}
}
}
return 1;
} // Driver Code int main()
{ vector<vector< int > > grid = { { 7, 1, 4, 5, 6 },
{ 2, 5, 1, 6, 4 },
{ 1, 5, 4, 3, 2 },
{ 1, 2, 7, 3, 4 } };
cout << largestSquareValidMatrix(grid);
return 0;
} |
// Java program for the above approach class GFG
{ // Define the prefix sum arrays globally
public static int [][] prefix_sum_row = new int [ 50 ][ 51 ];
public static int [][] prefix_sum_col = new int [ 51 ][ 50 ];
public static boolean is_valid( int r, int c, int size, int [][] grid) {
int r_end = r + size, c_end = c + size;
// Diagonal sum
int sum = 0 ;
for ( int i = r, j = c; i < r_end; i++, j++) {
sum += grid[i][j];
}
// Check each row
for ( int i = r; i < r_end; i++) {
if (prefix_sum_row[i][c_end] - prefix_sum_row[i] != sum) {
return false ;
}
}
// Check each column
for ( int i = c; i < c_end; i++) {
if (prefix_sum_col[r_end][i] - prefix_sum_col[r][i] != sum) {
return false ;
}
}
// Check anti-diagonal
int ad_sum = 0 ;
for ( int i = r, j = c_end - 1 ; i < r_end; i++, j--) {
ad_sum += grid[i][j];
}
return ad_sum == sum;
}
public static int largestSquareValidMatrix( int [][] grid) {
// Store the size of the given grid
int m = grid.length, n = grid[ 0 ].length;
// Compute the prefix sum for the rows
for ( int i = 0 ; i < m; i++) {
for ( int j = 1 ; j <= n; j++) {
prefix_sum_row[i][j] = prefix_sum_row[i][j - 1 ] + grid[i][j - 1 ];
}
}
// Compute the prefix sum for the columns
for ( int i = 1 ; i <= m; i++) {
for ( int j = 0 ; j < n; j++) {
prefix_sum_col[i][j] = prefix_sum_col[i - 1 ][j] + grid[i - 1 ][j];
}
}
// Check for all possible square submatrix
for ( int size = Math.min(m, n); size > 1 ; size--) {
for ( int i = 0 ; i <= m - size; i++) {
for ( int j = 0 ; j <= n - size; j++) {
if (is_valid(i, j, size, grid)) {
return size;
}
}
}
}
return 1 ;
}
// Driver Code
public static void main(String args[]) {
int [][] grid = { { 7 , 1 , 4 , 5 , 6 }, { 2 , 5 , 1 , 6 , 4 }, { 1 , 5 , 4 , 3 , 2 }, { 1 , 2 , 7 , 3 , 4 } };
System.out.println(largestSquareValidMatrix(grid));
}
} // This code is contributed by saurabh_jaiswal. |
## Python program for the above approach: ## Define the prefix sum arrays globally prefix_sum_row = [[ 0 ] * 51 for _ in range ( 50 )]
prefix_sum_col = [[ 0 ] * 50 for _ in range ( 51 )]
def is_valid(r, c, size, grid):
r_end = r + size
c_end = c + size
## Diagonal sum
sum = 0 ;
j = c
for i in range (r, r_end):
sum + = grid[i][j];
j + = 1
## Check each row
for i in range (r, r_end):
if ((prefix_sum_row[i][c_end] - prefix_sum_row[i]) ! = sum ):
return False
## Check each column
for i in range (c, c_end):
if ((prefix_sum_col[r_end][i] - prefix_sum_col[r][i]) ! = sum ):
return False
## Check anti-diagonal
ad_sum = 0 ;
j = c_end - 1
for i in range (r, r_end):
ad_sum + = grid[i][j]
j - = 1
return (ad_sum = = sum )
def largestSquareValidMatrix(grid):
## Store the size of the given grid
m = len (grid)
n = len (grid[ 0 ])
## Compute the prefix sum for the rows
for i in range ( 0 , m):
for j in range ( 1 , n + 1 ):
prefix_sum_row[i][j] = prefix_sum_row[i][j - 1 ] + grid[i][j - 1 ];
## Compute the prefix sum for the columns
for i in range ( 1 , m + 1 ):
for j in range ( 0 , n):
prefix_sum_col[i][j] = prefix_sum_col[i - 1 ][j] + grid[i - 1 ][j]
## Check for all possible square submatrix
for size in range ( min (m, n), 1 , - 1 ):
for i in range ( 0 , m - size + 1 ):
for j in range ( 0 , n - size + 1 ):
if (is_valid(i, j, size, grid)):
return size
return 1
## Driver code if __name__ = = '__main__' :
grid = [ [ 7 , 1 , 4 , 5 , 6 ],
[ 2 , 5 , 1 , 6 , 4 ],
[ 1 , 5 , 4 , 3 , 2 ],
[ 1 , 2 , 7 , 3 , 4 ]
]
print (largestSquareValidMatrix(grid))
# This code is contributed by subhamgoyal2014.
|
// C# program for the above approach using System;
class GFG
{ // Define the prefix sum arrays globally
public static int [,] prefix_sum_row = new int [50,51];
public static int [,] prefix_sum_col = new int [51,50];
public static bool is_valid( int r, int c, int size, int [,] grid) {
int r_end = r + size, c_end = c + size;
// Diagonal sum
int sum = 0;
for ( int i = r, j = c; i < r_end; i++, j++) {
sum += grid[i,j];
}
// Check each row
for ( int i = r; i < r_end; i++) {
if (prefix_sum_row[i,c_end] - prefix_sum_row[i,c] != sum) {
return false ;
}
}
// Check each column
for ( int i = c; i < c_end; i++) {
if (prefix_sum_col[r_end,i] - prefix_sum_col[r,i] != sum) {
return false ;
}
}
// Check anti-diagonal
int ad_sum = 0;
for ( int i = r, j = c_end - 1; i < r_end; i++, j--) {
ad_sum += grid[i,j];
}
return ad_sum == sum;
}
public static int largestSquareValidMatrix( int [,] grid) {
// Store the size of the given grid
int m = grid.GetLength(0), n = grid.GetLength(1);
// Compute the prefix sum for the rows
for ( int i = 0; i < m; i++) {
for ( int j = 1; j <= n; j++) {
prefix_sum_row[i,j] = prefix_sum_row[i,j - 1] + grid[i,j - 1];
}
}
// Compute the prefix sum for the columns
for ( int i = 1; i <= m; i++) {
for ( int j = 0; j < n; j++) {
prefix_sum_col[i,j] = prefix_sum_col[i - 1,j] + grid[i - 1,j];
}
}
// Check for all possible square submatrix
for ( int size = Math.Min(m, n); size > 1; size--) {
for ( int i = 0; i <= m - size; i++) {
for ( int j = 0; j <= n - size; j++) {
if (is_valid(i, j, size, grid)) {
return size;
}
}
}
}
return 1;
}
// Driver Code
public static void Main() {
int [,] grid = { { 7, 1, 4, 5, 6 }, { 2, 5, 1, 6, 4 },
{ 1, 5, 4, 3, 2 }, { 1, 2, 7, 3, 4 } };
Console.WriteLine(largestSquareValidMatrix(grid));
}
} // This code is contributed by ukasp. |
<script> // JavaScript Program to implement
// the above approach
// Define the prefix sum arrays globally
let prefix_sum_row = new Array(50);
for (let i = 0; i < prefix_sum_row.length; i++) {
prefix_sum_row[i] = new Array(51).fill(0);
}
let prefix_sum_col = new Array(50);
for (let i = 0; i < prefix_sum_col.length; i++) {
prefix_sum_col[i] = new Array(51).fill(0);
}
function is_valid(r, c, size, grid) {
let r_end = r + size, c_end = c + size;
// Diagonal sum
let sum = 0;
for (let i = r, j = c; i < r_end; i++, j++) {
sum += grid[i][j];
}
// Check each row
for (let i = r; i < r_end; i++) {
if (prefix_sum_row[i][c_end]
- prefix_sum_row[i]
!= sum) {
return false ;
}
}
// Check each column
for (let i = c; i < c_end; i++) {
if (prefix_sum_col[r_end][i]
- prefix_sum_col[r][i]
!= sum) {
return false ;
}
}
// Check anti-diagonal
let ad_sum = 0;
for (let i = r, j = c_end - 1; i < r_end;
i++, j--) {
ad_sum += grid[i][j];
}
return ad_sum == sum;
}
function largestSquareValidMatrix(grid) {
// Store the size of the given grid
let m = grid.length, n = grid[0].length;
// Compute the prefix sum for the rows
for (let i = 0; i < m; i++) {
for (let j = 1; j <= n; j++) {
prefix_sum_row[i][j]
= prefix_sum_row[i][j - 1]
+ grid[i][j - 1];
}
}
// Compute the prefix sum for the columns
for (let i = 1; i <= m; i++) {
for (let j = 0; j < n; j++) {
prefix_sum_col[i][j]
= prefix_sum_col[i - 1][j]
+ grid[i - 1][j];
}
}
// Check for all possible square submatrix
for (let size = Math.min(m, n); size > 1; size--) {
for (let i = 0; i <= m - size; i++) {
for (let j = 0; j <= n - size; j++) {
if (is_valid(i, j, size, grid)) {
return size;
}
}
}
}
return 1;
}
// Driver Code
let grid = [[7, 1, 4, 5, 6],
[2, 5, 1, 6, 4],
[1, 5, 4, 3, 2],
[1, 2, 7, 3, 4]];
document.write(largestSquareValidMatrix(grid));
// This code is contributed by Potta Lokesh
</script>
|
3
Time Complexity: O(N*M*min(N, M)2)
Auxiliary Space: O(N*M)
Example in c:
Approach:
Initialize the maximum size of the sub-matrix to 1.
Initialize arrays to store the row, column, and diagonal sums for the first row and column.
Initialize the diagonal sums to include the element (0, 0) of the matrix.
Iterate over all sub-matrices in the matrix, excluding the first row and column.
For each sub-matrix, update the row, column, and diagonal sums based on the current element.
Check if the current sub-matrix has equal row, column, and diagonal sums.
If so, update the maximum size of the sub-matrix to be the size of the current sub-matrix.
Reset the row and diagonal sums for the next row.
Check if the sub-matrix formed by the last column and the current row has equal row, column, and diagonal sums.
If so, update the maximum size of the sub-matrix to be the size of this sub-matrix.
Print the maximum size of the sub-matrix.
#include <iostream> using namespace std;
// function to find the largest square sub-matrix with equal // row, column, and diagonal sum void findLargestSquare( int mat[][100], int n)
{ int i, j, k, size = 1, max_size = 1;
int row_sum[n], col_sum[n], diag_sum[2];
// initialize the sums for the first row and column
for (i = 0; i < n; i++) {
row_sum[i] = mat[0][i];
col_sum[i] = mat[i][0];
}
diag_sum[0] = mat[0][0];
diag_sum[1] = 0;
// iterate over all sub-matrices and check if they have
// equal row, column, and diagonal sums
for (i = 1; i < n; i++) {
for (j = 1; j < n; j++) {
// update the row, column, and diagonal sums
row_sum[j] += mat[i][j];
col_sum[i] += mat[i][j];
diag_sum[i % 2] += mat[i][j];
// check if the current sub-matrix has equal
// row, column, and diagonal sums
if (row_sum[j] == col_sum[i]
&& col_sum[i] == diag_sum[i % 2]
&& size < j - i + 1) {
// update the maximum size of the sub-matrix
size = j - i + 1;
}
}
// reset the row and diagonal sums for the next row
row_sum[0] = col_sum[i];
diag_sum[0] = diag_sum[1];
diag_sum[1] = 0;
// check if the current sub-matrix has equal row,
// column, and diagonal sums
if (row_sum[0] == col_sum[i]
&& col_sum[i] == diag_sum[0] && size < n - i) {
// update the maximum size of the sub-matrix
size = n - i;
}
}
// print the maximum size of the sub-matrix
cout << "The largest square sub-matrix with equal row, "
"column, and diagonal sum has size "
<< size << endl;
} int main()
{ int n = 5;
int mat[100][100] = { { 1, 2, 3, 4, 5 },
{ 2, 2, 3, 4, 5 },
{ 3, 3, 3, 4, 5 },
{ 4, 4, 4, 4, 5 },
{ 5, 5, 5, 5, 5 } };
findLargestSquare(mat, n);
return 0;
} |
#include <stdio.h> // function to find the largest square sub-matrix with equal row, column, and diagonal sum void findLargestSquare( int mat[][100], int n) {
int i, j, k, size = 1, max_size = 1;
int row_sum[n], col_sum[n], diag_sum[2];
// initialize the sums for the first row and column
for (i = 0; i < n; i++) {
row_sum[i] = mat[0][i];
col_sum[i] = mat[i][0];
}
diag_sum[0] = mat[0][0];
diag_sum[1] = 0;
// iterate over all sub-matrices and check if they have equal row, column, and diagonal sums
for (i = 1; i < n; i++) {
for (j = 1; j < n; j++) {
// update the row, column, and diagonal sums
row_sum[j] += mat[i][j];
col_sum[i] += mat[i][j];
diag_sum[i % 2] += mat[i][j];
// check if the current sub-matrix has equal row, column, and diagonal sums
if (row_sum[j] == col_sum[i] && col_sum[i] == diag_sum[i % 2] && size < j - i + 1) {
// update the maximum size of the sub-matrix
size = j - i + 1;
}
}
// reset the row and diagonal sums for the next row
row_sum[0] = col_sum[i];
diag_sum[0] = diag_sum[1];
diag_sum[1] = 0;
// check if the current sub-matrix has equal row, column, and diagonal sums
if (row_sum[0] == col_sum[i] && col_sum[i] == diag_sum[0] && size < n - i) {
// update the maximum size of the sub-matrix
size = n - i;
}
}
// print the maximum size of the sub-matrix
printf ( "The largest square sub-matrix with equal row, column, and diagonal sum has size %d\n" , size);
} int main() {
int n = 5;
int mat[100][100] = {{1, 2, 3, 4, 5},
{2, 2, 3, 4, 5},
{3, 3, 3, 4, 5},
{4, 4, 4, 4, 5},
{5, 5, 5, 5, 5}};
findLargestSquare(mat, n);
return 0;
} |
// Java program for the above approach public class Main {
// function to find the largest square sub-matrix with
// equal row, column, and diagonal sum
public static void findLargestSquare( int [][] mat, int n)
{
int i, j, k, size = 1 , max_size = 1 ;
int [] row_sum = new int [n];
int [] col_sum = new int [n];
int [] diag_sum = new int [ 2 ];
// initialize the sums for the first row and column
for (i = 0 ; i < n; i++) {
row_sum[i] = mat[ 0 ][i];
col_sum[i] = mat[i][ 0 ];
}
diag_sum[ 0 ] = mat[ 0 ][ 0 ];
diag_sum[ 1 ] = 0 ;
// iterate over all sub-matrices and check if they
// have equal row, column, and diagonal sums
for (i = 1 ; i < n; i++) {
for (j = 1 ; j < n; j++) {
// update the row, column, and diagonal sums
row_sum[j] += mat[i][j];
col_sum[i] += mat[i][j];
diag_sum[i % 2 ] += mat[i][j];
// check if the current sub-matrix has equal
// row, column, and diagonal sums
if (row_sum[j] == col_sum[i]
&& col_sum[i] == diag_sum[i % 2 ]
&& size < j - i + 1 ) {
// update the maximum size of the
// sub-matrix
size = j - i + 1 ;
}
}
// reset the row and diagonal sums for the next
// row
row_sum[ 0 ] = col_sum[i];
diag_sum[ 0 ] = diag_sum[ 1 ];
diag_sum[ 1 ] = 0 ;
// check if the current sub-matrix has equal
// row, column, and diagonal sums
if (row_sum[ 0 ] == col_sum[i]
&& col_sum[i] == diag_sum[ 0 ]
&& size < n - i) {
// update the maximum size of the sub-matrix
size = n - i;
}
}
// print the maximum size of the sub-matrix
System.out.printf(
"The largest square sub-matrix with equal row, column, and diagonal sum has size %d\n" ,
size);
}
public static void main(String[] args)
{
int n = 5 ;
int [][] mat = { { 1 , 2 , 3 , 4 , 5 },
{ 2 , 2 , 3 , 4 , 5 },
{ 3 , 3 , 3 , 4 , 5 },
{ 4 , 4 , 4 , 4 , 5 },
{ 5 , 5 , 5 , 5 , 5 } };
findLargestSquare(mat, n);
}
} // Contributed by adityasha4x71 |
def findLargestSquare(mat, n):
size = 1
max_size = 1
row_sum = [ 0 ] * n
col_sum = [ 0 ] * n
diag_sum = [ 0 ] * 2
# initialize the sums for the first row and column
for i in range (n):
row_sum[i] = mat[ 0 ][i]
col_sum[i] = mat[i][ 0 ]
diag_sum[ 0 ] = mat[ 0 ][ 0 ]
diag_sum[ 1 ] = 0
# iterate over all sub-matrices and check if they have equal row, column, and diagonal sums
for i in range ( 1 , n):
for j in range ( 1 , n):
# update the row, column, and diagonal sums
row_sum[j] + = mat[i][j]
col_sum[i] + = mat[i][j]
diag_sum[i % 2 ] + = mat[i][j]
# check if the current sub-matrix has equal row, column, and diagonal sums
if (row_sum[j] = = col_sum[i] and col_sum[i] = = diag_sum[i % 2 ] and size < j - i + 1 ):
# update the maximum size of the sub-matrix
size = j - i + 1
# reset the row and diagonal sums for the next row
row_sum[ 0 ] = col_sum[i]
diag_sum[ 0 ] = diag_sum[ 1 ]
diag_sum[ 1 ] = 0
# check if the current sub-matrix has equal row, column, and diagonal sums
if (row_sum[ 0 ] = = col_sum[i] and col_sum[i] = = diag_sum[ 0 ] and size < n - i):
# update the maximum size of the sub-matrix
size = n - i
# print the maximum size of the sub-matrix
print ( "The largest square sub-matrix with equal row, column, and diagonal sum has size" , size)
n = 5
mat = [[ 1 , 2 , 3 , 4 , 5 ],
[ 2 , 2 , 3 , 4 , 5 ],
[ 3 , 3 , 3 , 4 , 5 ],
[ 4 , 4 , 4 , 4 , 5 ],
[ 5 , 5 , 5 , 5 , 5 ]]
findLargestSquare(mat, n) |
using System;
public class Program
{ // function to find the largest square sub-matrix with
// equal row, column, and diagonal sum
public static void FindLargestSquare( int [,] mat, int n)
{
int i, j, k, size = 1, max_size = 1;
int [] row_sum = new int [n];
int [] col_sum = new int [n];
int [] diag_sum = new int [2];
// initialize the sums for the first row and column
for (i = 0; i < n; i++)
{
row_sum[i] = mat[0, i];
col_sum[i] = mat[i, 0];
}
diag_sum[0] = mat[0, 0];
diag_sum[1] = 0;
// iterate over all sub-matrices and check if they
// have equal row, column, and diagonal sums
for (i = 1; i < n; i++)
{
for (j = 1; j < n; j++)
{
// update the row, column, and diagonal sums
row_sum[j] += mat[i, j];
col_sum[i] += mat[i, j];
diag_sum[i % 2] += mat[i, j];
// check if the current sub-matrix has equal
// row, column, and diagonal sums
if (row_sum[j] == col_sum[i]
&& col_sum[i] == diag_sum[i % 2]
&& size < j - i + 1)
{
// update the maximum size of the
// sub-matrix
size = j - i + 1;
}
}
// reset the row and diagonal sums for the next
// row
row_sum[0] = col_sum[i];
diag_sum[0] = diag_sum[1];
diag_sum[1] = 0;
// check if the current sub-matrix has equal
// row, column, and diagonal sums
if (row_sum[0] == col_sum[i]
&& col_sum[i] == diag_sum[0]
&& size < n - i)
{
// update the maximum size of the sub-matrix
size = n - i;
}
}
// print the maximum size of the sub-matrix
Console.WriteLine( "The largest square sub-matrix with equal row, column, and diagonal sum has size {0}" , size);
}
public static void Main()
{
int n = 5;
int [,] mat = new int [,] {
{ 1, 2, 3, 4, 5 },
{ 2, 2, 3, 4, 5 },
{ 3, 3, 3, 4, 5 },
{ 4, 4, 4, 4, 5 },
{ 5, 5, 5, 5, 5 }
};
FindLargestSquare(mat, n);
}
} |
function findLargestSquare(mat, n) {
let size = 1; let max_size = 1; let row_sum = new Array(n).fill(0);
let col_sum = new Array(n).fill(0);
let diag_sum = new Array(2).fill(0);
// initialize the sums for the first row and column for (let i = 0; i < n; i++) {
row_sum[i] = mat[0][i]; col_sum[i] = mat[i][0]; } diag_sum[0] = mat[0][0]; diag_sum[1] = 0; // iterate over all sub-matrices and // check if they have equal row, column, and diagonal sums for (let i = 1; i < n; i++)
{ for (let j = 1; j < n; j++)
{ // update the row, column, and diagonal sums row_sum[j] += mat[i][j]; col_sum[i] += mat[i][j]; diag_sum[i % 2] += mat[i][j]; // check if the current sub-matrix has equal row, column, and diagonal sums if (row_sum[j] == col_sum[i] && col_sum[i] == diag_sum[i % 2] && size < j - i + 1)
{ // update the maximum size of the sub-matrix size = j - i + 1; } } // reset the row and diagonal sums for the next row row_sum[0] = col_sum[i]; diag_sum[0] = diag_sum[1]; diag_sum[1] = 0; // check if the current sub-matrix has equal row, column, and diagonal sums if (row_sum[0] == col_sum[i] && col_sum[i] == diag_sum[0] && size < n - i)
{ // update the maximum size of the sub-matrix size = n - i; } } // print the maximum size of the sub-matrix console.log( 'The largest square sub-matrix with equal row,
column, and diagonal sum has size :' +size);
} const n = 5; const mat = [[1, 2, 3, 4, 5], [2, 2, 3, 4, 5], [3, 3, 3, 4, 5], [4, 4, 4, 4, 5], [5, 5, 5, 5, 5]]; findLargestSquare(mat, n); |
The largest square sub-matrix with equal row, column, and diagonal sum has size 1
The time complexity of this approach is O(n^2), since it involves iterating over all sub-matrices in the matrix.
The auxiliary space is O(n), since it involves storing arrays to store the row, column, and diagonal sums.