AlgoPlus//greedy / fractional-knapsack
Read the theory

Fractional Knapsack

Fill the bag by value-density — take whole items, then a fraction to top it off.

Capacity
Value
Your items (value/weight, comma-separated)
Legend
Current / partial item
Taken whole
AI Tutor Workspace
In a nutshell
The fractional knapsack problem fills a bag with a fixed weight capacity to collect the most total value, and — unlike the 0/1 version — you may take any fraction of an item. That makes a simple greedy rule optimal: rank items by value-per-weight (their density), take the densest items whole until the bag is nearly full, then take just enough of a fraction of the next one to reach the capacity exactly. The only real work is the sort, so it runs in O(n log n).
Ready
Press play to begin the cinematic walkthrough.
When you can take fractions, value-per-weight is all that matters: pour in the densest item first, and top off the bag with a slice of the next. Greed is optimal here.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Fractional Knapsack.