Data Structures & AlgorithmsVery hard
Consider all cyclic rotations of 3 1 2 5 4 (including the identity). For each rotation count its inversions (pairs i < j with a[i] > a[j]). What is the MINIMUM inversion count over the rotations?
- A. 9
- B. 3
- C. 4
- D. 2
Correct answer: B. 3
Enumerate the n rotations and count inversions in each; the minimum is attained by the rotation that starts at the smallest block. Counting the original arrangement, the maximum, or an off-by-one count each give different values.
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
- Using only swaps of ADJACENT elements, what is the minimum number of swaps needed to make the MINIMUM value of 3 1 5 2 4 the FIRST element…
- What is the maximum number of pairwise DISJOINT contiguous blocks of 1 -1 2 3 -3 4 -4 5 whose sums are all zero? (Blocks must not overlap…
- How many contiguous subarrays of 2 1 3 2 1 3 3 2 contain EVERY distinct value that appears in the whole array?
- How many contiguous windows of length exactly 4 in 1 3 4 5 2 6 7 1 4 have a sum that is a perfect square?
- How many contiguous subarrays of 1 2 3 4 5 6 7 8 of length at least 2 contain EXACTLY as many odd values as even values?
- What is the length of the shortest contiguous subarray of 1 4 6 3 8 5 2 7 that contains at least 2 odd values AND at least 2 even values?