Data Structures & AlgorithmsHardGATE CS
How many subsets of {4, 7, 9, 10, 11, 13, 14} have sum exactly 31?
- A. 17
- B. 3
- C. 5
- D. 4
Correct answer: D. 4
dp[s] counts subsets with sum s; for each element update s from HIGH to low so it is used at most once, giving 4. Updating low to high lets an element be reused and gives 17.
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
- Unit-time jobs (deadline, profit): J1 (3, 45), J2 (2, 47), J3 (1, 77), J4 (1, 75), J5 (1, 13), J6 (2, 37), J7 (3, 59). One job runs at a…
- A directed graph on A–H has edges B→A, B→C, C→F, C→H, D→C, E→C, E→H, F→G, F→H, G→C, H→A. How many strongly connected components does it…
- 0/1 knapsack with capacity 18: items (weight, value) = (2, 25), (5, 31), (8, 37), (4, 22), (4, 27). What is the maximum total value?
- Keys 78, 54, 58, 22, 55, 70, 25, 12, 39, 86 are inserted into an empty AVL tree (giving preorder 58, 25, 22, 12, 54, 39, 55, 78, 70, 86)…
- DFS runs on the directed graph with adjacency lists A: BDF; B: FG; C: ABE; D: G; E: –; F: D; G: A, starting at A, scanning lists in the…
- The directed graph A→C (5), A→D (4), A→E (7), B→D (2), C→B (4), C→D (1), D→E (7), E→C (-5) has one negative edge but no negative cycle…