Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many inversions (pairs i < j with a[i] > a[j]) does the array [2, 4, 1, 3, 5] contain?
- A. 1
- B. 3
- C. 4
- D. 2
Correct answer: B. 3
Pairs (2,1), (4,1) and (4,3) are inversions. Counting adjacent descents gives only 1, which misses the non-adjacent pairs (2,1) and (4,1).
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
- The array [5, 1, 6, 2, 7, 3, 8] must be made strictly increasing by deleting as few elements as possible. How many deletions are needed?
- How many pairs i < j in [2, 3, 4, 6, 9] have gcd(a[i], a[j]) = 1?
- What is the sum of gcd over all contiguous subarrays of [4, 6, 8, 9]?
- How much rain water is trapped between bars of heights [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]?
- What is the maximum area of water a container can hold between two bars of heights [1, 8, 6, 2, 5, 4, 8, 3, 7] (area = min(height) times…
- What is the length of the longest palindromic substring of "forgeeksskeegfor"?