Data Structures & AlgorithmsHard
When the maximum and minimum must end at the two extremes by adjacent swaps, why can their costs interact?
- A. The costs are always independent of each other.
- B. Only the minimum ever needs to move.
- C. If the maximum lies before the minimum, moving the minimum to the front pushes the maximum by one position, so the two costs are not simply additive.
- D. Swaps cannot change the position of the maximum.
Correct answer: C. If the maximum lies before the minimum, moving the minimum to the front pushes the maximum by one position, so the two costs are not simply additive.
Moving the minimum leftward across the maximum shifts the maximum's index, so one of the moves saves a swap compared with the naive sum. Accounting for this interaction is the subtle part.
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 does 'maximum number of disjoint zero-sum blocks' use an interval DP over prefixes rather than a greedy scan?
- To count subarrays of length at least 2 with equal odd and even counts, why is the length-1 case excluded?
- 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?
- 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?