Data Structures & AlgorithmsHardGATE CS · ICPC regional · Product Company SDE
0/1 knapsack, capacity 11. Items (weight, value): (4, 6), (1, 7), (6, 3), (2, 2). What is the maximum total value?
- A. 16
- B. 18
- C. 10
- D. 15
Correct answer: A. 16
The DP table dp[i][c] = max(dp[i−1][c], dp[i−1][c−w]+v) over all items and capacities finds the true optimum, 16. A greedy choice by value/weight ratio instead gives 15, which can be suboptimal in 0/1 knapsack because items can't be split.
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
- Keys 92, 78, 40, 55, 53, 90, 42 are inserted via separate chaining (h(k) = k mod 5, new keys appended to the end of the chain) into a…
- A binary tree has preorder traversal [53, 5, 2, 10, 30, 72, 65, 60] and inorder traversal [2, 5, 10, 30, 53, 60, 65, 72]. Reconstructing…
- A directed graph has edges E→C, B→E, A→E, C→A, B→C, A→D, E→D. Using 3-colour DFS (white/grey/black, a grey-to-grey back edge means a…
- A binary tree has preorder traversal [75, 38, 23, 7, 60, 94, 80, 95] and inorder traversal [7, 23, 38, 60, 75, 80, 94, 95]. Reconstructing…
- Undirected graph edges: D–A, D–B, A–C, B–C, A–B, D–C. Using a BFS 2-colouring, is this graph bipartite?
- Keys 18, 24, 4, 33, 84, 49, 74, 69, 8, 79 are inserted via separate chaining (h(k) = k mod 8, new keys appended to the end of the chain)…