Data Structures & AlgorithmsModerateGATE CS · TCS NQT

How many ordered ways are there to write 8 as a sum of parts, each part being 1, 2 or 3 (the order of parts matters)?

  1. A. 88
  2. B. 81
  3. C. 44
  4. D. 40

Correct answer: B. 81

dp[i] = dp[i−1] + dp[i−2] + dp[i−3] counts ordered compositions because the last part is chosen from {1, 2, 3}. The count of multisets (order ignored) is much smaller and is a different quantity.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic