Prerequisites: Introduction to Knapsack Problem, its Types and How to solve them
Given N items where each item has some weight and profit associated with it and also given a bag with capacity W, [i.e., the bag can hold at most W weight in it]. The task is to put the items into the bag such that the sum of profits associated with them is the maximum possible.
Note: The constraint here is we can either put an item completely into the bag or cannot put it at all [It is not possible to put a part of an item into the bag].
Examples:
Input: N = 3, W = 4, profit[] = {1, 2, 3}, weight[] = {4, 5, 1}
Output: 3
Explanation: There are two items which have weight less than or equal to 4. If we select the item with weight 4, the possible profit is 1. And if we select the item with weight 1, the possible profit is 3. So the maximum possible profit is 3. Note that we cannot put both the items with weight 4 and 1 together as the capacity of the bag is 4.
Input: N = 3, W = 3, profit[] = {1, 2, 3}, weight[] = {4, 5, 6}
Output: 0
Recursion Approach for 0/1 Knapsack Problem:
To solve the problem follow the below idea:
A simple solution is to consider all subsets of items and calculate the total weight and profit of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the subset with maximum profit.
Optimal Substructure: To consider all subsets of items, there can be two cases for every item.
- Case 1: The item is included in the optimal subset.
- Case 2: The item is not included in the optimal set.
Follow the below steps to solve the problem:
The maximum value obtained from ‘N’ items is the max of the following two values.
- Case 1 (include the Nth item): Value of the Nth item plus maximum value obtained by remaining N-1 items and remaining weight i.e. (W-weight of the Nth item).
- Case 2 (exclude the Nth item): Maximum value obtained by N-1 items and W weight.
- If the weight of the ‘Nth‘ item is greater than ‘W’, then the Nth item cannot be included and Case 2 is the only possibility.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max( int a, int b) { return (a > b) ? a : b; }
int knapSack( int W, int wt[], int val[], int n)
{
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(
val[n - 1]
+ knapSack(W - wt[n - 1], wt, val, n - 1),
knapSack(W, wt, val, n - 1));
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof (profit) / sizeof (profit[0]);
cout << knapSack(W, weight, profit, n);
return 0;
}
|
C
#include <stdio.h>
int max( int a, int b) { return (a > b) ? a : b; }
int knapSack( int W, int wt[], int val[], int n)
{
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(
val[n - 1]
+ knapSack(W - wt[n - 1], wt, val, n - 1),
knapSack(W, wt, val, n - 1));
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof (profit) / sizeof (profit[0]);
printf ( "%d" , knapSack(W, weight, profit, n));
return 0;
}
|
Java
class Knapsack {
static int max( int a, int b) { return (a > b) ? a : b; }
static int knapSack( int W, int wt[], int val[], int n)
{
if (n == 0 || W == 0 )
return 0 ;
if (wt[n - 1 ] > W)
return knapSack(W, wt, val, n - 1 );
else
return max(val[n - 1 ]
+ knapSack(W - wt[n - 1 ], wt,
val, n - 1 ),
knapSack(W, wt, val, n - 1 ));
}
public static void main(String args[])
{
int profit[] = new int [] { 60 , 100 , 120 };
int weight[] = new int [] { 10 , 20 , 30 };
int W = 50 ;
int n = profit.length;
System.out.println(knapSack(W, weight, profit, n));
}
}
|
Python
def knapSack(W, wt, val, n):
if n = = 0 or W = = 0 :
return 0
if (wt[n - 1 ] > W):
return knapSack(W, wt, val, n - 1 )
else :
return max (
val[n - 1 ] + knapSack(
W - wt[n - 1 ], wt, val, n - 1 ),
knapSack(W, wt, val, n - 1 ))
if __name__ = = '__main__' :
profit = [ 60 , 100 , 120 ]
weight = [ 10 , 20 , 30 ]
W = 50
n = len (profit)
print knapSack(W, weight, profit, n)
|
C#
using System;
class GFG {
static int max( int a, int b) { return (a > b) ? a : b; }
static int knapSack( int W, int [] wt, int [] val, int n)
{
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(val[n - 1]
+ knapSack(W - wt[n - 1], wt,
val, n - 1),
knapSack(W, wt, val, n - 1));
}
public static void Main()
{
int [] profit = new int [] { 60, 100, 120 };
int [] weight = new int [] { 10, 20, 30 };
int W = 50;
int n = profit.Length;
Console.WriteLine(knapSack(W, weight, profit, n));
}
}
|
Javascript
<script>
function max(a, b)
{
return (a > b) ? a : b;
}
function knapSack(W, wt, val, n)
{
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(val[n - 1] +
knapSack(W - wt[n - 1], wt, val, n - 1),
knapSack(W, wt, val, n - 1));
}
let profit = [ 60, 100, 120 ];
let weight = [ 10, 20, 30 ];
let W = 50;
let n = profit.length;
document.write(knapSack(W, weight, profit, n));
</script>
|
PHP
<?php
function knapSack( $W , $wt , $val , $n )
{
if ( $n == 0 || $W == 0)
return 0;
if ( $wt [ $n - 1] > $W )
return knapSack( $W , $wt , $val , $n - 1);
else
return max( $val [ $n - 1] +
knapSack( $W - $wt [ $n - 1],
$wt , $val , $n - 1),
knapSack( $W , $wt , $val , $n -1));
}
$profit = array (60, 100, 120);
$weight = array (10, 20, 30);
$W = 50;
$n = count ( $profit );
echo knapSack( $W , $weight , $profit , $n );
?>
|
Time Complexity: O(2N)
Auxiliary Space: O(N), Stack space required for recursion
Dynamic Programming Approach for 0/1 Knapsack Problem
Memoization Approach for 0/1 Knapsack Problem:
Note: It should be noted that the above function using recursion computes the same subproblems again and again. See the following recursion tree, K(1, 1) is being evaluated twice.
In the following recursion tree, K() refers to knapSack(). The two parameters indicated in the following recursion tree are n and W.
The recursion tree is for following sample inputs.
weight[] = {1, 1, 1}, W = 2, profit[] = {10, 20, 30}
K(3, 2)
/ \
/ \
K(2, 2) K(2, 1)
/ \ / \
/ \ / \
K(1, 2) K(1, 1) K(1, 1) K(1, 0)
/ \ / \ / \
/ \ / \ / \
K(0, 2) K(0, 1) K(0, 1) K(0, 0) K(0, 1) K(0, 0)
Recursion tree for Knapsack capacity 2 units and 3 items of 1 unit weight.
As there are repetitions of the same subproblem again and again we can implement the following idea to solve the problem.
If we get a subproblem the first time, we can solve this problem by creating a 2-D array that can store a particular state (n, w). Now if we come across the same state (n, w) again instead of calculating it in exponential complexity we can directly return its result stored in the table in constant time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int knapSackRec( int W, int wt[], int val[], int index, int ** dp)
{
if (index < 0)
return 0;
if (dp[index][W] != -1)
return dp[index][W];
if (wt[index] > W) {
dp[index][W] = knapSackRec(W, wt, val, index - 1, dp);
return dp[index][W];
}
else {
dp[index][W] = max(val[index]
+ knapSackRec(W - wt[index], wt, val,
index - 1, dp),
knapSackRec(W, wt, val, index - 1, dp));
return dp[index][W];
}
}
int knapSack( int W, int wt[], int val[], int n)
{
int ** dp;
dp = new int *[n];
for ( int i = 0; i < n; i++)
dp[i] = new int [W + 1];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < W + 1; j++)
dp[i][j] = -1;
return knapSackRec(W, wt, val, n - 1, dp);
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof (profit) / sizeof (profit[0]);
cout << knapSack(W, weight, profit, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int max( int a, int b) { return (a > b) ? a : b; }
static int knapSackRec( int W, int wt[], int val[],
int n, int [][] dp)
{
if (n == 0 || W == 0 )
return 0 ;
if (dp[n][W] != - 1 )
return dp[n][W];
if (wt[n - 1 ] > W)
return dp[n][W]
= knapSackRec(W, wt, val, n - 1 , dp);
else
return dp[n][W]
= max((val[n - 1 ]
+ knapSackRec(W - wt[n - 1 ], wt, val,
n - 1 , dp)),
knapSackRec(W, wt, val, n - 1 , dp));
}
static int knapSack( int W, int wt[], int val[], int N)
{
int dp[][] = new int [N + 1 ][W + 1 ];
for ( int i = 0 ; i < N + 1 ; i++)
for ( int j = 0 ; j < W + 1 ; j++)
dp[i][j] = - 1 ;
return knapSackRec(W, wt, val, N, dp);
}
public static void main(String[] args)
{
int profit[] = { 60 , 100 , 120 };
int weight[] = { 10 , 20 , 30 };
int W = 50 ;
int N = profit.length;
System.out.println(knapSack(W, weight, profit, N));
}
}
|
Python3
def knapsack(wt, val, W, n):
if n = = 0 or W = = 0 :
return 0
if t[n][W] ! = - 1 :
return t[n][W]
if wt[n - 1 ] < = W:
t[n][W] = max (
val[n - 1 ] + knapsack(
wt, val, W - wt[n - 1 ], n - 1 ),
knapsack(wt, val, W, n - 1 ))
return t[n][W]
elif wt[n - 1 ] > W:
t[n][W] = knapsack(wt, val, W, n - 1 )
return t[n][W]
if __name__ = = '__main__' :
profit = [ 60 , 100 , 120 ]
weight = [ 10 , 20 , 30 ]
W = 50
n = len (profit)
t = [[ - 1 for i in range (W + 1 )] for j in range (n + 1 )]
print (knapsack(weight, profit, W, n))
|
C#
using System;
public class GFG {
static int max( int a, int b) { return (a > b) ? a : b; }
static int knapSackRec( int W, int [] wt, int [] val,
int n, int [, ] dp)
{
if (n == 0 || W == 0)
return 0;
if (dp[n, W] != -1)
return dp[n, W];
if (wt[n - 1] > W)
return dp[n, W]
= knapSackRec(W, wt, val, n - 1, dp);
else
return dp[n, W]
= max((val[n - 1]
+ knapSackRec(W - wt[n - 1], wt, val,
n - 1, dp)),
knapSackRec(W, wt, val, n - 1, dp));
}
static int knapSack( int W, int [] wt, int [] val, int N)
{
int [, ] dp = new int [N + 1, W + 1];
for ( int i = 0; i < N + 1; i++)
for ( int j = 0; j < W + 1; j++)
dp[i, j] = -1;
return knapSackRec(W, wt, val, N, dp);
}
static public void Main()
{
int [] profit = new int [] { 60, 100, 120 };
int [] weight = new int [] { 10, 20, 30 };
int W = 50;
int N = profit.Length;
Console.WriteLine(knapSack(W, weight, profit, N));
}
}
|
Javascript
<script>
function max(a, b)
{
return (a > b) ? a : b;
}
function knapSackRec(W, wt, val, n,dp)
{
if (n == 0 || W == 0)
return 0;
if (dp[n][W] != -1)
return dp[n][W];
if (wt[n - 1] > W)
return dp[n][W] = knapSackRec(W, wt, val,
n - 1, dp);
else
return dp[n][W] = max((val[n - 1] +
knapSackRec(W - wt[n - 1], wt,
val, n - 1, dp)),
knapSackRec(W, wt, val,
n - 1, dp));
}
function knapSack( W, wt,val,N)
{
var dp = new Array(N+1).fill(-1).map(el => new Array(W+1).fill(-1);
return knapSackRec(W, wt, val, N, dp);
}
var profit= [ 60, 100, 120 ];
var weight = [ 10, 20, 30 ];
var W = 50;
var N = profit.length;
document.write(knapSack(W, weight, profit, N));
</script>
|
Time Complexity: O(N * W). As redundant calculations of states are avoided.
Auxiliary Space: O(N * W) + O(N). The use of a 2D array data structure for storing intermediate states and O(N) auxiliary stack space(ASS) has been used for recursion stack
Bottom-up Approach for 0/1 Knapsack Problem:
To solve the problem follow the below idea:
Since subproblems are evaluated again, this problem has Overlapping Sub-problems property. So the 0/1 Knapsack problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, re-computation of the same subproblems can be avoided by constructing a temporary array K[][] in a bottom-up manner.
Illustration:
Below is the illustration of the above approach:
Let, weight[] = {1, 2, 3}, profit[] = {10, 15, 40}, Capacity = 6
- If no element is filled, then the possible profit is 0.
- For filling the first item in the bag: If we follow the above mentioned procedure, the table will look like the following.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
10 |
10 |
10 |
10 |
10 |
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- For filling the second item:
When jthWeight = 2, then maximum possible profit is max (10, DP[1][2-2] + 15) = max(10, 15) = 15.
When jthWeight = 3, then maximum possible profit is max(2 not put, 2 is put into bag) = max(DP[1][3], 15+DP[1][3-2]) = max(10, 25) = 25.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
10 |
10 |
10 |
10 |
10 |
10 |
0 |
10 |
15 |
25 |
25 |
25 |
25 |
|
|
|
|
|
|
|
- For filling the third item:
When jthWeight = 3, the maximum possible profit is max(DP[2][3], 40+DP[2][3-3]) = max(25, 40) = 40.
When jthWeight = 4, the maximum possible profit is max(DP[2][4], 40+DP[2][4-3]) = max(25, 50) = 50.
When jthWeight = 5, the maximum possible profit is max(DP[2][5], 40+DP[2][5-3]) = max(25, 55) = 55.
When jthWeight = 6, the maximum possible profit is max(DP[2][6], 40+DP[2][6-3]) = max(25, 65) = 65.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
10 |
10 |
10 |
10 |
10 |
10 |
0 |
10 |
15 |
25 |
25 |
25 |
25 |
0 |
10 |
15 |
40 |
50 |
55 |
65 |
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max( int a, int b) { return (a > b) ? a : b; }
int knapSack( int W, int wt[], int val[], int n)
{
int i, w;
vector<vector< int > > K(n + 1, vector< int >(W + 1));
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof (profit) / sizeof (profit[0]);
cout << knapSack(W, weight, profit, n);
return 0;
}
|
C
#include <stdio.h>
int max( int a, int b) { return (a > b) ? a : b; }
int knapSack( int W, int wt[], int val[], int n)
{
int i, w;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof (profit) / sizeof (profit[0]);
printf ( "%d" , knapSack(W, weight, profit, n));
return 0;
}
|
Java
import java.io.*;
class Knapsack {
static int max( int a, int b) { return (a > b) ? a : b; }
static int knapSack( int W, int wt[], int val[], int n)
{
int i, w;
int K[][] = new int [n + 1 ][W + 1 ];
for (i = 0 ; i <= n; i++) {
for (w = 0 ; w <= W; w++) {
if (i == 0 || w == 0 )
K[i][w] = 0 ;
else if (wt[i - 1 ] <= w)
K[i][w]
= max(val[i - 1 ]
+ K[i - 1 ][w - wt[i - 1 ]],
K[i - 1 ][w]);
else
K[i][w] = K[i - 1 ][w];
}
}
return K[n][W];
}
public static void main(String args[])
{
int profit[] = new int [] { 60 , 100 , 120 };
int weight[] = new int [] { 10 , 20 , 30 };
int W = 50 ;
int n = profit.length;
System.out.println(knapSack(W, weight, profit, n));
}
}
|
Python3
def knapSack(W, wt, val, n):
K = [[ 0 for x in range (W + 1 )] for x in range (n + 1 )]
for i in range (n + 1 ):
for w in range (W + 1 ):
if i = = 0 or w = = 0 :
K[i][w] = 0
elif wt[i - 1 ] < = w:
K[i][w] = max (val[i - 1 ]
+ K[i - 1 ][w - wt[i - 1 ]],
K[i - 1 ][w])
else :
K[i][w] = K[i - 1 ][w]
return K[n][W]
if __name__ = = '__main__' :
profit = [ 60 , 100 , 120 ]
weight = [ 10 , 20 , 30 ]
W = 50
n = len (profit)
print (knapSack(W, weight, profit, n))
|
C#
using System;
class GFG {
static int max( int a, int b) { return (a > b) ? a : b; }
static int knapSack( int W, int [] wt, int [] val, int n)
{
int i, w;
int [, ] K = new int [n + 1, W + 1];
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i, w] = 0;
else if (wt[i - 1] <= w)
K[i, w] = Math.Max(
val[i - 1]
+ K[i - 1, w - wt[i - 1]],
K[i - 1, w]);
else
K[i, w] = K[i - 1, w];
}
}
return K[n, W];
}
static void Main()
{
int [] profit = new int [] { 60, 100, 120 };
int [] weight = new int [] { 10, 20, 30 };
int W = 50;
int n = profit.Length;
Console.WriteLine(knapSack(W, weight, profit, n));
}
}
|
Javascript
<script>
function max(a, b)
{
return (a > b) ? a : b;
}
function knapSack(W, wt, val, n)
{
let i, w;
let K = new Array(n + 1);
for (i = 0; i <= n; i++)
{
K[i] = new Array(W + 1);
for (w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w]
= max(val[i - 1]
+ K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
let profit = [ 60, 100, 120 ];
let weight = [ 10, 20, 30 ];
let W = 50;
let n = profit.length;
document.write(knapSack(W, weight, profit, n));
</script>
|
PHP
<?php
function knapSack( $W , $wt , $val , $n )
{
$K = array ( array ());
for ( $i = 0; $i <= $n ; $i ++)
{
for ( $w = 0; $w <= $W ; $w ++)
{
if ( $i == 0 || $w == 0)
$K [ $i ][ $w ] = 0;
else if ( $wt [ $i - 1] <= $w )
$K [ $i ][ $w ] = max( $val [ $i - 1] +
$K [ $i - 1][ $w -
$wt [ $i - 1]],
$K [ $i - 1][ $w ]);
else
$K [ $i ][ $w ] = $K [ $i - 1][ $w ];
}
}
return $K [ $n ][ $W ];
}
$profit = array (60, 100, 120);
$weight = array (10, 20, 30);
$W = 50;
$n = count ( $profit );
echo knapSack( $W , $weight , $profit , $n );
?>
|
Time Complexity: O(N * W). where ‘N’ is the number of elements and ‘W’ is capacity.
Auxiliary Space: O(N * W). The use of a 2-D array of size ‘N*W’.
Space optimized Approach for 0/1 Knapsack Problem using Dynamic Programming:
To solve the problem follow the below idea:
For calculating the current row of the dp[] array we require only previous row, but if we start traversing the rows from right to left then it can be done with a single row only
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int knapSack( int W, int wt[], int val[], int n)
{
int dp[W + 1];
memset (dp, 0, sizeof (dp));
for ( int i = 1; i < n + 1; i++) {
for ( int w = W; w >= 0; w--) {
if (wt[i - 1] <= w)
dp[w] = max(dp[w],
dp[w - wt[i - 1]] + val[i - 1]);
}
}
return dp[W];
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof (profit) / sizeof (profit[0]);
cout << knapSack(W, weight, profit, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int knapSack( int W, int wt[], int val[], int n)
{
int [] dp = new int [W + 1 ];
for ( int i = 1 ; i < n + 1 ; i++) {
for ( int w = W; w >= 0 ; w--) {
if (wt[i - 1 ] <= w)
dp[w]
= Math.max(dp[w], dp[w - wt[i - 1 ]]
+ val[i - 1 ]);
}
}
return dp[W];
}
public static void main(String[] args)
{
int profit[] = { 60 , 100 , 120 };
int weight[] = { 10 , 20 , 30 };
int W = 50 ;
int n = profit.length;
System.out.print(knapSack(W, weight, profit, n));
}
}
|
Python3
def knapSack(W, wt, val, n):
dp = [ 0 for i in range (W + 1 )]
for i in range ( 1 , n + 1 ):
for w in range (W, 0 , - 1 ):
if wt[i - 1 ] < = w:
dp[w] = max (dp[w], dp[w - wt[i - 1 ]] + val[i - 1 ])
return dp[W]
if __name__ = = '__main__' :
profit = [ 60 , 100 , 120 ]
weight = [ 10 , 20 , 30 ]
W = 50
n = len (profit)
print (knapSack(W, weight, profit, n))
|
C#
using System;
public class GFG {
static int knapSack( int W, int [] wt, int [] val, int n)
{
int [] dp = new int [W + 1];
for ( int i = 1; i < n + 1; i++) {
for ( int w = W; w >= 0; w--) {
if (wt[i - 1] <= w)
dp[w]
= Math.Max(dp[w], dp[w - wt[i - 1]]
+ val[i - 1]);
}
}
return dp[W];
}
public static void Main(String[] args)
{
int [] profit = { 60, 100, 120 };
int [] weight = { 10, 20, 30 };
int W = 50;
int n = profit.Length;
Console.Write(knapSack(W, weight, profit, n));
}
}
|
Javascript
<script>
function knapSack(W , wt , val , n)
{
var dp = Array(W + 1).fill(0);
for (i = 1; i < n + 1; i++) {
for (w = W; w >= 0; w--) {
if (wt[i - 1] <= w)
dp[w] = Math.max(dp[w], dp[w - wt[i - 1]] + val[i - 1]);
}
}
return dp[W];
}
var profit = [ 60, 100, 120 ];
var weight = [ 10, 20, 30 ];
var W = 50;
var n = profit.length;
document.write(knapSack(W, weight, profit, n));
</script>
|
Time Complexity: O(N * W). As redundant calculations of states are avoided
Auxiliary Space: O(W) As we are using a 1-D array instead of a 2-D array
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!