Flip Columns For Maximum Number of Equal Rows - Problem

You are given an m x n binary matrix matrix. You can choose any number of columns in the matrix and flip every cell in that column (i.e., change the value of the cell from 0 to 1 or vice versa).

Return the maximum number of rows that have all values equal after some number of flips.

A row has all values equal if all cells in that row are either 0 or 1.

Input & Output

Example 1 — Basic Case
$ Input: matrix = [[0,1],[1,1]]
Output: 1
💡 Note: We can flip column 0 to make row 1 become [1,1] (uniform), or flip column 1 to make row 1 become [0,0] (uniform). Either way, maximum 1 row can be uniform.
Example 2 — Multiple Uniform Rows
$ Input: matrix = [[0,1],[1,0]]
Output: 2
💡 Note: Flipping column 0 makes both rows uniform: [1,1] and [0,0]. Both patterns [0,1] and [1,0] are complements, so they can both become uniform with the same flip.
Example 3 — Already Uniform
$ Input: matrix = [[0,0,0],[0,0,1],[1,1,0]]
Output: 2
💡 Note: Row 1 is already uniform. Flipping columns 0 and 2 makes rows 2 and 3 become [1,1,1] and [0,0,1]. We can make at most 2 rows uniform.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 ≤ m, n ≤ 300
  • matrix[i][j] is either 0 or 1

Visualization

Tap to expand
Flip Columns For Maximum Number of Equal RowsInput Matrix0111Row 1: MixedRow 2: UniformFlip Column 0After Flipping1101Row 1: Uniform ✓Row 2: MixedResult: 1Maximum uniform rowsafter optimal flips
Understanding the Visualization
1
Input Matrix
Binary matrix with rows that may have mixed 0s and 1s
2
Column Flips
Choose columns to flip (0→1, 1→0) to maximize uniform rows
3
Count Uniform
Count rows that become all 0s or all 1s
Key Takeaway
🎯 Key Insight: Rows with complementary bit patterns can both become uniform with the same column flips
Asked in
Google 8 Amazon 5
26.7K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen