Sum of all submatix from a given matrix(brute-force approach)

#include
using namespace std;
int main(){
int a[10][10];
int n;
cin>>n;
int sum=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
for(int li=0;li<n;li++){
for(int lj=0;lj<n;lj++){
for(int bi=li+1;bi<n;bi++){
for(int bj=lj+1;bj<n;bj++){

				for(int i=li;i<=bi;i++){
					for(int j=lj;j<=bj;j++){
						sum=sum+a[i][j];
					}
				}
			}
		}
	}
}

}
is this code correct?

Yes it is alright. According to the brute force solution as mentioned in the video, 1 x 1 matrix is not included.
To include that you just have to modify the loops for bottom right coordinates. Just begin these loops from top left coordinate values.

1 Like

i already started it from top left coordinates.
Ans should be 16,but it is showing 4.

for(int bi=li+1;bi<n;bi++){
for(int bj=lj+1;bj<n;bj++){

These must start from li and lj. Change this and check.

1 Like

okay…i got it…
Thanx

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.

1 Like