recursive enumeration of integer subsets?

Posted by KDaker on Stack Overflow See other posts from Stack Overflow or by KDaker
Published on 2011-03-08T23:53:00Z Indexed on 2011/03/09 0:10 UTC
Read the original article Hit count: 146

Filed under:
|
|

I have an NSArray of NSNumbers with integer values such as [1,10,3]. I want to get the sum of all the possible subsets of these numbers. For example for 1,10 and 3 i would get:

1, 10, 3, 1+10=11, 1+3=4, 10+3=13, 1+10+3=14

there are 2^n possible combinations. I understand the math of it but im having difficulties putting this into code. so how can i put this into a method that would take the initial array of numbers and return an array with all the sums of the subsets? e.g -(NSArray *) getSums:(NSArray *)numbers;

I understand that the results grow exponentially but im going to be using it for small sets of numbers.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c