Click here to Skip to main content
15,169,249 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
for example
my list of item=2,3,4,5,6,7,8,9,10
summation of target=30
item in set=5

output like:-

23456,34567,96541, ........, ......
every set should be unique

and same number should not be repeat

like 22345 that wrong... but 2 is two time in list of item then that is correct.

What I have tried:

VB
Dim finalList As New List(Of List(Of Object))
i -= 1
'Add the objects to the first sub list.
Dim indexs(k - 1) As Integer
Dim firstSubList As New List(Of Object)
For j As Integer = 0 To k - 1
    indexs(j) = j
    firstSubList.Add(items(j))
Next
'Add the first sub list to the parent list.
finalList.Add(firstSubList)
'Continue adding lasts.
While indexs(0) <> n - k AndAlso finalList.Count < 2147483647
    If indexs(i) < i + (n - k) Then
        indexs(i) += 1
        Dim subList As New List(Of Object)
        For Each j As Integer In indexs
            'Add the objects to the sub list.
            subList.Add(items(j))
        Next
        'Add the sub list to the parent list.
        finalList.Add(subList)
    Else
        Do
            i -= 1
        Loop While indexs(i) = i + (n - k)
        indexs(i) += 1
        For j As Integer = i + 1 To k - 1
            indexs(j) = indexs(j - 1) + 1
        Next
        Dim subList As New List(Of Object)
        For Each j As Integer In indexs
            'Add the objects to the sub list.
            subList.Add(items(j))
        Next
        'Add the sub list to the parent list.
        finalList.Add(subList)
        i = k - 1
    End If
End While
Posted
Updated 28-Jul-19 10:57am
v2
Comments
Patrice T 27-Jul-19 16:14pm
   
What is the problem with this code ?

Start by sorting your data to ensure it's ordered and then work from the lowest digit up. (This isn't strictly necessary, but it allows you to eliminate whole branches quicker.)
But there is no such thing as a unique combination of 5 values from that set which sums to less than or equal to 30; there are many solutions:
2 + 3 + 4 + 5 + 6  = 20
2 + 3 + 4 + 5 + 7  = 21
2 + 3 + 4 + 5 + 8  = 22
2 + 3 + 4 + 5 + 9  = 23
2 + 3 + 4 + 5 + 10 = 24
2 + 3 + 4 + 6 + 7  = 22
2 + 3 + 4 + 6 + 8  = 23
...
If you are looking for exactly 30, there are also multiple solutions:
2 + 3 + 6 + 9 + 10 = 30
2 + 3 + 7 + 8 + 10 = 30
2 + 4 + 5 + 9 + 10 = 30
...
So you should be prepared to find multiple combinations.
Think about how you would do it manually, and then write yourself a recursive method to process them - it's not complicated when you get your head around it: start with the "first" element, and then find all the combinations using just those to the right. If you pass the target and the number of digits allowed to the method along with the current index it's pretty simple once you get your head around recursion - which is what your tutor wants you to do!

This is your homework, not mine - so I'll give you no code! Just remember to think carefully, and use the debugger to work out exactly what is going on with your code.
Give it a try - with a bit of effort you'll get there!
   
In addition to solution #1 by OriginalGriff, i've got a small tip for you: You have to get the number of combinations without repetitions to be able to create 2d array of 5-elements numbers.

We use a combination without repetitions (Cwor) when we want to know how much we can create different k-element systems, having n-elements at our disposal, the order of elements in the system is irrelevant and elements can not be repeated.

Example: Drawing 6 numbers (k) from 49 (n) (lotto), how many chips can be obtained? The numbers can not be repeated and the order is not important. The result: 1, 3, 12, 34, 45, 46 is the same as the result: 3; 12; 45; 1; 46; 34

Cwor = nk = n!/k!(n - k)! = 9!/5!(9-5)! = 362880/120 * 24 = 362880/2880= 126


Conclusion: A resultset have to be an array of 5-elements, which counts 126 of unique subsets.
   
Quote:
How to find the possible unique combination of number of set where sum is less than equal to something

As far as I can see, this code is not related to the requirement.
Are you faking some work on the requirement ?

Solving this problem is like for most problems.
Take a sheet of paper, a pencil and solve the problem by hand in a mechanical way.
The mechanical way of solving the problem is basically your algorithm.
 2 3 4 5 6 7 8 9 10
 x x x x x          sum is ..
 x x x x   x        sum is ..
 x x x x     x      sum is ..
...
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900