Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Which technique counts the inversions of an array of n distinct integers in O(n log n) time?
- A. Insertion sort with a single comparison per element
- B. A simple nested loop only
- C. Merge sort, counting cross-inversions while merging (or a Fenwick tree)
- D. Hashing each element into buckets
Correct answer: C. Merge sort, counting cross-inversions while merging (or a Fenwick tree)
During the merge step, every element taken from the right half jumps over the remaining left-half elements, and each such jump counts as 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
- How many contiguous subarrays of [2, 4, 6, 3, 9] have an odd sum?
- What is the length of the longest proper border of the string aabaaab (the longest proper prefix that is also a suffix)?
- Which algorithm finds all occurrences of a pattern in a text in expected linear time using a rolling hash of each window?
- You must perform exactly 3 operations on the array [4, -2, 5, -7, 3, -1]; each operation replaces one element by its negation (the same…
- What is the length of the longest substring of "aaabbcccbbaad" in which EVERY letter that appears in the substring appears at least 2 times?
- The universe is {1, ..., 8} and the available sets are [[1, 2, 3], [3, 4, 5], [5, 6, 7], [1, 7, 8], [2, 4, 6, 8]]. What is the minimum…