Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many ordered sequences of positive integers, each at most 3 and with at least two terms, sum to 6?

  1. A. 25
  2. B. 24
  3. C. 22
  4. D. 13

Correct answer: B. 24

Count ordered compositions with parts in {1, 2, 3} via dp[i] = dp[i−1] + dp[i−2] + dp[i−3], then subtract the one-part composition (the single term n, valid only when n ≤ 3). Forgetting to remove the one-part case overcounts.

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

Exams that ask this topic