Set Matrix Zero using JavaScript
We are going to solve the "Set Matrix Zeroes" problem using JavaScript. The problem requires modifying a matrix in place such that if an element is 0, its entire row and column are set to 0.
Given a Matrix arr of size M x N, the task is to set all rows and columns to zeroes if a particular element is zero, in constant space complexity.

Set Matrix Zeroes
Examples:
Input: [ [1, 1, 1],
[1, 0, 1],
[1, 1, 1]]
Output: [ [1, 0, 1],
[0, 0, 0],
[1, 0, 1]]
Explanation: one zero is present at cell(2,2), and all the elements in the 2nd row and 2nd column are marked as zeroes.
Input: [[ 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: There are zeroes present in the 1st row at 1st column and 4th column.
Approach
To solve this problem efficiently without using extra space, we can utilize the first row and first column of the matrix to mark which rows and columns need to be set to zero. The main steps involved are:
- Use the first row to mark which columns should be set to zero.
- Use the first column to mark which rows should be set to zero.
- Traverse through the matrix, and for each element matrix[i][j]:
- If matrix[i][j] is 0, set matrix[i][0] and matrix[0][j] to 0 (using the first row and first column as markers).
- Based on the markers in the first row and first column, set the corresponding rows and columns to zero.
Table of Content
Marking First Row and Column
This approach utilizes the first row and first column of the matrix as markers to indicate whether the corresponding row or column should be set to zero. It first scans the matrix to identify the rows and columns containing zeroes, marking them in the first row and first column. Then, it traverses the matrix again, using the markers in the first row and column to set the corresponding rows and columns to zero. This approach performs the operation in-place without using additional data structures, resulting in improved space efficiency.
Example: Implementation to show how to set matrix zero using JavaScript.
function printMatrix(matrix) {
for (let row of matrix) {
console.log(row.join(' '));
}
console.log('\n'); // Add a newline for clarity
}
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowHasZero = false;
let firstColHasZero = false;
// Check if the first row should be zero
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) {
firstRowHasZero = true;
break;
}
}
// Check if the first column should be zero
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) {
firstColHasZero = true;
break;
}
}
// Use first row and first column as markers
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
// Set rows to zero
for (let i = 1; i < m; i++) {
if (matrix[i][0] === 0) {
for (let j = 1; j < n; j++) {
matrix[i][j] = 0;
}
}
}
// Set columns to zero
for (let j = 1; j < n; j++) {
if (matrix[0][j] === 0) {
for (let i = 1; i < m; i++) {
matrix[i][j] = 0;
}
}
}
// Set first row to zero if needed
if (firstRowHasZero) {
for (let j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
// Set first column to zero if needed
if (firstColHasZero) {
for (let i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
}
// Example usage:
const matrix = [
[1, 0, 1],
[1, 1, 1],
[1, 1, 0]
];
console.log('Original Matrix:');
printMatrix(matrix);
setZeroes(matrix);
console.log('After Matrix:');
printMatrix(matrix);
Output
Original Matrix: 1 0 1 1 1 1 1 1 0 After Matrix: 0 0 0 1 0 0 0 0 0
Time complexity: ?(?×?), where ? is the number of rows and ? is the number of columns in the matrix.
Space complexity: ?(1), as we are using only a constant amount of extra space regardless of the input size.
Set Matrix Zeroes using Sets
This approach utilizes sets to track the rows and columns containing zeroes in the matrix. It first traverses the matrix to identify these rows and columns, storing their indices in separate sets. Then, it iterates through the sets to set all elements in the corresponding rows and columns to zero. This approach provides a straightforward solution to the problem of setting matrix zeroes.
Example: Implementation to show how to set matrix zero using JavaScript.
function setMatrixZeroes(matrix) {
const n = matrix.length;
const m = matrix[0].length;
// To store which rows and columns
// are supposed to be marked with zeroes.
const row = new Array(n).fill(0);
const col = new Array(m).fill(0);
// Traverse the matrix using nested loops
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
// If the cell contains zero, mark
// its row and column as zero
if (matrix[i][j] === 0) {
row[i] = 1;
col[j] = 1;
}
}
}
for (let i = 0; i < n; i++) {
let rowString = "";
for (let j = 0; j < m; j++) {
// Set cells to zero if any of
// the row[i] or col[j] is 1
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
rowString += matrix[i][j] + " ";
}
console.log(rowString);
}
}
// Driver Code
const arr = [
[0, 1, 2, 0],
[3, 4, 5, 2],
[1, 3, 1, 5]
];
// Function call
setMatrixZeroes(arr);
function setMatrixZeroes(matrix) {
const n = matrix.length;
const m = matrix[0].length;
// To store which rows and columns
// are supposed to be marked with zeroes.
const row = new Array(n).fill(0);
const col = new Array(m).fill(0);
// Traverse the matrix using nested loops
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
// If the cell contains zero, mark
// its row and column as zero
if (matrix[i][j] === 0) {
row[i] = 1;
col[j] = 1;
}
}
}
for (let i = 0; i < n; i++) {
let rowString = "";
for (let j = 0; j < m; j++) {
// Set cells to zero if any of
// the row[i] or col[j] is 1
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
rowString += matrix[i][j] + " ";
}
console.log(rowString);
}
}
// Driver Code
const arr = [
[0, 1, 2, 0],
[3, 4, 5, 2],
[1, 3, 1, 5]
];
// Function call
setMatrixZeroes(arr);
Output
0 0 0 0 0 4 5 0 0 3 1 0
Time complexity: ?(?×?), where ? is the number of rows and ? is the number of columns in the matrix.
Space complexity: ?(m+n), where ? is the number of rows and ? is the number of columns in the matrix.