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,
2,
4,
8,
9
],
"k": 3
}
Execution steps
Test separation 4
Test separation 2
Test separation 3
Computed output
3
JavaScript reference implementation
const solve = ({
a,k
},s)=>{
a.sort((x,y)=>x-y);
let l=1,r=a.at(-1)-a[0],b=0;
while(l<=r){
let m=(l+r)>>1,p=[a[0]];
for(let x of a.slice(1))if(x-p.at(-1)>=m)p.push(x);
s("Test separation "+m,{
Stalls:a,Placed:p,Bounds:[l,m,r]
});
if(p.length>=k){
b=m;
l=m+1
}else r=m-1
}
return b
};