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.

Maximum sum rectangle in a 2D matrix

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 4 nested loops and time complexity of this solution would be O(n^4).

[ad type=”banner”]

Kadane’s algorithm:

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 sum 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.

 

Java implementation for Maximum sum rectangle in a 2D matrix:

[pastacode lang=”java” manual=”import%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%20%0A%0Aclass%20Ideone%0A%7B%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%20throws%20java.lang.Exception%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20findMaxSubMatrix(new%20int%5B%5D%5B%5D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B1%2C%202%2C%20-1%2C%20-4%2C%20-20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B-8%2C%20-3%2C%204%2C%202%2C%201%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B3%2C%208%2C%2010%2C%201%2C%203%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B-4%2C%20-1%2C%201%2C%207%2C%20-6%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20int%5B%5D%20kadane(int%5B%5D%20a)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%5B%5D%20result%20%3D%20new%20int%5B%5D%7BInteger.MIN_VALUE%2C%200%2C%20-1%7D%3B%0A%20%20%20%20%20%20%20%20int%20currentSum%20%3D%200%3B%0A%20%20%20%20%20%20%20%20int%20localStart%20%3D%200%3B%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20a.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20currentSum%20%2B%3D%20a%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(currentSum%20%3C%200)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20currentSum%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20localStart%20%3D%20i%20%2B%201%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20if%20(currentSum%20%3E%20result%5B0%5D)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5B0%5D%20%3D%20currentSum%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5B1%5D%20%3D%20localStart%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5B2%5D%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(result%5B2%5D%20%3D%3D%20-1)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20result%5B0%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20a.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(a%5Bi%5D%20%3E%20result%5B0%5D)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5B0%5D%20%3D%20a%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5B1%5D%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%5B2%5D%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20result%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20public%20static%20void%20findMaxSubMatrix(int%5B%5D%5B%5D%20a)%20%7B%0A%20%20%20%20%20%20%20%20int%20cols%20%3D%20a%5B0%5D.length%3B%0A%20%20%20%20%20%20%20%20int%20rows%20%3D%20a.length%3B%0A%20%20%20%20%20%20%20%20int%5B%5D%20currentResult%3B%0A%20%20%20%20%20%20%20%20int%20maxSum%20%3D%20Integer.MIN_VALUE%3B%0A%20%20%20%20%20%20%20%20int%20left%20%3D%200%3B%0A%20%20%20%20%20%20%20%20int%20top%20%3D%200%3B%0A%20%20%20%20%20%20%20%20int%20right%20%3D%200%3B%0A%20%20%20%20%20%20%20%20int%20bottom%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20for%20(int%20leftCol%20%3D%200%3B%20leftCol%20%3C%20cols%3B%20leftCol%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%5B%5D%20tmp%20%3D%20new%20int%5Brows%5D%3B%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20rightCol%20%3D%20leftCol%3B%20rightCol%20%3C%20cols%3B%20rightCol%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20rows%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tmp%5Bi%5D%20%2B%3D%20a%5Bi%5D%5BrightCol%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20currentResult%20%3D%20kadane(tmp)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(currentResult%5B0%5D%20%3E%20maxSum)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maxSum%20%3D%20currentResult%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20left%20%3D%20leftCol%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20top%20%3D%20currentResult%5B1%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20right%20%3D%20rightCol%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bottom%20%3D%20currentResult%5B2%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22MaxSum%3A%20%22%20%2B%20maxSum%20%2B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2C%20range%3A%20%5B(%22%20%2B%20left%20%2B%20%22%2C%20%22%20%2B%20top%20%2B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22)(%22%20%2B%20right%20%2B%20%22%2C%20%22%20%2B%20bottom%20%2B%20%22)%5D%22)%3B%0A%20%20%20%20%7D%0A%7D” message=”Java” highlight=”” provider=”manual”/]

Output :

Max sum is: 29, range: [(1,1)(3,3]

Time Complexity: O(n^3)

[ad type=”banner”]