Minimum number of 1s present in a submatrix of given dimensions in a Binary Matrix
Given a 2D binary matrix mat[][] of size N Ă— M and two integers A, B, the task is to find the least number of 1s present in a submatrix of dimensions A Ă— B or B Ă— A.
Examples:
Input:
mat[][] = {{1, 1, 1},
{1, 1, 1},
{1, 1, 1}}
A = 2, B = 1
Output: 2
Explanation: Any submatrix of size 2 X 1 or 1 X 2 will have 2 1s in it.Input:
mat[][] = {{1, 1, 0, 1, 1, 1, 0, 0},
{0, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 0, 0, 1},
{0, 1, 1, 1, 1, 0, 1, 0},
{0, 1, 1, 0, 1, 1, 0, 1},
{0, 1, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 1, 0, 1},
{0, 1, 1, 0, 1, 1, 1, 1},
{0, 1, 1, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 1, 0, 1, 1}}
A = 4, B = 9
Output: 20
Explanation:
Submatrix from (0, 0) to (8, 3) of dimensions 9 Ă— 4 have 20 1s present in it, which is minimum possible for this matrix.
Approach: To solve the problem, the idea is to print all possible submatrices of dimensions A * B and B * A, and for each submatrix, count the number of 1s present in them.
Follow the steps below to solve the given problem:
- Initialize a variable, say minimum, to store the minimum count of 1s.
- Iterate through each cell (i, j) of the matrix mat[][] and for each (i, j):
- If i + A is less than equal to N and j + B is less than equal to M, then perform the following operations:
- Initialize a variable, say count, to store the count of 1s in a submatrix {mat[i][j], mat[i + A][j + B]}.
- Iterate through each cell of the submatrix and store the number of 1s in the variable count.
- Check if the value of count is less than the current minimum. If found to be true, then update minimum equal to count.
- If i + B is less than equal to N and j + A is less than equal to M, then perform the following operations:
- Initialize a variable, say count, to store the count of 1s in the submatrix {mat[i][j], mat[i + B][j + A]}.
- Iterate through each cell of the submatrix and store the number of 1s in count.
- Check if the value of count is less than the current minimum. If found to be true, then update minimum equal to count.
- If i + A is less than equal to N and j + B is less than equal to M, then perform the following operations:
- Finally, print minimum as the final result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; #define P 51 // Function to count number of 1s // present in a sub matrix from // (start_i, start_j) to (end_i, end_j) int count1s( int start_i, int start_j, int end_i, int end_j, int mat[][P]) { // Stores the number of 1s // present in current submatrix int count = 0; // Traverse the submatrix for ( int x = start_i; x < end_i; x++) { for ( int y = start_j; y < end_j; y++) { // If mat[x][y] is equal to 1 if (mat[x][y] == 1) // Increase count by 1 count++; } } // Return the total count of 1s return count; } // Function to find the minimum number of 1s // present in a sub-matrix of size A * B or B * A int findMinimumCount( int N, int M, int A, int B, int mat[][P]) { // Stores the minimum count of 1s int minimum = 1e9; // Iterate i from 0 to N for ( int i = 0; i < N; i++) { // Iterate j from 0 to M for ( int j = 0; j < M; j++) { // If a valid sub matrix of size // A * B from (i, j) is possible if (i + A <= N && j + B <= M) { // Count the number of 1s // present in the sub matrix // of size A * B from (i, j) int count = count1s(i, j, i + A, j + B, mat); // Update minimum if count is // less than the current minimum minimum = min(count, minimum); } // If a valid sub matrix of size // B * A from (i, j) is possible if (i + B <= N && j + A <= M) { // Count the number of 1s in the // sub matrix of size B * A from (i, j) int count = count1s(i, j, i + B, j + A, mat); // Update minimum if count is // less than the current minimum minimum = min(count, minimum); } } } // Return minimum as the final result return minimum; } // Driver Code int main() { // Given Input int A = 2, B = 2; int N = 3, M = 4; int mat[P][P] = { { 1, 0, 1, 0 }, { 0, 1, 0, 1 }, { 1, 0, 1, 0 } }; // Function call to find the minimum number // of 1s in a submatrix of size A * B or B * A cout << findMinimumCount(N, M, A, B, mat); } |
Java
// Java program for the above approach class GFG{ // Function to count number of 1s // present in a sub matrix from // (start_i, start_j) to (end_i, end_j) static int count1s( int start_i, int start_j, int end_i, int end_j, int [][] mat) { // Stores the number of 1s // present in current submatrix int count = 0 ; // Traverse the submatrix for ( int x = start_i; x < end_i; x++) { for ( int y = start_j; y < end_j; y++) { // If mat[x][y] is equal to 1 if (mat[x][y] == 1 ) // Increase count by 1 count++; } } // Return the total count of 1s return count; } // Function to find the minimum number of 1s // present in a sub-matrix of size A * B or B * A static int findMinimumCount( int N, int M, int A, int B, int [][] mat) { // Stores the minimum count of 1s int minimum = ( int ) 1e9; // Iterate i from 0 to N for ( int i = 0 ; i < N; i++) { // Iterate j from 0 to M for ( int j = 0 ; j < M; j++) { // If a valid sub matrix of size // A * B from (i, j) is possible if (i + A <= N && j + B <= M) { // Count the number of 1s // present in the sub matrix // of size A * B from (i, j) int count = count1s(i, j, i + A, j + B, mat); // Update minimum if count is // less than the current minimum minimum = Math.min(count, minimum); } // If a valid sub matrix of size // B * A from (i, j) is possible if (i + B <= N && j + A <= M) { // Count the number of 1s in the // sub matrix of size B * A from (i, j) int count = count1s(i, j, i + B, j + A, mat); // Update minimum if count is // less than the current minimum minimum = Math.min(count, minimum); } } } // Return minimum as the final result return minimum; } // Driver code public static void main(String[] args) { // Given Input int A = 2 , B = 2 ; int N = 3 , M = 4 ; int [][] mat = { { 1 , 0 , 1 , 0 }, { 0 , 1 , 0 , 1 }, { 1 , 0 , 1 , 0 } }; // Function call to find the minimum number // of 1s in a submatrix of size A * B or B * A System.out.println(findMinimumCount(N, M, A, B, mat)); } } // This code is contributed by user_qa7r |
Python3
# Python3 program for the above approach P = 51 # Function to count number of 1s # present in a sub matrix from # (start_i, start_j) to (end_i, end_j) def count1s(start_i, start_j, end_i, end_j, mat): # Stores the number of 1s # present in current submatrix count = 0 # Traverse the submatrix for x in range (start_i, end_i): for y in range (start_j, end_j): # If mat[x][y] is equal to 1 if (mat[x][y] = = 1 ): # Increase count by 1 count + = 1 # Return the total count of 1s return count # Function to find the minimum number of 1s # present in a sub-matrix of size A * B or B * A def findMinimumCount(N, M, A, B, mat): # Stores the minimum count of 1s minimum = 1e9 # Iterate i from 0 to N for i in range (N): # Iterate j from 0 to M for j in range (M): # If a valid sub matrix of size # A * B from (i, j) is possible if (i + A < = N and j + B < = M): # Count the number of 1s # present in the sub matrix # of size A * B from (i, j) count = count1s(i, j, i + A, j + B, mat) # Update minimum if count is # less than the current minimum minimum = min (count, minimum) # If a valid sub matrix of size # B * A from (i, j) is possible if (i + B < = N and j + A < = M): # Count the number of 1s in the # sub matrix of size B * A from (i, j) count = count1s(i, j, i + B, j + A, mat) # Update minimum if count is # less than the current minimum minimum = min (count, minimum) # Return minimum as the final result return minimum # Driver Code if __name__ = = "__main__" : # Given Input A = 2 B = 2 N = 3 M = 4 mat = [[ 1 , 0 , 1 , 0 ], [ 0 , 1 , 0 , 1 ], [ 1 , 0 , 1 , 0 ]] # Function call to find the minimum number # of 1s in a submatrix of size A * B or B * A print (findMinimumCount(N, M, A, B, mat)) # This code is contributed by ukasp. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to count number of 1s // present in a sub matrix from // (start_i, start_j) to (end_i, end_j) static int count1s( int start_i, int start_j, int end_i, int end_j, List<List< int >> mat) { // Stores the number of 1s // present in current submatrix int count = 0; // Traverse the submatrix for ( int x = start_i; x < end_i; x++) { for ( int y = start_j; y < end_j; y++) { // If mat[x][y] is equal to 1 if (mat[x][y] == 1) // Increase count by 1 count++; } } // Return the total count of 1s return count; } // Function to find the minimum number of 1s // present in a sub-matrix of size A * B or B * A static void findMinimumCount( int N, int M, int A, int B, List<List< int >> mat) { // Stores the minimum count of 1s int minimum = 1000000; // Iterate i from 0 to N for ( int i = 0; i < N; i++) { // Iterate j from 0 to M for ( int j = 0; j < M; j++) { // If a valid sub matrix of size // A * B from (i, j) is possible if ((i + A <= N) && (j + B <= M)) { // Count the number of 1s // present in the sub matrix // of size A * B from (i, j) int count = count1s(i, j, i + A, j + B, mat); // Update minimum if count is // less than the current minimum minimum = Math.Min(count, minimum); } // If a valid sub matrix of size // B * A from (i, j) is possible if ((i + B <= N) && (j + A <= M)) { // Count the number of 1s in the // sub matrix of size B * A from (i, j) int count = count1s(i, j, i + B, j + A, mat); // Update minimum if count is // less than the current minimum minimum = Math.Min(count, minimum); } } } // Return minimum as the final result Console.WriteLine(minimum); } // Driver Code public static void Main() { // Given Input int A = 2, B = 2; int N = 3, M = 4; List<List< int >> mat = new List<List< int >>(); mat.Add( new List< int >( new int []{1, 0, 1, 0})); mat.Add( new List< int >( new int []{0, 1, 0, 1})); mat.Add( new List< int >( new int []{1, 0, 1, 0})); // Function call to find the minimum number // of 1s in a submatrix of size A * B or B * A findMinimumCount(N, M, A, B, mat); } } // This code is contributed by ipg2016107 |
Javascript
<script> // Javascript program for the above approach // Function to count number of 1s // present in a sub matrix from // (start_i, start_j) to (end_i, end_j) function count1s(start_i, start_j, end_i, end_j, mat) { // Stores the number of 1s // present in current submatrix let count = 0; // Traverse the submatrix for (let x = start_i; x < end_i; x++) { for (let y = start_j; y < end_j; y++) { // If mat[x][y] is equal to 1 if (mat[x][y] == 1) // Increase count by 1 count++; } } // Return the total count of 1s return count; } // Function to find the minimum number of 1s // present in a sub-matrix of size A * B or B * A function findMinimumCount(N, M, A, B, mat) { // Stores the minimum count of 1s let minimum = 1e9; // Iterate i from 0 to N for (let i = 0; i < N; i++) { // Iterate j from 0 to M for (let j = 0; j < M; j++) { // If a valid sub matrix of size // A * B from (i, j) is possible if (i + A <= N && j + B <= M) { // Count the number of 1s // present in the sub matrix // of size A * B from (i, j) let count = count1s(i, j, i + A, j + B, mat); // Update minimum if count is // less than the current minimum minimum = Math.min(count, minimum); } // If a valid sub matrix of size // B * A from (i, j) is possible if (i + B <= N && j + A <= M) { // Count the number of 1s in the // sub matrix of size B * A from (i, j) let count = count1s(i, j, i + B, j + A, mat); // Update minimum if count is // less than the current minimum minimum = Math.min(count, minimum); } } } // Return minimum as the final result return minimum; } // Driver code // Given Input let A = 2, B = 2; let N = 3, M = 4; let mat = [ [ 1, 0, 1, 0 ], [ 0, 1, 0, 1 ], [ 1, 0, 1, 0 ] ]; // Function call to find the minimum number // of 1s in a submatrix of size A * B or B * A document.write(findMinimumCount(N, M, A, B, mat)); // This code is contributed by target_2 </script> |
2
Time Complexity: O(N2)
Auxiliary Space: O(1)
Please Login to comment...