Data Structures & AlgorithmsVery hard
Which statement about inversion counting with repeated values is correct?
- A. Equal values form inversions and should be counted with a[i] ≥ a[j].
- B. Duplicates are removed before counting, so they cannot change the answer.
- C. Inversions exist only between adjacent positions.
- D. Equal values are not inversions, so the test must be strictly a[i] > a[j] for i < j.
Correct answer: D. Equal values are not inversions, so the test must be strictly a[i] > a[j] for i < j.
An inversion needs a strictly larger value before a smaller one; equal values are in order and never form an inversion. Counting with ≥ or removing duplicates changes the number and is a common error.
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
- Houses stand in a circle with values [5, 1, 9, 8, 4, 6, 2, 7]. Adjacent houses cannot both be robbed, and the first and last houses are…
- A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps…
- In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts…
- Each digit string maps to letters with 1 to A ... 26 to Z. The string "2262012" is split into groups of one or two digits, and each group…
- For the array [3, -7, 4, -2, 5, -9, 6, 1], you may delete at most ONE element (the rest keep their order). Then you take a non-empty…
- Array [2, 3, 1, 1, 4, 2, 1, 3, 1, 2]: from index i you may jump to any index in the range i+1 ... i+arr[i]. You start at index 0 and want…