## Minimum Number of Flips to Convert Binary Matrix into Zero Matrix in JavaIt is a very interesting problem frequently asked in interviews of top IT companies like ## Binary MatrixA boolean matrix is a matrix whose all elements are either 1 or 0. ## Zero MatrixA zero matrix is a matrix whose all elements are zero. Therefore, a zero matrix contains only zero's as its elements are called a ## Problem StatementWe have given a binary matrix of m*n. At a time, we can choose one cell and flip its and all the four neighbors of the matrix if they exist (flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighbors if they share one edge. The task is to return the minimum number of flips required to translate the binary matrix to a zero matrix, else return -1. The above matrix can be converted into a zero matrix in three flips that are (1, 0), (0, 1), and finally (1, 1) as shown above. Let's see some other examples.
## Solution to The ProblemThe problem can be solved by using the following two approaches: - DFS, Backtracking, and Memorization
- BFS, Memorization, and Bit Operation
## Using DFS, Backtracking, and MemorizationIn this approach, for each cell either we flip at all or we flip it once. Note that any cell cannot flip more than one. It ensures 2 Let's understand it through an example. Consider an array say If a flip (from i1 to i2) has been transit before, it means we just entered into a cycle. In this cycle, some flips may be wasted and may be made no state transition progress. In order to overcome the problem, we need a boolean array to track the state if we have visited before. If we have visited the state before, return -1 to indicate the flips belongs to a cycle and is not going to yield a possible result.
- Initialize dp and seen array, dp[0] = 0;
- Begin from the initial state, try each different cell to flip. Now recursively solve (steps i, ii, and iii) all the next states that give the minimum cost among all possible states.
- If we have visited the state before, return
**-1**. It indicates that the previous flip picks are not going to be an answer; - Else compute a state that has a valid result to it. Return the result without re-computing the state.
- Otherwise, we need to compute the state using backtracking. Mark the state as visited. After that, for each possible next state, do a flip that leads to the next state.
- Recursively solve for this next state, then flip the same cell back to finish the backtracking. After solving all the possible next states and updating the current state's result, mark the current state as unvisited to finish the backtracking.
- If we have visited the state before, return
Let's implement the above algorithm in a Java program.
The minimum number of flips required is: 6
The time and space complexity is ## BFS, Memorization, and Bit OperationThe problem can also be solved by using the
- To get the initial state, use bit operation.
- Initially all states to be Integer.MAX_VALUE and the state cost is 0.
- Add initial state to the queue and perform BFS as follows:
- If reaching state is 0, return its bare minimum cost;
- Else, try all the possible flips at different cells by using the XOR operations.
- If the next state is computed for the first time, add it to the queue.
- Return -1 if all states are exhausted without success.
Let's understand it through an example. Min steps => BFS
## Note: Flip the i-th bit of an integer i.e. x^=(1 << i).Let's implement the steps in an algorithm.
Let's implement the above steps in a Java program. ## Java Program to Convert Binary Matrix into Zero Matrix
The minimum number of flips required is: 3 ## ComplexityThe time and space complexity is |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]