Data Structures & AlgorithmsHardICPC regional · Codeforces Div 2
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?
- A. 5
- B. 3
- C. 2
- D. 7
Correct answer: B. 3
Moving 4 from the front to the back removes the 3 inversions it had with the smaller values 1, 2, 3, and adds 1 inversion with the larger 5: 5 − 3 + 1 = 3.
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 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]?
- What is the worst-case time complexity of finding the minimum in a rotated sorted array that may contain duplicates, using a binary search…