Data Structures & Algorithms Multiple Choice Questions on “Master’s Theorem – 2”.
1. Solve the following recurrence using Master’s theorem. Answer: c 2. Solve the following recurrence using Master’s theorem. Answer: c 3. Solve the following recurrence using Master’s theorem. Answer: d 4. Solve the following recurrence using Master’s theorem. Answer: d 5. Solve the following recurrence using Master’s theorem. Answer: d 6. Solve the following recurrence using Master’s theorem. 7. Solve the following recurrence using Master’s theorem. Answer: a 8. What will be the recurrence relation of the following code? a) T(n) = T(n/2) + n 9. What will be the recurrence relation of the following code? a) T(n) = T(n/2) + n Answer: d 10. What will be the time complexity of the following code? a) O(log n) Answer: a
T(n) = 4T (n/2) + n2
a) T(n) = O(n)
b) T(n) = O(log n)
c) T(n) = O(n2log n)
d) T(n) = O(n2)
Clarification: The given recurrence can be solved by using the second case of Master’s theorem.
T(n) = O(nc log n) = Here nc = n2
So the solution becomes T(n) = O(n2log n).
T(n) = T (n/2) + 2n
a) T(n) = O(n2)
b) T(n) = O(n2 log n)
c) T(n) = O(2n)
d) cannot be solved
Clarification: The given recurrence can be solved by using the third case (as f(n) > log21) of Master’s theorem.
T(n) = O(f(n)) = Here f(n) = 2n.
So the solution becomes T(n) = O(2n).
T(n) = 16T (n/4) + n
a) T(n) = O(n)
b) T(n) = O(log n)
c) T(n) = O(n2log n)
d) T(n) = O(n2)
View Answer
Clarification: The given recurrence can be solved by using the first case of Master’s theorem. So the solution becomes T(n) = O(n2).
T(n) = 2T (n/2) + n/ log n
a) T(n) = O(n)
b) T(n) = O(log n)
c) T(n) = O(n2log n)
d) cannot be solved using master’s theorem
Clarification: The given recurrence cannot be solved by using the Master’s theorem. It is because this recurrence relation does not fit into any case of Master’s theorem.
T(n) = 0.7 T (n/2) + 1/n
a) T(n) = O(n)
b) T(n) = O(log n)
c) T(n) = O(n2log n)
d) cannot be solved using master’s theorem
Clarification: The given recurrence cannot be solved by using the Master’s theorem. It is because in this recurrence relation a < 1 so master’s theorem cannot be applied.
T(n) = 4 T (n/2) + n!
a) T(n) = O(n!)
b) T(n) = O(n! log n)
c) T(n) = O(n2log n)
d) cannot be solved using master’s theorem
Answer: a
Clarification: The given recurrence can be solved by using the third case of Master’s theorem. So the solution becomes T(n) = O(n!).
T(n) = 4T (n/4) + n log n
a) T(n) = O(n (log n)2)
b) T(n) = O(n log n)
c) T(n) = O(n2log n)
d) cannot be solved using master’s theorem
Clarification: The given recurrence can be solved by using the extended second case of Master’s theorem. So the solution becomes T(n) = O(n (log n)2).Int sum(int n)
{
If(n==1)
return 1;
else
return n+sum(n-1);
}
b) T(n) = T(n-1) + n
c) T(n) = T(n-1) + O(1)
d) T(n) = T(n/2) + O(1)
Answer: c
Clarification: As after every recursive call the integer up to which the sum is to be calculated decreases by 1. So the recurrence relation for the given code will be T(n) = T(n-1) + O(1). int xpowy(int x, int n)
if (n==0) return 1;
if (n==1) return x;
if ((n % 2) == 0)
return xpowy(x*x, n/2);
else
return xpowy(x*x, n/2) * x;
b) T(n) = T(n-1) + n
c) T(n) = T(n-1) + O(1)
d) T(n) = T(n/2) + O(1)
Clarification: As after every recursive call the integer up to which the power is to be calculated decreases by half. So the recurrence relation for the given code will be T(n) = T(n/2) + O(1). It can be solved by using master’s theorem.int xpowy(int x, int n)
{
if (n==0)
return 1;
if (n==1)
return x;
if ((n % 2) == 0)
return xpowy(x*x, n/2);
else
return xpowy(x*x, n/2) * x;
}
b) O(n)
c) O(n log n)
d) O(n2)
Clarification: As the recurrence relation of the code is given by T(n) = T(n/2) + O(1) so it can be solved by using master’s theorem second case.