Maximum Sum Rectangle Problem

Given an M x N matrix of integers ARR, your task is to identify the rectangle within the matrix that has the greatest sum of its elements.

Input:

The first line of input contains an integer 'T' representing the number of test cases. For each test case, the first line contains two space-separated integers, M and N, indicating the dimensions of the matrix ARR. The following M lines each contain N space-separated integers, representing the elements of the matrix ARR.

Output:

For each test case, return the maximum sum obtained from any rectangle within the matrix.

Example:

Input:
1
4 5
1 2 -1 -4 -20
-8 -3 4 2 1
3 8 10 1 3
-4 -1 1 7 -6
Output:
29
Explanation:

The submatrix from (1,1) to (3,3) has the maximum sum of 29, illustrated as:

 1   2 -1 -4  -20
-8 |-3 4 2 | 1
3 | 8 10 1 | 3
-4 |-1 1 7 | -6

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ M, N ≤ 100
  • -105 ≤ ARR[i] ≤ 105

Note: You do not need to print anything; implement the function to return the result only.

AnswerBot
1y

The task is to find the maximum sum rectangle in a given matrix of integers.

  • Iterate through all possible rectangles in the matrix

  • Calculate the sum of each rectangle

  • Keep track of the maximum sum rectan...read more

Help your peers!
Add answer anonymously...
Texas Instruments Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter