Back to questionsDSA Visualizers: 388 Coding Questions Search a 2D Matrix visualizer Learn Search a 2D Matrix with a step-by-step Binary Search visualizer, JavaScript source code and a worked example. Practice matrices.
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 / 3 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
[
1,
3,
5
],
[
7,
9,
11
]
],
"k": 9
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,k
3 },s)=>{
4 let n=a[0 ].length,l=0 ,r=a.length*n-1 ;
5 while (l<=r){
6 let m=(l+r)>>1 ,i=Math.floor(m/n),j=m%n,x=a[i][j];
7 s("Probe flattened midpoint",{
8 Matrix:a,Cell:[i,j],Bounds:[l,m,r]
9 });
10 if (x===k)return true ;
11 if (x<k)l=m+1 ;
12 else r=m-1
13 }
14 return false
15 };
Execution canvas Active ChangedSTEP 1 Probe flattened midpoint Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Binary Search · Matrices
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 {
"a": [
[
1,
3,
5
],
[
7,
9,
11
]
],
"k": 9
} Execution steps Probe flattened midpoint Computed output true JavaScript reference implementation const solve = ({
a,k
},s)=>{
let n=a[0].length,l=0,r=a.length*n-1;
while(l<=r){
let m=(l+r)>>1,i=Math.floor(m/n),j=m%n,x=a[i][j];
s("Probe flattened midpoint",{
Matrix:a,Cell:[i,j],Bounds:[l,m,r]
});
if(x===k)return true;
if(x<k)l=m+1;
else r=m-1
}
return false
};Continue learning Practice related DSA questions in the same topic.