Data Structures & AlgorithmsHardGATE CS
0/1 knapsack with capacity 27: weights [13, 8, 9, 14, 12, 4, 11] and values [25, 19, 16, 27, 22, 8, 23] (item i has weight w[i] and value v[i]). What is the maximum total value?
- A. 50
- B. 54
- C. 52
- D. 57
Correct answer: B. 54
Exhaustive search/DP gives 54. Greedy by value-to-weight ratio picks items worth only 50, and greedy by value gives 52; neither is optimal for 0/1 items.
More Coding Interview Problems questions
- How many structurally distinct binary trees with 8 nodes have at most 4 levels (a single root alone is 1 level)?
- How many binary strings of length 12 contain neither 101 nor 010 as a substring?
- What are the last three digits of 7^2025?
- In a jump game, a[i] is the maximum jump length from index i (any length from 1 to a[i] is allowed). For a = [3, 1, 4, 1, 5, 9, 2, 6, 5…
- The sequence a1, ..., a14 is generated by a1 = 1 and a(i+1) = (5a(i) + 3) mod 17. How many pairs (i, j) with i < j satisfy a(i) > a(j)?
- In how many ways can 30 be written as a sum of distinct positive integers, each at most 10, if the order of the summands does not matter?