Question
Consider the following mystery method that is passed a non-null array filled with integers and that uses a recursive companion method: public static int mystery(int[]
Consider the following mystery method that is passed a non-null array filled with integers and that uses a recursive companion method:
public static int mystery(int[] A) { return mystery(A, 0, A.length - 1); } private static int mystery(int[] A, int first, int last) { if (first == last) return A[first]; int middle = (first + last)/2; return mystery(A, first, middle) + mystery(A, middle+1, last); }
Answer the following questions regarding the code above:
A) What is returned by calling mystery(A) if A contains 7, 6, 5, 4?
B) What is returned by calling mystery(A) if A contains 5, 2, 5, 8, 7, 2, 4?
C) Draw an execution tree trace of the recursive companion method as called in b. above. Draw your tree as shown below listing the values of parameters first and last for each recursive call. Make sure your tree displays correctly in your pdf file.
0-6 ... + ... ... + ... ... + ...
D) What does method mystery(A) do in general? Do not just redescribe the code. Instead, briefly state what the method does at a high level.
E) Analyze the time complexity for the recursive companion method above:
1) Identify the problem size that affects the method's runtime.
2) Write the recurrence equations for base and recursive cases. (You may assume the array size is some power of 2.)
3) Determine the solution by looking for a pattern in the table.
4) Verify the solution by substituting it into the recurrence equation.
5) Determine the method's complexity by expressing the solution in big-O notation.
Step 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