# Final Matrix after incrementing submatrices by K in range given by Q queries A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Given a 2D matrix mat[][] of size N*M and Q queries of the form {x1, y1, x2, y2, K}. For each query, the task is to add the value K to submatrix from cell (x1, y1) to (x2, y2). Print the matrix after all the queries performed.

Examples:

Input:_ N = 3, M = 4, mat[][] = {{1, 0, 1, 2}, {0, 2, 4, 1}, {1, 2, 1, 0}}, Q = 1, Queries[][] = {{0, 0, 1, 1, 2}}_

Output:

3 2 1 2

2 4 4 1

1 2 1 0

Explanation:

There is only one query i.e., updating the submatrix from cell mat to mat by increment of 2, the matrix becomes:

3 2 1 2

2 4 4 1

1 2 1 0

Input:_ N = 2, M = 3, mat[][] = {{3, 2, 1}, {2, 4, 4}}, Q = 1, Queries[][] = { {0, 1, 1, 2, -1}, {0, 0, 1, 1, 5}}_

Output:

8 6 0

7 8 3

Explanation:

For query 1, i.e., updating the submatrix from cell mat to mat by increment of (-1), the matrix becomes:

3 1 0

2 3 3

For query 2, i.e., updating the submatrix from cell mat to mat by increment of 5, the matrix becomes:

8 6 0

7 8 3

Naive Approach: The simplest approach is to iterate over the submatrix and add K to all elements from mat[x1][y1] to mat[x2][y2] for each query. Print the matrix after the above operations.

Time Complexity:_ O(NMQ)_

Auxiliary Space:_ O(1)_

Efficient Approach: The idea is to use an auxiliary matrix to perform the update operations on the corners of the submatrix cells and then find the prefix sum of the matrix to get the resultant matrix. Below are the steps:

1. Initialize all elements of the auxiliary matrix say aux[][] to 0.
2. For each query {x1, y1, x2, y2, K} update the auxiliary matrix as:
• aux[x1][y1] += K
• if(x2 + 1 < N) then aux[x2 + 1][y1] -= K
• if(x2 + 1 < N && y2 + 1 < N) then aux[x2 + 1][y2 + 1] += K
• if(y2 + 1 < N) then aux[x1][y2 + 1] -= K
1. Find the prefix sum of each row of the auxiliary matrix.
2. Find the prefix sum of each column of the auxiliary matrix.
3. Now, update the auxiliary matrix as sum of elements at each respective cell of the auxiliary and the given matrix.
4. Print the auxiliary matrix after all the above operations.

Below is the _**_illustration_ for how auxiliary matrix is created and updated for _query[][] = {{0, 0, 1, 1, 2}, {0, 1, 2, 3, -1}}**: Below is the implementation of the above approach:

### C++

`// C++ program for the above approach`

`#include <bits/stdc++.h>`

`**using**` `**namespace**` `std;`

`#define N 3`

`#define M 4`

`// Query data type`

`**struct**` `query {`

`**int**` `x1, x2, y1, y2, K;`

`};`

`// Function to update the given query`

`**void**` `updateQuery(``**int**` `from_x,` `**int**` `from_y,`

`**int**` `to_x,` `**int**` `to_y,`

`**int**` `k,` `**int**` `aux[][M])`

`{`

`// Update top cell`

`aux[from_x][from_y] += k;`

`// Update bottom left cell`

`**if**` `(to_x + 1 < N)`

`aux[to_x + 1][from_y] -= k;`

`// Update bottom right cell`

`**if**` `(to_x + 1 < N && to_y + 1 < M)`

`aux[to_x + 1][to_y + 1] += k;`

`// Update top right cell`

`**if**` `(to_y + 1 < M)`

`aux[from_x][to_y + 1] -= k;`

`}`

`// Function that updates the matrix`

`// mat[][] by adding elements of aux[][]`

`**void**` `updateMatrix(``**int**` `mat[][M],` `**int**` `aux[][M])`

`{`

`// Compute the prefix sum of all columns`

`**for**` `(``**int**` `i = 0; i < N; i++) {`

`**for**` `(``**int**` `j = 1; j < M; j++) {`

`aux[i][j] += aux[i][j - 1];`

`}`

`}`

`// Compute the prefix sum of all rows`

`**for**` `(``**int**` `i = 0; i < M; i++) {`

`**for**` `(``**int**` `j = 1; j < N; j++) {`

`aux[j][i] += aux[j - 1][i];`

`}`

`}`

`// Get the final matrix by adding`

`// mat and aux matrix at each cell`

`**for**` `(``**int**` `i = 0; i < N; i++) {`

`**for**` `(``**int**` `j = 0; j < M; j++) {`

`mat[i][j] += aux[i][j];`

`}`

`}`

`}`

`// Function that prints matrix mat[]`

`**void**` `printMatrix(``**int**` `mat[][M])`

`{`

`// Traverse each row`

`**for**` `(``**int**` `i = 0; i < N; i++) {`

`// Traverse each columns`

`**for**` `(``**int**` `j = 0; j < M; j++) {`

`cout << mat[i][j] <<` `" "``;`

`}`

`cout <<` `"\n"``;`

`}`

`}`

`// Function that performs each query in`

`// the given matrix and print the updated`

`// matrix after each operation performed`

`**void**` `matrixQuery(``**int**` `mat[][M],` `**int**` `Q,`

`query q[])`

`{`

`// Initialize all elements to 0`

`**int**` `aux[N][M] = {};`

`// Update auxiliary matrix`

`// by traversing each query`

`**for**` `(``**int**` `i = 0; i < Q; i++) {`

`// Update Query`

`updateQuery(q[i].x1, q[i].x2,`

`q[i].y1, q[i].y2,`

`q[i].K, aux);`

`}`

`// Compute the final answer`

`updateMatrix(mat, aux);`

`// Print the updated matrix`

`printMatrix(mat);`

`}`

`// Driver Code`

`**int**` `main()`

`{`

`// Given Matrix`

`**int**` `mat[N][M] = { { 1, 0, 1, 2 },`

`{ 0, 2, 4, 1 },`

`{ 1, 2, 1, 0 } };`

`**int**` `Q = 1;`

`// Given Queries`

`query q[] = { { 0, 0, 1, 1, 2 } };`

`// Function Call`

`matrixQuery(mat, Q, q);`

`**return**` `0;`

`}`

Output:

``````3 2 1 2
2 4 4 1
1 2 1 0``````

Time Complexity:_ O(Q + N*M)_

Auxiliary Space:_ O(N*M)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

## Binary Matrix after flipping submatrices in given range for Q queries

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Queries to find the Lower Bound of K from Prefix Sum Array

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Sum of prime numbers in range [L, R] from given Array for Q queries

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Count of permutations such that sum of K numbers from given range is even

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Count of subarrays having sum equal to its length

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.