Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many inversions (pairs i < j with a[i] > a[j]) does the array [3, 1, 4, 1, 5, 9, 2, 6] contain?
- A. 9
- B. 8
- C. 6
- D. 11
Correct answer: B. 8
Strict inequality means equal values such as the two 1s do not form an inversion. Merge sort counts inversions in O(n log n) with the same rule.
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 index triples i < j < k in the array [1, 2, 3, 4, 5, 6] have a[i] + a[j] + a[k] divisible by 5?
- A queue has capacity 4 and an enqueue into a full queue is rejected. Operations in order: enqueue 1, 2, 3, 4; dequeue; dequeue; enqueue 5…
- What is the sum of the maxima of every contiguous window of size 3 in the array [1, 3, -1, -3, 5, 3, 6, 7]?
- How many valid sequences of 4 push and 4 pop operations are there on an initially empty stack, where every pop is applied only to a…
- Three processes arrive at time 0 with CPU bursts 5, 3 and 8. Round-robin scheduling uses a time quantum of 2, a process that is not…
- Remove exactly 3 digits from the number 1432219 (without reordering the rest) so that the remaining number is as small as possible. What…