logo CodeStepByStep logo

subsetSums

Language/Type: Java dynamic programming

Write a method named subsetSums that uses dynamic programming to find the number of sub-lists of an array that have a given sum. Your method accepts an array 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 array 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, list, 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 array.

Method: Write a Java method as described, not a complete program or class.

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.