Workspace Find Peak Element Ii
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Find Peak Element II visualizer

Learn Find Peak Element 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 / 2Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    1,
    4
  ],
  [
    3,
    2
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let l=0,r=a[0].length-1;
while(l<=r){
let j=(l+r)>>1,i=0;
for(let k=1;
k<a.length;
k++)if(a[k][j]>a[i][j])i=k;
let x=a[i][j],left=a[i][j-1]??-Infinity,right=a[i][j+1]??-Infinity;
s("Find maximum in column",{
Matrix:a,Cell:[i,j]
});
if(x>left&&x>right)return [i,j];
if(left>x)r=j-1;
else l=j+1
}
return []
};

Execution canvas

Active Changed
STEP 1

Find maximum in column

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

Matrix2 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

[
  [
    1,
    4
  ],
  [
    3,
    2
  ]
]

Execution steps

  1. Find maximum in column

Computed output

[
  1,
  0
]
JavaScript reference implementation
const solve = (a,s)=>{
  let l=0,r=a[0].length-1;
  while(l<=r){
    let j=(l+r)>>1,i=0;
    for(let k=1;
    k<a.length;
    k++)if(a[k][j]>a[i][j])i=k;
    let x=a[i][j],left=a[i][j-1]??-Infinity,right=a[i][j+1]??-Infinity;
    s("Find maximum in column",{
      Matrix:a,Cell:[i,j]
    });
    if(x>left&&x>right)return [i,j];
    if(left>x)r=j-1;
    else l=j+1
  }
  return []
};

Continue learning

Practice related DSA questions in the same topic.