Complete the code to initialize the binary search boundaries.
int low = 0, high = [1];
We set high to a large number like 1e9 to cover the maximum possible answer range.
Complete the code to calculate the middle point in binary search.
int mid = (low [1] high) / 2;
We add low and high and then divide by 2 to find the middle.
Fix the error in the condition to update the binary search range.
if (canAchieve(mid) [1] true) { high = mid - 1; } else { low = mid + 1; }
= instead of double equals ==.< which is not a boolean check.The condition must check equality with true using ==.
Fill both blanks to complete the binary search loop condition and update.
while (low [1] high) { int mid = (low + high) / 2; if (canAchieve(mid) [2] true) { high = mid - 1; } else { low = mid + 1; } }
= instead of comparison.The loop runs while low <= high or low < high. Here, low < high is used. The condition checks if canAchieve(mid) == true.
Fill all three blanks to complete the binary search function returning the minimum valid answer.
int binarySearch() {
int low = 0, high = [1];
int result = -1;
while (low [2] high) {
int mid = (low + high) / 2;
if (canAchieve(mid) [3] true) {
result = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return result;
}< instead of <= in loop condition.Set high to a large number like 1,000,000,000. The loop runs while low <= high. The condition checks if canAchieve(mid) == true.
