Matrix Block Sum - Problem

You are given a m x n matrix mat and an integer k. Your task is to create a new matrix answer where each cell answer[i][j] contains the sum of all elements within a square block centered at position (i, j).

Specifically, for each position (i, j), you need to sum all elements mat[r][c] where:

  • i - k ≤ r ≤ i + k
  • j - k ≤ c ≤ j + k
  • (r, c) is a valid position within the matrix boundaries

Think of it as placing a (2k+1) x (2k+1) square window around each cell and summing all values within that window. If the window extends beyond the matrix boundaries, only consider the valid cells within the matrix.

Example: If k = 1, each cell sums a 3x3 block centered on itself.

Input & Output

example_1.py — Basic 3x3 matrix
$ Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[12,21,16],[27,45,33],[24,39,28]]
💡 Note: For each cell, we sum a 3x3 block centered on it. For example, answer[1][1] = sum of entire matrix = 45, while answer[0][0] = 1+2+4+5 = 12 (only 2x2 block fits within bounds).
example_2.py — Larger radius
$ Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
Output: [[45,45,45],[45,45,45],[45,45,45]]
💡 Note: Since k=2 creates a 5x5 block, every cell's block covers the entire 3x3 matrix, so all answers are the total sum = 45.
example_3.py — Single cell
$ Input: mat = [[5]], k = 1
Output: [[5]]
💡 Note: With only one cell, regardless of k value, the block sum is just that single cell's value.

Visualization

Tap to expand
Matrix Block Sum Concept123456789Target cell (1,1) with k=1Block covers 3×3 area around itSum = 1+2+3+4+5+6+7+8+9 = 45Prefix Sum OptimizationInstead of recalculating each block:1. Build prefix sum matrix once2. Calculate any rectangle sum in O(1)3. Use formula: A - B - C + DTime: O(mn×k²) → O(mn)Prefix Sum Formula VisualizationDCBARectangle Sum =A - B - C + DA = prefix[r2+1][c2+1]B = prefix[r2+1][c1], C = prefix[r1][c2+1]D = prefix[r1][c1]
Understanding the Visualization
1
Understand the Problem
Each cell needs to know the sum of all cells within distance k from it, forming a square block
2
Brute Force Approach
For each cell, manually sum all cells in its block - simple but slow O(mn×k²)
3
Prefix Sum Optimization
Pre-compute cumulative sums to enable O(1) rectangular queries
4
Apply Formula
Use inclusion-exclusion: sum = A - B - C + D where A,B,C,D are prefix sum lookups
Key Takeaway
🎯 Key Insight: Pre-computing prefix sums transforms an O(k²) operation per cell into an O(1) lookup, dramatically improving performance for large matrices or large k values.

Time & Space Complexity

Time Complexity
⏱️
O(m × n)

O(mn) to build prefix matrix + O(mn) to calculate all results = O(mn) total

n
2n
Linear Growth
Space Complexity
O(m × n)

Additional space needed for the prefix sum matrix

n
2n
Linearithmic Space

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 ≤ m, n ≤ 300
  • 1 ≤ mat[i][j] ≤ 104
  • 0 ≤ k ≤ 100
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
42.8K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen