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.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
Java
import java.util.*; import java.lang.*; import java.io.*; /** * Given a 2D array, find the maximum sum subarray in it */class Ideone { public static void main(String[] args) throws java.lang.Exception { findMaxSubMatrix(new int[][] { { 1, 2, -1, -4, -20 }, { -8, -3, 4, 2, 1 }, { 3, 8, 10, 1, 3 }, { -4, -1, 1, 7, -6 } }); } /** * To find maxSum in 1d array * * return {maxSum, left, right} */ public static int[] kadane(int[] a) { // result[0] == maxSum, result[1] == start, result[2] == end; int[] result = new int[] { Integer.MIN_VALUE, 0, -1 }; int currentSum = 0; int localStart = 0; for (int i = 0; i < a.length; i++) { currentSum += a[i]; if (currentSum < 0) { currentSum = 0; localStart = i + 1; } else if (currentSum > result[0]) { result[0] = currentSum; result[1] = localStart; result[2] = i; } } // all numbers in a are negative if (result[2] == -1) { result[0] = 0; for (int i = 0; i < a.length; i++) { if (a[i] > result[0]) { result[0] = a[i]; result[1] = i; result[2] = i; } } } return result; } /** * To find and print maxSum, (left, top), (right, bottom) */ public static void findMaxSubMatrix(int[][] a) { int cols = a[0].length; int rows = a.length; int[] currentResult; int maxSum = Integer.MIN_VALUE; int left = 0; int top = 0; int right = 0; int bottom = 0; for (int leftCol = 0; leftCol < cols; leftCol++) { int[] tmp = new int[rows]; for (int rightCol = leftCol; rightCol < cols; rightCol++) { for (int i = 0; i < rows; i++) { tmp[i] += a[i][rightCol]; } currentResult = kadane(tmp); if (currentResult[0] > maxSum) { maxSum = currentResult[0]; left = leftCol; top = currentResult[1]; right = rightCol; bottom = currentResult[2]; } } } System.out.println("MaxSum: " + maxSum + ", range: [(" + left + ", " + top + ")(" + right + ", " + bottom + ")]"); } } // Thanks to Ilia Savin for contributing this code. |
chevron_right
filter_none
Output:
MaxSum: 29, range: [(1, 1)(3, 3)]
Please refer complete article on Maximum sum rectangle in a 2D matrix | DP-27 for more details!
Recommended Posts:
- JavaFX | Rectangle and Rounded Rectangle with examples
- Rectangle with Maximum Area using Java Pair
- Java Program for Maximum size square sub-matrix with all 1s
- Draw a ellipse and a rectangle in Java Applet
- Java Program to Print Matrix in Z form
- Java Program to find transpose of a matrix
- Java Program for Matrix Chain Multiplication | DP-8
- Java Program for Maximum height when coins are arranged in a triangle
- Java Program for Program to calculate area of a Tetrahedron
- Java Program for Program to find area of a circle
- Java Program for Program for array rotation
- Java Program for Range sum queries without updates
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Java Program for cube sum of first n natural numbers
- Java Program for Sum of squares of first n natural numbers
- Java Program to Find sum of even factors of a number
- Java Program to Find minimum sum of factors of number
- Java Program for Sum the digits of a given number
- Java Program to find sum of array
- Java Program to Find sum of Series with n-th term as n^2 - (n-1)^2


