Binary Search Algorithm
- 24Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the prerequisite for applying binary search to an array?
24 questions
- 1
What is the prerequisite for applying binary search to an array?
Model answer
The array must be sorted.
Explanation
Binary search relies on the ability to eliminate half the search space based on comparisons; this only works if the array is sorted.
- 2
Describe the basic process of binary search for a target value in a sorted array.
Model answer
Check the middle element; if it equals the target, return its index. If the target is smaller, search the left half; if larger, search the right half. Repeat until the target is found or the search range becomes empty.
Explanation
This divide-and-conquer approach halves the search range each step until the target is located or the range is empty.
- 3
What indicates that the target value is not present in the array during binary search?
Model answer
The search range becomes empty (Left > Right).
Explanation
When the entire range has been exhausted without finding the target, return -1 or indicate absence.
- 4
Why is the formula M=(L+R)/2 unsafe in languages like C++ and Java?
Model answer
It can cause integer overflow when L and R are large, because the sum L+R may exceed the maximum value the integer type can hold.
Explanation
Overflow leads to incorrect computation of the middle index, potentially causing infinite loops or wrong results.
- 5
What is the safe alternative formula for computing the middle index in binary search?
Model answer
M=L+2R−L or equivalently M=L+(R−L)/2.
Explanation
This formula computes the difference first, which fits within the type as long as R≥L, avoiding overflow.
- 6
In the standard binary search implementation, how do we update the range when the middle element is greater than the target?
Model answer
Set R=M−1 (search left half).
Explanation
Since the middle element is too large, the target must be in the left half (smaller indices).
- 7
In the standard binary search implementation, how do we update the range when the middle element is less than the target?
Model answer
Set L=M+1 (search right half).
Explanation
Since the middle element is too small, the target must be in the right half (larger indices).
- 8
What is the time complexity of binary search?
Model answer
O(logN), where N is the number of elements.
Explanation
Each step halves the search space, so the number of steps is logarithmic in the input size.
- 9
How can binary search be used to determine if a given integer X is a perfect square, without using the square root function?
Model answer
Binary search for integer M in [0,X] such that M2=X; adjust range based on whether M2<X or M2>X.
Explanation
This is a classic application of binary search on integers with a custom comparison: compare M2 to X.
- 10
In the lower bound problem (find first element ≥X), why do we need an extra variable
ans?Model answer
Because we are looking for the first occurrence, and we may encounter multiple satisfying elements; we need to store the best (leftmost) candidate seen so far.
Explanation
When we find a satisfying element, it is a candidate, but there might be a better (smaller index) candidate to the left, so we continue searching and update
ans. - 11
What should
ansbe initialized to in the lower bound problem if no element satisfies the condition?Model answer
A sentinel value like −1 or infinity, depending on problem requirements.
Explanation
If the entire array fails the condition,
answill remain the initial sentinel, which is returned to indicate no such element. - 12
In the lower bound algorithm, after finding a candidate
midthat satisfies the condition a[mid]≥X, why do we set R=mid−1?Model answer
Because we want to search for a possibly better (smaller) candidate to the left.
Explanation
The condition is satisfied at
mid, but a smaller index might also satisfy it; we look left by shrinking the right bound. - 13
In the lower bound algorithm, if the current element does NOT satisfy the condition, why do we set L=mid+1?
Model answer
Because any satisfying element must be to the right (since the array is sorted and condition is monotonic).
Explanation
If the prefix is falses, then all trues are to the right; we move left bound to search the right half.
- 14
How does the lower bound problem illustrate the 'prefix false, suffix true' pattern in binary search?
Model answer
Elements that are <X form the false prefix; elements ≥X form the true suffix. Binary search finds the boundary (first true).
Explanation
The condition is monotonic: once true, remains true for all larger indices. This is key to applying binary search on such custom conditions.
- 15
In the rotated array problem (find minimum), what condition can be used to split the array into a 'true' prefix and 'false' suffix?
Model answer
Compare each element with the last element: elements greater than the last element are true, those less or equal are false. Find the first false.
Explanation
In a rotated sorted array, elements to the left of the minimum are greater than the last element; those from the minimum onward are not. The first false is the minimum.
- 16
How does binary search find the minimum in a rotated sorted array?
Model answer
Use condition a[mid]>a[last]; if true (prefix), search right (L=mid+1); if false (suffix), record
ans = midand search left (R=mid−1). Returnans.Explanation
The minimum is the first element not greater than the last element. By searching for that boundary, we find the rotation point.
- 17
What special case must be considered when using the 'compare with last element' condition for rotated array?
Model answer
If the array is not rotated at all (sorted in increasing order), the whole array is true (all elements > last? Actually only the first element equals last? Wait: In a non-rotated array, the last element is the maximum, so no element is greater than the last. So the condition should be a[mid]>a[last] gives all falses. So the first false is at index 0, which is the minimum.) So it works.
Explanation
Actually, in a non-rotated array, the last element is the largest, so no element is greater than it; the condition yields all falses, and the first false is the first element (minimum).
- 18
In the peak element problem (mountain array), what condition allows binary search to find the maximum?
Model answer
Condition a[mid]>a[mid−1] (true for elements in the increasing part, false for decreasing part). Find the last true.
Explanation
The maximum is the last element that is greater than its previous element; after it, all are smaller than the previous.
- 19
How do we handle the first element when using the condition a[mid]>a[mid−1] for the peak problem?
Model answer
We can treat the first element as true by special-casing (mid==0) or consider a sentinel −∞ before the array.
Explanation
The first element has no previous element; it is always considered true because it belongs to the increasing part.
- 20
When performing binary search on real numbers (e.g., to compute square root), what is the termination condition?
Model answer
Stop when the range length R−L is less than a specified epsilon (tolerance).
Explanation
Since we cannot guarantee exact equality, we iterate until the interval is small enough to provide the required precision.
- 21
In binary search on reals, how do we update the range? Why don't we use mid+1 or mid−1?
Model answer
We set L=mid or R=mid depending on the condition. There is no integer indexing, so we cannot exclude mid entirely.
Explanation
The real search space is continuous; we simply narrow the interval to either side of mid.
- 22
Given a function that is monotonic, how can binary search be used to find the root of an equation f(x)=0?
Model answer
Binary search on x in an interval where f(a) and f(b) have opposite signs; update interval based on the sign of f(mid).
Explanation
This is a general application of binary search for continuous functions (bisection method).
- 23
How does the number of iterations in binary search on reals relate to the required precision?
Model answer
Approximately log2(ϵinitial_range) iterations are needed to achieve precision ϵ.
Explanation
Each iteration halves the interval, so the number of steps is logarithmic in the ratio of initial range to epsilon.
- 24
What is a key difference between binary search on integers and binary search on reals in terms of the middle element calculation?
Model answer
For integers, we use floor division to get an integer index; for reals, we use exact division (no rounding).
Explanation
Integer mid must be an index in the array; real mid can be any value in the continuous interval.

Ask anything
We've read this whole study set, so questions about it get the best answers.