Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 D
How many inversions does the array [2, 4, 1, 3, 5] have? An inversion is a pair of positions i < j with a[i] > a[j].
- A. 1
- B. 4
- C. 3
- D. 10
Correct answer: C. 3
Count inversions with merge sort: while merging two sorted halves, every time an element from the right half is placed first, it forms inversions with all remaining left elements. Counting only adjacent descents gives a different number.
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
- What is the minimum number of elements you must INSERT into [1, 4, 2, 7, 4, 1, 3] (at any positions) so that the array becomes a palindrome?
- How many contiguous subarrays of length at least 2 of [23, 2, 4, 6, 7] have a sum that is a multiple of 6?
- Sum the lengths of all contiguous subarrays of [1, 2, 3, 4] whose element sum is even.
- How many index pairs i < j in [1, 4, 6, 9, 10, 12] satisfy a[i] + a[j] >= 15?
- How many subarrays [i..j] of [3, 1, 4, 1, 5] have their first element strictly greater than every other element of the subarray?
- What is the smallest positive integer x such that x(x+1)(x+2)/6 is at least 100?