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.

Type your Java solution code here:


This is a method exercise. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.