Data Structures & AlgorithmsHard
Count the inversions of [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: pairs of positions i < j with N[i] > N[j]. Equal values are not inversions.
- A. 17
- B. 22
- C. 5
- D. 33
Correct answer: A. 17
An inversion needs a strictly larger value before a smaller one. Using ≥ counts equal pairs as inversions, counting only adjacent pairs misses non-adjacent inversions, and counting the opposite order gives the non-inversions instead.
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
- How many indices i satisfy: the sum of the elements before i equals the sum of the elements after i, excluding P2[i] itself? (P2 = [1, 2…
- How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [1, 2, 3, 1, 2, 3]?
- How many contiguous subarrays of [6, 10, 15, 4, 9] have gcd equal to 1?
- How many contiguous subarrays of [1, 3, 5, 7, 2, 4] have a sum that is a perfect square (0 counts as a square)?
- How many index pairs i < j in [5, 1, 9, 4, 8, 3, 7] satisfy T[i] − T[j] = 4?
- How many contiguous subarrays of [1, 2, 3, 1, 2, 4, 5, 0, 6] are strictly increasing and have length at least 2?