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 952587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:55:06+00:00 2026-05-15T23:55:06+00:00

My goal here is to find all possible combinations that sums to a given

  • 0

My goal here is to find all possible combinations that sums to a given total.
For example, if the array is
2 59 3 43 5 9 8 62 10 4 and if the total is 12, then possible combinations are

2 10
3 9
8 4
5 3 4

Here is the first set of code, that I’ve written. Wondering the best improvements that can be done on this.

   int find_numbers_matching_sum(int *number_coll, int total)
{

    int *search_till = lower_bound(number_coll,number_coll+TOTAL_SIZE, total);
    int location = search_till - number_coll;
    if (*search_till > total && location > 0 )
    {
        --location;
    }

    while ( location >= 0 )
    {
        find_totals(number_coll,total,location);
        --location;
    }
    return 1;
}

int find_totals(int *number_coll, int total, int end_location)
{
    int left_ones = total - number_coll[end_location];
    int curloc = end_location;
    int *search_till = 0;
    int location ;
    int all_numbers[10];
    int i = 0;

    all_numbers[i] = number_coll[end_location];
    while ( left_ones && curloc >= 0 )
    {
        search_till = lower_bound(number_coll,number_coll+end_location, left_ones);
        location = search_till - number_coll;
        if (*search_till > left_ones && location > 0 )
        {
            --location;
        }
        else if ( left_ones < *search_till )
        {
            break;
        }
        curloc=location;
        left_ones = left_ones - number_coll[curloc];
        all_numbers[++i] = number_coll[curloc];
        end_location = curloc - 1;
    }

    if ( !left_ones )
    {
        while ( i>=0)
        {
            cout << all_numbers[i--] << ' ';
        }
    }
    cout << endl;
    return 1;


}
  • 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-15T23:55:07+00:00Added an answer on May 15, 2026 at 11:55 pm
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    struct State {
        int v;
        const State *rest;
        void dump() const {
            if(rest) {
                cout << ' ' << v;
                rest->dump();
            } else {
                cout << endl;
            }
        }
        State() : v(0), rest(0) {}
        State(int _v, const State &_rest) : v(_v), rest(&_rest) {}
    };
    
    void ss(int *ip, int *end, int target, const State &state) {
        if(target < 0) return; // assuming we don't allow any negatives
        if(ip==end && target==0) {
            state.dump();
            return;
        }
        if(ip==end)
            return;
        { // without the first one
            ss(ip+1, end, target, state);
        }
        { // with the first one
            int first = *ip;
            ss(ip+1, end, target-first, State(first, state));
        }
    }
    
    int main() {
        int a[] = { 2,59,3,43,5,9,8,62,10,4 };
        int * start = &a[0];
        int * end = start + sizeof(a) / sizeof(a[0]);
        ss(start, end, 12, State());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the example: http://jsfiddle.net/FdT23/4/ The goal is that when you hover your mouse
My goal here is to open temp.php, explode by ### (line terminator), then by
The goal here is to take some list of coordinates, like [[1,2],[3,4],[7,1]] , then
Here's my ultimate goal in all of this. I have a viewcontroller with a
Here's my list that I want to expand/collapse: My goal is simply to toggle
I saw the following example disccussed here previously, where the goal was to return
All: I'm seeking input/guidance/and design ideas. My goal is to find a lean but
I've looked at all the tutorials I can find, examined the questions on here
Hi this is my first post here hope you all are well. So Im
The goal here is maximum conversion of users to an application. I tried to

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.