1. Home
  2. Explore
  3. Computer Science
  4. Binary Search Algorithm

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Binary Search Algorithm

  • 36Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Basic Binary Search
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is binary search and what input does it require?

Tap to flipClick to flipspace

An algorithm to find a target value in a sorted array by repeatedly dividing the search interval in half.

Or swipe the card

36 cards

  1. What is binary search and what input does it require?

    An algorithm to find a target value in a sorted array by repeatedly dividing the search interval in half.

  2. How does binary search work at each step?

    Check the middle element. If it equals the target, return its index. If the target is greater, search the right half; otherwise search the left half.

  3. What is the termination condition for classic binary search?

    The range becomes empty, i.e., left > right. Then the target is not present.

  4. What is returned if the target is not found?

    Typically -1 (or null/None).

  5. What are the initial values of left and right for an array of size n?

    left = 0, right = n-1.

  6. What is the time complexity of binary search?

    O(log N), because each step halves the search space.

  7. Why is binary search O(log N)?

    With each iteration, the search range is reduced by half, so the number of steps is log⁡2(N)\log_2(N)log2​(N) in the worst case.

  8. What is a common mistake when computing the middle index?

    Using mid=(L+R)/2mid = (L+R)/2mid=(L+R)/2, which can cause integer overflow if L+RL+RL+R exceeds the maximum value of the integer type.

  9. What is the safe formula to compute mid?

    mid=L+(R−L)/2mid = L + (R-L)/2mid=L+(R−L)/2. This avoids overflow since (R−L)(R-L)(R−L) is always ≤ R (assuming L ≤ R).

  10. In which programming languages is overflow a concern?

    In languages like C++ and Java where int has a fixed range (≈ 2×1092\times 10^92×109).

  11. Why is mid=L+(R−L)/2mid = L + (R-L)/2mid=L+(R−L)/2 safe?

    Because (R−L)(R-L)(R−L) is at most the maximum index, so adding it to L does not exceed the integer range.

  12. What is the lower bound problem?

    Given a sorted array and a value X, find the first element that is ≥ X. (Also called lower_bound.)

  13. Why can't we just return the first element that satisfies the condition in lower bound?

    Because there might be a better (smaller index) element that also satisfies the condition. We need the leftmost such element.

  14. How do we modify classic binary search for lower bound?

    Use an extra variable ans initialized to a sentinel value (e.g., -1). When a[mid]≥Xa[mid] \geq Xa[mid]≥X, update ans=midans = midans=mid and search left (R=mid−1R = mid-1R=mid−1); otherwise, search right (L=mid+1L = mid+1L=mid+1). After the loop, return ans.

  15. What is the sentinel value for ans if no element satisfies the condition in lower bound?

    Typically -1 for index, or n (size of array) for position, or infinity for value.

  16. In the lower bound algorithm, what happens when a[mid]≥Xa[mid] \geq Xa[mid]≥X?

    Update ans=midans = midans=mid (potential answer) and set R=mid−1R = mid-1R=mid−1 to search for any smaller index that also satisfies.

  17. In the lower bound algorithm, what happens when a[mid]<Xa[mid] < Xa[mid]<X?

    Set L=mid+1L = mid+1L=mid+1, because no element to the left can satisfy the condition (since array is sorted).

  18. How can binary search be generalized to find a boundary in a boolean array?

    Treat the problem as finding the first (or last) index where the predicate changes from false to true (or vice versa). The predicate must be monotonic.

  19. How to find the first true in a boolean array that is all false then all true?

    Use binary search: if mid is true, store ans=midans = midans=mid and search left (R=mid−1R = mid-1R=mid−1); else search right (L=mid+1L = mid+1L=mid+1). At the end, ans is the first true.

  20. How to find the last true in a boolean array that is all true then all false?

    If mid is true, store ans=midans = midans=mid and search right (L=mid+1L = mid+1L=mid+1); else search left (R=mid−1R = mid-1R=mid−1). At the end, ans is the last true.

  21. What if the boolean array is all false? How does the algorithm behave?

    ans will remain the sentinel value (e.g., -1) because no true is found.

  22. Why is understanding the predicate form important for binary search?

    It allows applying binary search to non-trivial problems like rotated arrays and peak elements.

  23. What is the rotated array problem?

    Find the minimum element in a rotated sorted array (e.g., [6,7,8,2,3][6,7,8,2,3][6,7,8,2,3] → minimum is 2).

  24. Why can't classic binary search be applied directly to a rotated array?

    The array is not fully sorted, so comparing middle with target or neighbors does not indicate which half contains the minimum.

  25. What property can we use for binary search on a rotated array?

    Compare a[mid]a[mid]a[mid] with the last element a[right]a[right]a[right] (or a[left]a[left]a[left]). If a[mid]>a[right]a[mid] > a[right]a[mid]>a[right], then the minimum is in the right half; otherwise, it's in the left half (or mid itself).

  26. How do we implement binary search to find the minimum in a rotated array?

    Condition: a[mid]>a[right]a[mid] > a[right]a[mid]>a[right] → true (we are in the first part). We search for the first false (i.e., first element ≤ a[right]). Set ans=−1ans = -1ans=−1. If condition true, go right (L=mid+1L = mid+1L=mid+1); else store ans=midans = midans=mid and go left (R=mid−1R = mid-1R=mid−1). At the end, ans is the index of minimum.

  27. What if the array is not rotated? How does the algorithm behave?

    The condition a[mid]>a[right]a[mid] > a[right]a[mid]>a[right] will never be true (since array is sorted, a[mid]≤a[right]a[mid] \leq a[right]a[mid]≤a[right]), so ans will become 0 (the first element). That is correct.

  28. What is the peak element problem?

    Find the maximum element in an array that strictly increases then strictly decreases (mountain array).

  29. How to apply binary search to the peak element problem?

    Define condition: a[mid]>a[mid−1]a[mid] > a[mid-1]a[mid]>a[mid−1] (if mid > 0) or treat first element as true. We want the last true (the peak). If true, store ans=midans = midans=mid and go right (L=mid+1L = mid+1L=mid+1); else go left (R=mid−1R = mid-1R=mid−1). At the end, ans is the peak index.

  30. How do we handle the first element of the array in the peak problem?

    Since there is no previous element, we can treat it as true (or check mid==0mid==0mid==0 separately if needed).

  31. How does binary search differ for real numbers?

    Use a while loop with a precision condition (e.g., R−L>ϵR - L > \epsilonR−L>ϵ). Do not use integer increment/decrement; set L or R directly to mid without ±1.

  32. What is the termination condition for binary search on reals?

    When (R−L)≤ϵ(R - L) \leq \epsilon(R−L)≤ϵ, where ϵ\epsilonϵ is the required precision.

  33. How do we compute mid for real numbers?

    mid=(L+R)/2mid = (L + R) / 2mid=(L+R)/2 (no integer overflow concern, but careful with floating point errors).

  34. How do we adjust the range for real-valued binary search?

    If mid2<targetmid^2 < targetmid2<target, set L=midL = midL=mid; else set R=midR = midR=mid. (Note: no +1 or -1, as we work with continuous values.)

  35. Why don't we check for exact equality in real-valued binary search?

    Due to floating point precision, exact equality is rare. We rely on the interval converging.

  36. Example: find square root of 2 with precision 0.0001. What is the algorithm?

    Initialize L=0, R=2. While R−L>0.0001R-L > 0.0001R−L>0.0001: mid=(L+R)/2mid=(L+R)/2mid=(L+R)/2; if mid2<2mid^2 < 2mid2<2, L=mid else R=mid. After loop, return mid (or any value in [L,R][L,R][L,R]).

Card 1 of 36
Card 1 of 36

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent