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.
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++
// C++ code to Compute before Matrix#include<bits/stdc++.h>usingnamespacestd;// Function to compute before matrixvector<vector<int>>computeBeforeMatrix(vector<vector<int>>&after){intn=after.size(),m=after[0].size();// Declaring a 2d array to store// the values of the before Matrixvector<vector<int>>before(n,vector<int>(m,0));for(inti=0;i<n;i++){for(intj=0;j<m;j++){if(i==0&&j==0)before[0][0]=after[0][0];elseif(i==0)before[i][j]=after[i][j]-after[i][j-1];elseif(j==0)before[i][j]=after[i][j]-after[i-1][j];elsebefore[i][j]=after[i][j]+after[i-1][j-1]-after[i-1][j]-after[i][j-1];}}// Return the before[][] matrixreturnbefore;}intmain(){vector<vector<int>>after{{1,3,6},{3,7,11}};vector<vector<int>>ans=computeBeforeMatrix(after);for(autou:ans){for(intx:u)cout<<x<<" ";cout<<endl;}return0;}
// Java code to Compute before MatrixclassGfG{// Function to compute before matrixstaticint[][]computeBeforeMatrix(int[][]after){intn=after.length,m=after[0].length;// Declaring a 2D array to store// the values of the before Matrixint[][]before=newint[n][m];for(inti=0;i<n;i++){for(intj=0;j<m;j++){if(i==0&&j==0)before[0][0]=after[0][0];elseif(i==0)before[i][j]=after[i][j]-after[i][j-1];elseif(j==0)before[i][j]=after[i][j]-after[i-1][j];elsebefore[i][j]=after[i][j]+after[i-1][j-1]-after[i-1][j]-after[i][j-1];}}// Return the before[][] matrixreturnbefore;}publicstaticvoidmain(String[]args){int[][]after={{1,3,6},{3,7,11}};int[][]ans=computeBeforeMatrix(after);for(int[]row:ans){for(intx:row)System.out.print(x+" ");System.out.println();}}}
Python
# Python code to Compute before Matrix# Function to compute before matrixdefcomputeBeforeMatrix(after):n,m=len(after),len(after[0])# Declaring a 2D list to store# the values of the before Matrixbefore=[[0]*mfor_inrange(n)]foriinrange(n):forjinrange(m):ifi==0andj==0:before[0][0]=after[0][0]elifi==0:before[i][j]=after[i][j]-after[i][j-1]elifj==0:before[i][j]=after[i][j]-after[i-1][j]else:before[i][j]=after[i][j]+after[i-1][j-1] \
-after[i-1][j]-after[i][j-1]# Return the before[][] matrixreturnbeforeif__name__=="__main__":after=[[1,3,6],[3,7,11]]ans=computeBeforeMatrix(after)forrowinans:print(" ".join(map(str,row)))
C#
// C# code to Compute before MatrixusingSystem;usingSystem.Collections.Generic;classGfG{// Function to compute before matrixstaticList<List<int>>computeBeforeMatrix(List<List<int>>after){intn=after.Count,m=after[0].Count;// Declaring a 2D list to store// the values of the before MatrixList<List<int>>before=newList<List<int>>();for(inti=0;i<n;i++)before.Add(newList<int>(newint[m]));for(inti=0;i<n;i++){for(intj=0;j<m;j++){if(i==0&&j==0)before[0][0]=after[0][0];elseif(i==0)before[i][j]=after[i][j]-after[i][j-1];elseif(j==0)before[i][j]=after[i][j]-after[i-1][j];elsebefore[i][j]=after[i][j]+after[i-1][j-1]-after[i-1][j]-after[i][j-1];}}// Return the before[][] matrixreturnbefore;}staticvoidMain(){List<List<int>>after=newList<List<int>>{newList<int>{1,3,6},newList<int>{3,7,11}};List<List<int>>ans=computeBeforeMatrix(after);foreach(List<int>rowinans){Console.WriteLine(string.Join(" ",row));}}}
JavaScript
// JavaScript code to Compute before Matrix// Function to compute before matrixfunctioncomputeBeforeMatrix(after){letn=after.length,m=after[0].length;// Declaring a 2D array to store// the values of the before Matrixletbefore=Array.from({length:n},()=>Array(m).fill(0));for(leti=0;i<n;i++){for(letj=0;j<m;j++){if(i===0&&j===0)before[0][0]=after[0][0];elseif(i===0)before[i][j]=after[i][j]-after[i][j-1];elseif(j===0)before[i][j]=after[i][j]-after[i-1][j];elsebefore[i][j]=after[i][j]+after[i-1][j-1]-after[i-1][j]-after[i][j-1];}}// Return the before[][] matrixreturnbefore;}letafter=[[1,3,6],[3,7,11]];letans=computeBeforeMatrix(after);ans.forEach(row=>console.log(row.join(" ")));
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)