Data Structures & AlgorithmsModerateGATE CS · Product placement

For the sorted array [1, 5, 6, 7, 7, 7, 8, 9, 9, 10, 10, 11] (0-based), what are lower_bound(9) (first index with value ≥ 9) and upper_bound(9) (first index with value > 9), and hence how many times does 9 occur?

  1. A. 8 and 9; count 1
  2. B. 7 and 9; count 2
  3. C. 7 and 10; count 3
  4. D. 7 and 8; count 1

Correct answer: B. 7 and 9; count 2

lower_bound is the first 9 at index 7; upper_bound is one past the last 9, index 9. Their difference 2 is the count — both found by O(log n) binary searches.

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

Exams that ask this topic