Back to questionsDSA Visualizers: 388 Coding Questions Allocate Books visualizer Learn Allocate Books with a step-by-step Binary Search visualizer, JavaScript source code and a worked example. Practice search on answer.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 8 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
12,
34,
67,
90
],
"k": 2,
"mode": "pack"
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,k,mode
3 },s)=>{
4 let l=mode==="ceil"?1 :Math.max(...a),r=mode==="ceil"?Math.max(...a):a.reduce((x,y)=>x+y,0 );
5 while (l<r){
6 let m=Math.floor((l+r)/2 ),used=0 ;
7 if (mode==="ceil")used=a.reduce((x,y)=>x+Math.ceil(y/m),0 );
8 else {
9 used=1 ;
10 let sum=0 ;
11 for (let x of a){
12 if (sum+x>m){
13 used++;
14 sum=0
15 }
16 sum+=x
17 }
18 }
19 s("Test capacity "+m,{
20 Input:a,Bounds:[l,m,r],Required:[used],Limit:[k]
21 });
22 if (used<=k)r=m;
23 else l=m+1
24 }
25 return l
26 };
Execution canvas Active ChangedSTEP 1 Test capacity 146 Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
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": [
12,
34,
67,
90
],
"k": 2,
"mode": "pack"
} Execution steps Test capacity 146 Test capacity 118 Test capacity 104 Test capacity 111 Test capacity 115 Test capacity 113 Test capacity 112 Computed output 113 JavaScript reference implementation const solve = ({
a,k,mode
},s)=>{
let l=mode==="ceil"?1:Math.max(...a),r=mode==="ceil"?Math.max(...a):a.reduce((x,y)=>x+y,0);
while(l<r){
let m=Math.floor((l+r)/2),used=0;
if(mode==="ceil")used=a.reduce((x,y)=>x+Math.ceil(y/m),0);
else{
used=1;
let sum=0;
for(let x of a){
if(sum+x>m){
used++;
sum=0
}
sum+=x
}
}
s("Test capacity "+m,{
Input:a,Bounds:[l,m,r],Required:[used],Limit:[k]
});
if(used<=k)r=m;
else l=m+1
}
return l
};Continue learning Practice related DSA questions in the same topic.