Data Structures & AlgorithmsVery hardGATE CS · CAT · MBA placement

In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts are counted as different.

  1. A. 8
  2. B. 28
  3. C. 64
  4. D. 40

Correct answer: C. 64

Count ordered compositions with dp[i] = dp[i-1] + dp[i-3] + dp[i-4], because the last part is 1, 3 or 4. Counting multisets (order ignored) gives a much smaller number, which is the usual mistake when the question says 'ways'.

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

Exams that ask this topic