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;
}