0-1 Knapsack Problem Problem
0-1 Knapsack Problem Problem — ExecCode Medium DSA Practice
Solve the 0-1 Knapsack Problem problem on ExecCode. Free online medium DSA practice in DP. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given weights and values of n items and a knapsack capacity W, choose a subset of items that maximizes total value without exceeding W. This is the classic 0/1 knapsack: each item may be taken at most once (you cannot take a fraction of an item). Return the maximum achievable value.
Examples
Input weights = [1, 2, 3], values = [6, 10, 12], capacity = 5; Output 22. Input weights = [1, 1], values = [10, 20], capacity = 2; Output 30. Input weights = [2, 3, 4], values = [3, 4, 5], capacity = 5; Output 7
Constraints
1 ≤ n ≤ 100 1 ≤ W ≤ 1000
Practice 0-1 Knapsack Problem free on ExecCode. Browse DSA problems, topic map, and placement guides.