Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

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:28Explanation: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 ≤ 10^{3}

1 ≤ M ≤ 10^{3}

1 ≤ Q ≤ 100

1 ≤ a ≤ N

1 ≤ b ≤ M

1 ≤ mat[i][j] ≤ 10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Max sum submatrix

...