Workspace Search A 2d Matrix Ii
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Search a 2D Matrix II visualizer

Learn Search a 2D Matrix II with a step-by-step Binary Search visualizer, JavaScript source code and a worked example. Practice matrices.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 5Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    [
      1,
      4,
      7
    ],
    [
      2,
      5,
      8
    ],
    [
      3,
      6,
      9
    ]
  ],
  "k": 6
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,k
},s)=>{
let i=0,j=a[0].length-1;
while(i<a.length&&j>=0){
s("Eliminate row or column",{
Matrix:a,Cell:[i,j]
});
if(a[i][j]===k)return true;
if(a[i][j]>k)j--;
else i++
}
return false
};

Execution canvas

Active Changed
STEP 1

Eliminate row or column

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Matrix3 rows
Cell2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

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,
      4,
      7
    ],
    [
      2,
      5,
      8
    ],
    [
      3,
      6,
      9
    ]
  ],
  "k": 6
}

Execution steps

  1. Eliminate row or column

Computed output

true
JavaScript reference implementation
const solve = ({
  a,k
},s)=>{
  let i=0,j=a[0].length-1;
  while(i<a.length&&j>=0){
    s("Eliminate row or column",{
      Matrix:a,Cell:[i,j]
    });
    if(a[i][j]===k)return true;
    if(a[i][j]>k)j--;
    else i++
  }
  return false
};

Continue learning

Practice related DSA questions in the same topic.