📖 Scenario: You are packing a backpack for a hiking trip. You have a list of items, each with a weight and a value. Your backpack can only carry a limited weight. You want to choose items to maximize the total value without exceeding the weight limit.
🎯 Goal: Build a program that uses the 0 1 Knapsack algorithm to find the maximum value of items you can carry without exceeding the backpack's weight limit.
📋 What You'll Learn
Create arrays for item weights and values with exact given values
Create an integer variable for the backpack weight limit
Implement the 0 1 Knapsack dynamic programming solution using a 2D array
Print the maximum value that fits in the backpack
💡 Why This Matters
🌍 Real World
Packing efficiently for trips, resource allocation, budget management, and selecting projects with limited resources.
💼 Career
Understanding dynamic programming and optimization problems is essential for software engineers, data scientists, and algorithm developers.
Progress0 / 4 steps
1
Create item weights and values arrays
Create an integer array called weights with values {2, 3, 4, 5} and an integer array called values with values {3, 4, 5, 6}.
DSA C++
Hint
Use C++ array syntax to create weights and values with the exact numbers.
2
Set the backpack weight limit
Create an integer variable called maxWeight and set it to 5.
DSA C++
Hint
Use a simple integer variable to store the backpack's weight limit.
3
Implement the 0 1 Knapsack dynamic programming solution
Create a 2D integer array called dp with dimensions 5 by maxWeight + 1. Use a nested for loop with variables i from 1 to 4 and w from 0 to maxWeight. Inside the loops, if weights[i-1] <= w, set dp[i][w] to the maximum of values[i-1] + dp[i-1][w - weights[i-1]] and dp[i-1][w]. Otherwise, set dp[i][w] to dp[i-1][w].
DSA C++
Hint
Use a 2D array dp to store max values. Loop through items and weights. Use std::max to choose the best value.
4
Print the maximum value that fits in the backpack
Print the value of dp[4][maxWeight] using std::cout.
DSA C++
Hint
Print the last cell of the dp table which holds the maximum value.
Practice
(1/5)
1. What does the 0 1 Knapsack Problem aim to maximize given a set of items with weights and values?
easy
A. The total weight of items without exceeding the value limit
B. The total value of items without exceeding the weight limit
C. The number of items selected regardless of weight
D. The minimum weight of items selected
Solution
Step 1: Understand the problem goal
The 0 1 Knapsack Problem tries to select items to maximize total value without exceeding the weight capacity.
Step 2: Identify the constraint
The total weight of selected items must not exceed the given weight limit.
Final Answer:
The total value of items without exceeding the weight limit -> Option B
Quick Check:
Maximize value with weight limit = A [OK]
Hint: Maximize value, keep total weight ≤ capacity [OK]
Common Mistakes:
Confusing weight limit with value limit
Trying to maximize weight instead of value
Ignoring the weight constraint
2. Which of the following is the correct way to declare the DP table for the 0 1 Knapsack Problem in C++ if n is number of items and W is max weight?
easy
A. int dp[n+1][W+1];
B. int dp[W+1][n+1];
C. int dp[n][W];
D. int dp[W][n];
Solution
Step 1: Understand DP table dimensions
The DP table stores solutions for items 0 to n and weights 0 to W, so size is (n+1) x (W+1).
Step 2: Match correct declaration
int dp[n+1][W+1]; declares dp with dimensions [n+1][W+1], matching the problem requirements.
3. What is the output of the following C++ code snippet for 0 1 Knapsack Problem?
int values[] = {60, 100, 120};
int weights[] = {10, 20, 30};
int W = 50;
int n = 3;
int dp[4][51] = {0};
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (weights[i-1] <= w) {
dp[i][w] = std::max(dp[i-1][w], dp[i-1][w - weights[i-1]] + values[i-1]);
} else {
dp[i][w] = dp[i-1][w];
}
}
}
std::cout << dp[n][W] << std::endl;
medium
A. 160
B. 180
C. 220
D. 250
Solution
Step 1: Understand the DP logic
The code fills dp table to find max value for weight limit 50 with items of given weights and values.
Step 2: Calculate max value manually
Items: (10,60), (20,100), (30,120). Max value for weight 50 is choosing items 2 and 3: 100 + 120 = 220.
Final Answer:
220 -> Option C
Quick Check:
Max value for W=50 = 220 [OK]
Hint: Pick items with max value under weight limit [OK]
Common Mistakes:
Adding weights instead of values
Choosing items exceeding weight limit
Misindexing arrays causing wrong dp values
4. Identify the bug in this 0 1 Knapsack DP code snippet:
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (weights[i] <= w) {
dp[i][w] = std::max(dp[i-1][w], dp[i-1][w - weights[i]] + values[i]);
} else {
dp[i][w] = dp[i-1][w];
}
}
}
medium
A. Indexing weights and values with i instead of i-1
B. Loop ranges should start from 0 instead of 1
C. Using std::max instead of a custom max function
D. Not initializing dp array to zero
Solution
Step 1: Check array indexing
Arrays weights and values are zero-indexed, but code uses i which starts at 1, causing out-of-bound or wrong access.
Step 2: Correct indexing
Use weights[i-1] and values[i-1] to access correct elements.
Final Answer:
Indexing weights and values with i instead of i-1 -> Option A
Quick Check:
Zero-based arrays need i-1 index [OK]
Hint: Use i-1 for zero-based arrays in loops starting at 1 [OK]
Common Mistakes:
Accessing arrays with wrong index causing runtime errors
Starting loops at 0 but using i-1 indexing
Ignoring dp initialization
5. Given items with values {20, 5, 10, 40, 15, 25} and weights {1, 2, 3, 8, 7, 4}, and max weight 10, what is the maximum value achievable using 0 1 Knapsack?