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

What is the time complexity of binary searching for a value in a sorted array of n elements, then linearly scanning the next k elements from that point?

  1. A. O(log n + k)
  2. B. O(n + k)
  3. C. O(log n * k)
  4. D. O(k log n)

Correct answer: A. O(log n + k)

Binary search costs O(log n); the extra linear scan of k elements costs O(k), and the two stages simply add: O(log n + k).

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

Exams that ask this topic