Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an m x n binary matrix grid.
A row or column is considered palindromic if its values read the same forward and backward.
You can flip any number of cells in grid from 0 to 1, or from 1 to 0.
Return the minimum number of cells that need to be flipped to make all rows and columns palindromic, and the total number of 1's in grid divisible by 4.
Example 1:
Input: grid = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Explanation:
Example 2:
Input: grid = [[0,1],[0,1],[0,0]]
Output: 2
Explanation:
Example 3:
Input: grid = [[1],[1]]
Output: 2
Explanation:
Constraints:
m == grid.lengthn == grid[i].length1 <= m * n <= 2 * 1050 <= grid[i][j] <= 1Problem summary: You are given an m x n binary matrix grid. A row or column is considered palindromic if its values read the same forward and backward. You can flip any number of cells in grid from 0 to 1, or from 1 to 0. Return the minimum number of cells that need to be flipped to make all rows and columns palindromic, and the total number of 1's in grid divisible by 4.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[[1,0,0],[0,1,0],[0,0,1]]
[[0,1],[0,1],[0,0]]
[[1],[1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3240: Minimum Number of Flips to Make Binary Grid Palindromic II
class Solution {
public int minFlips(int[][] grid) {
int m = grid.length, n = grid[0].length;
int ans = 0;
for (int i = 0; i < m / 2; ++i) {
for (int j = 0; j < n / 2; ++j) {
int x = m - i - 1, y = n - j - 1;
int cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y];
ans += Math.min(cnt1, 4 - cnt1);
}
}
if (m % 2 == 1 && n % 2 == 1) {
ans += grid[m / 2][n / 2];
}
int diff = 0, cnt1 = 0;
if (m % 2 == 1) {
for (int j = 0; j < n / 2; ++j) {
if (grid[m / 2][j] == grid[m / 2][n - j - 1]) {
cnt1 += grid[m / 2][j] * 2;
} else {
diff += 1;
}
}
}
if (n % 2 == 1) {
for (int i = 0; i < m / 2; ++i) {
if (grid[i][n / 2] == grid[m - i - 1][n / 2]) {
cnt1 += grid[i][n / 2] * 2;
} else {
diff += 1;
}
}
}
ans += cnt1 % 4 == 0 || diff > 0 ? diff : 2;
return ans;
}
}
// Accepted solution for LeetCode #3240: Minimum Number of Flips to Make Binary Grid Palindromic II
func minFlips(grid [][]int) int {
m, n := len(grid), len(grid[0])
ans := 0
for i := 0; i < m/2; i++ {
for j := 0; j < n/2; j++ {
x, y := m-i-1, n-j-1
cnt1 := grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]
ans += min(cnt1, 4-cnt1)
}
}
if m%2 == 1 && n%2 == 1 {
ans += grid[m/2][n/2]
}
diff, cnt1 := 0, 0
if m%2 == 1 {
for j := 0; j < n/2; j++ {
if grid[m/2][j] == grid[m/2][n-j-1] {
cnt1 += grid[m/2][j] * 2
} else {
diff += 1
}
}
}
if n%2 == 1 {
for i := 0; i < m/2; i++ {
if grid[i][n/2] == grid[m-i-1][n/2] {
cnt1 += grid[i][n/2] * 2
} else {
diff += 1
}
}
}
if cnt1%4 == 0 || diff > 0 {
ans += diff
} else {
ans += 2
}
return ans
}
# Accepted solution for LeetCode #3240: Minimum Number of Flips to Make Binary Grid Palindromic II
class Solution:
def minFlips(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m // 2):
for j in range(n // 2):
x, y = m - i - 1, n - j - 1
cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y]
ans += min(cnt1, 4 - cnt1)
if m % 2 and n % 2:
ans += grid[m // 2][n // 2]
diff = cnt1 = 0
if m % 2:
for j in range(n // 2):
if grid[m // 2][j] == grid[m // 2][n - j - 1]:
cnt1 += grid[m // 2][j] * 2
else:
diff += 1
if n % 2:
for i in range(m // 2):
if grid[i][n // 2] == grid[m - i - 1][n // 2]:
cnt1 += grid[i][n // 2] * 2
else:
diff += 1
ans += diff if cnt1 % 4 == 0 or diff else 2
return ans
// Accepted solution for LeetCode #3240: Minimum Number of Flips to Make Binary Grid Palindromic II
/**
* [3240] Minimum Number of Flips to Make Binary Grid Palindromic II
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_flips(grid: Vec<Vec<i32>>) -> i32 {
let (m, n) = (grid.len(), grid[0].len());
let mut result = 0;
// For the kernel
for i in 0..m / 2 {
for j in 0..n / 2 {
let mut one_count = 0;
if grid[i][j] == 1 {
one_count += 1;
}
if grid[m - 1 - i][j] == 1 {
one_count += 1;
}
if grid[i][n - 1 - j] == 1 {
one_count += 1;
}
if grid[m - 1 - i][n - 1 - j] == 1 {
one_count += 1;
}
result += one_count.min(4 - one_count);
}
}
let mut one_count = 0;
let mut flip_count = 0;
if m % 2 == 1 {
for i in 0..n / 2 {
if grid[m / 2][i] != grid[m / 2][n - 1 - i] {
flip_count += 1;
} else {
if grid[m / 2][i] == 1 {
one_count += 2;
}
}
}
}
if n % 2 == 1 {
for i in 0..m / 2 {
if grid[i][n / 2] != grid[m - 1 - i][n / 2] {
flip_count += 1;
} else {
if grid[i][n / 2] == 1 {
one_count += 2;
}
}
}
}
if one_count % 4 == 2 {
if flip_count == 0 {
result += 2;
}
}
result += flip_count;
if m % 2 == 1 && n % 2 == 1 {
if grid[m / 2][n / 2] == 1 {
result += 1;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3240() {
assert_eq!(2, Solution::min_flips(vec![vec![0, 0, 1], vec![1, 1, 1]]));
assert_eq!(
2,
Solution::min_flips(vec![vec![0, 1], vec![0, 1], vec![0, 0]])
);
assert_eq!(
3,
Solution::min_flips(vec![vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1]])
);
assert_eq!(2, Solution::min_flips(vec![vec![1], vec![1]]));
}
}
// Accepted solution for LeetCode #3240: Minimum Number of Flips to Make Binary Grid Palindromic II
function minFlips(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
let ans = 0;
for (let i = 0; i < Math.floor(m / 2); i++) {
for (let j = 0; j < Math.floor(n / 2); j++) {
const x = m - i - 1;
const y = n - j - 1;
const cnt1 = grid[i][j] + grid[x][j] + grid[i][y] + grid[x][y];
ans += Math.min(cnt1, 4 - cnt1);
}
}
if (m % 2 === 1 && n % 2 === 1) {
ans += grid[Math.floor(m / 2)][Math.floor(n / 2)];
}
let diff = 0,
cnt1 = 0;
if (m % 2 === 1) {
for (let j = 0; j < Math.floor(n / 2); j++) {
if (grid[Math.floor(m / 2)][j] === grid[Math.floor(m / 2)][n - j - 1]) {
cnt1 += grid[Math.floor(m / 2)][j] * 2;
} else {
diff += 1;
}
}
}
if (n % 2 === 1) {
for (let i = 0; i < Math.floor(m / 2); i++) {
if (grid[i][Math.floor(n / 2)] === grid[m - i - 1][Math.floor(n / 2)]) {
cnt1 += grid[i][Math.floor(n / 2)] * 2;
} else {
diff += 1;
}
}
}
ans += cnt1 % 4 === 0 || diff > 0 ? diff : 2;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.