Data Structures & AlgorithmsVery hardGATE CS · Product placement

Two sorted arrays of sizes m and n (m ≤ n) are given. What is the best possible time to find the median of their union?

  1. A. O((m + n) log(m + n)), by sorting
  2. B. O(log m), by binary searching the partition of the smaller array
  3. C. O(log(m + n)²)
  4. D. O(m + n), by merging

Correct answer: B. O(log m), by binary searching the partition of the smaller array

Choosing how many elements of the smaller array go to the left half fixes the rest; binary search on that count finds a valid partition in O(log m). Merging is correct but linear.

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

Exams that ask this topic