0% found this document useful (0 votes)
76 views2 pages

Sum of Subsets

The document describes an algorithm to find all subsets of a given set that sum to a target number K using backtracking. It takes a set as input, sorts the elements, then uses a recursive backtracking method SumOfSubsets to iteratively try including or excluding elements to reach the target sum K. The method prints any subsets it finds that sum to K. An example is provided where it finds 3 subsets of {2,4,6,8,10} that sum to 20.

Uploaded by

lokeshmit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Sum of Subsets

The document describes an algorithm to find all subsets of a given set that sum to a target number K using backtracking. It takes a set as input, sorts the elements, then uses a recursive backtracking method SumOfSubsets to iteratively try including or excluding elements to reach the target sum K. The method prints any subsets it finds that sum to K. An example is provided where it finds 3 subsets of {2,4,6,8,10} that sum to 20.

Uploaded by

lokeshmit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sum of Subsets Problem

AIM
To find subset of elements that are selected from a given set whose sum adds up to a given number
'K' using Backtracking.
CODE
import java.io.*;
import java.util.*;
public class SumOfSubsets
{
static int [] w;
static int [] x;
public static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public static void SumOfSubsets(int s, int k, int r, int m)
{
int i;
x[k]=1;
if((s+w[k])==m)
{
System.out.print("{");
for(i=1;i<=k;i++) System.out.print(x[i]+" ");
System.out.print("}\t");
}
else if((s+w[k]+w[k+1])<=m)
{
SumOfSubsets(s+w[k],k+1,r-w[k],m);
}
if(((s+r-w[k])>=m)&&((s+w[k+1])<=m))
{
x[k]=0;
SumOfSubsets(s,k+1,r-w[k],m);
}
}
public static void main(String[] args) throws IOException
{
int i,j,temp,ord,max,sum=0;
System.out.print("\n\nEnter the order of the set: ");
ord = Integer.parseInt(br.readLine());
w = new int[ord+1];
x = new int[ord+1];
System.out.println("\n\nEnter the elements of the set:");
for(i=1;i<=ord;i++)
{
System.out.print("Element #"+i+": ");
w[i] = Integer.parseInt(br.readLine());

sum+=w[i];
}
System.out.println("\nEnter the sum to be made: ");
max = Integer.parseInt(br.readLine());
Arrays.sort(w, 1, w.length);
if((max<w[1])||(sum < max))
System.out.print("Sorry, Subsets can't be formed.");
else
{
System.out.print("\nGiven set: \n{");
for(i=1;i<=ord;System.out.print(w[i]+" "),i++);
System.out.print("}");
System.out.println("\n\nSubsets formed with sum as "+max+": ");
SumOfSubsets.SumOfSubsets(0,1,sum,max);
}
}
}
OUTPUT
Enter the order of the set : 5
Enter the elements of the set:
Element #1: 2
Element #2: 4
Element #3: 6
Element #4: 8
Element #5: 10
Enter the sum to be made : 20
Given set : {2 4 6 8 10 }
Subsets formed with sum as 20 :
{1 1 1 1 0}

{1 0 0 1 1}

{0 1 1 0 1}

You might also like