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.

  1. A. 5
  2. B. 3
  3. C. 6
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic