Maximize Binary Row Values with Row and Column Toggles
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
In the binary rectangular grid, a move flips every bit in one chosen row or one chosen column. Interpret each row as a binary number with its first column most significant. Return the maximum sum of row values after any number of moves, including zero.
```cpp
class Solution {
public:
int matrixScore(vector<vector<int>>& grid);
};
```
### Examples
**Example 1:**
```text
grid = [[0,1],[1,1]]
Output: 5
Explanation: Flip the first row to obtain10 and11; the score is2+3. The second column can have at most one1 once both leading bits are1.
```
**Example 2:**
```text
grid = [[0,0,0]]
Output: 7
Explanation: Flip the sole row to111.
```
### Constraints
- Rectangular dimensions m,n each lie in [1,20].
- Every cell is 0 or 1.
### Contract considerations
Preserve binary place values and arbitrary row/column toggles.
```hint Read the boundary condition
Why is the leading bit worth more than every lower bit combined?
```
### Boundary reminders
Preserve binary place values and arbitrary row/column toggles.
Overview: Maximize Binary Row Values with Row and Column Toggles. In the binary rectangular grid, a move flips every bit in one chosen row or one chosen column.
Maximize Binary Row Values with Row and Column Toggles
Amazon
Jun 26, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
In the binary rectangular grid, a move flips every bit in one chosen row or one chosen column. Interpret each row as a binary number with its first column most significant. Return the maximum sum of row values after any number of moves, including zero.
class Solution {
public:
int matrixScore(vector<vector<int>>& grid);
};
Examples
Example 1:
grid = [[0,1],[1,1]]
Output: 5
Explanation: Flip the first row to obtain10 and11; the score is2+3. The second column can have at most one1 once both leading bits are1.
Example 2:
grid = [[0,0,0]]
Output: 7
Explanation: Flip the sole row to111.
Constraints
Rectangular dimensions m,n each lie in [1,20].
Every cell is 0 or 1.
Contract considerations
Preserve binary place values and arbitrary row/column toggles.
Boundary reminders
Preserve binary place values and arbitrary row/column toggles.