Cells with Odd Values in a Matrix - Problem

Given an m x n matrix initialized to all zeros, and a 2D array indices where each indices[i] = [ri, ci] represents a location to perform increment operations.

For each location indices[i], do both of the following:

  • Increment all cells in row ri
  • Increment all cells in column ci

Return the number of cells with odd values in the matrix after applying all increment operations.

Input & Output

Example 1 — Basic Case
$ Input: m = 2, n = 3, indices = [[0,1],[1,1]]
Output: 6
💡 Note: Matrix starts as zeros. After [0,1]: increment row 0 and column 1. After [1,1]: increment row 1 and column 1 again. Final matrix [[1,3,1],[1,3,1]] has 6 odd values.
Example 2 — Single Operation
$ Input: m = 2, n = 2, indices = [[1,1]]
Output: 4
💡 Note: After [1,1]: increment row 1 and column 1. Final matrix [[0,1],[1,2]] has 4 cells, but only 2 are odd: positions (0,1) and (1,0). Wait, let me recalculate: [[0,1],[1,2]] has values 0,1,1,2 so 2 odd values.
Example 3 — No Operations
$ Input: m = 2, n = 2, indices = []
Output: 0
💡 Note: No operations performed, matrix remains all zeros, so 0 odd values.

Constraints

  • 1 ≤ m, n ≤ 50
  • 1 ≤ indices.length ≤ 100
  • 0 ≤ ri < m
  • 0 ≤ ci < n

Visualization

Tap to expand
Matrix Odd Cells: Transform Zero Matrix with Increment OperationsInput: m=2, n=3, indices=[[0,1],[1,1]]Initial matrix (all zeros):000000After operations:131131Count odd values:✓ Green cells (value 1): 4 cells✗ Orange cells (value 3): 2 cellsTotal odd cells: 4 + 2 = 6Process: [0,1] increments row 0 and col 1, [1,1] increments row 1 and col 1Output: 6 odd-valued cells
Understanding the Visualization
1
Input
Matrix dimensions m×n and increment indices
2
Process
Each index [r,c] increments entire row r and column c
3
Output
Count cells with odd final values
Key Takeaway
🎯 Key Insight: A cell's value equals row increments + column increments. It's odd when exactly one of these counts is odd.
Asked in
Google 15 Amazon 12 Microsoft 8
125.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen