Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4032714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:45:21+00:00 2026-05-20T11:45:21+00:00

I have an NSArray of NSNumbers with integer values such as [1,10,3]. I want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:45:22+00:00Added an answer on May 20, 2026 at 11:45 am

    A simple recursive solution – probably somewhat inefficient, and it’s untested, but the general idea should be clear.

    - (NSArray*)getSums:(NSArray*)numbers {
        return [[self getSumsHelper:numbers startingFrom:0] copy];
    }
    
    - (NSMutableArray*)getSumsHelper:(NSArray*)numbers startingFrom:(NSUInteger)index {
        /* (1) */
        if (index >= [numbers count])
            return [NSMutableArray arrayWithObject:[NSNumber numberWithFloat:0]];
    
        /* (2) Generate all the subsets where the `index`th element is not included */
        NSMutableArray* result = [self getSumsHelper:numbers startingFrom:index+1];
    
        /* (3) Add all the cases where the `index`th element is included */
        NSUInteger i, n = [result count];
        float element = [[numbers objectAtIndex:index] floatValue];
        for (i = 0; i < n; i++) {
            float element2 = [[result objectAtIndex:i] floatValue];
            [result addObject:[NSNumber numberWithFloat:element+element2]];
        }
    
        return result;
    }
    

    The key to the problem is to generate all the subsets of a given array. This can be done recursively by recognizing the following:

    1. The set of subsets of an empty array is the empty array, and the sum of the elements in an empty array is zero. This is handled by the lines marked with (1).

    2. If the array is not empty, then let the first element be X. Any subset either includes X or does not include it. Therefore, we will generate all the subsets of the array that does not include X (there’s the recursion, marked by (2), calculate the sums of each subset, and then duplicate the array of sums and add X to each of the sums in the duplicated part (marked by (3)).

    If you have only a handful of items in the original array (say, less than 16), then you can also count from 0 to 2n-1 in a for loop; each index will then correspond to a subset. The elements in the ith subset can be determined by writing i in base 2 and selecting those items from the array which correspond to a digit 1 in the base 2 form. I believe this is even less efficient than my solution above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSArray of NSNumbers and want to find the maximum value in
I have an NSArray consisting of NSNumbers and I want to convert this to
If I have an NSArray with some values in them. Is there a way
Example: I have an NSArray with 40 objects. What is the most efficient way
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
I have an NSArray and a lot of the values have the same values
I have a few NSDictionaries inside of an NSArray. I want to modify a
I have an NSArray of strings and I want to add a certain amount
Here is a example of what I want to do with NSArray contains NSNumber.
I have an NSArray of 500 annotations and I basically want to show only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.