|
| 1 | +package com.thealgorithms.matrix; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * Kadane's Algorithm for 2D arrays (Maximum Sum Rectangle in a 2D Matrix) |
| 6 | + * |
| 7 | + * This algorithm finds the maximum sum of elements in a rectangular submatrix |
| 8 | + * of a given 2D matrix. It uses Kadane's algorithm for 1D arrays extended to 2D. |
| 9 | + * |
| 10 | + * Time Complexity: O(n^2 * m) where n is the number of columns and m is the number of rows |
| 11 | + * Space Complexity: O(m) for the temporary array |
| 12 | + * |
| 13 | + * @author Adarsh Singh |
| 14 | + */ |
| 15 | +public final class Kadane2D { |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * Finds the maximum sum rectangle in a 2D matrix |
| 21 | + * |
| 22 | + * @param matrix The input 2D matrix |
| 23 | + * @return The maximum sum found in any rectangular submatrix |
| 24 | + * @throws IllegalArgumentException if matrix is null or empty |
| 25 | + */ |
| 26 | + public static int maxSumRectangle(int[][] matrix) { |
| 27 | + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
| 28 | + throw new IllegalArgumentException("Matrix cannot be null or empty"); |
| 29 | + } |
| 30 | + |
| 31 | + int rows = matrix.length; |
| 32 | + int cols = matrix[0].length; |
| 33 | + int maxSum = Integer.MIN_VALUE; |
| 34 | + |
| 35 | + // Fix the left column |
| 36 | + for (int left = 0; left < cols; left++) { |
| 37 | + int[] temp = new int[rows]; |
| 38 | + |
| 39 | + // Calculate sum between left and right columns |
| 40 | + for (int right = left; right < cols; right++) { |
| 41 | + // Add elements of current column to temp array |
| 42 | + for (int i = 0; i < rows; i++) { |
| 43 | + temp[i] += matrix[i][right]; |
| 44 | + } |
| 45 | + |
| 46 | + // Apply Kadane's 1D algorithm on temp array |
| 47 | + int currentMax = kadane1D(temp); |
| 48 | + maxSum = Math.max(maxSum, currentMax); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return maxSum; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Kadane's algorithm for 1D array |
| 57 | + * Finds maximum sum of contiguous subarray |
| 58 | + * |
| 59 | + * @param arr The input array |
| 60 | + * @return Maximum sum of contiguous subarray |
| 61 | + */ |
| 62 | + private static int kadane1D(int[] arr) { |
| 63 | + int maxSoFar = arr[0]; |
| 64 | + int maxEndingHere = arr[0]; |
| 65 | + |
| 66 | + for (int i = 1; i < arr.length; i++) { |
| 67 | + maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]); |
| 68 | + maxSoFar = Math.max(maxSoFar, maxEndingHere); |
| 69 | + } |
| 70 | + |
| 71 | + return maxSoFar; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Result class to store coordinates along with maximum sum |
| 76 | + */ |
| 77 | + public static class Result { |
| 78 | + public final int maxSum; |
| 79 | + public final int topRow; |
| 80 | + public final int leftCol; |
| 81 | + public final int bottomRow; |
| 82 | + public final int rightCol; |
| 83 | + |
| 84 | + public Result(int maxSum, int topRow, int leftCol, int bottomRow, int rightCol) { |
| 85 | + this.maxSum = maxSum; |
| 86 | + this.topRow = topRow; |
| 87 | + this.leftCol = leftCol; |
| 88 | + this.bottomRow = bottomRow; |
| 89 | + this.rightCol = rightCol; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + return String.format("Max Sum: %d, Top-Left: (%d, %d), Bottom-Right: (%d, %d)", |
| 95 | + maxSum, topRow, leftCol, bottomRow, rightCol); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Finds the maximum sum rectangle along with its coordinates |
| 101 | + * |
| 102 | + * @param matrix The input 2D matrix |
| 103 | + * @return Result object containing maximum sum and rectangle coordinates |
| 104 | + * @throws IllegalArgumentException if matrix is null or empty |
| 105 | + */ |
| 106 | + public static Result maxSumRectangleWithCoordinates(int[][] matrix) { |
| 107 | + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
| 108 | + throw new IllegalArgumentException("Matrix cannot be null or empty"); |
| 109 | + } |
| 110 | + |
| 111 | + int rows = matrix.length; |
| 112 | + int cols = matrix[0].length; |
| 113 | + int maxSum = Integer.MIN_VALUE; |
| 114 | + int finalLeft = 0, finalRight = 0, finalTop = 0, finalBottom = 0; |
| 115 | + |
| 116 | + for (int left = 0; left < cols; left++) { |
| 117 | + int[] temp = new int[rows]; |
| 118 | + |
| 119 | + for (int right = left; right < cols; right++) { |
| 120 | + for (int i = 0; i < rows; i++) { |
| 121 | + temp[i] += matrix[i][right]; |
| 122 | + } |
| 123 | + |
| 124 | + // Modified Kadane's to track row positions |
| 125 | + int currentSum = temp[0]; |
| 126 | + int maxTemp = temp[0]; |
| 127 | + int start = 0, end = 0, s = 0; |
| 128 | + |
| 129 | + for (int i = 1; i < rows; i++) { |
| 130 | + if (currentSum < 0) { |
| 131 | + currentSum = temp[i]; |
| 132 | + s = i; |
| 133 | + } else { |
| 134 | + currentSum += temp[i]; |
| 135 | + } |
| 136 | + |
| 137 | + if (currentSum > maxTemp) { |
| 138 | + maxTemp = currentSum; |
| 139 | + start = s; |
| 140 | + end = i; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (maxTemp > maxSum) { |
| 145 | + maxSum = maxTemp; |
| 146 | + finalLeft = left; |
| 147 | + finalRight = right; |
| 148 | + finalTop = start; |
| 149 | + finalBottom = end; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return new Result(maxSum, finalTop, finalLeft, finalBottom, finalRight); |
| 155 | + } |
| 156 | +} |
0 commit comments