Open In App

Compute Matrix before Given Operations

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
8 Likes
Like
Report

For a given matrix before[][], the corresponding cell (x, y) of the after[][] matrix is calculated as follows: 

res = 0;
for(i = 0; i <= x; i++){
  for( j = 0; j <= y; j++){              
    res += before(i, j);
  }
}
after(x, y) = res;

Given an n*m matrix after[][], your task is to find the corresponding before[][] matrix for the given matrix.

Examples:

Input:  N = 2, M = 3, after[][] = [ [1, 3, 6], [3, 7, 11] ]
Output:
1 2 3
2 2 1
Explanation: The before matrix for the given after matrix is [ [1, 2, 3], [2, 2, 1] ].
Because according to the code given in problem, 
after(0, 0) = before(0, 0) = 1
after(0, 1) = before(0, 0) + before(0, 1) = 1 + 2 = 3.
after(0, 2) = before(0, 0) + before(0, 1) + before(0, 2) = 1 + 2 + 3 = 6.
after(1, 0) = before(0, 0) + before(1, 0) = 1 + 2 = 3;, 
after(1, 1) = before(0, 0) + before(0, 1) + before(1, 0) + before(1, 1) = 1 + 2 + 2 + 2 = 7.
after(1, 2) = before(0, 0) + before(0, 1) + before(0, 2) + before(1, 0) + before(1, 1) + before(1, 2) = 1 + 2 + 3 + 2 + 2 + 1 = 11

Input: N = 1, M = 3, after[][] = [[1, 3, 5]]
Output:
1 2 2

Approach: The problem can be solved based on the following observation:

Consider the opposite task, i.e. to convert before[][] matrix to after[][]. As seen from the problem statement, after[i][j] is the summation of all the cells to the left of jth column and all the rows above the ith row. That is basically the prefix sum of a matrix.  So this problem is mainly an extension of Original Array from given Prefix Sums

Based on the above observation the problem can be solved with the help of the example as shown below:

Consider before[][] = { {1, 2, 3}, {2, 2, 1} }

before[][] matrix

See how this matrix is converted into after[][] matrix.

For (0, 0): after[0][0] = before[0][0] = 1
For (0, 1): after[0][1] = before[0][0] + before[0][1] = after[0][0] + before[0][1] = 1 + 2 = 3
For (0, 2): after[0][2] = before[0][0] + before[0][1] + before[0][2] = after[0][1] + before[0][2] = 3 + 3 = 6
For (1, 0): after[1][0] = before[0][0] + before[1][0] = after[0][0] + before[1][0] = 1 + 2 = 3
For (1, 1): after[1][1] = before[0][0] + before[0][1] + before[1][0] + before[1][1]
                = before[0][0] + before[0][1] + before[0][0] + before[1][0] - before[0][0] + before[1][1]
                = after[0][1] + after[1][0] - after[0][0] + before[1][1] = 3 + 3 - 1 + 2 = 7
For (1, 2): Similarly like the previous one 
after[1][2] = after[0][2] + after[1][1] - after[0][1] + before[1][2] = 6 + 7 - 3 + 1 = 11

Note: In the procedure, if the value of i and j is non-zero then in that case while taking the values from up and left sides of i and j respectively, we have counted twice the value of the after[i-1][j-1].

Now from the above procedure only we are going to determine how to get the before[][] matrix when we have been given after[][] matrix 

For the first cell (i=0, j=0): before[0][0] = after[0][0] 

For the first row:
after[i][j] = after[i][j-1] + before[i][j] can be converted to before[i][j] = after[i][j] - after[i][j-1]

For the first column:  
after[i][j] = after[i-1][j] + before[i][j] can be converted to before[i][j] = after[i][j] - after[i-1][j]

For the rest of the cell:
after[i][j] = after[i-1][j] + after[i][j-1] - after[i-1][j-1] + before[i][j] can be converted to  before[i][j] = after[i][j] + after[i-1][j-1] - after[i-1][j] - after[i][j-1]

Follow the steps mentioned below to implement the approach:

  • Iterate through all the cells (i, j) of the matrix:
    • Check the values of (i, j) to determine the location of the cell.
    • Based on the location, use one of the formulae shown above and determine the value of before[i][j].
  • Return the before[][] matrix after the iteration is over.

Below is the implementation of the above approach.

C++
Java Python C# JavaScript

Output
1 2 3 
2 2 1 

Time Complexity: O(n * m) where n is the number of rows and m is the number of columns
Auxiliary Space: O(n * m)


Explore