Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

A sorted array holds the 39 values 2, 5, 8, … (step 3). Binary search with mid = ⌊(lo+hi)/2⌋ and 0-based indices looks for 50. How many elements are compared with 50 until it is found?

  1. A. 7
  2. B. 2
  3. C. 4
  4. D. 6

Correct answer: C. 4

Simulate: lo=0, hi=38; each probe halves the range; 50 sits at index 16 and is found on probe 4.

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

Exams that ask this topic