Related to accessing index i-1 and j-1 in sum of all sub matrices using pre sum matrix

Shouldn’t we first check whether i-1 & j-1 exist and then add and subtract their related elements(pre[i-1][j-1] pre[i-1][y] pre[x][j-1]) to the sum because they could be out of bound.

yes we need to check the cases (i) i and j both = 0 (ii) if i==0 only (iii) if j==0 only and accordingly write our code.

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.