Maximum size of square such that all submatrices of that size have sum less than K
Given an N x M matrix of integers and an integer K, the task is to find the size of the maximum square sub-matrix (S x S), such that all square sub-matrices of the given matrix of that size have a sum less than K.
Examples:
Input: K = 30
mat[N][M] = {{1, 2, 3, 4, 6},
{5, 3, 8, 1, 2},
{4, 6, 7, 5, 5},
{2, 4, 8, 9, 4} };
Output: 2
Explanation:
All Sub-matrices of size 2 x 2
have sum less than 30
Input : K = 100
mat[N][M] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Output: 3
Explanation:
All Sub-matrices of size 3 x 3
have sum less than 100
Naive Approach The basic solution is to choose the size S of the submatrix and find all the submatrices of that size and check that the sum of all sub-matrices is less than the given sum whereas, this can be improved by computing the sum of the matrix using this approach. Therefore, the task will be to choose the maximum size possible and the starting and ending position of the every possible sub-matrices. Due to which the overall time complexity will be O(N3).
Below is the implementation of the above approach:
C++
// C++ implementation to find the // maximum size square submatrix // such that their sum is less than K #include <bits/stdc++.h> using namespace std; // Size of matrix #define N 4 #define M 5 // Function to preprocess the matrix // for computing the sum of every // possible matrix of the given size void preProcess(int mat[N][M], int aux[N][M]) { // Loop to copy the first row // of the matrix into the aux matrix for (int i = 0; i < M; i++) aux[0][i] = mat[0][i]; // Computing the sum column-wise for (int i = 1; i < N; i++) for (int j = 0; j < M; j++) aux[i][j] = mat[i][j] + aux[i - 1][j]; // Computing row wise sum for (int i = 0; i < N; i++) for (int j = 1; j < M; j++) aux[i][j] += aux[i][j - 1]; } // Function to find the sum of a // submatrix with the given indices int sumQuery(int aux[N][M], int tli, int tlj, int rbi, int rbj) { // Overall sum from the top to // right corner of matrix int res = aux[rbi][rbj]; // Removing the sum from the top // corer of the matrix if (tli > 0) res = res - aux[tli - 1][rbj]; // Remove the overlapping sum if (tlj > 0) res = res - aux[rbi][tlj - 1]; // Add the sum of top corner // which is substracted twice if (tli > 0 && tlj > 0) res = res + aux[tli - 1][tlj - 1]; return res; } // Function to find the maximum // square size possible with the // such that every submatrix have // sum less than the given sum int maximumSquareSize(int mat[N][M], int K) { int aux[N][M]; preProcess(mat, aux); // Loop to choose the size of matrix for (int i = min(N, M); i >= 1; i--) { bool satisfies = true; // Loop to find the sum of the // matrix of every possible submatrix for (int x = 0; x < N; x++) { for (int y = 0; y < M; y++) { if (x + i - 1 <= N - 1 && y + i - 1 <= M - 1) { if (sumQuery(aux, x, y, x + i - 1, y + i - 1) > K) satisfies = false; } } } if (satisfies == true) return (i); } return 0; } // Driver Code int main() { int K = 30; int mat[N][M] = { { 1, 2, 3, 4, 6 }, { 5, 3, 8, 1, 2 }, { 4, 6, 7, 5, 5 }, { 2, 4, 8, 9, 4 } }; cout << maximumSquareSize(mat, K); return 0; } |
Java
// Java implementation to find the // maximum size square submatrix // such that their sum is less than K class GFG{ // Size of matrix static final int N = 4; static final int M = 5; // Function to preprocess the matrix // for computing the sum of every // possible matrix of the given size static void preProcess(int [][]mat, int [][]aux) { // Loop to copy the first row // of the matrix into the aux matrix for (int i = 0; i < M; i++) aux[0][i] = mat[0][i]; // Computing the sum column-wise for (int i = 1; i < N; i++) for (int j = 0; j < M; j++) aux[i][j] = mat[i][j] + aux[i - 1][j]; // Computing row wise sum for (int i = 0; i < N; i++) for (int j = 1; j < M; j++) aux[i][j] += aux[i][j - 1]; } // Function to find the sum of a // submatrix with the given indices static int sumQuery(int [][]aux, int tli, int tlj, int rbi, int rbj) { // Overall sum from the top to // right corner of matrix int res = aux[rbi][rbj]; // Removing the sum from the top // corer of the matrix if (tli > 0) res = res - aux[tli - 1][rbj]; // Remove the overlapping sum if (tlj > 0) res = res - aux[rbi][tlj - 1]; // Add the sum of top corner // which is substracted twice if (tli > 0 && tlj > 0) res = res + aux[tli - 1][tlj - 1]; return res; } // Function to find the maximum // square size possible with the // such that every submatrix have // sum less than the given sum static int maximumSquareSize(int [][]mat, int K) { int [][]aux = new int[N][M]; preProcess(mat, aux); // Loop to choose the size of matrix for (int i = Math.min(N, M); i >= 1; i--) { boolean satisfies = true; // Loop to find the sum of the // matrix of every possible submatrix for (int x = 0; x < N; x++) { for (int y = 0; y < M; y++) { if (x + i - 1 <= N - 1 && y + i - 1 <= M - 1) { if (sumQuery(aux, x, y, x + i - 1, y + i - 1) > K) satisfies = false; } } } if (satisfies == true) return (i); } return 0; } // Driver Code public static void main(String[] args) { int K = 30; int mat[][] = { { 1, 2, 3, 4, 6 }, { 5, 3, 8, 1, 2 }, { 4, 6, 7, 5, 5 }, { 2, 4, 8, 9, 4 } }; System.out.print(maximumSquareSize(mat, K)); } } // This code is contributed by PrinciRaj1992 |
C#
// C# implementation to find the // maximum size square submatrix // such that their sum is less than K using System; public class GFG{ // Size of matrix static readonly int N = 4; static readonly int M = 5; // Function to preprocess the matrix // for computing the sum of every // possible matrix of the given size static void preProcess(int [,]mat, int [,]aux) { // Loop to copy the first row // of the matrix into the aux matrix for (int i = 0; i < M; i++) aux[0,i] = mat[0,i]; // Computing the sum column-wise for (int i = 1; i < N; i++) for (int j = 0; j < M; j++) aux[i,j] = mat[i,j] + aux[i - 1,j]; // Computing row wise sum for (int i = 0; i < N; i++) for (int j = 1; j < M; j++) aux[i,j] += aux[i,j - 1]; } // Function to find the sum of a // submatrix with the given indices static int sumQuery(int [,]aux, int tli, int tlj, int rbi, int rbj) { // Overall sum from the top to // right corner of matrix int res = aux[rbi,rbj]; // Removing the sum from the top // corer of the matrix if (tli > 0) res = res - aux[tli - 1,rbj]; // Remove the overlapping sum if (tlj > 0) res = res - aux[rbi,tlj - 1]; // Add the sum of top corner // which is substracted twice if (tli > 0 && tlj > 0) res = res + aux[tli - 1,tlj - 1]; return res; } // Function to find the maximum // square size possible with the // such that every submatrix have // sum less than the given sum static int maximumSquareSize(int [,]mat, int K) { int [,]aux = new int[N,M]; preProcess(mat, aux); // Loop to choose the size of matrix for (int i = Math.Min(N, M); i >= 1; i--) { bool satisfies = true; // Loop to find the sum of the // matrix of every possible submatrix for (int x = 0; x < N; x++) { for (int y = 0; y < M; y++) { if (x + i - 1 <= N - 1 && y + i - 1 <= M - 1) { if (sumQuery(aux, x, y, x + i - 1, y + i - 1) > K) satisfies = false; } } } if (satisfies == true) return (i); } return 0; } // Driver Code public static void Main(String[] args) { int K = 30; int [,]mat = { { 1, 2, 3, 4, 6 }, { 5, 3, 8, 1, 2 }, { 4, 6, 7, 5, 5 }, { 2, 4, 8, 9, 4 } }; Console.Write(maximumSquareSize(mat, K)); } } // This code contributed by PrinciRaj1992 |
2
- Time complexity: O(N3)
- Auxillary Space: O(N2)
Efficient Approach: The key observation is, if a square of side s is the maximum size satisfying the condition, then all sizes smaller than it will satisfy the condition. Using this we can reduce our search space at each step by half which is precisely the idea of Binary Search. Below is the illustration of the steps of the approach:
- Search Space: The search space for this problem will be from [1, min(N, M)]. That is the search space for binary search is defined as –
low = 1 high = min(N, M)
- Next Search Space: In each iteration find the mid of the search space and then Finally, check that all subarrays of that size have the sum less than K. If all subarrays of that size have sum less than K. Then the next search space possible will be in the right of the middle. Otherwise, the next search space possible will be in the left of the middle. That is less than the middle.
- Case 1: Condition when the all the subarrays of size mid have sum less than K. Then –
if checkSubmatrix(mat, mid, K): low = mid + 1 - Case 2: Condition when the all the subarrays of size mid have sum greater than K. Then –
if not checkSubmatrix(mat, mid, K): high = mid - 1
- Case 1: Condition when the all the subarrays of size mid have sum less than K. Then –
Below is the implementation of the above approach:
C++
// C++ implementation to find the // maximum size square submatrix // such that their sum is less than K #include <bits/stdc++.h> using namespace std; // Size of matrix #define N 4 #define M 5 // Function to preprocess the matrix // for computing the sum of every // possible matrix of the given size void preProcess(int mat[N][M], int aux[N][M]) { // Loop to copy the first row // of the matrix into the aux matrix for (int i = 0; i < M; i++) aux[0][i] = mat[0][i]; // Computing the sum column-wise for (int i = 1; i < N; i++) for (int j = 0; j < M; j++) aux[i][j] = mat[i][j] + aux[i - 1][j]; // Computing row wise sum for (int i = 0; i < N; i++) for (int j = 1; j < M; j++) aux[i][j] += aux[i][j - 1]; } // Function to find the sum of a // submatrix with the given indices int sumQuery(int aux[N][M], int tli, int tlj, int rbi, int rbj) { // Overall sum from the top to // right corner of matrix int res = aux[rbi][rbj]; // Removing the sum from the top // corer of the matrix if (tli > 0) res = res - aux[tli - 1][rbj]; // Remove the overlapping sum if (tlj > 0) res = res - aux[rbi][tlj - 1]; // Add the sum of top corner // which is substracted twice if (tli > 0 && tlj > 0) res = res + aux[tli - 1][tlj - 1]; return res; } // Function to check whether square // sub matrices of size mid satisfy // the condition or not bool check(int mid, int aux[N][M], int K) { bool satisfies = true; // Iterating throught all possible // submatrices of given size for (int x = 0; x < N; x++) { for (int y = 0; y < M; y++) { if (x + mid - 1 <= N - 1 && y + mid - 1 <= M - 1) { if (sumQuery(aux, x, y, x + mid - 1, y + mid - 1) > K) satisfies = false; } } } return (satisfies == true); } // Function to find the maximum // square size possible with the // such that every submatrix have // sum less than the given sum int maximumSquareSize(int mat[N][M], int K) { int aux[N][M]; preProcess(mat, aux); // Search space int low = 1, high = min(N, M); int mid; // Binary search for size while (high - low > 1) { mid = (low + high) / 2; // Check if the mid satisfies // the given condition if (check(mid, aux, K)) { low = mid; } else high = mid; } if (check(high, aux, K)) return high; return low; } // Driver Code int main() { int K = 30; int mat[N][M] = { { 1, 2, 3, 4, 6 }, { 5, 3, 8, 1, 2 }, { 4, 6, 7, 5, 5 }, { 2, 4, 8, 9, 4 } }; cout << maximumSquareSize(mat, K); return 0; } |
2
Performance Analysis:
- Time Complexity: O(N2 * log(N) )
- Auxillary Space: O(N2)
Recommended Posts:
- Maximum size square sub-matrix with all 1s
- Print maximum sum square sub-matrix of given size
- Maximum size square Sub-Matrix with sum less than or equals to K
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Maximum of all Subarrays of size k using set in C++ STL
- Size of The Subarray With Maximum Sum
- Maximum size subset with given sum
- Submatrix of given size with maximum 1's
- Maximum difference between groups of size two
- Minimum and Maximum of all subarrays of size K using Map
- Maximum size of sub-array that satisfies the given condition
- Maximum of all subarrays of size K using Segment Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : princiraj1992


