Data Structures & AlgorithmsModerateGATE CS · competitive programming

For binary search over 1000 distinct sorted values with mid = (lo + hi) / 2 and the loop running while lo ≤ hi, what is the largest number of iterations needed over all present targets?

  1. A. 10
  2. B. 9
  3. C. 11
  4. D. 500

Correct answer: A. 10

Each probe halves the range, and 2⁹ = 512 < 1000 ≤ 2¹⁰ = 1024. So at most 10 probes are needed.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic