logo CodeStepByStep logo

subsetSums

Language/Type: C++ dynamic programming

Write a function named subsetSums that uses dynamic programming to find the number of sub-lists of a vector that have a given sum. Your function accepts a reference to a vector of integers and a target value k as parameters and returns the number of sub-lists that sum to exactly k. For example, if the vector stores {9, 4, 20, 10, 3, 5} and k is 33, the sub-lists of {9, 4, 20} and {20, 3, 10} add to that sum, so you should return 2.

The key constraint of this problem is that you must solve it using a bottom-up dynamic programming approach. Do not use recursion. Your solution must use dynamic programming instead. You are allowed to construct any data structures (array, vector, set, map, etc.) necessary to store the data for your dynamic programming algorithm. Note: It is possible to solve this problem in O(N) time where N is the number of elements in the vector.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.