codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

29 / 65 topics
27Dynamic Programming Basics28Fibonacci Sequence using DP29Knapsack Problem30Longest Common Subsequence (LCS)31Edit Distance
Tutorials/Data Structures & Algorithms/Knapsack Problem
🧮Data Structures & Algorithms

Knapsack Problem

Updated 2026-04-20
3 min read

The Knapsack Problem

The Knapsack Problem is a classic optimization problem that has applications in various fields such as economics, finance, and resource allocation. It involves selecting items with given weights and values to maximize the total value while keeping the total weight within a given limit.

Overview

Problem Statement

You are given a set of items, each with a weight and a value. You also have a knapsack that can carry a certain maximum weight. The goal is to determine the number of each item to include in the knapsack so that the total weight is less than or equal to the maximum capacity and the total value is as large as possible.

Types of Knapsack Problems

  1. 0/1 Knapsack Problem: Each item can be taken or not taken (binary decision).
  2. Fractional Knapsack Problem: Items can be broken into smaller pieces, so you can take fractions of items.
  3. Unbounded Knapsack Problem: You can take unlimited quantities of each item.

Dynamic Programming Approach

Dynamic programming is an effective method to solve the 0/1 Knapsack Problem due to its optimal substructure and overlapping subproblems properties.

Steps to Solve

  1. Define the State:

    • Let dp[i][w] represent the maximum value that can be obtained with the first i items and a knapsack capacity of w.
  2. State Transition:

    • If the weight of the i-th item is greater than w, then it cannot be included in the optimal solution for this subproblem: dp[i][w] = dp[i-1][w].
    • Otherwise, consider two cases:
      • Exclude the i-th item: dp[i][w] = dp[i-1][w]
      • Include the i-th item: dp[i][w] = value[i-1] + dp[i-1][w-weight[i-1]]
    • The optimal solution is the maximum of these two cases: dp[i][w] = Math.max(dp[i-1][w], value[i-1] + dp[i-1][w-weight[i-1]]).
  3. Initialization:

    • dp[0][w] should be 0 for all w, as no items can contribute to the value.
  4. Result:

    • The maximum value that can be obtained with all items and capacity W is stored in dp[n][W].

Code Example

Here's a Java implementation of the 0/1 Knapsack Problem using dynamic programming:

public class Knapsack {
    public static int knapSack(int W, int[] weight, int[] value, int n) {
        // Create and initialize dp array
        int[][] dp = new int[n + 1][W + 1];
        
        // Build table in bottom up manner
        for (int i = 0; i <= n; i++) {
            for (int w = 0; w <= W; w++) {
                if (i == 0 || w == 0) {
                    dp[i][w] = 0;
                } else if (weight[i - 1] <= w) {
                    dp[i][w] = Math.max(value[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
                } else {
                    dp[i][w] = dp[i - 1][w];
                }
            }
        }
        
        return dp[n][W];
    }

    public static void main(String[] args) {
        int[] value = {60, 100, 120};
        int[] weight = {10, 20, 30};
        int W = 50;
        int n = value.length;
        
        System.out.println("Maximum value in Knapsack: " + knapSack(W, weight, value, n));
    }
}

Explanation

  • Initialization: A 2D array dp is initialized with dimensions (n+1) x (W+1) to store the maximum values for different subproblems.
  • Filling the DP Table: The nested loops iterate over each item and each possible weight capacity, filling in the table based on whether the current item can be included or not.
  • Result Extraction: The final result is found at dp[n][W], which represents the maximum value that can be obtained with all items and a knapsack capacity of W.

Space Optimization

The above solution uses O(nW) space. However, it can be optimized to use O(W) space by using only two rows of the DP table at any time.

public static int knapSackOptimized(int W, int[] weight, int[] value, int n) {
    int[][] dp = new int[2][W + 1];
    
    for (int i = 0; i <= n; i++) {
        for (int w = 0; w <= W; w++) {
            if (i == 0 || w == 0) {
                dp[i % 2][w] = 0;
            } else if (weight[i - 1] <= w) {
                dp[i % 2][w] = Math.max(value[i - 1] + dp[(i - 1) % 2][w - weight[i - 1]], dp[(i - 1) % 2][w]);
            } else {
                dp[i % 2][w] = dp[(i - 1) % 2][w];
            }
        }
    }
    
    return dp[n % 2][W];
}

Explanation

  • Two Rows: Only two rows of the DP table are used at any time, reducing space complexity to O(W).
  • Indexing: i % 2 is used to toggle between the two rows.

Best Practices

  1. Understand Problem Constraints: Always check the constraints and choose the appropriate approach (0/1, fractional, or unbounded knapsack).
  2. Optimize Space Usage: Use space optimization techniques like rolling arrays to reduce memory usage.
  3. Edge Cases: Handle edge cases such as empty items list or zero capacity separately.

Conclusion

The Knapsack Problem is a fundamental problem in computer science with wide-ranging applications. By understanding the dynamic programming approach, you can efficiently solve this problem and apply similar techniques to other optimization problems.


PreviousFibonacci Sequence using DPNext Longest Common Subsequence (LCS)

Recommended Gear

Fibonacci Sequence using DPLongest Common Subsequence (LCS)