Binary Search on Answer Technique in DSA C# - Time & Space Complexity
We want to understand how fast the binary search on answer technique finds the correct answer.
How does the number of steps grow as the search range gets bigger?
Analyze the time complexity of the following code snippet.
int low = 0, high = maxPossibleAnswer;
while (low <= high) {
int mid = low + (high - low) / 2;
if (Check(mid)) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
This code tries to find the smallest value that satisfies a condition by checking the middle of the current range and narrowing it down.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: The loop runs about log base 2 of the size of the search range.
Each step cuts the search range in half, so the number of steps grows slowly even if the range grows a lot.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 checks |
| 100 | About 7 checks |
| 1000 | About 10 checks |
Pattern observation: Doubling the input size adds only one more step.
Time Complexity: O(log n)
This means the number of steps grows slowly, making it efficient for large search ranges.
[X] Wrong: "The loop runs n times because it looks like a while loop over a range."
[OK] Correct: Each step cuts the range in half, so the total steps are much fewer than n.
Understanding this technique shows you can solve problems by smartly narrowing down answers, a key skill in many challenges.
"What if the Check function takes linear time? How would that affect the overall time complexity?"
Practice
Binary Search on Answer technique?Solution
Step 1: Understand the technique
Binary Search on Answer guesses a possible solution and verifies if it meets the problem's conditions. If valid, adjust the guess (smaller/larger) using binary search to find the optimal answer.Final Answer:
We guess an answer and check if it satisfies the problem, then adjust the guess using binary search. -> Option BQuick Check:
Guess and verify with binary search = B [OK]
- Confusing binary search on answer with normal binary search on array
- Thinking it only works on sorted arrays
- Not verifying the guessed answer properly
low and high?Solution
Step 1: Identify correct loop and mid calculation
The binary search loop uses while (low <= high) and mid = (low + high) / 2 with integer division.Final Answer:
while (low <= high) { int mid = (low + high) / 2; } -> Option DQuick Check:
Correct binary search loop syntax = A [OK]
- Using for loop instead of while
- Incorrect mid calculation like (low - high)/2
- Using if instead of loop
int low = 10, high = 50;
while (low <= high) {
int mid = (low + high) / 2;
if (IsPossible(mid))
high = mid - 1;
else
low = mid + 1;
}
Console.WriteLine(low);
If IsPossible(30) returns true and IsPossible(20) returns false, what will be printed?Solution
Step 1: Trace binary search behavior
IsPossible(mid) true means valid (try smaller, high=mid-1); false means invalid (low=mid+1). Given IsPossible(30)=true, IsPossible(20)=false, assuming monotonicity the search converges to smallest valid at 21.Final Answer:
21 -> Option CQuick Check:
Minimum valid answer after binary search = 21 [OK]
- Printing high instead of low after loop
- Confusing true/false meaning of IsPossible
- Not updating low and high correctly
int low = 0, high = maxLoad;
while (low < high) {
int mid = (low + high) / 2;
if (IsPossible(mid))
low = mid;
else
high = mid - 1;
}
Console.WriteLine(low);Solution
Step 1: Identify incorrect update causing infinite loop
When IsPossible(mid) true, low = mid risks no progress (infinite loop if low == mid); should set low = mid + 1 to advance.Final Answer:
When IsPossible(mid) is true, 'low' should be set to 'mid + 1' instead of 'mid'. -> Option AQuick Check:
Increment low by 1 to avoid infinite loop [OK]
- Not incrementing low causing infinite loop
- Confusing when to move low or high
- Ignoring loop condition correctness
k contiguous subarrays so that the maximum sum among these subarrays is minimized. Which approach correctly uses binary search on answer to solve this?Solution
Step 1: Define search space and feasibility check
Binary search from max weight (min possible max sum) to total sum; for mid, check if can split into ≤ k contiguous subarrays each with sum ≤ mid. If yes, try smaller mid; else larger.Final Answer:
Binary search between max weight and sum of all weights; check if array can be split into ≤ k parts with max sum ≤ mid. -> Option AQuick Check:
Search answer range and verify splits ≤ k [OK]
- Binary searching on indices instead of answer range
- Not checking contiguous subarray sums properly
- Sorting array unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
