Submatrix of given size with maximum 1’s

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the 
// maximum count of 1's in 
// submatrix of order K 
#include <bits/stdc++.h> 
using namespace std;
  
// Function to find the maximum 
// count of 1's in the 
// submatrix of order K 
int maxCount(vector<vector<int>> &mat, int k) {
  
    int n = mat.size();
    int m = mat[0].size(); 
    vector<vector<int>> a;
  
    // Loop to find the count of 1's 
    // in every possible windows 
    // of rows of matrix 
    for (int e = 0; e < n; ++e){ 
        vector<int> s = mat[e];
        vector<int> q;
        int    c = 0;
          
        // Loop to find the count of 
        // 1's in the first window 
        int i;
        for (i = 0; i < k; ++i)
            if(s[i] == 1)
                c += 1;
  
        q.push_back(c);
        int p = s[0];
          
        // Loop to find the count of 
        // 1's in the remaining windows 
        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;
      
    // Loop to find the count of 1's 
    // in every possible submatrix 
    for (int i = 0; i < a[0].size(); ++i) { 
        int c = 0;
        int p = a[0][i];
          
        // Loop to find the count of 
        // 1's in the first window 
        int j;
        for (j = 0; j < k; ++j) {
            c+= a[j][i];
        }
        vector<int> q; 
        if (c>max) 
            max = c;
        q.push_back(c);
          
        // Loop to find the count of 
        // 1's in the remaining windows 
        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;
}
  
// Driver code 
int main() 
    vector<vector<int>> mat = {{1, 0, 1}, {1, 1, 0}, {0, 1, 0}};
    int k = 3;
      
    // Function call 
    cout<< maxCount(mat, k);
  
    return 0; 
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation to find the
# maximum count of 1's in 
# submatrix of order K
  
# Function to find the maximum
# count of 1's in the 
# submatrix of order K
def maxCount(mat, k):
    n, m = len(mat), len(mat[0])
    a =[]
      
    # Loop to find the count of 1's
    # in every possible windows 
    # of rows of matrix 
    for e in range(n):
        s = mat[e]
        q =[]
        c = 0
          
        # Loop to find the count of 
        # 1's in the first window
        for i in range(k):
            if s[i] == 1:
                c += 1
        q.append(c)
        p = s[0]
          
        # Loop to find the count of 
        # 1's in the remaining windows
        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
      
    # Loop to find the count of 1's 
    # in every possible submatrix
    for i in range(len(a[0])):
        c = 0
        p = a[0][i]
          
        # Loop to find the count of
        # 1's in the first window
        for j in range(k):
            c+= a[j][i]
        q =[]
        if c>max:
            max = c
        q.append(c)
          
        # Loop to find the count of
        # 1's in the remaining windows
        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
      
# Driver Code
if __name__ == "__main__":
    mat = [[1, 0, 1], [1, 1, 0], [0, 1, 0]]
    k = 3
      
    # Function call
    print(maxCount(mat, k))

chevron_right


Output:

5

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).

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : Sanjit_Prasad