I have a listbox in C# with the following items:
Package1
Package2
Package3
Package4
Package5
and so on...
The user can select multiple items from this listbox. I need an algorithm in c# or Java (preferably in c#) that can tell me all the possible selections that a user can do, for example Package1 and Package2, Package3 and Package1, Package2, Package4 and Package 3, etc.
You can use recursion, “guess” if the first package is in the combination or not – and invoke recursively without the first package.
Pseudo-code:
Notes:
2^npossible combinations for chosing packages, so any algorithm that do what you ask for [including this one] will needO(2^n)time, so I would not try to invoke this algorithm with 100 packages….In java, it could look something like this:
and invokation is by:
Where
packagesis aLinkedListwith all your packages.LinkedListforpackagesand created new objects because Ifind it more clear. To enhance performance – you can use a single
array with
index[which will be changed between recursive calls] inorder to avoid duplicating
packagesso many times.