Can you please tell whats wrong with my code? i used this 1st approach of 6 nested loops

#include
using namespace std;

int main(){
int arr[100][100];
int m,n;
cin>>m;
cin>>n;

for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
}
}

int sum=0;
int li,lj,bi,bj;

for(li=0;li<m-1;li++){
for(lj=0;lj<n-1;lj++){
for(bi=li;bi<m-1;bi++){
for(bj=lj;bj<n-1;bj++){
for(int k=li;k<=bi;k++){
for(int l=lj;l<=bj;l++){
sum+=arr[k][l];
}
}
}
}
}
}
cout<<sum;
}

Hi @palash,
I think you missed the last element of each row and column. Your for loop should be for(li = 0; li < m; li++) or for(li = 0; li <= m - 1; li++) and it should be the same for other for loops (you should check it on your own) because we’re considering the case that one element could also be a submatrix, so go until the end.

hello @palash
whatever @halihoof has said is correct, make those changes in ur code and it will work fine

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.