**Introduction to Unbounded Knapsack**

**The unbounded knapsack** determines the most valuable collection of objects that can fit in a knapsack of a particular volume given several sorts of items of various values and volumes.

The **unbounded knapsack** problem is based on dynamic programming and is an extension of the basic 0-1 knapsack problem. You may learn more about the 0-1 knapsack problem here. The only difference between 0/1 Knapsack and Unbounded Knapsack is that we may utilize an infinite number of items in this case.

Among the several types of knapsack problems, this article will discuss the **unbounded knapsack problem** in detail.

Since it is essential first to understand how an unbounded knapsack is different from the rest.

There is a brief explanation of the various kinds of knapsack problems and the primary difference in the **algorithm for unbounded knapsacks** from the rest of them.

In this blog, we have also used an example and its code snippet to understand the concept better.

(Also see Data Structures)

Also See, __Byte Array to String__

**What are knapsack problems?**

Knapsack problems are combinatorial optimization problems used to illustrate both problem and solution.

A knapsack problem includes a given set of items having some assigned weight and value, and the goal is to add the most value to the knapsack within the given weight constraint.

In such problems, there are items and a bag (knapsack) where:

- Input is the weight array and the value array (â€˜Nâ€™ set of items)
- Output must be the maximum possible profit
- The

Source: en.wikipedia.org

**Types of Knapsack**

**Fractional Knapsack **

This is a â€˜greedyâ€™ type that allows items to be divided if the bagâ€™s capacity doesnâ€™t allow the entire item.

For example, if â€˜Wâ€™ is 10 kg and the bag is filled up to 9 kg, a 2 kg item cannot be added to it. In this case, a fractional knapsack would allow just a fraction of the item to be added (1 kg here) and divide its value accordingly (dividing to half in this case).

**0-1 Knapsack**

The 0-1 knapsack is named so because it either takes an item as a whole or not at all. If the bagâ€™s capacity is 10 kg and filled up to 9 kg, it cannot pick a 2 kg item to be added. Moreover, among all the items given, one item can only be picked once for the knapsack.

**Unbounded Knapsack **

The** unbounded knapsack** too can only take items as a whole and not in parts or fractions.

However, an item first picked for the knapsack can be chosen again in the next iteration.** **

The only difference between the code for 0-1 and **unbounded knapsack** would be that the already picked elements wonâ€™t be considered the second time in the case of 0-1 knapsack, but for **unbounded knapsack**, the entire array will be considered all over again.

**Recommended**: Try the Problem yourself before moving on to the solution