Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 C
Problem: What is the minimum number of swaps of two neighbouring characters needed to turn "abcdef" into "fedcba"?
- A. 30
- B. 15
- C. 3
- D. 5
Correct answer: B. 15
Each adjacent swap fixes exactly one inversion, and the full reversal has C(6, 2) = 15 inversions. Counting ordered pairs gives 30, swapping arbitrary positions would need only 3, and 5 is just n − 1.
More Coding Interview Problems questions
- Problem: Find the number of distinct strings obtained by reading the grid of letters ab cd moving right or down from the top-left, where…
- Problem: The string "ababa" has how many distinct substrings of length 2 and 3 combined?
- Problem: Count the palindromic substrings of "abcbaabc" that start at index 0 (each prefix that is a palindrome counts once).
- Problem: Maximum sum of a subarray with length exactly 3 in [4, −2, 5, 1, 0, 2, −3, 6]. Report the maximum window sum.
- Problem: The longest subarray with sum divisible by 4 in [5, 3, 7, 2, 2, 6, 1, 1]: what is its length?
- Problem: In the array [9, 3, 7, 3, 9, 1, 7], how many elements are equal to the maximum of the elements to their right (the last element…