{"id":944,"date":"2025-08-21T17:59:28","date_gmt":"2025-08-21T12:29:28","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=944"},"modified":"2026-04-04T14:32:09","modified_gmt":"2026-04-04T09:02:09","slug":"fractional-knapsack","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/fractional-knapsack\/","title":{"rendered":"Fractional Knapsack | Solved using Greedy Algorithm"},"content":{"rendered":"\n<p>The <strong>Fractional Knapsack<\/strong> problem is one of the most famous problems in the <strong>Greedy Algorithm<\/strong> category. It is widely asked in coding interviews and also forms a key part of understanding optimization problems.<\/p>\n\n\n\n<p>In this blog, we will go through the problem statement, understand why the greedy approach works here, and then look at the optimal Python implementation step by step.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/fractional-knapsack-1587115620\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understand by watching video<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\" style=\"margin-top:var(--wp--preset--spacing--60);margin-bottom:var(--wp--preset--spacing--60)\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"DSA in Python - Fractional Knapsack | Greedy + Sorting Optimal Solution | GFG - Part 99 [Hindi]\" width=\"814\" height=\"611\" src=\"https:\/\/www.youtube.com\/embed\/K_qtZbLSYm0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Problem Statement \u2013 What Does the Problem Say?<\/h2>\n\n\n\n<p>You are given:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A set of items, each with a <strong>value<\/strong> and a <strong>weight<\/strong>.<\/li>\n\n\n\n<li>A knapsack (bag) with a maximum <strong>capacity<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>Your task is to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Fill the knapsack such that the <strong>total value<\/strong> is maximized.<\/li>\n\n\n\n<li>Unlike the <strong>0\/1 Knapsack problem<\/strong>, you are allowed to take <strong>fractions of items<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>Input: \nval = &#91;60, 100, 120&#93; \nwt = &#91;10, 20, 30&#93; \ncapacity = 50\n\nOutput: 240.0\nExplanation:\n- Pick item2 (value=100, weight=20) fully \u2192 remaining capacity = 30\n- Pick item3 (value=120, weight=30) fully \u2192 remaining capacity = 0\nTotal value = 100 + 120 = 220\n\nBut better choice:\n- Pick item1 (value=60, weight=10) fully \u2192 remaining capacity = 40\n- Pick item2 (value=100, weight=20) fully \u2192 remaining capacity = 20\n- Pick 2\/3 fraction of item3 (value=120, weight=30) \u2192 add 80\nTotal value = 60 + 100 + 80 = 240<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Input: <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">val = &#91;<\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">120<\/span><span style=\"color: #D4D4D4\">&#93; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">wt = &#91;<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">&#93; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">capacity = <\/span><span style=\"color: #B5CEA8\">50<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Output: <\/span><span style=\"color: #B5CEA8\">240.0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Explanation:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">- Pick item2 (<\/span><span style=\"color: #9CDCFE\">value<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">weight<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">) fully \u2192 remaining capacity = <\/span><span style=\"color: #B5CEA8\">30<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">- Pick item3 (<\/span><span style=\"color: #9CDCFE\">value<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">120<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">weight<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">) fully \u2192 remaining capacity = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Total value = <\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">120<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">220<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">But better choice:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">- Pick item1 (<\/span><span style=\"color: #9CDCFE\">value<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">weight<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">) fully \u2192 remaining capacity = <\/span><span style=\"color: #B5CEA8\">40<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">- Pick item2 (<\/span><span style=\"color: #9CDCFE\">value<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">weight<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">) fully \u2192 remaining capacity = <\/span><span style=\"color: #B5CEA8\">20<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">- Pick <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">\/<\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\"> fraction of item3 (<\/span><span style=\"color: #9CDCFE\">value<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">120<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">weight<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">) \u2192 add <\/span><span style=\"color: #B5CEA8\">80<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Total value = <\/span><span style=\"color: #B5CEA8\">60<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">80<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">240<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This shows why the greedy approach is important here.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Intuition and Approach (Optimal \u2013 Greedy)<\/h2>\n\n\n\n<p>The greedy idea comes from one key observation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To maximize value, we should always prefer items with the <strong>highest value-to-weight ratio<\/strong> first.<\/li>\n<\/ul>\n\n\n\n<p>If we are allowed to take fractions of an item, then we can always pick as much as possible from the item that gives the best &#8220;value per unit weight.&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to Solve:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>For each item, calculate its <strong>value\/weight ratio<\/strong>.<\/li>\n\n\n\n<li>Sort all items in <strong>descending order of ratio<\/strong>.<\/li>\n\n\n\n<li>Start filling the knapsack:\n<ul class=\"wp-block-list\">\n<li>If the current item fits completely, take it.<\/li>\n\n\n\n<li>If not, take only the fraction that fits in the remaining capacity.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Stop once the knapsack is full.<\/li>\n<\/ol>\n\n\n\n<p>This ensures we always get the maximum possible value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation<\/h2>\n\n\n\n<p>Here\u2019s the Python code for the <strong>Fractional Knapsack<\/strong> problem using the greedy method (with comments added for clarity):<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class Solution:\n    def fractionalknapsack(self, val, wt, capacity):\n        # Build list of (value\/weight ratio, value, weight) for each item\n        # Ignore any items with zero weight to avoid division by zero\n        items = &#91;(v \/ w, v, w) for v, w in zip(val, wt) if w > 0&#93;\n\n        # Sort by ratio in descending order\n        items.sort(key=lambda x: x&#91;0&#93;, reverse=True)\n\n        cur_weight = 0\n        total_value = 0.0\n\n        for ratio, v, w in items:\n            if cur_weight + w &lt;= capacity:\n                # take the whole item\n                cur_weight += w\n                total_value += v\n            else:\n                # take only the fraction that fits\n                remain = capacity - cur_weight\n                if remain &lt;= 0:\n                    break\n                total_value += ratio * remain\n                break  # knapsack is full\n\n        return total_value<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">fractionalknapsack<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">val<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">wt<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">capacity<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Build list of (value\/weight ratio, value, weight) for each item<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Ignore any items with zero weight to avoid division by zero<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        items = &#91;(v \/ w, v, w) <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> v, w <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">zip<\/span><span style=\"color: #D4D4D4\">(val, wt) <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> w &gt; <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Sort by ratio in descending order<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        items.sort(<\/span><span style=\"color: #9CDCFE\">key<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">lambda<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">x<\/span><span style=\"color: #D4D4D4\">: x&#91;<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">&#93;, <\/span><span style=\"color: #9CDCFE\">reverse<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        cur_weight = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        total_value = <\/span><span style=\"color: #B5CEA8\">0.0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> ratio, v, w <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> items:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cur_weight + w &lt;= capacity:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># take the whole item<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cur_weight += w<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                total_value += v<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># take only the fraction that fits<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                remain = capacity - cur_weight<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> remain &lt;= <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                total_value += ratio * remain<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">break<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># knapsack is full<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> total_value<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We first prepare a list of items with their <strong>value-to-weight ratio<\/strong> along with their original value and weight.<\/li>\n\n\n\n<li>Then we sort the items by ratio in descending order, so that the best item (most profitable per unit weight) comes first.<\/li>\n\n\n\n<li>We start filling the knapsack:\n<ul class=\"wp-block-list\">\n<li>If the item completely fits, we add its full value.<\/li>\n\n\n\n<li>If it does not fit, we take only a fraction equal to the remaining capacity.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>The process stops as soon as the knapsack reaches full capacity.<\/li>\n<\/ul>\n\n\n\n<p>This approach ensures that we are always choosing the most profitable option at every step, which is why greedy works here.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Time and Space Complexity<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Calculating ratios: <strong>O(n)<\/strong><\/li>\n\n\n\n<li>Sorting items: <strong>O(n log n)<\/strong><\/li>\n\n\n\n<li>Filling the knapsack: <strong>O(n)<\/strong><\/li>\n\n\n\n<li><strong>Overall: O(n log n)<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>We use extra space to store the list of items.<\/li>\n\n\n\n<li><strong>O(n)<\/strong> space.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>So, the efficiency mainly comes from sorting.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <strong>Fractional Knapsack<\/strong> problem is a classic example where the <strong>Greedy Algorithm<\/strong> gives the optimal solution. By always taking the item with the highest <strong>value-to-weight ratio<\/strong>, we can ensure that every unit of capacity is used in the most profitable way.<\/p>\n\n\n\n<p>This method works in <strong>O(n log n)<\/strong> time due to sorting and is both clean and efficient for practical use.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1775293004778\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Why does the greedy approach work in the Fractional Knapsack problem?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>The greedy approach works because we are allowed to take fractions of items. This means we can always pick the item with the highest value-to-weight ratio first and maximize profit at every step, ensuring an optimal solution.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293016633\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">What is the difference between Fractional Knapsack and 0\/1 Knapsack?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>In the Fractional Knapsack problem, items can be divided and taken partially, while in the 0\/1 Knapsack problem, items must be either fully taken or completely left. Because of this, greedy works for Fractional Knapsack, but not for 0\/1 Knapsack.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293026592\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">What is the time complexity of the Fractional Knapsack algorithm?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>The overall time complexity is <strong>O(n log n)<\/strong> due to sorting items based on their value-to-weight ratio. The rest of the operations (iteration and calculation) take linear time.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293041512\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Why do we sort items by value-to-weight ratio?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Sorting by value-to-weight ratio ensures that we always pick the most valuable item per unit weight first. This helps maximize the total value we can fit into the knapsack.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1775293051407\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Can Fractional Knapsack be solved using Dynamic Programming?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>While it is possible to approach it using Dynamic Programming, it is unnecessary and inefficient. The greedy method already provides an optimal and faster solution, making it the preferred approach.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<div class=\"wp-block-buttons alignwide is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-text-align-center wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Watch our python dsa course on youtube (200+ questions)<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Fractional Knapsack problem is one of the most famous problems in the Greedy Algorithm category. It is widely asked in coding interviews and also forms a key part of understanding optimization problems. In this blog, we will go through the problem statement, understand why the greedy approach works here, and then look at the<\/p>\n","protected":false},"author":1,"featured_media":946,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[41,19],"class_list":{"0":"post-944","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-greedy-algorithm","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/08\/fractional-knapsack-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions\/1119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/946"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}