Data Structures & AlgorithmsVery hard
Why does the median of two sorted arrays of sizes m and n not require merging both arrays fully?
- A. The median is always the first element of the larger array.
- B. The median of two sorted arrays equals the average of their first elements.
- C. A binary search over the partition point of one array determines the median in O(log(min(m, n))) time.
- D. Median computation is impossible without merging.
Correct answer: C. A binary search over the partition point of one array determines the median in O(log(min(m, n))) time.
Choosing how many elements come from each array fixes the left half; a binary search on the split of the smaller array finds the correct split quickly. Only the boundary elements are then needed.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Which property makes the number of inversions of a rotated sorted array equal to a simple product?
- When merging two sorted lists with the standard two-pointer method, how many comparisons are made in the worst case?
- When computing the k-th smallest element of two sorted arrays, why can a binary search on the answer's position replace a full merge?
- Why do the positions of a stable partition by parity keep their relative order, and why is that important?
- The sorted array [2, 3, 4, 7, 11] contains some positive integers. What is the 5th positive integer that does NOT appear in this array?
- In how many ways can you choose 3 indices from the 9 positions 1 to 9 so that no two chosen positions are adjacent (consecutive integers)?