DSA Tracker

Medium

Unbounded Knapsack

A medium Dynamic Programming problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Dynamic Programming
Sheets
2
Core for
9 roles
Platform
GeeksforGeeks

The problem

You are given weights and values of n items and a knapsack with capacity W. Each item can be used unlimited times. Find the maximum total value obtainable.

Example 1

Input
values = [1,3,4,5], weights = [1,3,4,5], W = 8
Output
10
Why
Use two items of weight 4 and value 4 each, total value 10.

Example 2

Input
values = [10,20,30], weights = [5,10,15], W = 25
Output
60
Why
Use two items of weight 10 and value 20 each, total value 60.

Example 3

Input
values = [5], weights = [10], W = 3
Output
0
Why
No item fits in the knapsack of capacity 3.

Constraints

  • 1 <= n <= 1000
  • 1 <= W <= 1000
  • 1 <= weights[i] <= W
  • 1 <= values[i] <= 1000

How to think about it

Updated 2026-09-09

Unlike 0/1 knapsack where an item can be taken at most once, unbounded knapsack allows picking the same item repeatedly. Sweeping capacities forward from weight up to W allows the updated value at capacity w - weight to be reused immediately at capacity w.

Approaches, worst first

  1. Recursive item branching

    time O(n^W) · space O(W)

    At remaining capacity cap, branch into taking any item whose weight <= cap without advancing the item index. Branching blows up exponentially.

  2. 2D dynamic programming grid

    time O(n * W) · space O(n * W)

    dp[i][w] stores max value using first i items at capacity w. dp[i][w] = max(dp[i-1][w], dp[i][w - weights[i-1]] + values[i-1]). Notice the second term stays on row i.

  3. 1D forward sweep arrayWrite this one

    time O(n * W) · space O(W)

    Maintain 1D array dp of size W + 1 initialized to 0. For each item, iterate w from weight up to W: dp[w] = max(dp[w], dp[w - weight] + value). Forward loop naturally enables multiple copies.

Where people lose marks · 3
  • Iterating capacity backward like in 0/1 knapsack, which accidentally restricts every item to at most one usage.
  • Assuming a greedy value-to-weight density heuristic works: fractional items work with greedy, but integer unbounded items require dynamic programming.
  • Zero capacity base case: knapsack of capacity 0 can hold 0 value.

The theory behind it

Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems

What Dynamic Programming is

Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.

When to reach for it

Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.

How the pattern works

Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.

What each operation costs

OperationTime
fill dynamic programming table of n statesO(n)
solve two-dimensional grid of m by n statesO(m * n)
space-optimized state transition keeping one rowO(n)
What usually goes wrong with Dynamic Programming
  • Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
  • Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
  • Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.

Which roles need this problem

Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Dynamic Programming problems

Problem set and role mapping as of .