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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
There is an m x n matrix that is initialized to all 0's. There is also a 2D array indices where each indices[i] = [ri, ci] represents a 0-indexed location to perform some increment operations on the matrix.
For each location indices[i], do both of the following:
ri.ci.Given m, n, and indices, return the number of odd-valued cells in the matrix after applying the increment to all locations in indices.
Example 1:
Input: m = 2, n = 3, indices = [[0,1],[1,1]] Output: 6 Explanation: Initial matrix = [[0,0,0],[0,0,0]]. After applying first increment it becomes [[1,2,1],[0,1,0]]. The final matrix is [[1,3,1],[1,3,1]], which contains 6 odd numbers.
Example 2:
Input: m = 2, n = 2, indices = [[1,1],[0,0]] Output: 0 Explanation: Final matrix = [[2,2],[2,2]]. There are no odd numbers in the final matrix.
Constraints:
1 <= m, n <= 501 <= indices.length <= 1000 <= ri < m0 <= ci < nFollow up: Could you solve this in O(n + m + indices.length) time with only O(n + m) extra space?
Problem summary: There is an m x n matrix that is initialized to all 0's. There is also a 2D array indices where each indices[i] = [ri, ci] represents a 0-indexed location to perform some increment operations on the matrix. For each location indices[i], do both of the following: Increment all the cells on row ri. Increment all the cells on column ci. Given m, n, and indices, return the number of odd-valued cells in the matrix after applying the increment to all locations in indices.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
2 3 [[0,1],[1,1]]
2 2 [[1,1],[0,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1252: Cells with Odd Values in a Matrix
class Solution {
public int oddCells(int m, int n, int[][] indices) {
int[][] g = new int[m][n];
for (int[] e : indices) {
int r = e[0], c = e[1];
for (int i = 0; i < m; ++i) {
g[i][c]++;
}
for (int j = 0; j < n; ++j) {
g[r][j]++;
}
}
int ans = 0;
for (int[] row : g) {
for (int v : row) {
ans += v % 2;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1252: Cells with Odd Values in a Matrix
func oddCells(m int, n int, indices [][]int) int {
g := make([][]int, m)
for i := range g {
g[i] = make([]int, n)
}
for _, e := range indices {
r, c := e[0], e[1]
for i := 0; i < m; i++ {
g[i][c]++
}
for j := 0; j < n; j++ {
g[r][j]++
}
}
ans := 0
for _, row := range g {
for _, v := range row {
ans += v % 2
}
}
return ans
}
# Accepted solution for LeetCode #1252: Cells with Odd Values in a Matrix
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
g = [[0] * n for _ in range(m)]
for r, c in indices:
for i in range(m):
g[i][c] += 1
for j in range(n):
g[r][j] += 1
return sum(v % 2 for row in g for v in row)
// Accepted solution for LeetCode #1252: Cells with Odd Values in a Matrix
struct Solution;
impl Solution {
fn odd_cells(n: i32, m: i32, indices: Vec<Vec<i32>>) -> i32 {
let n = n as usize;
let m = m as usize;
let mut a = vec![vec![0; m]; n];
let mut res = 0;
for p in indices {
let r = p[0] as usize;
let c = p[1] as usize;
for j in 0..m {
a[r][j] += 1;
if a[r][j] % 2 == 1 {
res += 1;
} else {
res -= 1;
}
}
for i in 0..n {
a[i][c] += 1;
if a[i][c] % 2 == 1 {
res += 1;
} else {
res -= 1;
}
}
}
res
}
}
#[test]
fn test() {
let n = 3;
let m = 3;
let indices: Vec<Vec<i32>> = vec_vec_i32![[0, 1], [1, 1]];
assert_eq!(Solution::odd_cells(n, m, indices), 6);
let n = 2;
let m = 2;
let indices: Vec<Vec<i32>> = vec_vec_i32![[1, 1], [0, 0]];
assert_eq!(Solution::odd_cells(n, m, indices), 0);
}
// Accepted solution for LeetCode #1252: Cells with Odd Values in a Matrix
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1252: Cells with Odd Values in a Matrix
// class Solution {
// public int oddCells(int m, int n, int[][] indices) {
// int[][] g = new int[m][n];
// for (int[] e : indices) {
// int r = e[0], c = e[1];
// for (int i = 0; i < m; ++i) {
// g[i][c]++;
// }
// for (int j = 0; j < n; ++j) {
// g[r][j]++;
// }
// }
// int ans = 0;
// for (int[] row : g) {
// for (int v : row) {
// ans += v % 2;
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.