Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
Given a N x M matrix where N is the number of rows and M is the number of columns in the given matrix and an integer K. The task is to find the maximum length of a square submatrix having the sum of elements less than or equal to K or print 0 if no such square exits.
Examples:
Input: r = 4, c = 4 , k = 6
matrix[][] = { {1, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
}
Output: 3
Explanation:
Square from (0,0) to (2,2) with
sum 5 is one of the valid answer.
Input: r = 4, c = 4 , k = 1
matrix[][] = { {2, 2, 2},
{2, 2, 2},
{2, 2, 2},
{2, 2, 2},
}
Output: 0
Explanation:
There is no valid answer.
Approach:
The idea is to calculate the prefix sum matrix. Once the prefix sum matrix is calculated, the required sub-matrix sum can be calculated in O(1) time complexity. Use the sliding window technique to calculate the maximum length square submatrix. For every square of length cur_max+1, where cur_max is the currently found maximum length of a square submatrix, we check whether the sum of elements in the current submatrix, having the length of cur_max+1, is less than or equal to K or not. If yes, then increase the result by 1, otherwise, we continue to check.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int maxLengthSquare( int row, int column,
int arr[][4], int k)
{
int sum[row + 1][column + 1] ;
for ( int i = 1; i <= row; i++)
for ( int j = 0; j <= column; j++)
sum[i][j] = 0;
int cur_max = 1;
int max = 0;
for ( int i = 1; i <= row; i++)
{
for ( int j = 1; j <= column; j++)
{
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] +
arr[i - 1][j - 1] - sum[i - 1][j - 1];
if (i >= cur_max && j >= cur_max &&
sum[i][j] - sum[i - cur_max][j]
- sum[i][j - cur_max] +
sum[i - cur_max][j - cur_max] <= k)
{
max = cur_max++;
}
}
}
return max;
}
int main()
{
int row = 4, column = 4;
int matrix[4][4] = { {1, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}
};
int k = 6;
int ans = maxLengthSquare(row, column, matrix, k);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int maxLengthSquare( int row, int column,
int [][] arr, int k)
{
int sum[][] = new int [row + 1 ][column + 1 ];
int cur_max = 1 ;
int max = 0 ;
for ( int i = 1 ; i <= row; i++)
{
for ( int j = 1 ; j <= column; j++)
{
sum[i][j] = sum[i - 1 ][j] + sum[i][j - 1 ] +
arr[i - 1 ][j - 1 ] - sum[i - 1 ][j - 1 ];
if (i >=cur_max&&j>=cur_max&&sum[i][j]-sum[i - cur_max][j]
- sum[i][j - cur_max]
+ sum[i - cur_max][j - cur_max] <= k){
max = cur_max++;
}
}
}
return max;
}
public static void main(String args[])
{
int row = 4 , column = 4 ;
int matrix[][] = { { 1 , 1 , 1 , 1 },
{ 1 , 0 , 0 , 0 },
{ 1 , 0 , 0 , 0 },
{ 1 , 0 , 0 , 0 }
};
int k = 6 ;
int ans = maxLengthSquare(row,column,matrix, k);
System.out.println(ans);
}
}
|
Python3
import numpy as np
def maxLengthSquare(row, column, arr, k) :
sum = np.zeros((row + 1 , column + 1 ));
cur_max = 1 ;
max = 0 ;
for i in range ( 1 , row + 1 ) :
for j in range ( 1 , column + 1 ) :
sum [i][j] = sum [i - 1 ][j] + sum [i][j - 1 ] + \
arr[i - 1 ][j - 1 ] - \
sum [i - 1 ][j - 1 ];
if (i > = cur_max and j > = cur_max and
sum [i][j] - sum [i - cur_max][j] - sum [i][j -
cur_max] + sum [i -
cur_max][j - cur_max] < = k) :
max = cur_max;
cur_max + = 1 ;
return max ;
if __name__ = = "__main__" :
row = 4 ;
column = 4 ;
matrix = [[ 1 , 1 , 1 , 1 ],
[ 1 , 0 , 0 , 0 ],
[ 1 , 0 , 0 , 0 ],
[ 1 , 0 , 0 , 0 ]];
k = 6 ;
ans = maxLengthSquare(row, column, matrix, k);
print (ans);
|
C#
using System;
class GFG
{
public static int maxLengthSquare( int row, int column,
int [,] arr, int k)
{
int [,]sum = new int [row + 1,column + 1];
int cur_max = 1;
int max = 0;
for ( int i = 1; i <= row; i++)
{
for ( int j = 1; j <= column; j++)
{
sum[i, j] = sum[i - 1, j] + sum[i, j - 1] +
arr[i - 1, j - 1] - sum[i - 1, j - 1];
if (i >=cur_max && j>=cur_max && sum[i, j]-sum[i - cur_max, j]
- sum[i, j - cur_max]
+ sum[i - cur_max, j - cur_max] <= k)
{
max = cur_max++;
}
}
}
return max;
}
public static void Main()
{
int row = 4 , column = 4;
int [,]matrix = { {1, 1, 1, 1},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}
};
int k = 6;
int ans = maxLengthSquare(row, column, matrix, k);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function maxLengthSquare(row, column, arr, k) {
let sum = new Array();[row + 1][column + 1];
for (let i = 0; i < row + 1; i++) {
let temp = new Array();
for (let j = 0; j < column + 1; j++) {
temp.push([])
}
sum.push(temp)
}
for (let i = 1; i <= row; i++)
for (let j = 0; j <= column; j++)
sum[i][j] = 0;
let cur_max = 1;
let max = 0;
for (let i = 1; i <= row; i++) {
for (let j = 1; j <= column; j++) {
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] +
arr[i - 1][j - 1] - sum[i - 1][j - 1];
if (i >= cur_max && j >= cur_max &&
sum[i][j] - sum[i - cur_max][j]
- sum[i][j - cur_max] +
sum[i - cur_max][j - cur_max] <= k) {
max = cur_max++;
}
}
}
return max;
}
let row = 4, column = 4;
let matrix = [[1, 1, 1, 1],
[1, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0]
];
let k = 6;
let ans = maxLengthSquare(row, column, matrix, k);
document.write(ans);
</script>
|
Time Complexity: O(N x M)
Auxiliary Space: O(N x M), where N and M are the given rows and columns of the matrix.
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...