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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:07:56+00:00 2026-06-01T14:07:56+00:00

This is one of the solution of getting true or false from given set

  • 0

This is one of the solution of getting true or false from given set and target value

bool subsetSumExists(Set<int> & set, int target) {
    if (set.isEmpty()) {
        return target == 0;
    } else {
        int element = set.first();
        Set<int> rest = set - element;
        return subsetSumExists(rest, target)
        || (subsetSumExists(rest, target- element));
    }
}

However, this solution will return true or false value only. How is it possible to get the element that involve in the subset(set that add together will equal to target) as well?

Do I have to use dynamic programming? Coz as i know.. recursion is building up stack actually and after the function return the value, the value inside the frame will be discarded as well.

So, is it possible to get the elements that add up equal to the target value.

Is passing an object a solution of the problem?

Thank you

  • 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-01T14:07:58+00:00Added an answer on June 1, 2026 at 2:07 pm

    First of all you can optimize your program a little bit – check if target is 0 and if it is always return true. Now what you need is to have somewhere to store the elements that you have already used. I will show you a way to do that with a global “stack”(vector in fact so that you can iterate over it), because then the code will be easier to understand, but you can also pass it by reference to the function or avoid making it global in some other way.
    By the way the stl container is called set not Set.

    vector<int> used;
    bool subsetSumExists(Set<int> & set, int target) {
        if (target == 0) {
            cout << "One possible sum is:\n";
            for (int i = 0; i < used.size(); ++i) {
              cout << used[i] << endl;
            }
            return true;
        } else if(set.empty()) {
          return false;
        }else {
            int element = set.first();
            Set<int> rest = set - element;
            used.push_back(element);
            if (subsetSumExists(rest, target- element)) {
              return true;
            } else {
              used.pop_back();
            }
            return subsetSumExists(rest, target);
        }
    }
    

    Hope this helps.

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

Sidebar

Related Questions

This question has two parts (maybe one solution?): Sample vectors from a sparse matrix
This one has me puzzled. It seemed like an easy task, but the solution
Edited to Add * I haven't found a solution for this one yet, can
I use contructor injection in my solution, but this one class has a property
Alright, this one's interesting. I have a solution, but I don't like it. The
I know one awkward solution for this taks will be : first use ct
I doubt I am the only one who has come up with this solution,
I can't find any solution to get this jquery events to work. $(window).one('click',function(){ console.log('hide
Getting an odd problem with a unit test in my solution. One of the
This one will take some explaining. What I've done is create a specific custom

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.