Challenge - 5 Problems
Fractional Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fractional Knapsack Calculation
What is the output of the following C++ code that calculates the maximum value for the fractional knapsack problem?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Item { int value, weight; }; bool cmp(Item a, Item b) { double r1 = (double)a.value / a.weight; double r2 = (double)b.value / b.weight; return r1 > r2; } double fractionalKnapsack(int W, vector<Item> &items) { sort(items.begin(), items.end(), cmp); double totalValue = 0.0; for (auto &item : items) { if (W == 0) break; if (item.weight <= W) { W -= item.weight; totalValue += item.value; } else { totalValue += item.value * ((double)W / item.weight); W = 0; } } return totalValue; } int main() { vector<Item> items = {{60, 10}, {100, 20}, {120, 30}}; int W = 50; cout << fractionalKnapsack(W, items) << endl; return 0; }
Attempts:
2 left
💡 Hint
Sort items by value/weight ratio and take as much as possible from highest ratio first.
✗ Incorrect
The items sorted by value/weight ratio are (60/10=6), (100/20=5), (120/30=4). Taking full 10 weight of first item adds 60 value, full 20 weight of second adds 100 value, and remaining 20 weight fraction of third adds 120*(20/30)=80 value. Total = 60+100+80=240.
🧠 Conceptual
intermediateUnderstanding Fractional Knapsack Strategy
Why does the fractional knapsack problem use a greedy approach based on value-to-weight ratio?
Attempts:
2 left
💡 Hint
Think about which items give the most value per unit weight.
✗ Incorrect
The greedy approach works because taking items with the highest value per weight first ensures the knapsack is filled with the most valuable fractions possible, maximizing total value.
🔧 Debug
advancedIdentify the Bug in Fractional Knapsack Code
What error will the following code produce when compiled and run?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Item { int value, weight; }; bool cmp(Item a, Item b) { return a.value / a.weight > b.value / b.weight; } double fractionalKnapsack(int W, vector<Item> &items) { sort(items.begin(), items.end(), cmp); double totalValue = 0.0; for (auto &item : items) { if (W == 0) break; if (item.weight <= W) { W -= item.weight; totalValue += item.value; } else { totalValue += item.value * ((double)W / item.weight); W = 0; } } return totalValue; } int main() { vector<Item> items = {{60, 10}, {100, 20}, {120, 30}}; int W = 50; cout << fractionalKnapsack(W, items) << endl; return 0; }
Attempts:
2 left
💡 Hint
Does integer division affect the sorting for these specific items? Check the ratios: 60/10=6, 100/20=5, 120/30=4.
✗ Incorrect
For these items, the value/weight ratios are exact integers (6, 5, 4), so integer division produces the same comparison results as floating-point division. The sorting is correct, and the code outputs 240.
📝 Syntax
advancedSyntax Error in Fractional Knapsack Code
Which option contains the syntax error that prevents compilation?
DSA C++
struct Item {
int value, weight
};
bool cmp(Item a, Item b) {
return (double)a.value / a.weight > (double)b.value / b.weight;
}Attempts:
2 left
💡 Hint
Check punctuation after struct declarations.
✗ Incorrect
In C++, struct definitions must end with a semicolon. Missing it causes a syntax error.
🚀 Application
expertMaximum Value with Fractional Knapsack for Custom Items
Given items with values and weights: (40, 2), (100, 20), (50, 10), and knapsack capacity 25, what is the maximum value the fractional knapsack can hold?
Attempts:
2 left
💡 Hint
Calculate value/weight ratio for each item and take fractions accordingly.
✗ Incorrect
Ratios: 40/2=20, 100/20=5, 50/10=5. Take full (40,2): value 40, rem 23. Take full (50,10): value 50, total value 90, rem 13. Fraction of (100,20): (13/20)*100 = 65. Total: 155. Alternatively, after (40,2), take full (100,20): value 100, total 140, rem 3, fraction (50,10): (3/10)*50=15, total 155.
