Flip Columns For Maximum Number of Equal Rows

MediumMatrixhashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an m x n binary matrix matrix. You may choose any set of columns and toggle every bit in each chosen column. After flipping, a row is *uniform* if all of its values are equal (all 0s or all 1s). Return the maximum number of uniform rows you can obtain with a single choice of columns to flip.

Examples

Input: matrix = [[0,1],[1,1]]
Output: 1
Without flipping, row 1 is uniform. No choice makes both rows uniform.
Input: matrix = [[0,1],[1,0]]
Output: 2
Flip column 1 to get [[0,0],[1,1]].
Input: matrix = [[0,0,0],[0,0,1],[1,1,0]]
Output: 2
Flip the last column to get [[0,0,1],[0,0,0],[1,1,1]]; rows 1 and 2 are uniform.

Constraints

  • `1 <= m, n <= 300`
  • `matrix[i][j]` is `0` or `1`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems