09 Sept 2026Go / Python / TypeScriptEasy

Cells with Odd Values in a Matrix

Count odd matrix cells after incrementing selected rows and columns.

Only increment parity matters. Toggle row and column parity for each operation, then count cells whose two parities differ.

complexity

O(k + nm) time and O(n + m) space.

solution files

  • Go cells-with-odd-values-in-a-matrix/solution.go
  • Python cells-with-odd-values-in-a-matrix/solution.py
  • TypeScript cells-with-odd-values-in-a-matrix/solution.ts

Solution files

Gocells-with-odd-values-in-a-matrix/solution.go
package main

func oddCells(m int, n int, indices [][]int) int {
	rows, columns := make([]bool, m), make([]bool, n)
	for _, index := range indices {
		rows[index[0]] = !rows[index[0]]
		columns[index[1]] = !columns[index[1]]
	}
	odd := 0
	for _, row := range rows {
		for _, column := range columns {
			if row != column {
				odd++
			}
		}
	}
	return odd
}
Pythoncells-with-odd-values-in-a-matrix/solution.py
class Solution:
    def oddCells(self, m: int, n: int, indices: list[list[int]]) -> int:
        rows, columns = [False] * m, [False] * n
        for row, column in indices: rows[row] = not rows[row]; columns[column] = not columns[column]
        return sum(row != column for row in rows for column in columns)
TypeScriptcells-with-odd-values-in-a-matrix/solution.ts
function oddCells(m: number, n: number, indices: number[][]): number {
  const rows = new Array<boolean>(m).fill(false); const columns = new Array<boolean>(n).fill(false);
  for (const [row, column] of indices) { rows[row] = !rows[row]; columns[column] = !columns[column]; }
  let odd = class="syntax-number">0; for (const row of rows) for (const column of columns) if (row !== column) odd += class="syntax-number">1; return odd;
}