Back to questionsDSA Visualizers: 388 Coding Questions Set Matrix Zeroes visualizer Learn Set Matrix Zeroes with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice optimization and matrix.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 5 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input [
[
1,
1,
1
],
[
1,
0,
1
],
[
1,
1,
1
]
] Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (m,s)=>{
2 let r=new Set,c=new Set;
3 for (let i=0 ;
4 i<m.length;
5 i++)for (let j=0 ;
6 j<m[i].length;
7 j++)if (!m[i][j]){
8 r.add(i);
9 c.add(j);
10 s("Record zero row and column",{
11 Matrix:m,Rows:[...r],Columns:[...c]
12 })
13 }
14 for (let i=0 ;
15 i<m.length;
16 i++){
17 for (let j=0 ;
18 j<m[i].length;
19 j++)if (r.has(i)||c.has(j))m[i][j]=0 ;
20 s("Apply markers",{
21 Matrix:m
22 })
23 }
24 return m
25 };
Execution canvas Active ChangedSTEP 1 Record zero row and column Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Arrays · Optimization and matrix
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input [
[
1,
1,
1
],
[
1,
0,
1
],
[
1,
1,
1
]
] Execution steps Record zero row and column Apply markers Computed output [
[
1,
0,
1
],
[
0,
0,
0
],
[
1,
0,
1
]
] JavaScript reference implementation const solve = (m,s)=>{
let r=new Set,c=new Set;
for(let i=0;
i<m.length;
i++)for(let j=0;
j<m[i].length;
j++)if(!m[i][j]){
r.add(i);
c.add(j);
s("Record zero row and column",{
Matrix:m,Rows:[...r],Columns:[...c]
})
}
for(let i=0;
i<m.length;
i++){
for(let j=0;
j<m[i].length;
j++)if(r.has(i)||c.has(j))m[i][j]=0;
s("Apply markers",{
Matrix:m
})
}
return m
};Continue learning Practice related DSA questions in the same topic.