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.
- A. 8
- B. 28
- C. 64
- 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'.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Each digit string maps to letters with 1 to A ... 26 to Z. The string "2262012" is split into groups of one or two digits, and each group…
- For the array [3, -7, 4, -2, 5, -9, 6, 1], you may delete at most ONE element (the rest keep their order). Then you take a non-empty…
- Array [2, 3, 1, 1, 4, 2, 1, 3, 1, 2]: from index i you may jump to any index in the range i+1 ... i+arr[i]. You start at index 0 and want…
- You split the array [1, 15, 7, 9, 2, 5, 10] into contiguous non-empty segments, each of length at most 3. A segment contributes (its…
- What is the maximum product of a non-empty contiguous subarray of [-2, 3, -4, 0, -1, 5, -3, -2]?
- How many binary strings of length 12 contain no three consecutive 1s and end with the digit 0?