Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
Items as (weight, value) are [(2, 3), (3, 4), (1, 1)]. The 0/1 knapsack table dp[c] for capacities c = 0..6 is built correctly by scanning capacity downward. A buggy version scans capacity upward (c from w to 6). Comparing the final tables, what is the smallest capacity index at which they differ? Answer −1 if none.
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Scanning upward lets an item be reused within the same pass, so a capacity can gain value from an item it already used. The first capacity whose stored value is inflated shows the first place the two tables differ.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Intervals [(1, 6), (2, 4), (3, 9), (5, 7), (8, 10), (4, 5)] are given as closed ranges. You must remove the fewest intervals so that no…
- How many non-empty contiguous subarrays of [1, -1, 2, 0, 1, 3, -2, 1] have a sum equal to their length (number of elements)? Transform…
- Choose any subset of the positions of [4, -2, 7, 3, 9, 1, -5, 8, 2] (possibly empty) so that every two chosen positions are at least 3…
- Assertion (A): Dynamic programming over subsets for TSP needs exponential time in the number of cities. Reason (R): The number of subsets…
- Assertion (A): Kadane's algorithm can be used to find the maximum subarray sum in O(n) time. Reason (R): It keeps the best sum ending at…
- Assertion (A): Binary search on the answer requires that the feasibility predicate is monotone. Reason (R): Only a monotone predicate lets…