Data Structures & AlgorithmsModerateTCS Digital · Infosys SP

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

  1. A. 4 and 5; count 1
  2. B. 3 and 6; count 3
  3. C. 3 and 5; count 2
  4. D. 3 and 4; count 1

Correct answer: C. 3 and 5; count 2

lower_bound is the first 5 at index 3; upper_bound is one past the last 5, index 5. 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