mystery(n):
if n == 1 then
println("A");
else
mystery(n/2);
println("B");
mystery(n/2);
end if
ans)
base case:
if n == 1 then
println("A");
recursive case :
else
mystery(n/2);
println("B");
mystery(n/2);
computing time complexity:
Recurrence relation of the above recursive function :
T(n) = 2T(n/2) + 1, T(1) = 1
T(n) = 2T(n/2) + 1 ---- 1
T(n/2) = 2T(n/2^2) + 1 ---- 2
substitue 2 in 1
T(n) = 2^2T(n/2^2) + 2^1 + 2^0 ---- 3
T(n/2^2) = 2 T(n/2^3) + 1 ----- 4
substitue 4 in 3
T(n) = 2^3 T(n/2^3) + 2^2 + 2^1 + 2^0
.
.
.
.
T(n) = 2^k T(n/2^k) + (2^0 + 2^1 + 2^2 + .... + 2^(k-1))
assume 2^k = n ==> k = log n
So T(n) = 2^k T(1) + (2^0 + 2^1 + 2^2 + .... + 2^(k-1))
= 2^k + (2^0 + 2^1 + 2^2 + .... + 2^(k-1))
= n + (2^k - 1)
= n + n - 1
= 2n
T(n)= O(n)
For the following code, figure out the recurrences (base case and recursive step), calculate the running...
Find a) overall run time T(n) and b) worst case run timeO(N) of this recursive function. int mystery (int n) { if (n<= 1) {return 1 } else { return mystery(n-1) + mystery(n-2); } }
CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120 1 test passed 4 6 All tests 1 passed 8 9 1 def factorial_str(fact_counter, fact_value): 2 output_string = 3 if fact_counter == 0: # Base case: 0! = 1 5 output_string += '1' elif fact counter == 1: # Base case: print 1 and result 7...
In a recursive solution, the base case terminates the recursive processing. In the below code, which of the following calls to recurse() would stop further recursive calls? #include <iostream> void recurse(int x, int y) { if (y > 0) { x++; y--; std::cout << x << " " << y << " "; recurse(x, y); std::cout << x << " " << y << std::endl; } } 1recurse(1, 2); 2recurse(1, 0); 3recurse(1, 1); 4recurse(0, 1);
This code is based on creating a recursive funciton. You cannot alter the code besides adding your solution in the particular area. Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5: 5! = 5 * 4 * 3 * 2 * 1 = 120 #include void PrintFactorial(int factCounter, int factValue){ int nextCounter = 0; int nextValue = 0; if (factCounter == 0) { // Base case: 0! = 1 printf("1\n"); }...
The code for a recursive function 'mystery' appears below. Assume we passed the following array as x[]: int x[] = { 10, 20, 25, 25 }; How would we call mystery() so the return value is 1? int mystery(const int x[], int n, int mysteryValue) { int count = 0; if (n <= 0) return 0; else { if (x[n - 1] == mysteryValue) count = 1; return mystery(x, n - 1, mysteryValue) + count; } } 1mystery(x, 4, 0)...
2. The mergesort is a recursive sort. What is the base case for the merge sort? A.There is none. B. When right = left -1 C. When start = end D. When end - start = 1 3. Given the array [ 23, 83, 82, 92, 28, 21, 91, 17, 54, 32, 41, 14], what is the value of mid for the call mergesort (elements, 0, 5, temp) ? A. The value of mid is 3 B. The value of...
Please code this in Java, thank you!
(a) Implement a sublinear running time complexity recursive function in Java public static long long x, int n) to calculate X Note: In your function you can use only the basic arithmetic operators (+, -,, %, and /) (b) What is the running time complexity of your function? Justify (c) Give a number of multiplications used by your function to calculate x63.
Problem 5: Recurrence relations and detailed analysis of recursive algorithm efficiency g(n: non-negative integer) 1. if n ≤ 1 then return n 2. else return (5 * g(n─1) ─ 6 * g(n─2)) MergeSort divides the array to be sorted into two equal halves, calls itself recursively on each half to sort that subarray, and then calls the Merge algorithm to merge the two sorted halves in linear time. This leads to its two recurrence relations T(n)=2T(n/2)+cn, n>1;...
1
2
3
The base case; reduce problem and general solution of the recursive algorithm of n! are: O A. O! =0 (n-1)! n(n-1)! OB.O!= 1 (n-1)! n(n-1)! OC. O!=0 (n-1)! n(n-1) OD.0! = 1 (n-1) n(n-1) To set up the table for a party, if we use n tables and set up one next another as the picture. How many seats are there when if one side of the table can place only one chair? When determine Four-Step Methodology...
Consider the following pseudo-code fragment. What is the value of mystery(2, 3)? BEGIN PROGRAM mystery(int a, int b) IF (b==1) RETURN a END IF ELSE RETURN a + mystery(a, b - 1) END ELSE END PROGRAM mystery 2 4 6 8 the program generates a run time error (infinite recursion) I have no idea