Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using recursion if possible. Thank you =) Question 2: Subsets of Array of Integers Given a set of integers, does there exist a subset that
Using recursion if possible.
Thank you =)
Question 2: Subsets of Array of Integers Given a set of integers, does there exist a subset that sums to zero? For example the set (3, 4, -2, -1, 9y has a subset (3, -2, -1 that sums to zero. We are only interested in the existence of a subset summing to zero, not in how many there are, as in the set 1, 2, 3, -3, -1). Clearly, if a set contains a zero value, then this value forms a subset of size one summing to zero (e.g. {4, 9. 0, -3, 2) The existence of a subset summing to zero can be found from a recursive definition. The set of n integers is stored in an array of length n. This set has a subset summing to zero if the first n-1 integers have a subset summing to either o or o-a[n-1]. That is, we consider two possibilities: the last integer is either not in the subset or it is. Here is a general definition including two bases cases (the set is empty or a subset summing to zero has been found) ifi>0 AND sum-a[i-1] 0 S(a,1-1,sun) OR S(a,1-1,sum a[i-1]) - false true sum-a[i-1] -0 where s (a, , surn) is true if a subset of the first i elements of array a sums to sum, and false otherwise. The recursion starts with s (a,n,0). Implement this recursive definition as a helper method to the method public static boolean zeroSun (int] a. Test your method with a main method that reads 5 integers into an array and calls zeroSun Question 2: Subsets of Array of Integers Given a set of integers, does there exist a subset that sums to zero? For example the set (3, 4, -2, -1, 9y has a subset (3, -2, -1 that sums to zero. We are only interested in the existence of a subset summing to zero, not in how many there are, as in the set 1, 2, 3, -3, -1). Clearly, if a set contains a zero value, then this value forms a subset of size one summing to zero (e.g. {4, 9. 0, -3, 2) The existence of a subset summing to zero can be found from a recursive definition. The set of n integers is stored in an array of length n. This set has a subset summing to zero if the first n-1 integers have a subset summing to either o or o-a[n-1]. That is, we consider two possibilities: the last integer is either not in the subset or it is. Here is a general definition including two bases cases (the set is empty or a subset summing to zero has been found) ifi>0 AND sum-a[i-1] 0 S(a,1-1,sun) OR S(a,1-1,sum a[i-1]) - false true sum-a[i-1] -0 where s (a, , surn) is true if a subset of the first i elements of array a sums to sum, and false otherwise. The recursion starts with s (a,n,0). Implement this recursive definition as a helper method to the method public static boolean zeroSun (int] a. Test your method with a main method that reads 5 integers into an array and calls zeroSunStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started