1

Regarding a question I got from one of my friends I want to ask the best possible solution The situation is that I have a list of integers for example

2 5 6 8

And I want to get to the integers 17 I can only use each integers ones.

The closest you can get in this case is 16 because no combination leads up to 17.

public class Item
{
    public int Weight { get; set; }
    public int Value { get; set; }
}
public class Program
{
    public static void Main()
    {

        var items = new[]
        {
            new Item {Value = 60, Weight = 10},
            new Item {Value = 100, Weight = 20},
            new Item {Value = 120, Weight = 30},
        };

        Console.WriteLine(KnapSackRecursive(items, 50));
    }
    public static int KnapSackRecursive(Item[] items, int capacity)
    {
        // keep track of the best value seen.

        //TODO: Make it a list of numbers
        int best = 0;
        for (int i = 0; i < items.Length; i++)
        {
            // This is an array of the other items.
            var otherItems = items.Take(i).Concat(items.Skip(i + 1)).ToArray();

            // Calculate the best value without using the current item.
            int without = KnapSackRecursive(otherItems, capacity);
            int with = 0;

            // If the current item fits then calculate the best value for
            // a capacity less it's weight and with it removed from contention
            // and add the current items value to that.
            if (items[i].Weight <= capacity)
            {
                with = KnapSackRecursive(otherItems, capacity - items[i].Weight)
                    + items[i].Value;
            }

            // The current best is the max of the with or without.
            int currentBest = Math.Max(without, with);

            // determine if the current best is the overall best.
            if (currentBest > best)
                best = currentBest;
        }

        return best;
    }

}

Edit: It now finds the best possible weight based on the list. It'll result in finding that 20+30 = 50 so it returns 100+120 = 220 I want it to return ("Found best possible combination: 100 + 120 = 220") not just ("220")

RefDev001
  • 13
  • 4
  • 3
    Hint: take a look at the subset sum problem. – Willem Van Onsem Feb 16 '20 at 17:20
  • 3
    Does this answer your question? [Algorithm to pick values from array that sum closest to a target value?](https://stackoverflow.com/questions/3134275/algorithm-to-pick-values-from-array-that-sum-closest-to-a-target-value) – willman Feb 16 '20 at 17:23
  • 1
    related: https://stackoverflow.com/questions/34188694/if-there-is-no-subset-sum-equal-to-a-given-value-return-subset-sum-closest-to-t/34189398#34189398 – Willem Van Onsem Feb 16 '20 at 17:24
  • I got into the Knapsack problem and converted something like that. – RefDev001 Feb 16 '20 at 17:35
  • but the Knapsack is supposed to return the values together while I want the apart – RefDev001 Feb 16 '20 at 17:38
  • In any case, your code looks wrong: you are making a for loop for all the items but it is not necessary. You only need to choose whether you want to pick the __current__ item or not. If not, just pass to next recursive call. As for forwarding the best pick in the recursive way, you may read [that](https://stackoverflow.com/questions/60033904/recursive-algorithm-to-solve-change-making-problem/60035639#60035639) – grodzi Feb 17 '20 at 07:21

0 Answers0