Algorithms question flipping columns
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Matrix flipping problems appear frequently in algorithm interviews because they combine greedy reasoning and bitwise intuition. A common version asks for the maximum matrix score when you may flip any column, optionally after normalizing rows. The right approach avoids brute force and runs in linear time over matrix cells.
Problem Setup
Given a binary matrix, you may flip columns. In many formulations, row flips are also allowed and score is computed by reading each row as a binary number. The goal is to maximize total score.
Key greedy insight for score-based variant:
- the leftmost column has highest binary weight
- ensure each row starts with
1by flipping rows if needed - for each remaining column, choose flipped or unflipped state that yields more
1values
This avoids trying all combinations.
Efficient Greedy Algorithm
Assume matrix has m rows and n columns.
- Treat any row starting with
0as logically flipped. - For each column
j, count effective ones under that row-normalization. - Ones for column
jshould bemax(count, m - count). - Add contribution with binary weight
2^(n - 1 - j).
Python implementation:
Complexity is O(m*n) time and O(1) extra space.
Why the Greedy Choice Is Correct
After row normalization, each row has a leading 1. Any solution that leaves a leading 0 can be improved by flipping that row, because the first column weight dominates all lower bits combined.
Once first column is fixed, each remaining column is independent for score contribution. Flipping one column does not affect another column’s count of ones. So the best choice is local: maximize ones in each column individually.
That structure is what makes greedy optimal here.
Brute Force Contrast
Brute force over n columns tries 2^n combinations, which explodes quickly. With row flips included, state space can be even larger if implemented naively.
For m=100 and n=100, brute force is impossible. The greedy method still runs comfortably in a few milliseconds in Python for typical interview constraints.
Variant: Max Equal Rows After Column Flips
Another common variant asks for maximum number of equal rows after flipping columns. That is solved by normalizing each row against its first bit and counting identical normalized patterns.
Do not confuse this variant with score maximization. The objective function changes, so algorithm details differ.
Testing Strategy
Use small deterministic tests first:
- single row
- single column
- all zeros
- all ones
- mixed random matrix
Then cross-check against brute force for tiny sizes to validate implementation correctness.
Comparing greedy and brute on small random inputs is an effective guard against subtle logic mistakes.
Common Pitfalls
- Using brute force even when constraints require near-linear solutions.
- Forgetting that first-column weight dominates lower bits in score variant.
- Mutating matrix unnecessarily when logical flip interpretation is enough.
- Mixing up score-maximization and max-equal-rows variants.
- Incorrect bit weighting when summing column contributions.
Summary
- Flipping-columns matrix score problems are solved efficiently with greedy reasoning.
- Normalize rows conceptually so leading bits are maximized first.
- Then optimize each remaining column independently by majority ones.
- Time complexity is linear in number of matrix cells.
- Distinguish objective variants clearly before choosing an algorithm.

