Workspace Longest Bitonic Subsequence
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Longest Bitonic Subsequence visualizer

Learn Longest Bitonic Subsequence with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice stocks and increasing subsequences.

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 / 17Execution step
Reference example · JavaScript
Inspect reference input
[
  1,
  11,
  2,
  10,
  4,
  5,
  2,
  1
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let l=a.map(()=>1),r=a.map(()=>1);
for(let i=0;
i<a.length;
i++){
for(let j=0;
j<i;
j++)if(a[j]<a[i])l[i]=Math.max(l[i],l[j]+1);
s("Compute increasing lengths",{
Array:a,Increasing:l
},[i])
}
for(let i=a.length-1;
i>=0;
i--){
for(let j=i+1;
j<a.length;
j++)if(a[j]<a[i])r[i]=Math.max(r[i],r[j]+1);
s("Compute decreasing lengths",{
Array:a,Increasing:l,Decreasing:r
},[i])
}
return Math.max(...l.map((x,i)=>x+r[i]-1))
};

Execution canvas

Active Changed
STEP 1

Compute increasing lengths

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

Array8 items
Increasing8 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · Stocks and increasing subsequences

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,
  11,
  2,
  10,
  4,
  5,
  2,
  1
]

Execution steps

  1. Compute increasing lengths
  2. Compute decreasing lengths

Computed output

6
JavaScript reference implementation
const solve = (a,s)=>{
  let l=a.map(()=>1),r=a.map(()=>1);
  for(let i=0;
  i<a.length;
  i++){
    for(let j=0;
    j<i;
    j++)if(a[j]<a[i])l[i]=Math.max(l[i],l[j]+1);
    s("Compute increasing lengths",{
      Array:a,Increasing:l
    },[i])
  }
  for(let i=a.length-1;
  i>=0;
  i--){
    for(let j=i+1;
    j<a.length;
    j++)if(a[j]<a[i])r[i]=Math.max(r[i],r[j]+1);
    s("Compute decreasing lengths",{
      Array:a,Increasing:l,Decreasing:r
    },[i])
  }
  return Math.max(...l.map((x,i)=>x+r[i]-1))
};

Continue learning

Practice related DSA questions in the same topic.