Problem in final sum equation

sum+=(a[bi][bj]-a[li-1][bj]-a[bi][lj-1]+a[li-1][lj-1]);
In this showing wrong ans.
when li=0, how it will be possible to subtract 1 from it same for lj=0
and if i try to start loop from to prevent this suituation then again i m getting wrong answer

code:-

using namespace std;
#include
int main()
{
int r,c,sum=0;
cin>>r>>c;
int a[r+1][c+1]={0};

for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
cin>>a[i][j];

//************************************************
for(int i=1;i<=r;i++)
for(int j=2;j<=c;j++) //along column
a[i][j]+=a[i][j-1];
//prefix sum array
for(int j=1;j<=c;j++)
for(int i=2;i<=r;i++) //along row
a[i][j]+=a[i-1][j];
//************************************************

for(int li=1;li<=r;li++)
for(int lj=1;lj<=c;lj++)
for(int bi=li;bi<=r;bi++)
for(int bj=lj;bj<=c;bj++)
{
	sum+=(a[bi][bj]-a[li-1][bj]-a[bi][lj-1]+a[li-1][lj-1]);
}

cout<<sum;

}

hi @n.nishchaya2000
here is d updated code.


try initialising array a[][] to 0 using memset before running any operation on a[][].
refer to the updated code for d same.
Hope dis helps.

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.