VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-maximum-sum-square-sub-matrix-of-given-size/

⇱ Maximum sum square sub-matrix of given size - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum sum square sub-matrix of given size

Last Updated : 23 Jul, 2025

Given a 2d array mat[][] of order n * n, and an integer k. Your task is to find a submatrix of order k * k, such that sum of all the elements in the submatrix is maximum possible.

Note: Matrix mat[][] contains zero, positive and negative integers.

Examples:

👁 rectangle

Input: k = 3
mat[][] = [ [ 1, 2, -1, 4 ]
[ -8, -3, 4, 2 ]
[ 3, 8, 10, -8 ]
[ -4, -1, 1, 7 ] ]
Output: 20
Explanation: The submatrix enclosed in blue boundary is the maximum sum submatrix of order 3 * 3.

Input: k = 3
mat[][] = [ [ 1, 1, 1, 1, 1 ]
[ 2, 2, 2, 2, 2 ]
[ 3, 8, 6, 7, 3 ] 
[ 4, 4, 4, 4, 4 ] 
[ 5, 5, 5, 5, 5 ] ]
Output: 48
Explanation: The following submatrix of order k * k has sum 48, which is maximum possible:
[ [ 8, 6, 7 ]
[ 4, 4, 4 ]
[ 5, 5, 5 ] ]

Input: k = 1
mat[][] = [ [ 4 ] ]
Output: 4
Explanation: The given matrix contains only a single element, thus the max possible sum is 4.

This problem is mainly an extension of Maximum Sum of a Subarray of Size k

[Naive Approach] - Generating All Submatrices - O(n ^ 2 * k ^ 2) Time and O(1) Space

The idea is to generate all possible submatrices of order k * k, and find sum of its elements. The maximum obtained sum is the answer.

We run two nested loops, where the outer loop runs from 0 to n - k, and marks the row, while the inner row runs from 0 to n - k and marks the current column. For each iteration of the inner loop, again run two nested loops, where the outer loop runs from i to i + k - 1, and inner loop runs from j to j + k - 1. For each iteration of innermost loop add the cell value to the sum. At last store the max of obtained sum and current answer.


Output
20

[Expected Approach] - By Calculating Strips Sum - O(n ^ 2) Time and O(n ^ 2) Space

The idea is to preprocess the given square matrix. In the pre-processing step, calculate sum of all vertical strips of size k x 1 in a temporary square matrix stripSum[][]. Once we have sum of all vertical strips, we can calculate sum of first sub-square in a row as sum of first k strips in that row, and for remaining sub-squares, we can calculate sum in constant time by removing the leftmost strip of previous sub-square and adding the rightmost strip of new square. 


Output
20

[Alternate Approach] - Using Dynamic Programming - O(n ^ 2) Time and O(n ^ 2) Space

The idea is to use Dynamic Programming to precompute the sum of all the sub-matrices having their top-left cell [0, 0] (Like prefix sum in 1 D array), and then use it to find the sums of submatrices of order k * k and pick the maximum possible. To do so, create a 2d array dp[][] of order n * n, where dp[i][j] stores the sum of all the elements of sub-matrix mat[0...i][0...j].

Follow the below given steps:

  • Create a 2d array dp[][] of order n * n to store the sum of elements of all the submatrices
  • To calculate the sum of elements of submatrix mat[0...i][0...j], i.e., dp[i][j], add dp[i-1][j], dp[i][j-1], and mat[i][j], and subtract dp[i-1][j-1], as this submatrix will get added twice while adding two other sub-matrices.
  • Now, to calculate the sum of elements of sub-matrix having its right-bottom cell [i, j], subtract dp[i-k][j] and dp[i][j - k] from dp[i][j], and add dp[i - k][j - k], as this overlapping sub-matrix will get deleted twice.
  • Do the same for all the cells of the matrix, and store the maximum value as the answer.

Output
20

Related Articles:
Given an n x n square matrix, find sum of all sub-squares of size k x k
Maximum sum rectangle in a 2D matrix

Comment