ExamAdda Logo

Set Matrix Zeroes

Easy

Given an m x n integer matrix, if an element is 0, set its entire row and column to 0.

You must modify the matrix in-place.

Example 1

Input

matrix = [[1,1,1],
          [1,0,1],
          [1,1,1]]

Output

[[1,0,1],
 [0,0,0],
 [1,0,1]]

Explanation

The middle element is 0. Therefore, its entire row and column are changed to 0.

Example 2

Input

matrix = [[0,1,2,0],
          [3,4,5,2],
          [1,3,1,5]]

Output

[[0,0,0,0],
 [0,4,5,0],
 [0,3,1,0]]

Explanation

The zeros are present in the first row at columns 0 and 3. Therefore, the corresponding rows and columns are set to zero.0

Constraints

Array
Matrix
In-Place

Hints:

Hint 1

First identify which rows and columns contain zeroes before modifying the matrix.
 

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 18, 2026

Last updated

Aug 18, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output