**Matrix Rectangular Sums - Facebook Top Interview Questions**

### Problem Statement :

Given a two-dimensional list of integers matrix and an integer k, return a new matrix M of the same dimensions where M[i][j] is the sum of all numbers sum(matrix[r][c]) for all (i - k ≤ r ≤ i + k, j - k ≤ c ≤ j + k) Constraints n, m ≤ 250 where n and m is the number of rows and columns in matrix Example 1 Input matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] k = 1 Output [ [12, 21, 16], [27, 45, 33], [24, 39, 28] ]

### Solution :

` ````
Solution in C++ :
int rectangle(vector<vector<int>>& dp, int r1, int c1, int r2, int c2) {
int res = dp[r2 + 1][c2 + 1];
res -= dp[r2 + 1][c1];
res -= dp[r1][c2 + 1];
res += dp[r1][c1];
return res;
}
vector<vector<int>> solve(vector<vector<int>>& matrix, int k) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> res(m, vector<int>(n));
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
dp[i + 1][j + 1] = matrix[i][j] + dp[i][j + 1] + dp[i + 1][j] - dp[i][j];
}
}
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
int r1 = max(0, r - k);
int c1 = max(0, c - k);
int r2 = min(m - 1, r + k);
int c2 = min(n - 1, c + k);
res[r][c] = rectangle(dp, r1, c1, r2, c2);
}
}
return res;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int[][] solve(int[][] matrix, int k) {
RangeSumMatrix rsm = new RangeSumMatrix(matrix);
int[][] ret = new int[matrix.length][matrix[0].length];
for (int i = 0; i < ret.length; i++) {
for (int j = 0; j < ret[0].length; j++) {
ret[i][j] = rsm.total(i - k, j - k, i + k, j + k);
}
}
return ret;
}
}
class RangeSumMatrix {
int[][] matrix;
public RangeSumMatrix(int[][] matrix) {
this.matrix = matrix;
for (int i = 0; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
matrix[i][j] += matrix[i][j - 1];
}
}
}
public int total(int row0, int col0, int row1, int col1) {
row0 = Math.max(0, row0);
row1 = Math.min(matrix.length - 1, row1);
col0 = Math.max(0, col0);
col1 = Math.min(matrix[0].length - 1, col1);
int ret = 0;
for (int i = row0; i <= row1; i++) {
ret += matrix[i][col1];
if (col0 != 0) {
ret -= matrix[i][col0 - 1];
}
}
return ret;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, A, k):
R, C = len(A), len(A[0])
for r in range(R):
for c in range(C):
if r:
A[r][c] += A[r - 1][c]
if c:
A[r][c] += A[r][c - 1]
if r and c:
A[r][c] -= A[r - 1][c - 1]
def get_sum(top, left, bottom, right):
top = max(top, 0)
left = max(left, 0)
bottom = min(bottom, R - 1)
right = min(right, C - 1)
return (
A[bottom][right]
- (A[top - 1][right] if top else 0)
- (A[bottom][left - 1] if left else 0)
+ (A[top - 1][left - 1] if top and left else 0)
)
return [[get_sum(r - k, c - k, r + k, c + k) for c in range(C)] for r in range(R)]
```

## View More Similar Problems

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →