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

studio
variable

#1

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


#2

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

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?


#4

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


#5

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…


#6

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


#7

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


#8

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


#9

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

TIA