Sum of all sub matrix using prefix/cumulative sum

for( li=0; li <=max_row-1; li ++)

for(lj =0; lj <=max_col-1; lj ++)

 for(bi = li; bi <=max_row-1; bi++)

  for(bj=lj; bj<=max_col-1; bj++){
      
    sum+= arr[bi][bj] - arr[li-1][bj] - arr[bi][lj-1] + arr[li-1][lj-1];
      
  } 

Sir told us this code but when li or lj will be equal to 0 then li-1 and lj-1 will become -1 and then arr[ li-1] will become arr[-1] which will give garbage value.
So how to resolve this??

hello @abhayg1602
handle those cases separately using if statements.

use 0 for the array values if their indices are invalid.

I have tried it but I am unable to get the answer please share the 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.