Data Structures & AlgorithmsHardCodeforces Div2 B · GATE CS
Among all arrangements reachable from [4, 2, 1, 3] by swapping ONE pair of adjacent elements (or by doing nothing), what is the minimum possible number of inversions?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Swapping adjacent elements a[i] > a[i+1] removes exactly one inversion (the pair itself) and changes no others. Counting adjacent descents gives the number of swappable pairs, which is a different quantity from the total inversion count.
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 single-character flips needed to make "1101001011" alternate (no two adjacent characters equal)?
- What is the maximum sum of a contiguous subarray of [3, -2, 7, 1, -5, 4, 8, -1, 2] whose length is between 2 and 4 inclusive?
- How many strings of length 5 over the alphabet {0, 1, 2} have a digit sum divisible by 3 (the sum of their digit values)?
- A robot moves from the top-left to the bottom-right of a 4 × 5 grid of cells, each step going right, down, or diagonally down-right. How…
- A gold mine is the grid [[1, 3, 3], [2, 1, 4], [0, 6, 4]]. You start in any cell of the LEFT column and move one column to the right each…
- How many permutations of 0, 1, 2, 3, 4 are alternating, meaning p[0] < p[1] > p[2] < p[3] > p[4]?