Submatrix of given size with maximum 1’s
Last Updated :
22 Feb, 2023
Given a binary matrix mat[][] and an integer K, the task is to find the submatrix of size K*K such that it contains maximum number of 1’s in the matrix.
Examples:
Input: mat[][] = {{1, 0, 1}, {1, 1, 0}, {1, 0, 0}}, K = 2
Output: 3
Explanation:
In the given matrix, there are 4 sub-matrix of order 2*2,
|1 0| |0 1| |1 1| |1 0|
|1 1|, |1 0|, |1 0|, |0 0|
Out of these sub-matrix, two matrix contains 3, 1’s.
Input: mat[][] = {{1, 0}, {0, 1}}, K = 1
Output: 1
Explanation:
In the given matrix, there are 4 sub-matrix of order 1*1,
|1|, |0|, |1|, |0|
Out of these sub-matrix, two matrix contains 1, 1’s.
Approach: The idea is to use the sliding window technique to solve this problem, In this technique, we generally compute the value of one window and then slide the window one-by-one to compute the solution for every window of size K.
To compute the maximum 1’s submatrix, count the number of 1’s in the row for every possible window of size K using the sliding window technique and store the counts of the 1’s in the form of a matrix.
For Example:
Let the matrix be {{1,0,1}, {1, 1, 0}} and K = 2
For Row 1 -
Subarray 1: (1, 0), Count of 1 = 1
Subarray 2: (0, 1), Count of 1 = 1
For Row 2 -
Subarray 1: (1, 1), Count of 1 = 2
Subarray 2: (1, 0), Count of 1 = 1
Then the final matrix for count of 1's will be -
[ 1, 1 ]
[ 2, 1 ]
Similarly, apply the sliding window technique on every column on this matrix, to compute the count of 1’s in every possible sub-matrix and take the maximum out of those counts.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCount(vector<vector< int >> &mat, int k) {
int n = mat.size();
int m = mat[0].size();
vector<vector< int >> a;
for ( int e = 0; e < n; ++e){
vector< int > s = mat[e];
vector< int > q;
int c = 0;
int i;
for (i = 0; i < k; ++i)
if (s[i] == 1)
c += 1;
q.push_back(c);
int p = s[0];
for ( int j = i + 1; j < m; ++j) {
if (s[j] == 1)
c+= 1;
if (p == 1)
c-= 1;
q.push_back(c);
p = s[j-k + 1];
}
a.push_back(q);
}
vector<vector< int >> b;
int max = 0;
for ( int i = 0; i < a[0].size(); ++i) {
int c = 0;
int p = a[0][i];
int j;
for (j = 0; j < k; ++j) {
c+= a[j][i];
}
vector< int > q;
if (c>max)
max = c;
q.push_back(c);
for ( int l = j + 1; j < n; ++j) {
c+= a[l][i];
c-= p;
p = a[l-k + 1][i];
q.push_back(c);
if (c > max)
max = c;
}
b.push_back(q);
}
return max;
}
int main()
{
vector<vector< int >> mat = {{1, 0, 1}, {1, 1, 0}, {0, 1, 0}};
int k = 3;
cout<< maxCount(mat, k);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int maxCount(ArrayList<ArrayList<Integer> > mat, int k)
{
int n = mat.size();
int m = mat.get( 0 ).size();
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
for ( int e = 0 ; e < n; ++e)
{
ArrayList<Integer> s = mat.get(e);
ArrayList<Integer> q = new ArrayList<Integer>();
int c = 0 ;
int i;
for (i = 0 ; i < k; ++i)
{
if (s.get(i) == 1 )
{
c += 1 ;
}
}
q.add(c);
int p = s.get( 0 );
for ( int j = i + 1 ; j < m; ++j)
{
if (s.get(j) == 1 )
{
c += 1 ;
}
if (p == 1 )
{
c -= 1 ;
}
q.add(c);
p = s.get(j - k + 1 );
}
a.add(q);
}
ArrayList<ArrayList<Integer>> b = new ArrayList<ArrayList<Integer>>();
int max = 0 ;
for ( int i = 0 ; i < a.get( 0 ).size(); ++i)
{
int c = 0 ;
int p = a.get( 0 ).get(i);
int j;
for (j = 0 ; j < k; ++j)
{
c += a.get(j).get(i);
}
ArrayList<Integer> q = new ArrayList<Integer>();
if (c > max)
{
max = c;
}
q.add(c);
for ( int l = j + 1 ; j < n; ++j)
{
c += a.get(l).get(i);
c -= p;
p = a.get(l - k + 1 ).get(i);
q.add(c);
if (c > max)
{
max = c;
}
}
b.add(q);
}
return max;
}
public static void main(String[] args)
{
ArrayList<ArrayList<Integer>> mat = new ArrayList<ArrayList<Integer>>();
mat.add( new ArrayList<Integer>(Arrays.asList( 1 , 0 , 1 )));
mat.add( new ArrayList<Integer>(Arrays.asList( 1 , 1 , 0 )));
mat.add( new ArrayList<Integer>(Arrays.asList( 0 , 1 , 0 )));
int k = 3 ;
System.out.println(maxCount(mat, k));
}
}
|
Python3
def maxCount(mat, k):
n, m = len (mat), len (mat[ 0 ])
a = []
for e in range (n):
s = mat[e]
q = []
c = 0
for i in range (k):
if s[i] = = 1 :
c + = 1
q.append(c)
p = s[ 0 ]
for j in range (i + 1 , m):
if s[j] = = 1 :
c + = 1
if p = = 1 :
c - = 1
q.append(c)
p = s[j - k + 1 ]
a.append(q)
b = []
max = 0
for i in range ( len (a[ 0 ])):
c = 0
p = a[ 0 ][i]
for j in range (k):
c + = a[j][i]
q = []
if c> max :
max = c
q.append(c)
for l in range (j + 1 , n):
c + = a[l][i]
c - = p
p = a[l - k + 1 ][i]
q.append(c)
if c > max :
max = c
b.append(q)
return max
if __name__ = = "__main__" :
mat = [[ 1 , 0 , 1 ], [ 1 , 1 , 0 ], [ 0 , 1 , 0 ]]
k = 3
print (maxCount(mat, k))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxCount(List<List< int >> mat, int k)
{
int n = mat.Count;
int m = mat[0].Count;
List<List< int >> a = new List<List< int >>();
for ( int e = 0; e < n; ++e)
{
List< int > s = mat[e];
List< int > q = new List< int >();
int c = 0;
int i;
for (i = 0; i < k; ++i)
{
if (s[i] == 1)
{
c++;
}
}
q.Add(c);
int p = s[0];
for ( int j = i + 1; j < m; ++j)
{
if (s[j] == 1)
{
c++;
}
if (p == 1)
{
c--;
}
q.Add(c);
p = s[j - k + 1];
}
a.Add(q);
}
List<List< int >> b = new List<List< int >>();
int max = 0;
for ( int i = 0; i < a[0].Count; ++i)
{
int c = 0;
int p = a[0][i];
int j;
for (j = 0; j < k; ++j)
{
c += a[j][i];
}
List< int > q = new List< int >();
if (c > max)
{
max = c;
}
q.Add(c);
for ( int l = j + 1; j < n; ++j)
{
c += a[l][i];
c -= p;
p = a[l - k + 1][i];
q.Add(c);
if (c > max)
{
max = c;
}
}
b.Add(q);
}
return max;
}
static public void Main()
{
List<List< int >> mat = new List<List< int >>();
mat.Add( new List< int >(){1, 0, 1});
mat.Add( new List< int >(){1, 1, 0});
mat.Add( new List< int >(){0, 1, 0});
int k = 3;
Console.WriteLine(maxCount(mat, k));
}
}
|
Javascript
<script>
function maxCount(mat,k)
{
let n = mat.length;
let m = mat[0].length;
let a = [];
for (let e = 0; e < n; ++e)
{
let s = mat[e];
let q = [];
let c = 0;
let i;
for (i = 0; i < k; ++i)
{
if (s[i] == 1)
{
c += 1;
}
}
q.push(c);
let p = s[0];
for (let j = i + 1; j < m; ++j)
{
if (s[j] == 1)
{
c += 1;
}
if (p == 1)
{
c -= 1;
}
q.push(c);
p = s[j - k + 1];
}
a.push(q);
}
let b = [];
let max = 0;
for (let i = 0; i < a[0].length; ++i)
{
let c = 0;
let p = a[0][i];
let j;
for (j = 0; j < k; ++j)
{
c += a[j][i];
}
let q = [];
if (c > max)
{
max = c;
}
q.push(c);
for (let l = j + 1; j < n; ++j)
{
c += a[l][i];
c -= p;
p = a[l - k + 1][i];
q.push(c);
if (c > max)
{
max = c;
}
}
b.push(q);
}
return max;
}
let mat=[[1, 0, 1],[1, 1, 0],[0, 1, 0]];
let k = 3;
document.write(maxCount(mat, k));
</script>
|
Performance Analysis:
- Time Complexity: As in the above approach, there are two loops which takes O(N*M) time, Hence the Time Complexity will be O(N*M).
- Space Complexity: As in the above approach, there is extra space used, Hence the space complexity will be O(N).
Approach 2: [Dynamic Programming method] In this technique, we compute the dp[][] matrix using given mat[][] array.In dp[][] array we compute number of 1’s till the index (i,j) using previous dp[][] value and store it in dp[i][j] .
Algorithm :
1) Construct a dp[][] matrix and assign all elements to 0
initial dp[0][0] = mat[0][0]
a) compute first row and column of the dp matrix:
i) for first row:
dp[0][i] = dp[0][i-1] + mat[0][i]
ii) for first column:
dp[i][0] = dp[i-1][0] + mat[i][0]
b) now compute remaining dp matrix from (1,1) to (n,m):
dp[i][j] = mat[i][j] + dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]
2)now, we find the maximum 1's in k X k sub matrix:
a) initially we assign max = dp[k-1][k-1]
b) now first we have to check maximum for k-1 row and k-1 column:
i) for k-1 row:
if dp[k-1][j] - dp[k-1][j-k] > max:
max = dp[k-1][j] - dp[k-1][j-k]
ii) for k-1 column:
if dp[i][k-1] - dp[i-k][k-1] > max:
max = dp[i][k-1] - dp[i-k][k-1]
c) now, we check max for (k to n) row and (k to m) column:
for i from k to n-1:
for j from k to m-1:
if dp[i][j] + dp[i-k][j-k] - dp[i-k][j] - dp[i][j-k] > max:
max = dp[i][j] + dp[i-k][j-k] - dp[i-k][j] - dp[i][j-k]
now just return the max value.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int findMaxK(vector<vector< int >> dp,
int k, int n, int m)
{
int max_ = dp[k - 1][k - 1];
for ( int i = k; i < n; i++)
{
int su = dp[i - k][k - 1];
if (max_ < su)
max_ = su;
}
for ( int j = k; j < m; j++)
{
int su = dp[k - 1][j - k];
if (max_< su)
max_ = su;
}
for ( int i = k; i < n; i++)
{
for ( int j = k; j < m; j++)
{
int su = dp[i][j] +
dp[i - k][j - k] -
dp[i - k][j] -
dp[i][j - k];
if ( max_ < su)
max_ = su;
}
}
return max_;
}
vector<vector< int >> buildDPdp(vector<vector< int >> mat,
int k, int n, int m)
{
vector<vector< int >> dp(n, vector< int >(m, 0));
dp[0][0] = mat[0][0];
for ( int i = 1; i < m; i++)
dp[0][i] += (dp[0][i - 1] + mat[0][i]);
for ( int i = 1; i < n; i++)
dp[i][0] += (dp[i - 1][0] + mat[i][0]);
for ( int i = 1; i < n; i++)
for ( int j = 1; j < m; j++)
dp[i][j] = dp[i - 1][j] +
dp[i][j - 1] +
mat[i][j] -
dp[i - 1][j - 1];
return dp;
}
int maxOneInK(vector<vector< int >> mat, int k)
{
int n = mat.size();
int m = mat[0].size();
vector<vector< int >> dp = buildDPdp(
mat, k, n, m);
return findMaxK(dp, k, n, m);
}
int main()
{
vector<vector< int >> mat = { { 1, 0, 1 },
{ 1, 1, 0 },
{ 0, 1, 0 } };
int k = 3;
cout << maxOneInK(mat, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findMaxK( int [][] dp,
int k, int n, int m)
{
int max_ = dp[k - 1 ][k - 1 ];
for ( int i = k; i < n; i++)
{
int su = dp[i - k][k - 1 ];
if (max_ < su)
max_ = su;
}
for ( int j = k; j < m; j++)
{
int su = dp[k - 1 ][j - k];
if (max_< su)
max_ = su;
}
for ( int i = k; i < n; i++)
{
for ( int j = k; j < m; j++)
{
int su = dp[i][j] +
dp[i - k][j - k] -
dp[i - k][j] -
dp[i][j - k];
if ( max_ < su)
max_ = su;
}
}
return max_;
}
static int [][] buildDPdp( int [][] mat,
int k, int n, int m)
{
int [][] dp= new int [n][m];
dp[ 0 ][ 0 ] = mat[ 0 ][ 0 ];
for ( int i = 1 ; i < m; i++)
dp[ 0 ][i] += (dp[ 0 ][i - 1 ] + mat[ 0 ][i]);
for ( int i = 1 ; i < n; i++)
dp[i][ 0 ] += (dp[i - 1 ][ 0 ] + mat[i][ 0 ]);
for ( int i = 1 ; i < n; i++)
for ( int j = 1 ; j < m; j++)
dp[i][j] = dp[i - 1 ][j] +
dp[i][j - 1 ] +
mat[i][j] -
dp[i - 1 ][j - 1 ];
return dp;
}
static int maxOneInK( int [][] mat, int k)
{
int n = mat.length;
int m = mat[ 0 ].length;
int [][] dp = buildDPdp(
mat, k, n, m);
return findMaxK(dp, k, n, m);
}
public static void main (String[] args)
{
int [][] mat = { { 1 , 0 , 1 },
{ 1 , 1 , 0 },
{ 0 , 1 , 0 } };
int k = 3 ;
System.out.println( maxOneInK(mat, k));
}
}
|
Python3
def findMaxK(dp,k,n,m):
max_ = dp[k - 1 ][k - 1 ]
for i in range (k,n):
su = dp[i - k][k - 1 ]
if max_ < su:
max_ = su
for j in range (k,m):
su = dp[k - 1 ][i - k]
if max_< su:
max_ = su
for i in range (k,n):
for j in range (k,m):
su = dp[i][j] + dp[i - k][j - k] - dp[i - k][j] - dp[i][j - k]
if max_ < su:
max_ = su
return max_
def buildDPdp(mat,k,n,m):
dp = [[ 0 for i in range (m)] for j in range (n)]
dp[ 0 ][ 0 ] = mat[ 0 ][ 0 ]
for i in range ( 1 ,m):
dp[ 0 ][i] + = (dp[ 0 ][i - 1 ] + mat[ 0 ][i])
for i in range ( 1 ,n):
dp[i][ 0 ] + = (dp[i - 1 ][ 0 ] + mat[i][ 0 ])
for i in range ( 1 ,n):
for j in range ( 1 ,m):
dp[i][j] = dp[i - 1 ][j] + dp[i][j - 1 ] + mat[i][j] - dp[i - 1 ][j - 1 ]
return dp
def maxOneInK(mat,k):
n = len (mat)
m = len (mat[ 0 ])
dp = buildDPdp(mat,k,n,m)
return findMaxK(dp,k,n,m)
def main():
mat = [[ 1 , 0 , 1 ], [ 1 , 1 , 0 ], [ 0 , 1 , 0 ]]
k = 3
print (maxOneInK(mat,k))
main()
|
Javascript
function findMaxK(dp, k, n, m)
{
let max_ = dp[k - 1][k - 1];
for (let i = k; i < n; i++)
{
let su = dp[i - k][k - 1];
if (max_ < su)
max_ = su;
}
for (let j = k; j < m; j++)
{
let su = dp[k - 1][j - k];
if (max_< su)
max_ = su;
}
for (let i = k; i < n; i++)
{
for (let j = k; j < m; j++)
{
let su = dp[i][j] +
dp[i - k][j - k] -
dp[i - k][j] -
dp[i][j - k];
if ( max_ < su)
max_ = su;
}
}
return max_;
}
function buildDPdp( mat,k,n,m)
{
let dp= new Array(n);
for (let i=0;i<n;i++)
{
dp[i]= new Array(m);
for (let j=0;j<m;j++)
{
dp[i][j]=0;
}
}
dp[0][0] = mat[0][0];
for (let i = 1; i < m; i++)
dp[0][i] += (dp[0][i - 1] + mat[0][i]);
for (let i = 1; i < n; i++)
dp[i][0] += (dp[i - 1][0] + mat[i][0]);
for (let i = 1; i < n; i++)
for (let j = 1; j < m; j++)
dp[i][j] = dp[i - 1][j] +
dp[i][j - 1] +
mat[i][j] -
dp[i - 1][j - 1];
return dp;
}
function maxOneInK(mat,k)
{
let n = mat.length;
let m = mat[0].length;
let dp = buildDPdp(
mat, k, n, m);
return findMaxK(dp, k, n, m);
}
let mat = [[ 1, 0, 1 ],[ 1, 1, 0 ],
[ 0, 1, 0 ]];
let k = 3;
console.log(maxOneInK(mat, k));
|
C#
using System;
using System.Collections.Generic;
class Program {
static int FindMaxK(List<List< int > > dp, int k, int n,
int m)
{
int max_ = dp[k - 1][k - 1];
for ( int i = k; i < n; i++) {
int su = dp[i - k][k - 1];
if (max_ < su)
max_ = su;
}
for ( int j = k; j < m; j++) {
int su = dp[k - 1][j - k];
if (max_ < su)
max_ = su;
}
for ( int i = k; i < n; i++) {
for ( int j = k; j < m; j++) {
int su = dp[i][j] + dp[i - k][j - k]
- dp[i - k][j] - dp[i][j - k];
if (max_ < su)
max_ = su;
}
}
return max_;
}
static List<List< int > > BuildDPdp(List<List< int > > mat,
int k, int n, int m)
{
List<List< int > > dp = new List<List< int > >();
for ( int i = 0; i < n; i++)
dp.Add( new List< int >( new int [m]));
dp[0][0] = mat[0][0];
for ( int i = 1; i < m; i++)
dp[0][i] += (dp[0][i - 1] + mat[0][i]);
for ( int i = 1; i < n; i++)
dp[i][0] += (dp[i - 1][0] + mat[i][0]);
for ( int i = 1; i < n; i++)
for ( int j = 1; j < m; j++)
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
+ mat[i][j] - dp[i - 1][j - 1];
return dp;
}
static int MaxOneInK(List<List< int > > mat, int k)
{
int n = mat.Count;
int m = mat[0].Count;
List<List< int > > dp = BuildDPdp(mat, k, n, m);
return FindMaxK(dp, k, n, m);
}
static void Main( string [] args)
{
List<List< int > > mat = new List<List< int > >() {
new List< int >() { 1, 0, 1 }, new List< int >() {
1, 1, 0
}, new List< int >() { 0, 1, 0 }
};
int k = 3;
Console.WriteLine(MaxOneInK(mat, k));
}
}
|
Performance Analysis:
- Time Complexity: As in the above Dynamic program approach we have to calculate N X M dp matrix which takes O(N*M) time, Hence the Time Complexity will be O(N*M).
- Space Complexity: As in the above approach, there is extra space used for making dp N X M matrix, Hence the space complexity will be O(N*M).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...