Data Structures & AlgorithmsHardProduct-company placement
Using parts 1, 2, 3 (each any number of times), in how many ways can 7 be written as an ORDERED sum (so 1 + 2 and 2 + 1 count separately)?
- A. 8
- B. 44
- C. 24
- D. 52
Correct answer: B. 44
Ordered sums: f(n) = Σ f(n − c) over parts c, f(0) = 1, giving 44. Putting the coin loop OUTSIDE the amount loop counts unordered combinations instead (8).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- For the string ABRACADABRA, what are the lengths of the longest palindromic SUBSTRING and the longest palindromic SUBSEQUENCE?
- Heap sort (ascending) is run on [20, 37, 49, 16, 22, 10, 31, 33]: build a max-heap bottom-up, then repeatedly swap the root with the last…
- What is the length of the longest BITONIC subsequence (strictly increasing then strictly decreasing; either part may be empty) of [20, 21…
- 0/1 knapsack with capacity 17: items (weight, value) = (9, 21), (6, 19), (3, 9), (7, 13), (3, 27). What is the maximum total value?
- Coins of denominations 1, 6, 9, 10 are available in unlimited supply. What is the minimum number of coins needed to make 33, and how many…
- For the string BBABCBCAB, what are the lengths of the longest palindromic SUBSTRING and the longest palindromic SUBSEQUENCE?