Question

(In C) Given a list of numbers, find all the subsets of that list that sum...

(In C) Given a list of numbers, find all the subsets of that list that sum up to a given number? Ex: generate all subsets of {2, 4, 3, 1, 6, 5, 9} that sum up to 7. Output should be: {2, 4, 1}, {3, 4}, {5, 2}, {2, 1, 4}, {6, 1}

Homework Answers

Answer #1

// A recursive solution for subset sum problem
#include <stdio.h>

// Returns true if there is a subset of set[] with sun equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
// Base Cases
if (sum == 0)
   return true;
if (n == 0 && sum != 0)
   return false;

// If last element is greater than sum, then ignore it
if (set[n-1] > sum)
   return isSubsetSum(set, n-1, sum);

/* else, check if sum can be obtained by any of the following
   (a) including the last element
   (b) excluding the last element */
return isSubsetSum(set, n-1, sum) ||
                       isSubsetSum(set, n-1, sum-set[n-1]);
}

// Driver program to test above function
int main()
{
int set[] = {3, 34, 4, 12, 5, 2};
int sum = 9;
int n = sizeof(set)/sizeof(set[0]);
if (isSubsetSum(set, n, sum) == true)
   printf("Found a subset with given sum");
else
   printf("No subset with given sum");
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is...
In C Programming Language: Given an integer N, find whether the sum of the digits calculated...
In C Programming Language: Given an integer N, find whether the sum of the digits calculated repeatedly till a single digit is obtained results in the sum of 1 or not. Display 1 if the sum of the digits of N can equal 1 else display 0. Example: For 199, Sum of digits = 1+9+9=19, 1+9=10, 1+0=1 so it should display 1 For 57, sum of digits = 5+7=12, 1+2=3 so it should display 0
create a function that takes a dictionary and returns a list of int. The list should...
create a function that takes a dictionary and returns a list of int. The list should appear in decreasing order based on the sum of number in each dictionary. def total_num(dict1): #Code here input = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':3}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} output = [1,5,3,4,2] 1: 38 2: 5 3: 8 4: 6 5: 15 1>5>3>4>2
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as...
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as argument and determine the elements and indices of those elements that preceed a negative number in the given list. The returned information should be in the form of a pair of lists: ((values) . (indices)). E.g. (preceeding '(1 -2 3 4 -5 6 -7 -8)) → ((1 4 6 -7).(0 3 5 6)) ;note: drracket will display this as ((1 4 6 -7) 0...
Write a solution to the subset sum problem using Drracket. Write a procedure (subset-sum n nums)...
Write a solution to the subset sum problem using Drracket. Write a procedure (subset-sum n nums) where n is an integer and nums is a list of numbers that returns a subset of the numbers in the list nums whose values sum to n. If no subset of nums does this, the function should return #f. Ex: (subset-sum 12 '(2 10 5 7 3 8 6)) returns '(2 10) or any other subset of '(2 10 5 7 3 8...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...
1.) Suppose a pair of dice are rolled. Consider the sum of the numbers on the...
1.) Suppose a pair of dice are rolled. Consider the sum of the numbers on the top of the dice and find the probabilities. (Enter the probabilities as fractions.) (a) 5, given that the sum is odd (b) odd, given that a 5 was rolled 2.) Suppose a pair of dice are rolled. Consider the sum of the numbers on the top of the dice and find the probabilities. (Enter the probabilities as fractions.) (a) 8, given that exactly one...
def count_pairs(num_list): Given a list of numbers num_list, count how many ways unique pairs of values...
def count_pairs(num_list): Given a list of numbers num_list, count how many ways unique pairs of values from this list have a sum equal to a value anywhere in the list (including equaling one of the values being added). Watch out for duplicates – two locations may sum up to a value equal to many places in the list, but they only count as one pairing overall. The two summed values must be at unique locations in the list. • Parameters:...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...