Data Structures & AlgorithmsVery hardICPC regional · GATE CS
Problem: A derangement is a permutation with no fixed point. How many derangements of 5 objects exist?
- A. 9
- B. 76
- C. 44
- D. 265
Correct answer: C. 44
D(n) = (n − 1)(D(n − 1) + D(n − 2)) gives 1, 2, 9, 44, so D(5) = 44 (also round(5!/e)). D(4) = 9 is an off-by-one, 76 counts the permutations that DO have a fixed point, and D(6) = 265.
More Coding Interview Problems questions
- Problem: How many binary trees (distinct shapes, ignoring labels) can be formed with 6 nodes?
- Problem: Count the integers from 1 to 64 whose binary representation has an even number of 1-bits.
- Problem: For the string "aaabbbbcc" with at most one replacement of any character by any other letter, what is the length of the longest…
- Problem: The array [1, 4, 3, 2] (with even length) is split into two-element pairs in any way, and the minimum of each pair is added up…
- Problem: A matrix stores the number of paths in a 3×3 grid where each cell can be entered from the left or from above, and blocked cells…
- Problem: The string "racecar" has length 7. How many of its substrings are palindromes with length at least 3? Count index ranges.