Flip Columns For Maximum Number of Equal Rows in Python

Given a binary matrix of 0s and 1s, we can flip any columns to maximize the number of rows with equal values. Flipping a column changes all 0s to 1s and all 1s to 0s in that column. The goal is to find the maximum number of rows that can have identical values after optimal column flips.

Original Matrix: 0 0 0 0 0 1 1 1 0 Flip cols 1,2 After Flipping: 0 1 1 0 1 1 1 0 0 Rows 2 and 3 are identical! Maximum equal rows: 2

Algorithm Approach

The key insight is that for any row, there are only two possible target patterns after flipping columns: the row itself or its bitwise complement. We check how many rows can match each pattern ?

Implementation

def maxEqualRowsAfterFlips(matrix):
    max_rows = 0
    
    for current_row in matrix:
        count = 0
        # Create complement of current row
        complement = [1 - cell for cell in current_row]
        
        # Count rows that match either pattern
        for row in matrix:
            if row == current_row or row == complement:
                count += 1
        
        max_rows = max(max_rows, count)
    
    return max_rows

# Test with the example
matrix = [[0,0,0], [0,0,1], [1,1,0]]
result = maxEqualRowsAfterFlips(matrix)
print(f"Maximum equal rows: {result}")
Maximum equal rows: 2

How It Works

For the example matrix, let's trace through the algorithm ?

def maxEqualRowsAfterFlips(matrix):
    max_rows = 0
    
    for i, current_row in enumerate(matrix):
        count = 0
        complement = [1 - cell for cell in current_row]
        
        print(f"\nRow {i}: {current_row}")
        print(f"Complement: {complement}")
        
        for j, row in enumerate(matrix):
            if row == current_row:
                print(f"  Row {j} matches original: {row}")
                count += 1
            elif row == complement:
                print(f"  Row {j} matches complement: {row}")
                count += 1
        
        print(f"  Total matching rows: {count}")
        max_rows = max(max_rows, count)
    
    return max_rows

matrix = [[0,0,0], [0,0,1], [1,1,0]]
result = maxEqualRowsAfterFlips(matrix)
print(f"\nFinal answer: {result}")
Row 0: [0, 0, 0]
Complement: [1, 1, 1]
  Row 0 matches original: [0, 0, 0]
  Total matching rows: 1

Row 1: [0, 0, 1]
Complement: [1, 1, 0]
  Row 1 matches original: [0, 0, 1]
  Row 2 matches complement: [1, 1, 0]
  Total matching rows: 2

Row 2: [1, 1, 0]
Complement: [0, 0, 1]
  Row 1 matches complement: [0, 0, 1]
  Row 2 matches original: [1, 1, 0]
  Total matching rows: 2

Final answer: 2

Time Complexity

The algorithm has O(m² × n) time complexity, where m is the number of rows and n is the number of columns. For each row, we compare it with all other rows, and each comparison takes O(n) time.

Conclusion

The problem is solved by recognizing that rows can be made identical by flipping columns if they are either identical or complements of each other. We count matching patterns for each row to find the maximum achievable equal rows.

Updated on: 2026-03-25T08:33:00+05:30

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements