Intermediate Level
Implement the matrixBlockSumCenter method that returns the block sum around the center using distance k.
The input contains an integer matrix matrix, its rows and cols, and an integer k.
Your task is to find the sum of the block around the center cell of the matrix. The center cell is calculated using integer division: rows / 2 and cols / 2.
The block includes all cells within k rows and k columns from the center, while staying inside matrix boundaries. For example, in a 3 × 3 matrix with k = 1, the block covers the whole matrix.
First calculate the center row and center column.
Then find the valid block boundaries. The start row cannot go below 0, and the end row cannot go beyond rows - 1. Apply the same boundary check for columns.
Finally, loop through all cells inside that block and add their values to the sum.
Pseudocode:
function matrixBlockSumCenter(matrix, rows, cols, k):
centerRow = rows / 2
centerCol = cols / 2
startRow = max(0, centerRow - k)
endRow = min(rows - 1, centerRow + k)
startCol = max(0, centerCol - k)
endCol = min(cols - 1, centerCol + k)
sum = 0
for i from startRow to endRow:
for j from startCol to endCol:
sum = sum + matrix[i][j]
return sum