Max sum submatrix | Practice | GeeksforGeeks

Given a matrix of size NxM and a list of queries containing (a,b) pairs. Find the maximum sum among all (a x b) sub-matrices of the matrix. 
Note: The rows and columns of the submatrix must be contiguous. For example:

Example 1:
Input:
N = 3, M = 4
mat[][] = {{1, 2, 3, 9}, 
           {4, 5, 6, 2}, 
           {8, 3, 2, 6}}
Q = 1
Queries[] = {(3,2)}

Output: 28

Explanation:
Here a = 3 and b = 2.

The first 3x2 submatrix is:
1 2
4 5
8 3
The sum of elements in this is 23.

The second 3x2 submatrix is:
2 3
5 6
3 2
The sum of elements in this is 21.

The third 3x2 submatrix is:
3 9
6 2
2 6
The sum of elements in this is 28.

The maximum among these is 28.

Example 2:
Input:
N = 3, M = 4
mat[][] = {{1, 2, 3, 9}, 
           {4, 5, 6, 2}, 
           {8, 3, 2, 6}}
Q = 3
Queries[] = {(1, 1), (2, 2), (3, 3)}

Output: 9 20 38

Example 3:
Input:
N = 2, M = 7
mat[][] = {{49, 44, 46, 22, 24, 11, 2}, 
           {5, 25, 24, 28, 20, 47, 47}}
Q = 1
Queries = {(2, 1)}

Output: 70

Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSubMatrixSumQueries() which takes the 2D array of integers mat, n, m, queries and q as input parameters and returns an array of integers denoting the maximum sum for each query.
 
Expected Time Complexity: O(Q*N*M)
Expected Auxiliary Space: O(N*M)
 
Constraints:
1 ≤ N ≤ 103
1 ≤ M ≤ 103
1 ≤ Q ≤ 100
1 ≤ a ≤ N
1 ≤ b ≤ M
1 ≤ mat[i][j] ≤ 103
0
mohittpattel1 week ago
C++ code with explanation.
class Solution{
public:
    vector<int> maxSubMatrixSumQueries(vector<int> mat[], int n, int m,
                                   vector<pair<int, int>> &queries, int q) {
        // code here
        vector<int> ans;
        int pre_sum[n + 1][m + 1];
        // make the left most row 0
        for (int i = 0; i <= n; i++)
            pre_sum[i][0] = 0;
        // make the right most row 0
        for (int i = 0; i <= m; i++)
            pre_sum[0][i] = 0;
        
        // calculate the presum of the matrix
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
                pre_sum[i][j] = mat[i - 1][j - 1] + pre_sum[i - 1][j] + pre_sum[i][j - 1] - pre_sum[i - 1][j - 1];
        }
        
        // Then calculate the maximum and put intothe array
        for (pair<int, int> pr : queries)
        {
            int max = 0, temp, k = pr.first, l = pr.second;
        
            for (int i = k; i <= n; i++)
            {
                for (int j = l; j <= m; j++)
                {
                    temp = pre_sum[i][j] - pre_sum[i][j - l] - pre_sum[i - k][j] + pre_sum[i - k][j - l];
                    if (temp > max)
                        max = temp;
                }
            }
            ans.push_back(max);
        }
        return ans;
    }
};
0
D Sathwik9 months ago
D Sathwik
https://uploads.disquscdn.c...
0
Yash Patil10 months ago
Yash Patil
int[] maxSubMatrixSumQueries(int[][] arr1, int n, int m, int[][] queries, int q) {        int[][]dp =new int[n+1][m+1];        int []res =new int[queries.length];        for(int i=1;i<dp.length;i++) {="" for(int="" j="1;j&lt;dp[0].length;j++)" {="" dp[i][j]="arr1[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];" }="" }="" int="" count="0;" for(int="" []x1:queries)="" {="" int="" row="x1[0];" int="" col="x1[1];" int="" val="maxSum(dp,row,col);" res[count++]="val;" }="" return="" res;="" }="" int="" maxsum(int="" [][]dp,int="" row,int="" col)="" {="" int="" max="Integer.MIN_VALUE;" for(int="" i="1;i&lt;dp.length;i++)" {="" for(int="" j="1;j&lt;dp[i].length;j++)" {="" int="" sum="Integer.MIN_VALUE;" if(i-row="">=0&&j-col>=0)                 {                     sum=dp[i][j]-dp[i-row][j]-dp[i][j-col]+dp[i-row][j-col];                     max=Math.max(sum,max);                 }             }         }        return max;    }
0
SANDEEP MANDAL
This comment was deleted.
0
Shishank Rawat11 months ago
Shishank Rawat
This is my C++ solution:Thank me by Upvoting.
 https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.