Clipped Square-Neighborhood Sums for Every Matrix Cell
Quick Overview
Clipped Square-Neighborhood Sums for Every Matrix Cell. For every cell(i,j), add all original mat[r][c] with row distance at most k and column distance at most k, keeping only valid matrix positions.
Clipped Square-Neighborhood Sums for Every Matrix Cell
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
For every cell(i,j), add all original mat[r][c] with row distance at most k and column distance at most k, keeping only valid matrix positions. Return these totals in a matrix with the same dimensions.
```cpp
class Solution {
public:
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k);
};
```
### Examples
**Example 1:**
```text
Input: mat = [[1,2],[3,4]], k = 1
Output: [[10,10],[10,10]]
Explanation: Every clipped neighborhood covers all four cells.
```
**Example 2:**
```text
Input: mat = [[2,4,6]], k = 1
Output: [[6,12,10]]
Explanation: The three neighborhoods contain the first two, all three,and last two values respectively.
```
### Constraints
- mat has m rows and n columns.
- Each of m,n,k is an integer in `[1,100]`.
- Every original matrix value is an integer between 1 and 100.
### Contract considerations
Neighborhood totals use the original matrix values, not already computed output totals.
```hint Read the boundary condition
Check cells near corners and edges, including neighborhoods large enough to cover the entire matrix. Totals use original values.
```
### Boundary reminders
Neighborhood totals use the original matrix values, not already computed output totals.
Overview: Clipped Square-Neighborhood Sums for Every Matrix Cell. For every cell(i,j), add all original mat[r][c] with row distance at most k and column distance at most k, keeping only valid matrix positions.
Clipped Square-Neighborhood Sums for Every Matrix Cell
Amazon
Aug 22, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
For every cell(i,j), add all original mat[r][c] with row distance at most k and column distance at most k, keeping only valid matrix positions. Return these totals in a matrix with the same dimensions.
class Solution {
public:
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k);
};
Examples
Example 1:
Input: mat = [[1,2],[3,4]], k = 1
Output: [[10,10],[10,10]]
Explanation: Every clipped neighborhood covers all four cells.
Example 2:
Input: mat = [[2,4,6]], k = 1
Output: [[6,12,10]]
Explanation: The three neighborhoods contain the first two, all three,and last two values respectively.
Constraints
mat has m rows and n columns.
Each of m,n,k is an integer in
[1,100]
.
Every original matrix value is an integer between 1 and 100.
Contract considerations
Neighborhood totals use the original matrix values, not already computed output totals.
Boundary reminders
Neighborhood totals use the original matrix values, not already computed output totals.