Workspace Minimum Days To Make Bouquets
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Minimum Days to Make Bouquets visualizer

Learn Minimum Days to Make Bouquets with a step-by-step Binary Search visualizer, JavaScript source code and a worked example. Practice search on answer.

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 / 4Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    1,
    10,
    3,
    10,
    2
  ],
  "m": 3,
  "k": 1
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,m,k
},s)=>{
if(m*k>a.length)return -1;
let l=Math.min(...a),r=Math.max(...a);
while(l<r){
let x=(l+r)>>1,b=0,run=0;
for(let v of a){
run=v<=x?run+1:0;
if(run===k){
b++;
run=0
}
}
s("Count bouquets at day "+x,{
Flowers:a,Bounds:[l,x,r],Bouquets:[b]
});
if(b>=m)r=x;
else l=x+1
}
return l
};

Execution canvas

Active Changed
STEP 1

Count bouquets at day 5

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

Flowers5 items
Bounds3 items
Bouquets1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Binary Search · Search on answer

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,
    10,
    3,
    10,
    2
  ],
  "m": 3,
  "k": 1
}

Execution steps

  1. Count bouquets at day 5
  2. Count bouquets at day 3
  3. Count bouquets at day 2

Computed output

3
JavaScript reference implementation
const solve = ({
  a,m,k
},s)=>{
  if(m*k>a.length)return -1;
  let l=Math.min(...a),r=Math.max(...a);
  while(l<r){
    let x=(l+r)>>1,b=0,run=0;
    for(let v of a){
      run=v<=x?run+1:0;
      if(run===k){
        b++;
        run=0
      }
    }
    s("Count bouquets at day "+x,{
      Flowers:a,Bounds:[l,x,r],Bouquets:[b]
    });
    if(b>=m)r=x;
    else l=x+1
  }
  return l
};

Continue learning

Practice related DSA questions in the same topic.