Skip to content

73. Set Matrix Zeroes #1709

Answered by mah-shamim
mah-shamim asked this question in Q&A
May 21, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to set the entire row and column of any element in a matrix to zero if that element is zero. The challenge is to achieve this in-place with constant space complexity.

Approach

The key idea is to use the first row and first column of the matrix to keep track of which rows and columns need to be zeroed. This allows us to avoid using additional space for storing this information. Here's the step-by-step approach:

  1. Check First Row and Column for Zeros: Determine if the first row or column originally contains any zeros. This information is stored in boolean variables (firstRowZero and firstColZero).

  2. Mark Rows and Columns: Iterate through the matrix starting from the second row and s…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit May 21, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim May 21, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants