Worksheet 6
Worksheet 6
Worksheet 6
UID:20BCS9841
Section:615
Group: A
INDEX
2.4
3.1
3.2
Experiment – 2.2
2. Objective : Subset sum problem is that given a subset A of n positive integers and a
value sum is given, find whether or not there exists any subset of the given set, the sum of
whose elements is equal to the given value of sum.
class Mjava {
static boolean SubsetSum(int set[],int n, int sum){
if (sum == 0)
return true;
if (n == 0)
return false;
if (set[n - 1] > sum)
return SubsetSum(set, n - 1, sum);
return SubsetSum(set, n - 1, sum)
|| SubsetSum(set, n - 1, sum - set[n - 1]);
}
public static void main(String args[])
{
int set[] = { 3, 34, 4, 12, 5, 2 };
int sum = 9;
int n = set.length;
if (SubsetSum(set, n, sum) == true)
System.out.println("Found");
else
System.out.println("Not found");
}
}