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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:39:23+00:00 2026-06-10T20:39:23+00:00

Thanks @Scotty, @paddy. FYI, the optimal solution is this: void RecSubsets(string soFar, string rest)

  • 0

Thanks @Scotty, @paddy. FYI, the optimal solution is this:

void RecSubsets(string soFar, string rest) {
    if (rest == "") {
        cout << soFar << end;
    }
    else {
      RecSubsets(soFar + rest[0], rest.substr(1));
      RecSubsets(soFar, rest.substr(1));
    }
 }

void CanMakeSum(string set) {
    RecSubsets("", set);
 }

I’m writing a simple program to calculate all possible combinations in a set by splitting the data into 2 sets (C(n-1, k-1) & C(n-1, k)) and calling the function recursively.
Below is what I’ve written:

void RecSubsets(string soFar, string rest) {
    if (rest == "") cout << soFar << end;
    }
    else {
        for (int i = 0; i < rest.length(); i++) {
            string next = soFar + rest[i];
            string remaining = rest.substr(i+1);
            RecSubsets(next, remaining);
        }
    }
}

void CanMakeSum(string set) {
    RecSubsets("", set);
    RecSubsets("", set.substr(1));
}

int main() {    
    string set = "abcd";
    CanMakeSum(set);
    return 0;
}

So for an input string of ‘abcd’, it’d split into 2 groups (abcd and abc) and then print out the combinations by calling the function recursively. Under this logic, the output ought to be: abcd, abc, abd, ab, acd, ac, ad, a…
However, using the program above, the output is abcd, abd, acd, ad…
I understand conceptually what I’m trying to achieve, but am having difficulty translating it into codes. Can someone pls point out where I’m going wrong? Thanks

  • 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-06-10T20:39:25+00:00Added an answer on June 10, 2026 at 8:39 pm

    It’s a good attempt. There are just two small problems:

    1. You should not be “splitting the data into two sets (C(n-1, k-1) & C(n-1, k))”. That’s what your recursive function is for!

    2. RecSubsets() should always print out soFar. Remove the if (rest == "").

    For example:

    void RecSubsets(string soFar, string rest) {
        // First print out "a" or wherever we are up to
        // This ensures we correctly get "ab" and "ac" without trailing characters etc
        cout << soFar << end;
    
        // Now print out the other combinations that begin with soFar
        // This part of your algorithm was already correct :)
        for (int i = 0; i < rest.length(); i++) {
            string next = soFar + rest[i];
            string remaining = rest.substr(i+1);
            RecSubsets(next, remaining);
        }
    }
    
    void CanMakeSum(string set)
    {
        RecSubsets("", set);
        // Don't call it a second time here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for stopping by. this static analyser warning is annoying me here is my
Thanks for reading this. I have no idea why this is throwing a NullReferenceException
I have snippet of HTML in a string like this: var htmlString = '<input
Thanks for the help on here with my query on here the other day
Thanks to the epic work of Dennis Williamson, I am now able to calculate
Thanks in advance to anyone who can think of a more efficient or a
Thanks to some help I received yesterday I've got some dynamic summing working on
thanks for your reading. I am trying to modify Jquery Nivo Zoom plugin to
Thanks in advance for your help. I have a need within an application to
Thanks to the brilliant help on my XML parsing problem I got to a

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.