Data Structures & AlgorithmsHardICPC regional · GATE CS

Problem: How many permutations of the letters A, B, C, D leave no letter in its original position (for example, A is not in the first place, B is not in the second, and so on)?

  1. A. 24
  2. B. 6
  3. C. 12
  4. D. 9

Correct answer: D. 9

D(4) = 4!(1 − 1 + 1/2 − 1/6 + 1/24) = 9 (or D(n) = (n − 1)(D(n−1) + D(n−2)) = 3 · (2 + 1)). 24 counts all permutations, 6 is 3!, and stopping inclusion-exclusion after the pair terms gives 24 − 24 + 12 = 12.

More Coding Interview Problems questions

Exams that ask this topic