Question

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 6) that sums to 12; (subset-sum 17 '(2 10 6)) returns #f.

Homework Answers

Answer #1

1 this is done in c++

#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)

{

    // The value of subset[i][j] will be true if

    // there is a subset of set[0..j-1] with sum

    // equal to i

    bool subset[n + 1][sum + 1];

    // If sum is 0, then answer is true

    for (int i = 0; i <= n; i++)

        subset[i][0] = true;

    // If sum is not 0 and set is empty,

    // then answer is false

    for (int i = 1; i <= sum; i++)

        subset[0][i] = false;

    // Fill the subset table in botton up manner

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= sum; j++) {

            if (j < set[i - 1])

                subset[i][j] = subset[i - 1][j];

            if (j >= set[i - 1])

                subset[i][j] = subset[i - 1][j]

                               || subset[i - 1][j - set[i - 1]];

        }

    }

    /*   // uncomment this code to print table

     for (int i = 0; i <= n; i++)

     {

       for (int j = 0; j <= sum; j++)

          printf ("%4d", subset[i][j]);

       printf("\n");

     }*/

    return subset[n][sum];

}

// 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
2. There is a famous problem in computation called Subset Sum: Given a set S of...
2. There is a famous problem in computation called Subset Sum: Given a set S of n integers S = {a1, a2, a3, · · · , an} and a target value T, is it possible to find a subset of S that adds up to T? Consider the following example: S = {−17, −11, 22, 59} and the target is T = 65. (a) What are all the possible subsets I can make with S = {−17, −11, 22,...
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
1.Write a function which takes in a dictionary are returns the sum of the keys plus...
1.Write a function which takes in a dictionary are returns the sum of the keys plus the sum of the values, but only if all the keys and all the values are integers. Otherwise it returns False. >>>f({'a':1,'b':4,'c':7,'d':11}) False >>>f({1:2,3:4,5:6,7:8}) 36 2.Write a function to quickly compute the recaman sequence. 3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1,...
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...
This problem need to use DrRacket software. Racket Language. You must write each of the following...
This problem need to use DrRacket software. Racket Language. You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...
Finding the Mean The mean (or average) of a non-empty list of n numbers is the...
Finding the Mean The mean (or average) of a non-empty list of n numbers is the sum of the numbers divided by n. For example, the mean of 2, 7, 3, 9, and 13 is (2+7+3+9+13)/5, or 6.8. Write a function mean that takes as input a non-empty list of numbers (of any length > 0) and returns the mean.
1. Type a 'sumade2en2' function that receives an n number and returns the sum of the...
1. Type a 'sumade2en2' function that receives an n number and returns the sum of the numbers from 1 and increments of 2 without going over n. For example, if the function is invoked with 7, it must return 16 (1+3+5+7). Yes invoked with the 8 must return 16 (1+3+5+7). 2. Type a 'productmultiply' function that receives two integers a and b. The function must return the product of all multiples of a that do not exceed b. For example,...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...