How to get all possible subsets of an array in uipath?

Hi,

i have an array declared as {1,2,3,4,5,6,7,8,9};
Now i need to get the all possible Subsets of Array in UIPATH…

TIA

You can create Invoke Code activity with below code and arguments:

Dim n As Int32
Dim i As Int32
Dim j As Int32
Dim counter As Int32
Dim binary As Int32
Dim skipCount As Int32
n = CInt(InputArray.Length)
Dim tempArr(n,CInt(Math.Pow(CDbl(2),CDbl(n)))) as Int32
Dim tempList As System.Collections.Generic.List(Of int32)
OutpuList = New System.Collections.Generic.List(of System.Collections.Generic.List(Of int32))

skipCount = CInt(Math.Pow(CDbl(2),CDbl(n)))
For j=0 To n-1
	skipcount=CInt(skipCount/2)
	binary = 1
	counter = 0
	For i=0 To CInt(Math.Pow(CDbl(2),CDbl(n)))-1
		temparr(j,i)=binary
		counter=(counter + 1) Mod skipCount
		If counter = 0 Then
			binary = (binary + 1) Mod 2
		End If
	Next i
Next j

For i=0 To CInt(Math.Pow(CDbl(2),CDbl(n)))-1
	tempList = New System.Collections.Generic.List(Of int32)
	For j=0 To n-1
		If tempArr(j,i)=1 Then
			templist.Add(InputArray(j))
		End If
	Next j
	OutpuList.Add(tempList)
Next i

Example workflow attached: Main.xaml (7.6 KB)

3 Likes

Thanks for the reply,it worked,but i want to know how we do it in uipath(not by invoking the code but using the activities.)and how we get the sum of each subset that’s created?

Invoke Code is an activity :slight_smile:

You can translate provided code to activities (loops, if statements and assigns) but it will be unreadable.

Add to for each loop in attached xaml Write line activity to print sum.
image

1 Like

Hi heynow,
actually i need to Find the sum of subsets from array should equal to input sum

(if i declared an array with {1,2,3,4,5} and input sum as 10,then i need to get (1,2,3,4),(2,3,5),(1,4,5)…), and i am not able to get the output of sum of each subset by using for each activity

Uploading…

Add in for each loop something like this:
image
If you add it to workflow from my first post then it should work.

1 Like

Thanks, it worked ,
i am trying to translate each and every activity to uipath other activities from ur invoke code…i am stuck at this lines…
Dim tempArr(n,CInt(Math.Pow(CDbl(2),CDbl(n)))) as Int32
skipCount = CInt(Math.Pow(CDbl(2),CDbl(n)))

how to translate it to uipath activities?

TIA

You can try something like this, use n instead of 3.

Its showing error, if u can, please upload the full workflow

TIA

Hi! Below my code:

Try
Dim k As Int32
Dim i As Double
Dim j As Double
Dim n As Int32
Dim listCombination As List(Of Int32)
Dim listAllCombinations As List(Of List(Of Int32))
n = in_arrValuesForCombinatios.Count()
For i=1 To 2^n-1
listCombination = New List(Of Double)
j=i
k=0
Do While j <> 0
If j Mod 2 =1
listCombination.Add(in_arrValuesForCombinatios(k))
End If
j=Math.Floor(j/2)
k=k+1
Loop
listAllCombinations.Add(listCombination)
Next
Catch ex As Exception
out_Ex = ex
End Try

Hi @Bandi_Vinay_Kumar

You can try this:

Assign arr = {1,2,3,4,5,6,7,8,9}
Assign subsets = New List(Of Int32)
For Each item in arr
    Assign subset = New List(Of Int32)
    For Each index in Enumerable.Range(arr.ToList.IndexOf(item), arr.Length - arr.ToList.IndexOf(item))
        Add To Collection subset item
        Append To Collection subsets subset
    End For Each
End For Each