#1252
Easy Algorithms Cells with odd values in a matrix
Array Math Simulation
79.7% acceptance
Feb 25, 2026
1335
1560
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn odd_cells(m: i32, n: i32, indices: Vec<Vec<i32>>) -> i32 {
let m = m as usize;
let n = n as usize;
let mut row_parity = vec![0usize; m];
let mut col_parity = vec![0usize; n];
for idx in &indices {
row_parity[idx[0] as usize] += 1;
col_parity[idx[1] as usize] += 1;
}
let odd_rows = row_parity.iter().filter(|&&r| r % 2 == 1).count();
let odd_cols = col_parity.iter().filter(|&&c| c % 2 == 1).count();
let even_rows = m - odd_rows;
let even_cols = n - odd_cols;
// Cell (r, c) is odd if row_parity[r] + col_parity[c] is odd
// = (odd_row, even_col) or (even_row, odd_col)
(odd_rows * even_cols + even_rows * odd_cols) as i32
}
}