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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:24:39+00:00 2026-05-27T00:24:39+00:00

this is a solution for the subset sum problem. It uses backtracking. I have

  • 0

this is a solution for the subset sum problem. It uses backtracking. I have been thinking about it for more than 2 hours now and i just cannot understand it.

edit:I have added some comments to the code based on what i have understood. Please correct me if i am wrong.

#include <iostream>

int n, d, w[10], x[10], count=0;

void subset(int cs, int k, int r)//i dont understand the purpose of cs or of the array x[] 
{
    int i;
    x[k] = 1;

    if(cs + w[k] == d)      //if the first element is equivalent to the sum that is required 
    {
        std::cout<< "\n\nSolution " << ++count << " is:"; 
        for(i=0; i<=k; i++)  // so if the subset criteria are met then the array is printed.
        if(x[i] == 1) 
            std::cout << w[i] << " ";
    }
    else if(cs + w[k] + w[k+1] <= d) //if the node is promising then go to the next node and              
        subset(cs + w[k], k+1, r - w[k]); //check if subset criteria are met

    if(cs + w[k+1] <= d && cs + r - w[k] >= d) //else backtrack
    {
        x[k] = 0;
        subset(cs, k+1, r-w[k]);
    }
}

int main()
{
    int sum=0, i;

    std::cout << "enter n\n";
    std::cin >> n;

    std::cout < <"enter " << n << " elements\n";
    for(i=0; i<n; i++)
        std::cin >> w[i];

    for(i=0; i<n; i++)
        sum += w[i];

    std::cout << "enter value of sum\n";
    std::cin >> d;

    if(sum < d)
        std::cout<<"INVALID SOLUTION\n";
    else
        subset(0,0,sum);

}

Note: This is a working solution. It works when compiled with g++. I am sorry if this seems too obnoxious but i just did not understand much from the code and hence i cannot give much of an explanation.

  • 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-27T00:24:40+00:00Added an answer on May 27, 2026 at 12:24 am

    cs is the sum of values of the weights chosen so far, while r is the remainder of the summed values of the weights yet to be chosen. w[i] is weight i, x[i] is 1 when weight [i] is chosen. In the subset method there are two main decision branches:

    Weight k is chosen:

    // adding value of weight k to computed sum (cs) gives required sum, solution found
    if(cs+w[k]==d)
    {
    cout<<"\n\nSolution "<<++count<<" is:";
    for(i=0;i<=k;i++)
        if(x[i]==1)
        cout<<w[i]<<" ";
    }
    
    // both weight k and weight k+1 can be chosen without exceeding d, 
    // so we choose k, and see if there's a solution for weight k+1 onwards
    // note that available weight values decreased from r to r-w[k]
    else if(cs+w[k]+w[k+1]<=d)
        subset(cs+w[k],k+1,r-w[k]);
    

    Weight k is not chosen (notice that this is explored even when a solution was found right after weight k is chosen):

    // weight k+1 is choosable (does not exceed d), and despite not choosing weight k
    // there would be sufficient weights in r less w[k], and together with the chosen 
    // pool cs to meet the requirement of d.
    if(cs+w[k+1]<=d && cs+r-w[k]>=d)
    {
    x[k]=0;
    subset(cs,k+1,r-w[k]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this solution for a single button: myButton.Attributes.Add(onclick, this.disabled=true; + GetPostBackEventReference(myButton).ToString()); Which works
This was just what I was thinking for this solution and I would like
I am getting an error about the assembly C:\Ora10g\bin\Zip.exe. The trouble is this solution
recently I became interested in the subset-sum problem which is finding a zero-sum subset
Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo
This turned out to be more difficult than I thought. Basically, each day a
As you probably know, the SUBSET-SUM problem is defined as determining if a subset
I'm learning Python and I have a problem with this seems to be simple
(This is not exactly the problem that I have, but it's isomorphic, and I
Note: This is a sequel to my previous question about powersets. I have got

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.