Skip to content
Mad4Study

Engineering & Science

GATE Computer Science

  • GATE CS
  • 2026–27 pattern practice
  • Algorithms
  • Dynamic programming
  • Easy

The standard dynamic-programming solution to 0/1 knapsack with n items and capacity W runs in

Algorithm Design

Pick an option to check your answer.

The DP table has (n+1)×(W+1)(n + 1) \times (W + 1) cells, each filled in O(1)O(1), giving O(nW)O(nW). Because WW is a number whose input size is only log⁡W\log W bits, this is pseudo-polynomial, not polynomial — 0/1 knapsack remains NP-hard.

Why not the others

  • O(nlog⁡n)O(n\log n) is the greedy fractional knapsack, which sorts by value/weight.
  • O(2n)O(2^n) always is brute force; the DP avoids it.
  • O(n2)O(n^2), which is polynomial — the bound depends on WW, not just nn.

Report an error

GATE CS · Algorithms · Algorithm Design