Data Structures & AlgorithmsHardGATE CS · competitive programming
How many inversions does A = [7, 5, 3, 8, 1, 6, 2, 4] have, i.e. pairs i < j with A[i] > A[j]?
- A. 18
- B. 4
- C. 28
- D. 17
Correct answer: A. 18
Counting every such pair gives 18. Counting only adjacent descents gives 4, and n(n−1)/2 = 28 is the count of all pairs, not of inversions.
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
- P = [4, 1, 5, 2, 3] has 5 inversions. If its first element is moved to the end, how many inversions does the new array [1, 5, 2, 3, 4] have?
- How many pairs i < j in [5, 1, 6, 2, 7, 9, 3] satisfy A[i] > 2·A[j]?
- What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] into ascending order?
- One Lomuto partition pass with the last element as pivot runs on [3, 8, 2, 5, 1, 4, 7, 6]. What is the array after this pass, before any…
- A three-way partition (Dutch national flag) with a low pointer, a mid pointer and a high pointer runs on [2, 0, 2, 1, 1, 0]. What is the…
- What is the smallest positive integer missing from the array [3, 4, −1, 1]?