How to Solve the 0-1 Knapsack Problem in Excel with Formulas
Question details
The user needs an Excel formula to solve a 0-1 knapsack problem by finding the item combination with the highest total benefit while staying within a weight capacity.

- Product
- WPS Office Spreadsheets / Excel
- Device & OS
- not provided
- Scenario
- The user has items with benefits and weights and wants to choose the best subset whose total weight does not exceed a capacity limit.
- Observed behavior
- The desired state is a formula-driven Excel solution that evaluates every 0-1 item combination, respects the capacity constraint, and returns the item set with the highest total benefit.
Prepare the item names, benefits, and weights in a table and define them as named ranges such as item, benefit, and weight. Enter the capacity in a cell named cap; this formula is practical only for a limited item count because combinations double with each item.
Use a Matrix Formula to Enumerate All 0-1 Combinations
Use this when you need an exact Excel formula solution without VBA or Solver and have a small number of items.
This formula converts each item into a binary choice, builds all 2^n combinations, calculates total weight and benefit, and returns the highest-benefit combination that stays within capacity.
Because the number of combinations doubles with each added item, keep the item list short to avoid calculation slowdowns.
In WPS Spreadsheets, select the item list range, type item in the Name Box, and press Enter. Repeat for benefit and weight, then type cap in the Name Box for the capacity cell.
Select a blank cell and enter the LET formula: =LET(a,benefit,w,weight,n,COUNT(a),bits,MOD(INT(SEQUENCE(2^n,,0)/2^SEQUENCE(,n,0)),2),benefitTotals,MMULT(bits,TOCOL(a)),weightTotals,MMULT(bits,TOCOL(w)),valid,weightTotals<=cap,TEXTJOIN(", ",,FILTER(item,INDEX(bits,MATCH(MAX(FILTER(benefitTotals,valid)),IF(valid,benefitTotals),0)),0)))
Press Enter or Ctrl+Shift+Enter if your version requires legacy array entry. The cell returns a comma-separated list of item names for the highest-benefit valid combination.

Use a Random Binary Array LAMBDA for Larger or Approximate Trials
Use this when an approximate answer is acceptable or when you want a LAMBDA-based trial method that avoids precomputing every combination.
Solve the 0-1 Knapsack Problem in WPS Spreadsheets
Open the item data in WPS Spreadsheets, define the item, benefit, weight, and cap names, then enter the Excel formula to get the best capacity-valid item combination without VBA or Solver.
- 1. Open WPS Spreadsheets: Launch WPS Office, open a blank workbook or your existing .xlsx file, and enter item names, benefits, weights, and the capacity.
- 2. Define named ranges: Select each data range and type item, benefit, weight, and cap in the Name Box, pressing Enter after each name.
- 3. Input the formula: Click a blank cell, paste the LET/MMULT formula, and press Enter to calculate the optimal valid combination.
- 4. Check the result: Review the returned item list and adjust named ranges if your data changes.

Frequently Asked Questions
Does this 0-1 knapsack formula work in WPS Spreadsheets?
Yes. WPS Spreadsheets supports the Excel-style LET, SEQUENCE, MMULT, and FILTER functions used in the formula. Define the item, benefit, weight, and cap named ranges first, then paste the formula into a blank cell.
How many items can the brute-force formula handle?
The method generates 2^n combinations, so it is only practical for a small number of items. As item count increases, calculation time and memory grow quickly; keep the list as short as possible for reliable results.
What should I do if the formula returns a #CALC! or #NAME? error?
Check that all named ranges exist and that your version of WPS Spreadsheets supports dynamic array functions. You can also try entering the formula with Ctrl+Shift+Enter if legacy array mode is required.
Can I solve the knapsack problem without formulas in WPS Office?
Yes. You can use the built-in Solver add-in if it is installed for optimization models, but the formula approach is useful when you want a worksheet-only method without extra add-ins.




