Given a 2D array, find the maximum sum subarray in it. For example, in the following 2D array, the maximum sum subarray is highlighted with blue rectangle and sum of this subarray is 29.
This problem is mainly an extension of Largest Sum Contiguous Subarray for 1D array.
The Naive Solution for this problem is to check every possible rectangle in given 2D array. This solution requires 6 nested loops –
- 4 for start and end coordinate of the 2 axis O(n4)
- and 2 for the summation of the sub-matrix O(n2).
Overall time complexity of this solution would be O(n6).
Efficient Approach –
Kadane’s algorithm for 1D array can be used to reduce the time complexity to O(n^3). The idea is to fix the left and right columns one by one and find the maximum sum contiguous rows for every left and right column pair. We basically find top and bottom row numbers (which have maximum sum) for every fixed left and right column pair. To find the top and bottom row numbers, calculate sun of elements in every row from left to right and store these sums in an array say temp. So temp[i] indicates sum of elements from left to right in row i. If we apply Kadane’s 1D algorithm on temp, and get the maximum sum subarray of temp, this maximum sum would be the maximum possible sum with left and right as boundary columns. To get the overall maximum sum, we compare this sum with the maximum sum so far.
(Top, Left) (1, 1) (Bottom, Right) (3, 3) Max sum is: 29
Time Complexity: O(n3)
This article is compiled by Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Maximum size rectangle binary sub-matrix with all 1s
- Find if there is a rectangle in binary matrix with corners as 1
- Maximum area rectangle by picking four sides from array
- Rectangle with Maximum Area using Java Pair
- Sum of Area of all possible square inside a rectangle
- Maximum trace possible for any sub-matrix of the given matrix
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Create matrix whose sum of diagonals in each sub matrix is even
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Find the largest rectangle of 1's with swapping of columns allowed
- Count number of squares in a rectangle
- Possible number of Rectangle and Squares with the given set of elements
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Print maximum sum square sub-matrix of given size
- Maximum difference of sum of elements in two rows in a matrix
- Maximum sum of hour glass in matrix