Data Structure Problems on “Maximum Sum Rectangle in a 2D Matrix”.
1. Given a 2D matrix, find a submatrix that has the maximum sum. Which of the following methods can be used to solve this problem?
a) Brute force
b) Recursion
c) Dynamic programming
d) Brute force, Recursion, Dynamic programming
View Answer
Answer: d
Clarification: Brute force, Recursion and Dynamic programming can be used to find the submatrix that has the maximum sum.
2. In which of the following cases, the maximum sum rectangle is the 2D matrix itself? Answer: a 3. Consider the following statements and select which of the following statement are true. Answer: d 4. Consider a matrix in which all the elements are non-zero(at least one positive and at least one negative element). In this case, the sum of the elements of the maximum sum rectangle cannot be zero. Answer: a 5. Consider the 2×3 matrix {{1,2,3},{1,2,3}}. What is the sum of elements of the maximum sum rectangle? 6. Consider the 2×2 matrix {{-1,-2},{-3,-4}}. What is the sum of elements of the maximum sum rectangle? Answer: b 7. Consider the 3×3 matrix {{2,1,-3},{6,3,4},{-2,3,0}}. What is the sum of the elements of the maximum sum rectangle? Answer: c 8. What is the time complexity of the brute force implementation of the maximum sum rectangle problem? Answer: d 9. The dynamic programming implementation of the maximum sum rectangle problem uses which of the following algorithm? Answer: c 10. Consider the following code snippet: Which of the following lines should be inserted to complete the above code? Answer: c 11. What is the time complexity of the following dynamic programming implementation of the maximum sum rectangle problem? a) O(row*col) Answer: d 12. What is the space complexity of the following dynamic programming implementation of the maximum sum rectangle problem? a) O(row*col) Answer: b 13. What is the output of the following code? a) 7 Answer: b 14. What is the output of the following code? a) 17 Answer: b
a) When all the elements are negative
b) When all the elements are positive
c) When some elements are positive and some negative
d) When diagonal elements are positive and rest are negative
Clarification: When all the elements of a matrix are positive, the maximum sum rectangle is the 2D matrix itself.
Statement 1: The maximum sum rectangle can be 1X1 matrix containing the largest element If the matrix size is 1X1
Statement 2: The maximum sum rectangle can be 1X1 matrix containing the largest element If all the elements are zero
Statement 3: The maximum sum rectangle can be 1X1 matrix containing the largest element If all the elements are negative
a) Only statement 1 is correct
b) Only statement 1 and Statement 2 are correct
c) Only statement 1 and Statement 3 are correct
d) Statement 1, Statement 2 and Statement 3 are correct
Clarification: If the matrix size is 1×1 then the element itself is the maximum sum of that 1×1 matrix. If all elements are zero, then the sum of any submatrix of the given matrix is zero. If all elements are negative, then the maximum element in that matrix is the highest sum in that matrix which is again 1X1 submatrix of the given matrix. Hence all three statements are correct.
a) True
b) False
Clarification: If a matrix contains all non-zero elements with at least one positive and at least on negative element, then the sum of elements of the maximum sum rectangle cannot be zero.
a) 3
b) 6
c) 12
d) 18
Answer: c
Clarification: Since all the elements of the 2×3 matrix are positive, the maximum sum rectangle is the matrix itself and the sum of elements is 12.
a) 0
b) -1
c) -7
d) -12
View Answer
Clarification: Since all the elements of the 2×2 matrix are negative, the maximum sum rectangle is {-1}, a 1×1 matrix containing the largest element. The sum of elements of the maximum sum rectangle is -1.
a) 13
b) 16
c) 14
d) 19
Clarification: The complete matrix represents the maximum sum rectangle and it’s sum is 14.
a) O(n)
b) O(n2)
c) O(n3)
d) O(n4)
View Answer
Clarification: The time complexity of the brute force implementation of the maximum sum rectangle problem is O(n4).
a) Hirschberg’s algorithm
b) Needleman-Wunsch algorithm
c) Kadane’s algorithm
d) Wagner Fischer algorithm
Clarification: The dynamic programming implementation of the maximum sum rectangle problem uses Kadane’s algorithm.int max_sum_rectangle(int arr[][3],int row,int col)
{
int left, right, tmp[row], mx_sm = INT_MIN, idx, val;
for(left = 0; left < col; left++)
{
for(right = left; right < col; right++)
{
if(right == left)
{
for(idx = 0; idx < row; idx++)
tmp[idx] = arr[idx][right];
}
else
{
for(idx = 0; idx < row; idx++)
tmp[idx] += arr[idx][right];
}
val = kadane_algo(tmp,row);
if(val > mx_sm)
______;
}
}
return mx_sm;
}
a) val = mx_sm
b) return val
c) mx_sm = val
d) return mx_sm
Clarification: The line “mx_sm = val” should be inserted to complete the above code.int max_sum_rectangle(int arr[][3],int row,int col)
{
int left, right, tmp[row], mx_sm = INT_MIN, idx, val;
for(left = 0; left < col; left++)
{
for(right = left; right < col; right++)
{
if(right == left)
{
for(idx = 0; idx < row; idx++)
tmp[idx] = arr[idx][right];
}
else
{
for(idx = 0; idx < row; idx++)
tmp[idx] += arr[idx][right];
}
val = kadane_algo(tmp,row);
if(val > mx_sm)
mx_sm = val;
}
}
return mx_sm;
}
b) O(row)
c) O(col)
d) O(row*col*col)
Clarification: The time complexity of the above dynamic programming implementation of the maximum sum rectangle is O(row*col*col).int max_sum_rectangle(int arr[][3],int row,int col)
{
int left, right, tmp[row], mx_sm = INT_MIN, idx, val;
for(left = 0; left < col; left++)
{
for(right = left; right < col; right++)
{
if(right == left)
{
for(idx = 0; idx < row; idx++)
tmp[idx] = arr[idx][right];
}
else
{
for(idx = 0; idx < row; idx++)
tmp[idx] += arr[idx][right];
}
val = kadane_algo(tmp,row);
if(val > mx_sm)
mx_sm = val;
}
}
return mx_sm;
}
b) O(row)
c) O(col)
d) O(row*col*col)
Clarification: The space complexity of the above dynamic programming implementation of the maximum sum rectangle problem is O(row).#include
b) 8
c) 9
d) 10
Clarification: The program prints the sum of elements of the maximum sum rectangle, which is 8.#include
b) 18
c) 19
d) 20
Clarification: The program prints the sum of elements of the maximum sum rectangle, which is 18.