# Minimum sum submatrix in a given 2D array in C++

We are given a 2-D array of integer elements forming the matrix. The task is to calculate the count of minimum sum by pulling the submatrix from the matrix so formed.

Let us see various input output scenarios for this -

In −int matrix[size][size] = { {2, 3, -1, 5}, {-2, 9, -1, 6}, { 5, 6, 9, -9}, { -6, 1, 1, 1} }

Out −Minimum sum submatrix in a given 2D array is: -9

Explanation −we are given a 2-D array of size 4x4 i.e. 4 rows and 4 columns. Now, we will find out the sub-matrix from the given matrix such that the minimum sub is -9.

In −int matrix[row][column] = { {4, 1, 3}, {-1, -1, -1}, { 6, 2, 3}}

Out −Minimum sum submatrix in a given 2D array is: -3

Explanation −we are given a 2-D array of size 3x3 i.e. 3 rows and 3 columns. Now, we will find out the sub-matrix from the given matrix such that the minimum sub is -3 which is achieved with the second row in a given matrix and the sub-matrix will be of size 1x3 i.e. 1 row and 3 columns.

## Approach used in the below program is as follows −

• Input an integer 2-D array and pass the data to the function Minimum_Matrix(matrix) for further processing.

• Inside the function Minimum_Matrix(matrix)

• Declare temporary variables as int result = INT_MAX, int arr[row], int total, int first and int end.

• Start loop FOR from int to temp till temp less than column. Inside the loop set array elements as 0. Start another loop FOR from int to temp_2 till temp_2 less than column. Inside the loop, start another loop from int i to 0 till i less than row and set arr[i] as ar[i] + matrix[i][temo_2].

• Set total as call to the function Algo_Kad(arr, &first, &end, row)

• Check IF total less than result then set result as total

• Print the result as final output.

• Inside the function int Algo_Kad(int* arr, int* first, int* end, int max_size)

• Declare temporary variables as int total to 0, int result to INT_MAX, int temp to 0 and *end = -1

• Start loop FOR from i to 0 till i less than max_size. Inside the loop, set total as total + arr[i].

• Check IF total greater than 0 then set total as 0 and temp as i + 1.

• ELSE IF, check total less than result then set result to total, *first to temp and *end to i

• Now, check IF *end not equals to -1 then return result.

• Set result to arr[0], *first to 0 and *end to 0

• Start loop FOR from i to 1 till i less than max_size. Inside the loop, check IF arr[i] less than result then set result as arr[i], *first as i and *end as i

• Return result.

## Example

#include <bits/stdc++.h>
using namespace std;
#define row 4
#define column 4
int Algo_Kad(int* arr, int* first, int* end, int max_size)
{
int total = 0;
int result = INT_MAX;
int temp = 0;
*end = -1;
for(int i = 0; i < max_size; ++i)
{
total = total + arr[i];
if(total > 0)
{
total = 0;
temp = i + 1;
}
else if(total < result)
{
result = total;
*first = temp;
*end = i;
}
}
if(*end != -1)
{
return result;
}
result = arr[0];
*first = 0;
*end = 0;

for(int i = 1; i < max_size; i++)
{
if(arr[i] < result)
{
result = arr[i];
*first = i;
*end = i;
}
}
return result;
}
void Minimum_Matrix(int matrix[][column])
{
int result = INT_MAX;
int arr[row];
int total;
int first;
int end;

for(int temp = 0; temp < column; ++temp)
{
memset(arr, 0, sizeof(arr));
for(int temp_2 = temp; temp_2 < column; ++temp_2)
{
for(int i = 0; i < row; ++i)
{
arr[i] = arr[i] + matrix[i][temp_2];
}

total = Algo_Kad(arr, &first, &end, row);

if(total < result)
{
result = total;
}
}
}
cout<<"Minimum sum submatrix in a given 2D array is: "<<result;
}
int main()
{
int matrix[row][column] = {{2, 3, -1, 5},
{-2, 9, -1, 6},
{ 5, 6, 9, -9},
{ -6, 1, 1, 1} };
Minimum_Matrix(matrix);
return 0;
}

## Output

If we run the above code it will generate the following Output

Minimum sum submatrix in a given 2D array is: -9