Top MCQs on Complexity Analysis using Recurrence Relations with Answers

The analysis of the complexity of a recurrence relation involves finding the asymptotic upper bound on the running time of a recursive algorithm. More on Complexity Analysis using Recurrence..

Complexity Analysis Using Recurrence Quiz

Complexity Analysis Using Recurrence Quiz


Question 1

What is the value of following recurrence.

T(n) = T(n/4) + T(n/2) + cn2
T(1) = c
T(0) = 0

Where c is a positive constant

Cross

O(n3)

Tick

O(n2)

Cross

O(n2 Logn)

Cross

O(nLogn)



Question 1-Explanation: 

Following is the initial recursion tree for the given recurrence relation.

           cn^2
         /      \\
     T(n/4)     T(n/2)

If we further break down the expression T(n/4) and T(n/2), we get following recursion tree.

               cn^2
           /           \\      
       c (n^2)/16       c(n^2)/4
      /      \\          /     \\
  T(n/16)     T(n/8)  T(n/8)    T(n/4) 

Breaking down further gives us following

                 cn^2
            /             \\      
       c(n^2)/16           c(n^2)/4
       /      \\            /      \\
c(n^2)/256  c(n^2)/64  c(n^2)/64    c(n^2)/16
 /    \\      /    \\    /    \\         /    \\    
 

To know the value of T(n), we need to calculate sum of tree nodes level by level. If we sum the above tree level by level, we get the following series T(n) = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + .... The above series is geometrical progression with ratio 5/16 To get an upper bound, we can sum the above series for infinite terms. We get the sum as (n^2) / (1 - 5/16) which is O(n^2).

Question 2
What is the value of following recurrence. T(n) = 5T(n/5) + \\sqrt{n}, T(1) = 1, T(0) = 0
Tick
Theta (n)
Cross
Theta (n^2)
Cross
Theta (sqrt(n))
Cross
Theta (nLogn)


Question 2-Explanation: 
The given solution can be solved using Master Method. It falls in Case 1.
Question 3

What is the worst case time complexity of following implementation of subset sum problem. 

C

// Returns true if there is a subset of set[] with sum equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
   // Base Cases
   if (sum == 0)
     return true;
   if (n == 0 && sum != 0)
     return false;
 
   // If last element is greater than sum, then ignore it
   if (set[n-1] > sum)
     return isSubsetSum(set, n-1, sum);
 
   /* else, check if sum can be obtained by any of the following
      (a) including the last element
      (b) excluding the last element   */
   return isSubsetSum(set, n-1, sum) || 
          isSubsetSum(set, n-1, sum-set[n-1]);
}
Cross

O(n * 2^n)

Cross

O(n^2)

Cross

O(n^2 * 2^n)

Tick

O(2^n)



Question 3-Explanation: 

Following is the recurrence for given implementation of subset sum problem T(n) = 2T(n-1) + C1 T(0) = C1 Where C1 and C2 are some machine specific constants. The solution of recurrence is O(2^n) We can see it with the help of recurrence tree method

           C1
       /       \\
    T(n-1)     T(n-1) 


                    C1
                /       \\
              C1           C1
           /     \\        /    \\
      T(n-2)  T(n-2)   T(n-2)  T(n-2)

                    C1
                /       \\
              C1           C1
           /     \\        /    \\
          C1     C1      C1     C1
        /   \\   /  \\    /  \\   /  \\

       
If we sum the above tree level by level, we get the following series
T(n) = C1 + 2C1 + 4C1 + 8C1 + ...
The above series is Geometrical progression and there will be n terms in it.
So T(n) = O(2^n)    
Question 4
Suppose T(n) = 2T(n/2) + n, T(0) = T(1) = 1 Which one of the following is false. ( GATE CS 2005)
a) T(n) = O(n^2)
b) T(n) = \\theta(nLogn)
c) T(n) = \\Omega(n^2)
d) T(n) = O(nLogn)
Cross
A
Cross
B
Tick
C
Cross
D


Question 4-Explanation: 
Question 5
Consider the following recurrence:
gate_2006_51 Which one of the following is true?
(A) T(n) = \\theta(loglogn)
(B) T(n) = \\theta(logn)
(C) T(n) = \\theta(sqrt(n))
(D) T(n) = \\theta(n)
Cross
A
Tick
B
Cross
C
Cross
D


Question 5-Explanation: 
This question can be solved by first change of variable and then Master Method.
  Let n = 2^m
  T(2^m) = T(2^(m/2)) + 1
  Let T(2^m) =  S(m)
  S(m)  = 2S(m/2) + 1  
Above expression is a binary tree traversal recursion whose time complexity is \\theta(m). You can also prove using Master theorem.
S(m)  = \\theta(m)  
      = \\theta(logn)  /* Since n = 2^m */
Now, let us go back to the original recursive function T(n)
  T(n)  = T(2^m) = S(m)
                 = \\theta(Logn)


Question 6
The running time of an algorithm is represented by the following recurrence relation:
    if  n <= 3  then   T(n) = n
    else T(n) = T(n/3) + cn
Which one of the following represents the time complexity of the algorithm?
(A) \\theta(n)
(B) \\theta(n log n)
(C) \\theta(n^2)
(D) \\theta(n^2log n)
Tick
A
Cross
B
Cross
C
Cross
D


Question 6-Explanation: 
T(n) = cn + T(n/3)
     = cn + cn/3 + T(n/9)
     = cn + cn/3 + cn/9 + T(n/27)
Taking the sum of infinite GP series. The value of T(n) will
be less than this sum.
T(n) <= cn(1/(1-1/3))
     <= 3cn/2

or we can say 
cn <= T(n) <= 3cn/2
Therefore T(n) = \\theta(n)
This can also be solved using Master Theorem for solving recurrences. The given expression lies in Case 3 of the theorem.
Question 7
The running time of the following algorithm
  Procedure A(n)  
  If n <= 2 return(1) else return A(\\lceil \\sqrt{n}  \\rceil);
is best described by
Cross
O(n)
Cross
O(log n)
Tick
O(1og log n)
Cross
O(1)


Question 7-Explanation: 
For explanation, please see question 5 of this post
Question 8

What is the time complexity of the following recursive function: 
 

c

int DoSomething (int n) 
{
  if (n <= 2)
    return 1;
  else  
    return (DoSomething (floor(sqrt(n))) + n);
}

(A) \\theta   (n) 
(B) \\theta   (nlogn)
(C) \\theta   (logn)
(D) \\theta   (loglogn)
 

Cross

A

Cross

B

Tick

D

Cross

C



Question 8-Explanation: 

Recursive relation for the DoSomething() is 
 

  T(n) =  T(\\sqrt{n}) + C1 if n > 2  


We have ignored the floor() part as it doesn\'t matter here if it\'s a floor or ceiling. 
 

  Let n = 2^m,  T(n) = T(2^m)
  Let T(2^m) =  S(m)

  From the above two, T(n) = S(m)

  S(m) = S(m/2) + C1  /* This is simply binary search recursion*/
  S(m)  = O(logm)      
          = O(loglogn)  /* Since n = 2^m */
  
  Now, let us go back to the original recursive function T(n) 
  T(n)  = S(m) 
          = O(LogLogn)



 

Question 9
The time complexity of the following C function is (assume n > 0 (GATE CS 2004)
int recursive (mt n)
{
   if (n == 1)
     return (1);
   else
     return (recursive (n-1) + recursive (n-1));
}
Cross
0(n)
Cross
0(nlogn)
Cross
0(n^2)
Tick
0(2^n)


Question 9-Explanation: 
Recursive expression for the above program will be.
  T(n) = 2T(n-1) + c
  T(1) = c1.
Let us solve it.
  T(n) = 2(2T(n-2) + c) + c        = 4T(n-2) + 3c
  T(n) = 8T(n-3) + 6c + c          =  8T(n-3) + 7c
  T(n) = 16T(n-4) + 14c + c        =  16T(n-4) + 15c
  ............................................................
  .............................................................
  T(n) = (2^(n-1))T(1) +  (2^(n-1) - 1)c

  T(n) = O(2^n)
Question 10
Consider the following recurrence T(n) = 3T(n/5) + lgn * lgn What is the value of T(n)?
(A) \\Theta(n ^ \\log_5{3})
(B) \\Theta(n ^ \\log_3{5})
(c) \\Theta(n Log n )
(D) \\Theta( Log n )
Tick
A
Cross
B
Cross
C
Cross
D


Question 10-Explanation: 
By Case 1 of the Master Method, we have T(n) = Theta(n ^ (log5(3)) ). [^ is for power]
There are 35 questions to complete.


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads