If i start the linside loop of the column wise addition from j=m-1 then

if i start the inside loop of the column wise addition from j=m-1 then
a [ i ] [ j ] =a [ i ] [ j ] +a [ i ] [ j-1] is correct for not ?

as the loop start from end column and j decreases every time
we have to add last column in to second last column
but you are adding second last column to last column

if you want to use this expression then
use this code snippet

    for (int i = 0; i <n; i++) {
        for (int j = 1; j <m ; j++) {
            arr[i][j] += arr[i][j - 1];
        }
    }
    for (int i = 0; i <m; i++) {
        for (int j = 1; j <n; j++) {
            arr[j][i] += arr[j-1][i];
        }
    }
    int ans=INT_MIN;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            ans=max(ans,arr[i][j]);
        }
    }
    return ans;
```
1 Like