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": [
3,
6,
7,
11
],
"k": 8,
"mode": "ceil"
}
Execution steps
Test capacity 6
Test capacity 3
Test capacity 5
Test capacity 4
Computed output
4
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
};