Can we solve this problem by the PnC method (approach 3) which we used to find the sum of all submatrix from a given matrix?
topleft=(i+1)(j+1);
bottomright=(m-i)(n-j);
Submatrix sum query
No…approach 3 should not be followed
Take a look at this implementation https://ide.codingblocks.com/s/184797
you will simply need to take input for how many queries there are and then simply run a loop for accepting that many queries and using your formula that many times to compute the answer.
aux matrix is the matrix in which we are precomputing the prefix sums of the matrix elements so that we can answer the queries in O(1) time.
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.