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.
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
Count bouquets at day 5
Count bouquets at day 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
};