Given a binary matrix of N rows and M columns. The operation allowed on the matrix is to choose any index (x, y) and toggle all the elements between the rectangle having top-left as (0, 0) and bottom-right as (x-1, y-1). Toggling the element means changing 1 to 0 and 0 to 1. The task is to find minimum operations required to make set all the elements of the matrix i.e make all elements as 1.
Input : mat = 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 Output : 1 In one move, choose (3, 3) to make the whole matrix consisting of only 1s. Input : mat = 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 Output : 3
The idea is to start from the end point (N – 1, M – 1) and traverse the matrix in reverse order. Whenever we encounter a cell which has a value of 0, flip it.
Why traversing from end point ?
Suppose there are 0 at (x, y) and (x + 1, y + 1) cell. You shouldn’t flip a cell (x + 1, y + 1) after cell (x, y) because after you flipped (x, y) to 1, in next move to flip (x + 1, y + 1) cell, you will flip again (x, y) to 0. So there is no benefit from the first move for flipping (x, y) cell.
Below is implementation of this approach:
Time Complexity: O(N2 * M2).
Space Complexity: O(N*M).
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum number of operations required to set all elements of a binary matrix
- Minimum operations required to make each row and column of matrix equals
- Minimum operations of given type to make all elements of a matrix equal
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum flip required to make Binary Matrix symmetric
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Find minimum steps required to reach the end of a matrix | Set - 1
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum steps required to reach the end of a matrix | Set 2
- Minimum changes required to make each path in a matrix palindrome
- Minimum increments required to make given matrix palindromic
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
- Minimize steps required to move all 1's in a matrix to a given index
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Sum of all minimum frequency elements in Matrix
- Find minimum moves to bring all elements in one cell of a matrix
- Find all matrix elements which are minimum in their row and maximum in their column
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Minimum queens required to cover all the squares of a chess board