Data Structures & AlgorithmsVery hard
Why is the minimum over all rotations of a permutation's inversion count achieved by a specific rotation that can be found in linear time?
- A. All rotations have the same inversion count.
- B. Rotating by one moves the first element to the end, which changes the inversion count by an amount computable from counts of smaller and larger values in O(1) per step.
- C. The minimum is always at the identity rotation.
- D. Inversions cannot be counted after rotation.
Correct answer: B. Rotating by one moves the first element to the end, which changes the inversion count by an amount computable from counts of smaller and larger values in O(1) per step.
Moving the first element to the end removes its inversions with smaller elements and adds those with larger elements, a simple update. The sequence of counts is therefore computed incrementally.
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 covering all distinct values of an array with a subarray, why does a sliding window track counts rather than a single flag?
- Why does the sum-of-suffix-maxima total equal a simple right-to-left scan?
- When counting windows whose sum is a perfect square, why is the square test exact only with integer square roots?
- In the 'longest subarray with a single removal' interview problem, why is subtracting the minimum from the total a common mistake?
- Which property lets the 'longest subarray where every adjacent pair differs by exactly 1' be found in one pass?
- When computing the minimum adjacent swaps by breadth-first search over arrangements, why does the first time a target is reached give the…