Data Structures & AlgorithmsVery hard
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?
- A. The arrays can be indexed in random order only.
- B. Comparing the k/2-th elements of both arrays discards half of a candidate range each step, giving O(log k) work.
- C. k must be a power of two for the method to work.
- D. The answer is always found in the first array.
Correct answer: B. Comparing the k/2-th elements of both arrays discards half of a candidate range each step, giving O(log k) work.
Comparing the midpoints of the two candidate prefixes lets one whole prefix be discarded safely, halving the search for the k-th element each step.
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
- 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)?
- What is the length of the longest contiguous subarray of [4, 7, 5, 6, 8, 9, 7, 6, 3, 4] in which the largest value minus the smallest…
- How many contiguous subarrays of [3, 1, 3, 2, 3, 1] have their maximum value occurring EXACTLY twice inside the subarray?
- How many strings of length 5 over the alphabet {a, b, c} contain no two adjacent equal letters when the letter is a or b (two adjacent c's…