The binary flip matrix problem in Python

Problem statement

In this binary flip matrix problem, we’re provided with an initial and target binary matrix. The task is to determine if the initial matrix can be transformedMatrix transformation refers to the process of applying a function or operation to each element of a matrix, altering its values or structure according to predefined rules. into the target matrix using row and column flipsA flip refers to changing the cell's value from 0 to 1 and vice versa..

The transformation is shown in the illustration below:

Our initial and target matrices
Our initial and target matrices

We will define two temporary variables that store the total number of 1s in both matrices. If they don’t match, they aren’t transformable. In the context of the binary flip matrix problem, a bit (or a binary digit, 0 or 1) will be flipped when:

  1. Row Flip Condition: When performing a row flip, each bit in the row will change from 0 to 1 or from 1 to 0.

  2. Column Flip Condition: When performing a column flip, the bit at a specific position within each row will change from 0 to 1 or 1 to 0.

Coding solution

In this solution, we’ll transform a binary matrix by flipping rows and columns. This is done by checking if the sum of 1s in both matrices is either both even or odd. We’ll then iterate through the rows and columns, flipping them to match the target matrix. If the target matrix is reached, it returns True, otherwise, False is returned.

Run the code below to see the output:

def flipmatrix(matrix, indices, isrow):
for i in indices:
if isrow:
matrix[i] = [1 - cell for cell in matrix[i]]
else:
for row in matrix:
row[i] = 1 - row[i]
def transformable(start, end):
temp1 = sum(sum(row) for row in start) % 2
temp2 = sum(sum(row) for row in end) % 2
if temp1 != temp2:
return False
for i in range(len(start)):
if start[i] != end[i]:
flipmatrix(start, [i], isrow=True)
for j in range(len(start[0])):
if [row[j] for row in start] != [row[j] for row in end]:
flipmatrix(start, [j], isrow=False)
return start == end

The code is explained below:

  • Line 1: We define a function called flipmatrix that has three parameters:

    • matrix: The matrix to flip.

    • indices: The indices to flip (in list form).

    • isrow: A boolean that indicates whether to perform row flips.

  • Line 2: We traverse the matrix.

  • Lines 3–4: We check whether row flips should be performed (changing 1 to 0 and vice versa).

  • Lines 6–7: We perform column flips if conditions are unmet.

  • Line 9: We define a function called transformable that has two parameters:

    • start: The initial matrix we start with.

    • end: The matrix we aim to transform to.

  • Lines 10–14: We define two temporary variables that store the total number of 1s in the matrices. If they don’t match, they aren’t transformable.

  • Lines 16–18: We iterate over the rows in the start matrix. If the specific index isn’t equal to its counterpart in the end matrix, call the flipmatrix function.

  • Lines 16–18: We iterate over the columns in the start matrix. We perform the same checks as in the previous loops and call the flipmatrix function if the index values don’t match.

  • Line 24: We return whether the matrix has been successfully transformed.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved