# Python
import sys
# Implementation of Kadane's algorithm for 1D vector
def kadane(arr):
sum_ = 0
mx = -sys.maxsize
finish = -1
start = 0
localStart = 0
for i in range(len(arr)):
sum_ += arr[i]
if sum_ < 0:
sum_ = 0
localStart = i + 1
elif sum_ > mx:
mx = sum_
start = localStart
finish = i
# If all numbers are negative
if finish != -1:
return mx, start, finish
# Special case: when all elements are negative
mx = arr[0]
start = finish = 0
for i in range(1, len(arr)):
if arr[i] > mx:
mx = arr[i]
start = finish = i
return mx, start, finish
# Function that finds the maximum sum rectangle
def maxSumRectangle(mat):
if not mat or not mat[0]:
print("The matrix is empty.")
return 0
rows = len(mat)
cols = len(mat[0])
# Variables to store the final output
maxSum = -sys.maxsize
# Temporary array to store the sum of elements for each row
temp = [0] * rows
start = finish = sum_ = 0
# Set the left boundary of the rectangle
for left in range(cols):
# Initialize all elements of temp as 0
for i in range(rows):
temp[i] = 0
# Set the right boundary for the current left
for right in range(left, cols):
# Calculate sum between current left and right for every row 'i'
for i in range(rows):
temp[i] += mat[i][right]
sum_, s, f = kadane(temp)
start = s
finish = f
if sum_ > maxSum:
maxSum = sum_
return maxSum
# Initialize the 2D vector 'mat'
mat = [
[ 1, 2, -1, -4, -20 ],
[ -8, -3, 4, 2, 1 ],
[ 3, 8, 10, 1, 3 ],
[ -4, -1, 1, 7, -6 ]
]
# Call the function to find the maximum sum subarray
print(maxSumRectangle(mat))
// Java
import java.util.*;
class GfG {
// Implementation of Kadane's algorithm for 1D vector
static int kadane(int[] arr, int[] result) {
int sum = 0;
int mx = Integer.MIN_VALUE;
result[1] = -1;
int start = 0;
int localStart = 0;
for (int i = 0; i < arr.length; ++i) {
sum += arr[i];
if (sum < 0) {
sum = 0;
localStart = i + 1;
}
else if (sum > mx) {
mx = sum;
result[0] = localStart;
result[1] = i;
}
}
// If all numbers are negative
if (result[1] != -1)
return mx;
// Special case: when all elements are negative
mx = arr[0];
result[0] = result[1] = 0;
for (int i = 1; i < arr.length; ++i) {
if (arr[i] > mx) {
mx = arr[i];
result[0] = result[1] = i;
}
}
return mx;
}
// Function that finds the maximum sum rectangle
static int maxSumRectangle(int[][] mat) {
if (mat.length == 0 || mat[0].length == 0) {
System.out.println("The matrix is empty.");
return 0;
}
int rows = mat.length;
int cols = mat[0].length;
// Variables to store the final output
int maxSum = Integer.MIN_VALUE;
// Temporary array to store the sum of elements for each row
int[] temp = new int[rows];
int[] result = new int[2];
int sum;
// Set the left boundary of the rectangle
for (int left = 0; left < cols; ++left) {
// Initialize all elements of temp as 0
Arrays.fill(temp, 0);
// Set the right boundary for the current left
for (int right = left; right < cols; ++right) {
// Calculate sum between current left and right for every row 'i'
for (int i = 0; i < rows; ++i)
temp[i] += mat[i][right];
sum = kadane(temp, result);
if (sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}
public static void main(String[] args) {
// Initialize the 2D vector 'mat'
int[][] mat = {
{ 1, 2, -1, -4, -20 },
{ -8, -3, 4, 2, 1 },
{ 3, 8, 10, 1, 3 },
{ -4, -1, 1, 7, -6 }
};
// Call the function to find the maximum sum subarray
System.out.println(maxSumRectangle(mat));
}
}
// C#
using System;
using System.Collections.Generic;
class GfG
{
// Implementation of Kadane's algorithm for 1D vector
static int Kadane(List<int> arr, ref int start, ref int finish)
{
int sum = 0;
int mx = int.MinValue;
finish = -1;
int localStart = 0;
for (int i = 0; i < arr.Count; ++i)
{
sum += arr[i];
if (sum < 0)
{
sum = 0;
localStart = i + 1;
}
else if (sum > mx)
{
mx = sum;
start = localStart;
finish = i;
}
}
// If all numbers are negative
if (finish != -1)
return mx;
// Special case: when all elements are negative
mx = arr[0];
start = finish = 0;
for (int i = 1; i < arr.Count; ++i)
{
if (arr[i] > mx)
{
mx = arr[i];
start = finish = i;
}
}
return mx;
}
// Function that finds the maximum sum rectangle
static int MaxSumRectangle(List<List<int>> mat)
{
if (mat.Count == 0 || mat[0].Count == 0)
{
Console.WriteLine("The matrix is empty.");
return 0;
}
int rows = mat.Count;
int cols = mat[0].Count;
// Variables to store the final output
int maxSum = int.MinValue;
// Temporary array to store the sum of elements for each row
List<int> temp = new List<int>(new int[rows]);
int start = 0, finish = 0, sum = 0;
// Set the left boundary of the rectangle
for (int left = 0; left < cols; ++left)
{
// Initialize all elements of temp as 0
for (int i = 0; i < rows; ++i)
temp[i] = 0;
// Set the right boundary for the current left
for (int right = left; right < cols; ++right)
{
// Calculate sum between current left and right for every row 'i'
for (int i = 0; i < rows; ++i)
temp[i] += mat[i][right];
sum = Kadane(temp, ref start, ref finish);
if (sum > maxSum)
{
maxSum = sum;
}
}
}
return maxSum;
}
static void Main(string[] args)
{
// Initialize the 2D vector 'mat'
List<List<int>> mat = new List<List<int>> {
new List<int> { 1, 2, -1, -4, -20 },
new List<int> { -8, -3, 4, 2, 1 },
new List<int> { 3, 8, 10, 1, 3 },
new List<int> { -4, -1, 1, 7, -6 }
};
// Call the function to find the maximum sum subarray
Console.WriteLine(MaxSumRectangle(mat));
}
}
// JavaScript
// Implementation of Kadane's algorithm for 1D vector
function kadane(arr) {
let sum = 0;
let mx = -Infinity;
let finish = -1;
let start = 0;
let localStart = 0;
for (let i = 0; i < arr.length; ++i) {
sum += arr[i];
if (sum < 0) {
sum = 0;
localStart = i + 1;
}
else if (sum > mx) {
mx = sum;
start = localStart;
finish = i;
}
}
// If all numbers are negative
if (finish !== -1)
return { sum: mx, start, finish };
// Special case: when all elements are negative
mx = arr[0];
start = finish = 0;
for (let i = 1; i < arr.length; ++i) {
if (arr[i] > mx) {
mx = arr[i];
start = finish = i;
}
}
return { sum: mx, start, finish };
}
// Function that finds the maximum sum rectangle
function maxSumRectangle(mat) {
if (mat.length === 0 || mat[0].length === 0) {
console.log("The matrix is empty.");
return 0;
}
let rows = mat.length;
let cols = mat[0].length;
// Variables to store the final output
let maxSum = -Infinity;
// Temporary array to store the sum of elements for each row
let temp = new Array(rows).fill(0);
let start = 0, finish = 0, sum = 0;
// Set the left boundary of the rectangle
for (let left = 0; left < cols; ++left) {
// Initialize all elements of temp as 0
temp.fill(0);
// Set the right boundary for the current left
for (let right = left; right < cols; ++right) {
// Calculate sum between current left and right for every row 'i'
for (let i = 0; i < rows; ++i)
temp[i] += mat[i][right];
let result = kadane(temp);
sum = result.sum;
start = result.start;
finish = result.finish;
if (sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}
// Initialize the 2D vector 'mat'
let mat = [
[ 1, 2, -1, -4, -20 ],
[ -8, -3, 4, 2, 1 ],
[ 3, 8, 10, 1, 3 ],
[ -4, -1, 1, 7, -6 ]
];
// Call the function to find the maximum sum subarray
console.log(maxSumRectangle(mat));