VOOZH about

URL: https://www.geeksforgeeks.org/dsa/extended-knapsack-problem/

⇱ Extended Knapsack Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Extended Knapsack Problem

Last Updated : 15 Jul, 2025

Given N items, each item having a given weight Ci and a profit value Pi, the task is to maximize the profit by selecting a maximum of K items adding up to a maximum weight W.

Examples:

Input: N = 5, P[] = {2, 7, 1, 5, 3}, C[] = {2, 5, 2, 3, 4}, W = 8, K = 2. 
Output: 12 
Explanation:
Here, the maximum possible profit is when we take 2 items: item2 (P[1] = 7 and C[1] = 5) and item4 (P[3] = 5 and C[3] = 3). 
Hence, maximum profit = 7 + 5 = 12

Input: N = 5, P[] = {2, 7, 1, 5, 3}, C[] = {2, 5, 2, 3, 4}, W = 1, K = 2 
Output:
Explanation: All weights are greater than 1. Hence, no item can be picked. 

Approach: The dynamic programming approach is preferred over the general recursion approach. Let us first verify that the conditions of DP are still satisfied.  

  1. Overlapping sub-problems: When the recursive solution is tried, 1 item is added first and the solution set is (1), (2), ...(n). In the second iteration we have (1, 2) and so on where (1) and (2) are recalculated. Hence there will be overlapping solutions.
  2. Optimal substructure: Overall, each item has only two choices, either it can be included in the solution or denied. For a particular subset of z elements, the solution for (z+1)th element can either have a solution corresponding to the z elements or the (z+1)th element can be added if it doesn't exceed the knapsack constraints. Either way, the optimal substructure property is satisfied.

Let's derive the recurrence. Let us consider a 3-dimensional table dp[N][W][K], where N is the number of elements, W is the maximum weight capacity and K is the maximum number of items allowed in the knapsack. Let's define a state dp[i][j][k] where i denotes that we are considering the ith element, j denotes the current weight filled, and k denotes the number of items filled until now.
For every state dp[i][j][k], the profit is either that of the previous state (when the current state is not included) or the profit of the current item added to that of the previous state (when the current item is selected). Hence, the recurrence relation is:  

dp[i][j][k] = max( dp[i-1][j][k], dp[i-1][j-W[i]][k-1] + P[i])  

Note: In code we have used 1 based indexing so, we are doing i - 1 for weight and profit array.

Below is the implementation of the above approach:  


Output
12

Time Complexity:O(N * W * K)
Auxiliary Space:O(N * W * K)

Approach: 2 The above code takes O(N * W * K) extra space, however, this can be reduced to a 2d matrix. Note that only space would be reduced to O(K * W), and time complexity remains the same, i.e. O(N * W * K).

  1. The idea is to eliminate the array storage for items. If you see, we update a cell based on dp[i][j][k] = max( dp[i - 1][j][k],  dp[i - 1][j - weight[i ]][k - 1] + profit[i]);
    instead, we can only use a 2d matrix where the x-axis would represent knapsack capacity (0, 1, 2, 3...W) and y-axis represents k items picked (0, 1, 2, 3, ... k).
  2. Note that a cell is updated keeping only two values in mind, the previous value dp[i - 1][j][k], and the value for remaining capacity with k - 1 items. So we only need the previous row to check for [i - 1 ] and [i - 1][j - weight[i ]].
  3. A cell dp[j][k] in the matrix represents the maximum profit we can get with capacity j, if at most k, items are allowed, with all the items till i. This way we can still iterate over items but we don't need to keep storage for them.

Below is the implementation of the above approach:


Output
12

Time Complexity:O(N * W * K)
Auxiliary Space:O(W * K)


Comment
Article Tags:
Article Tags: