The Wayback Machine - https://web.archive.org/web/20210929170845/https://www.geeksforgeeks.org/largest-square-sub-matrix-with-equal-row-column-and-diagonal-sum/
Skip to content
Related Articles

Related Articles

Improve Article

Largest square sub-matrix with equal row, column, and diagonal sum

  • Last Updated : 29 Sep, 2021

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:
    1. 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.
    2. 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++




// 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;
}
Output:
3

Time Complexity: O(N*M*min(N, M)2)
Auxiliary Space: O(N*M)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :