Given two integers and , where . Find the maximum number of one’s in a binary matrix can have such that every sub-matrix of size has atleast one cell as zero.
Input:5 3 Output: Maximum number of ones = 24 The matrix will be: 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 Input:5 2 Output: Maximum number of ones = 21 The matrix will be: 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1
Approach The problem can be solved using a greedy approach. Place a zero at the right-bottom corner of the first square sub-matrix, i.e. the sub-matrix with coordinates (1, 1) and (x, x), and create the rest of the matrix symmetrically, we can get the minimum number of zeros, or, the maximum number of ones. Thus by observing, a common conclusion can be drawn that there are number of zeroes, in the minimum arrangement. The total number of cells available is in a NxN matrix.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
- Find the longest path in a matrix with given constraints
- Find row number of a binary matrix having maximum number of 1s
- Find alphabet in a Matrix which has maximum number of stars around it
- Maximum trace possible for any sub-matrix of the given matrix
- Maximum XOR value in matrix
- Maximum sum of elements from each row in the matrix
- Maximum path sum in matrix
- Pair with maximum sum in a Matrix
- Maximum sum rectangle in a 2D matrix | DP-27
- Find row with maximum sum in a Matrix
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix
- Maximum and Minimum in a square matrix.
- Maximum sum of cocktail glass in a 2D matrix
- Maximum sum of hour glass in matrix
- Sum of all maximum frequency elements in Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.