Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PROGRAMMING Warm up 3: Subset Sum [20 points] Write the following recursive method public static boolean isSubSetSum (ArrayList set, int targetNumber) This method takes

JAVA PROGRAMMING

image text in transcribed

Warm up 3: Subset Sum [20 points] Write the following recursive method public static boolean isSubSetSum (ArrayList set, int targetNumber) This method takes an ArrayList of integers, and a targetNumber and determines if there is a subset of those integers that sum to that targetNumber. For example, given the numbers (3, 7, 1, 8, -3) and the target sum 4, the result is true because subset $3 1 sums to 4. On the other hand, if the target sum were 2, the result is false since there is no subset that sums to 2. Hints If the set is empty, then it clearly has no elements that can add to the targetNumber, unless targetNumber is 0. If targetNumber is 0 answer is always true. . . The subset of elements that are used in the sum will either contain the first number in the set, or it will not. Either way, once you have chosen to use the number or throw it away, it will need to be removed from the set you pass into your recursive call. You will need to have two recursive cases here: one where the first number is in the subset, and one where the first number is not in the subset. Whether you use the first element or not will affect what you pass in for targetNumber in your recursive call. Then after you have done both recursive calls, consider how to combine them to give you your final answer. (It might make things faster to do only one, and only do the second if you need to)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Values: What is important to me?

Answered: 1 week ago