Data Structures & AlgorithmsVery hard
Which property makes the number of inversions of a rotated sorted array equal to a simple product?
- A. All elements are equal.
- B. The array has no duplicate values.
- C. The array is already sorted.
- D. Every element of the first block is larger than every element of the second block, so each cross pair is an inversion and within-block pairs are ordered.
Correct answer: D. Every element of the first block is larger than every element of the second block, so each cross pair is an inversion and within-block pairs are ordered.
In a rotation of an ascending array, the first block is entirely larger than the second, so every cross pair is an inversion and each block is internally sorted. Counting the cross pairs gives the product of the block sizes.
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
- 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)?
- 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…