GATE DA 2025 | Question 27
For which of the following inputs does binary search take time \(O(\log n)\) in the worst case?
Explanation
Answer: (C)
Binary search compares the target with the middle element and discards half of the remaining range. It needs two things to be correct and \(O(\log n)\):
- Sorted data. Discarding a half is justified only when the elements are in order. On unsorted input the search can throw away the half that holds the target, so binary search does not work for A or B.
- Constant-time access to the middle. A linked list must be walked node by node to reach its middle, costing \(\frac{n}{2} + \frac{n}{4} + \cdots = O(n)\) in total, so D is not \(O(\log n)\) even though the list is sorted.
Only the sorted array (C) meets both conditions.
Answer: C