0/1 KnapSack Code - No DP Recursive Approach

PHOTO EMBED

Sun Jul 02 2023 12:23:06 GMT+0000 (Coordinated Universal Time)

Saved by @rk_1329 #c++

int knapSack(int W, int wt[], int val[], int n) 
{ 
   if(W==0 || n==0) return 0;
   if(wt[n-1]<=W){
       return max(
         val[n-1] + knapSack(W-wt[n-1], wt, val, n-1), 
         knapSack(W, wt, val, n-1)
       );
   }
   else {
       return knapSack(W, wt, val, n-1);
   }
}
content_copyCOPY

This is most basic approach using recursion. This code can be optimized using Memoization and Tabulation methods of Dynamic Programming.

https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1