Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
In how many ways can 12 be written as a sum of DISTINCT positive integers, where order does not matter (so 12 itself counts as one way)?
- A. 77
- B. 15
- C. 16
- D. 12
Correct answer: B. 15
Use 0/1-knapsack style counting: iterate the item sizes and process the target downwards so each size is used at most once. Iterating upwards (as in the unbounded coin-change count) allows repeated parts and gives the larger count.
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
- Balloons with values [3, 1, 5, 8, 2] are burst one at a time. Bursting balloon i gives coins = left × value[i] × right, where left and…
- How many non-empty contiguous subarrays of [4, -2, 7, -5, 3, 6] have a sum of at most 4?
- Starting from n = 191, in each step you may replace n by n/2 when n is even, or by n−1 or n+1 when n is odd. What is the minimum number of…
- Coins of value 2, 3 and 5 are available in unlimited supply. In how many ways can you pay exactly 10 when the ORDER of coins does NOT…
- In the binary array [1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1] you may flip at most ONE 0 to a 1. What is the length of the longest run of…
- What is the length of the longest STRICTLY increasing subsequence of [3, 10, 2, 1, 20, 4, 6, 7, 6, 7, 8, 2]?