# Summed Area Table – Submatrix Summation

Given a matrix of size M x N, there are large number of queries to find submatrix sums. Inputs to queries are left top and right bottom indexes of submatrix whose sum is to find out.

How to preprocess the matrix so that submatrix sum queries can be performed in O(1) time.

Example:

tli :Row number of top left of query submatrixtlj :Column number of top left of query submatrixrbi :Row number of bottom right of query submatrixrbj :Column number of bottom right of query submatrix Input: mat[M][N] = {{1, 2, 3, 4, 6}, {5, 3, 8, 1, 2}, {4, 6, 7, 5, 5}, {2, 4, 8, 9, 4} }; Query1: tli = 0, tlj = 0, rbi = 1, rbj = 1 Query2: tli = 2, tlj = 2, rbi = 3, rbj = 4 Query3: tli = 1, tlj = 2, rbi = 3, rbj = 3; Output: Query1: 11 // Sum between (0, 0) and (1, 1) Query2: 38 // Sum between (2, 2) and (3, 4) Query3: 38 // Sum between (1, 2) and (3, 3)

**Naive Algorithm:**

We can loop all the queries and calculate each query in O (q*(N*M)) worst case which is too large for a large range of numbers.

// Pseudo code of Naive algorithm. Arr[][] = input_matrix For each query: Input tli, tlj, rbi, rbj sum = 0 for i from tli to tbi (inclusive): for j from tlj to rbj(inclusive): sum += Arr[i][j] print(sum)

**Optimized Solution : **

Summed Area Table can reduce this type of query into preprocessing time of O(M*N) and each query will execute in O(1).

Summed Area Table is a data structure and algorithm for quickly and efficiently generating the sum of values in a rectangular subset of a grid.

The value at any point (x, y) in the summed area table is just the sum of all the values above and to the left of (x, y), inclusive :

The optimized solution is implemented in below post.

**Implementation of optimized approach**

This article is contributed by **Shubham Saxena**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- XOR of a submatrix queries
- Submatrix Sum Queries
- Minimum sum submatrix in a given 2D array
- Count of submatrix with sum X in a given Matrix
- Submatrix of given size with maximum 1's
- Largest possible square submatrix with maximum AND value
- Maximum value in a matrix which contain intersecting concentric submatrix
- Maximize the binary matrix by filpping submatrix once
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Check if a matrix contains a square submatrix with 0 as boundary element
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Sparse Table
- C++ Program to implement Symbol Table
- Range sum query using Sparse Table
- Find the number of cells in the table contains X
- Range maximum query using Sparse Table
- Range Minimum Query (Square Root Decomposition and Sparse Table)
- Find the Surface area of a 3D figure
- Sum of Area of all possible square inside a rectangle