Dsa
Matrix
Easy
Matrix Block Sum
Given a 2D matrix of integers and a positive integer K, return a new matrix where each element at (i, j) is the sum of all elements of the original matrix within the K x K square centered at (i, j). If K is greater than the available rows/columns, consider the appropriate boundaries.
Input Format:
- The first line contains two integers, m and n, representing the dimensions of the matrix.
- The second line contains the integer K.
- The next m lines contain n integers each, representing the elements of the matrix.
Output Format:
- Print the resulting matrix, m lines of n integers each.
Example:
Input:
3 3
1
1 2 3
4 5 6
7 8 9
Output:
45 50 54
54 62 66
63 72 76
Key concepts
matrixprefix sum
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now