Consider the following algorithm BAR that gets a positive integer and works as follows:
BAR(int n):
- if (n <=0 ), return 0;
- else, return (n - BAR(BAR(n-1)));
(A.) What will be the output of BAR(3)?
(B.) Write an algorithm that has the same functionality as BAR, so that the runtime of BAR(n) is O(n)?
Question A.
BAR(0) = 0
BAR(1) = 1 - BAR(BAR(0)) = 1 - BAR(0) = 1 - 0 = 1
BAR(2) = 2 - BAR(BAR(1)) = 2 - BAR(1) = 2 - 1 = 1
BAR(3) = 3 - BAR(BAR(2)) = 3 - BAR(2) = 3 - 1 = 2
So, the output of BAR(3) would be 2.
Question B.
Approach: We can use dynamic programming bottom up approach to calculate BAR(n) in O(n) running time.
O(N) algorithm:
Step 1: Take an array dp of size n+1, i.e. dp[n+1];
Step 2: dp[0] = 0;
Step 3: for i = 1 to n
dp[i] = i - dp[dp[i-1]];
end of for loop
Step 4: dp[n] is the answer.
- The loop takes O(n) time to run. So, the time complexity of
the above algorithm is O(n). Also, the space complexity of the
above algorithm is O(n) as we are taking an array of size
n+1.
- dp[i] array stores the value of BAR( i ).
If the answer helped please upvote, it means a lot and for any query please comment.
Consider the following algorithm BAR that gets a positive integer and works as follows: BAR(int n):...
Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...
3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...
3) [16 points total] Consider the following algorithm int SillyCalc (int n) int i; int Num, answer; if (n <= 4) return n 10; else { Num-SillyCalcl n/4) answer = Num + Num + 10; for (i-2; i<-n-1; ++) answer- answer+ answer; return answer; Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2* for some...
8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...
3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...
Big-O notation. Consider the following function. int func1(int n) { int sum = 0, i; for(i = 0; i<n; i++;) { sum += i; return sum; } Express the running time of func1 as a function of n using big-O notation. Write a function that has the same functionality as func1, but runs in O(1) time.
Consider the following algorithm: ocedure Algorithm (b: integer, n: positive integer,i datinct integem) proc answer :", 0 nand 6 while (j print(j, z, b, answer) if jSn then answer:-j return answer (8 points] Assume that this algorithm receives as input the numbers b-17 andn9nd the corresponding sequence or iaie i 2 3 4 516 7 8 corresponding sequence of integers 19 Fill out the table below: i 는, ↓answer (b) [I point] Assume that the algorithm receives the same input...
T2(N) = Θ(N') Which algorithm is the least preferred one? a) Algorithm 1 b) Algorithm 2 ) Algorithm 3 Algorithm 1 and 3 2. Given the following function: (10 points) int F(int N) if (N0lIN1) return 2; else return N *FON-2); Show steps to find out F(5) Also give the runtime
2. Consider the function george (int n) computed by the following recursive C++ code. int george (int n) assert (n >= 0) if (n < 2) return 1; else return 2*george ((n+1)/2)+2*george (n/2)+2 george((n-1)/2)+2*george (n/2-1); (c) Design a memoization algorithm to compute george(n) for any given n. You do not have to write C++ code. This algorithm should be much faster than the dynamic programming algorithm. What is its time complexity? 2. Consider the function george (int n) computed by...
Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer numbers for i leftarrow 0 to n - 2 do for j leftarrow i +1 to n - 1 do if A[i] = = A[j] return false return true a) What does this algorithm do? b) Compute the running time of this algorithm.