Please explain how to generate prefix sum submatrix ?
Sum of all submatrices approach 2
for generating prefix sum submatrix
first you have to sum column wise and then row wise
use this
for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
arr[i][j]+=arr[i][j-1];
}
}
for(int j=0;j<n;j++){
for(int i=1;i<n;i++){
arr[i][j]+=arr[i-1][j];
}
}
now in the array arr[i][j] represent the sum of submatrix starting form (0,0) to (i,j)
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved 
if you have more doubts regarding this feel free to ask
Can we apply the following approach to solve this problem-
- Calculate prefix sum from [0, 0] to [x, y] = sum1
- Calculate prefix sum from [0, 0] to [i, j] = sum2
- Then sum required from [i, j] to [x, y] = sum2 - sum1 + matrix[i][j].
Here I am taking x, y, i, j in reference to matrix that is explained in video.
no it will not work
make a matrix and then try to visiualize this
correct way
bi => bottom most i
bj => bottom most j
ti ,tj => topmost i and j
int temp=arr[bi][bj];
if(tj>0)temp-=arr[bi][tj-1];
if(ti>0)temp-=arr[ti-1][bj];
if(ti>0&&tj>0)temp+=arr[ti-1][tj-1];
// cout<<temp<<"\n";
sum+=temp;
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.