Data Structures & AlgorithmsHardGATE CS · Product-company placement
Using parts 1, 5, 6 (each any number of times), in how many ways can 9 be written as an ORDERED sum (so 1 + 2 and 2 + 1 count separately)?
- A. 3
- B. 8
- C. 10
- D. 13
Correct answer: C. 10
Ordered sums: f(n) = Σ f(n − c) over parts c, f(0) = 1, giving 10. Putting the coin loop OUTSIDE the amount loop counts unordered combinations instead (3).
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
- In the DAG with edges A→B, A→G, B→D, D→G, E→A, E→B, E→C, E→D, E→F, E→G, G→C, how many distinct directed paths are there from E to C, and…
- A 5 × 5 grid is given row by row as .##.. / ..... / ....# / ...#. / .#... ('#' = wall). Moving one step up, down, left or right through…
- What is the minimum number of cuts needed to split 'madamimad' into pieces that are all palindromes?
- A binary min-heap is stored in a 0-indexed array [13, 33, 15, 35, 42, 23, 32]. Perform, in order: insert 6, delete-min, insert 60…
- The directed graph A→C (5), A→E (7), B→A (5), C→B (5), D→A (5), D→B (-6), E→C (9), E→D (7) has one negative edge but no negative cycle…
- Kruskal's algorithm runs on the weighted undirected graph A–C (29), A–F (19), A–G (2), B–D (8), B–G (25), C–G (14), D–F (1), E–F (11), E–G…