Complete the code to initialize the low boundary for binary search.
let low = [1];We start binary search from 0 as the lowest possible answer.
Complete the code to calculate the middle point in binary search.
let mid = (low [1] high) / 2;
We add low and high, then divide by 2 to find the middle.
Fix the error in the condition to update the high boundary.
if (canAchieve(mid)) { high = [1]; } else { low = mid + 1; }
If mid is achievable, we move high to mid to search lower values.
Fill both blanks to complete the binary search loop condition and update low.
while (low [1] high) { let mid = (low + high) / 2; if (canAchieve(mid)) { high = mid; } else { low = mid [2] 1; } }
The loop runs while low is less than high, and low updates by adding 1.
Fill all three blanks to create a function that uses binary search on answer technique.
function binarySearchAnswer(low: number, high: number, [1]: (mid: number) => boolean): number { while (low [2] high) { let mid = Math.floor((low + high) / 2); if ([3](mid)) { high = mid; } else { low = mid + 1; } } return low; }
The function takes a check function 'canAchieve', loops while low < high, and calls canAchieve(mid) inside.
